Windshaft-cartodb/docs/anonymous_maps.md

195 lines
5.8 KiB
Markdown
Raw Normal View History

2015-10-22 21:02:01 +08:00
# Anonymous Maps
2015-10-22 19:28:08 +08:00
2016-02-19 02:15:28 +08:00
Anonymous Maps allows you to instantiate a map given SQL and CartoCSS. It also allows you to add interaction capabilities using [UTF Grid.](https://github.com/mapbox/utfgrid-spec)
2015-10-22 19:28:08 +08:00
2015-10-28 23:02:23 +08:00
2015-10-22 21:02:01 +08:00
## Instantiate
2015-10-22 19:28:08 +08:00
2015-10-28 23:02:23 +08:00
#### Definition
2015-10-22 19:28:08 +08:00
```html
POST /api/v1/map
```
2015-10-28 23:02:23 +08:00
#### Params
2015-10-22 19:28:08 +08:00
```javascript
{
"version": "1.3.0",
"layers": [{
"type": "mapnik",
"options": {
"cartocss_version": "2.1.1",
"cartocss": "#layer { polygon-fill: #FFF; }",
"sql": "select * from european_countries_e",
"interactivity": ["cartodb_id", "iso3"]
2015-10-22 19:28:08 +08:00
}
}]
}
```
2016-05-26 22:22:52 +08:00
See [MapConfig File Formats](http://docs.carto.com/carto-engine/maps-api/mapconfig/) for details.
2015-10-22 19:28:08 +08:00
2015-10-28 23:02:23 +08:00
#### Response
2015-10-22 19:28:08 +08:00
The response includes:
2015-10-28 23:02:23 +08:00
Attributes | Description
--- | ---
layergroupid | The ID for that map, used to compose the URL for the tiles. The final URL is: `https://{username}.carto.com/api/v1/map/{layergroupid}/{z}/{x}/{y}.png`
2015-10-28 23:02:23 +08:00
updated_at | The ISO date of the last time the data involved in the query was updated.
metadata | Includes information about the layers.
cdn_url | URLs to fetch the data using the best CDN for your zone.
2015-10-22 19:28:08 +08:00
2015-10-22 21:02:01 +08:00
### Example
2015-10-22 19:28:08 +08:00
2015-10-28 23:02:23 +08:00
#### Call
2015-10-22 19:28:08 +08:00
```bash
curl 'https://{username}.carto.com/api/v1/map' -H 'Content-Type: application/json' -d @mapconfig.json
2015-10-22 19:28:08 +08:00
```
2015-10-28 23:02:23 +08:00
#### Response
2015-10-22 19:28:08 +08:00
```javascript
{
"layergroupid": "c01a54877c62831bb51720263f91fb33:0",
"last_updated": "1970-01-01T00:00:00.000Z",
"metadata": {
"layers": [
{
"type": "mapnik",
"meta": {}
}
]
},
"cdn_url": {
2016-06-03 01:22:07 +08:00
"http": "http://cdb.com",
"https": "https://cdb.com"
2015-10-22 19:28:08 +08:00
}
}
```
2015-10-28 23:02:23 +08:00
### Retrieve resources from the layergroup
2015-10-22 19:28:08 +08:00
When you have a layergroup, there are several resources for retrieving layergoup details such as, accessing Mapnik tiles, getting individual layers, accessing defined Attributes, and blending and layer selection.
2015-10-22 19:28:08 +08:00
2016-02-19 05:58:28 +08:00
#### Mapnik tiles
2016-02-22 22:01:04 +08:00
These tiles will get just the Mapnik layers. To get individual layers, see the following section.
2015-10-22 19:28:08 +08:00
```bash
https://{username}.carto.com/api/v1/map/{layergroupid}/{z}/{x}/{y}.png
2015-10-22 19:28:08 +08:00
```
2015-10-28 23:02:23 +08:00
#### Individual layers
2015-10-22 19:28:08 +08:00
The MapConfig specification holds the layers definition in a 0-based index. Layers can be requested individually in different formats depending on the layer type.
Individual layers can be accessed using that 0-based index. For UTF grid tiles:
```bash
https://{username}.carto.com/api/v1/map/{layergroupid}/{layer}/{z}/{x}/{y}.grid.json
2015-10-22 19:28:08 +08:00
```
In this case, `layer` as 0 returns the UTF grid tiles/attributes for layer 0, the only layer in the example MapConfig.
2015-10-22 19:28:08 +08:00
If the MapConfig had a Torque layer at index 1 it could be possible to request it with:
```bash
https://{username}.carto.com/api/v1/map/{layergroupid}/1/{z}/{x}/{y}.torque.json
2015-10-22 19:28:08 +08:00
```
2015-10-28 23:02:23 +08:00
#### Attributes defined in `attributes` section
2015-10-22 19:28:08 +08:00
```bash
https://{username}.carto.com/api/v1/map/{layergroupid}/{layer}/attributes/{feature_id}
2015-10-22 19:28:08 +08:00
```
Which returns JSON with the attributes defined, like:
```javascript
{ "c": 1, "d": 2 }
```
2015-10-28 23:02:23 +08:00
#### Blending and layer selection
2015-10-22 19:28:08 +08:00
```bash
https://{username}.carto.com/api/v1/map/{layergroupid}/{layer_filter}/{z}/{x}/{y}.png
2015-10-22 19:28:08 +08:00
```
Note: currently format is limited to `png`.
`layer_filter` can be used to select some layers to be rendered together. `layer_filter` supports two formats:
2015-10-22 19:28:08 +08:00
- `all` alias
Using `all` as `layer_filter` will blend all layers in the layergroup
2015-10-22 19:28:08 +08:00
```bash
https://{username}.carto.com/api/v1/map/{layergroupid}/all/{z}/{x}/{y}.png
2015-10-22 19:28:08 +08:00
```
- Filter by layer index
A list of comma separated layer indexes can be used to just render a subset of layers. For example `0,3,4` will filter and blend layers with indexes 0, 3, and 4.
```bash
https://{username}.carto.com/api/v1/map/{layergroupid}/0,3,4/{z}/{x}/{y}.png
2015-10-22 19:28:08 +08:00
```
Some notes about filtering:
- Invalid index values or out of bounds indexes will end in `Invalid layer filtering` errors.
2016-02-22 22:01:04 +08:00
- Once a Mapnik layer is selected, all Mapnik layers will get blended. As this may change in the future **it is
recommended** to always select all Mapnik layers if you want to select at least one so you will get a consistent
2015-10-22 19:28:08 +08:00
behavior in the future.
- Ordering is not considered. So right now filtering layers 0,3,4 is the very same thing as filtering 3,4,0. As this
may change in the future **it is recommended** to always select the layers in ascending order so you will get a
consistent behavior in the future.
2015-10-28 23:02:23 +08:00
2015-10-22 21:02:01 +08:00
## Create JSONP
2015-10-22 19:28:08 +08:00
The JSONP endpoint is provided in order to allow web browsers access which don't support CORS.
2015-10-28 23:02:23 +08:00
#### Definition
2015-10-22 19:28:08 +08:00
```bash
GET /api/v1/map?callback=method
```
2015-10-28 23:02:23 +08:00
#### Params
2015-10-22 19:28:08 +08:00
2015-10-28 23:02:23 +08:00
Param | Description
--- | ---
2016-02-22 22:01:04 +08:00
config | Encoded JSON with the params for creating Named Maps (the variables defined in the template).
2015-10-28 23:02:23 +08:00
lmza | This attribute contains the same as config but LZMA compressed. It cannot be used at the same time as `config`.
callback | JSON callback name.
2015-10-22 19:28:08 +08:00
2015-10-22 21:02:01 +08:00
### Example
2015-10-22 19:28:08 +08:00
2015-10-28 23:02:23 +08:00
#### Call
2015-10-22 19:28:08 +08:00
```bash
curl "https://{username}.carto.com/api/v1/map?callback=callback&config=%7B%22version%22%3A%221.0.1%22%2C%22layers%22%3A%5B%7B%22type%22%3A%22cartodb%22%2C%22options%22%3A%7B%22sql%22%3A%22select+%2A+from+european_countries_e%22%2C%22cartocss%22%3A%22%23european_countries_e%7B+polygon-fill%3A+%23FF6600%3B+%7D%22%2C%22cartocss_version%22%3A%222.3.0%22%2C%22interactivity%22%3A%5B%22cartodb_id%22%5D%7D%7D%5D%7D"
2015-10-22 19:28:08 +08:00
```
2015-10-28 23:02:23 +08:00
#### Response
2015-10-22 19:28:08 +08:00
```javascript
callback({
2015-10-28 23:02:23 +08:00
layergroupid: "d9034c133262dfb90285cea26c5c7ad7:0",
cdn_url: {
2016-06-03 01:22:07 +08:00
"http": "http://cdb.com",
"https": "https://cdb.com"
2015-10-28 23:02:23 +08:00
},
last_updated: "1970-01-01T00:00:00.000Z"
2015-10-22 19:28:08 +08:00
})
```
2015-10-28 23:02:23 +08:00
2015-10-22 21:02:01 +08:00
## Remove
2015-10-22 19:28:08 +08:00
2016-02-19 05:58:28 +08:00
Anonymous Maps cannot be removed by an API call. They will expire after about five minutes, or sometimes longer. If an Anonymous Map expires and tiles are requested from it, an error will be raised. This could happen if a user leaves a map open and after time, returns to the map and attempts to interact with it in a way that requires new tiles (e.g. zoom). The client will need to go through the steps of creating the map again to fix the problem.