2015-10-22 19:28:08 +08:00
# Static Maps API
2016-02-19 02:15:28 +08:00
The Static Maps API can be initiated using both Named and Anonymous Maps using the 'layergroupid' token. The API can be used to create static images of parts of maps and thumbnails for use in web design, graphic design, print, field work, and many other applications that require standard image formats.
2015-10-22 19:28:08 +08:00
## Maps API endpoints
2016-02-19 02:15:28 +08:00
Begin by instantiating either a Named or Anonymous Map using the `layergroupid token` as demonstrated in the Maps API documentation above. The `layergroupid` token calls to the map and allows for parameters in the definition to generate static images.
2015-10-22 19:28:08 +08:00
### Zoom + center
#### Definition
```bash
2016-03-22 21:19:50 +08:00
GET /api/v1/map/static/center/{token}/{z}/{lat}/{lng}/{width}/{height}.{format}
2015-10-22 19:28:08 +08:00
```
#### Params
2015-10-28 23:55:11 +08:00
Param | Description
--- | ---
2016-03-22 21:19:50 +08:00
token | the layergroupid token from the map instantiation
z | the zoom level of the map
lat | the latitude for the center of the map
2015-10-28 23:55:11 +08:00
2016-03-22 21:19:50 +08:00
format | the format for the image, supported types: `png` , `jpg`
2015-10-28 23:55:11 +08:00
--- | ---
| _ jpg | will have a default quality of 85.
2015-10-22 19:28:08 +08:00
### Bounding Box
#### Definition
```bash
2016-03-22 21:19:50 +08:00
GET /api/v1/map/static/bbox/{token}/{bbox}/{width}/{height}.{format}`
2015-10-22 19:28:08 +08:00
```
#### Params
2015-10-28 23:55:11 +08:00
Param | Description
--- | ---
2016-03-22 21:19:50 +08:00
token | the layergroupid token from the map instantiation
2015-10-28 23:55:11 +08:00
2016-03-22 21:19:50 +08:00
bbox | the bounding box in WGS 84 (EPSG:4326), comma separated values for:
2015-10-28 23:55:11 +08:00
--- | ---
| LowerCorner longitude, in decimal degrees (aka most western)
| LowerCorner latitude, in decimal degrees (aka most southern)
| UpperCorner longitude, in decimal degrees (aka most eastern)
| UpperCorner latitude, in decimal degrees (aka most northern)
2016-03-22 21:19:50 +08:00
width | the width in pixels for the output image
height | the height in pixels for the output image
format | the format for the image, supported types: `png` , `jpg`
2015-10-28 23:55:11 +08:00
--- | ---
| _ jpg | will have a default quality of 85.
2015-10-22 19:28:08 +08:00
2015-10-29 00:38:46 +08:00
Note: you can see this endpoint as
2015-10-22 19:28:08 +08:00
```bash
2016-03-22 23:23:04 +08:00
GET /api/v1/map/static/bbox/{token}/{west},{south},{east},{north}/{width}/{height}.{format}`
2015-10-22 19:28:08 +08:00
```
2016-02-19 02:15:28 +08:00
### Named Map
2015-10-22 19:28:08 +08:00
#### Definition
```bash
2016-03-22 21:19:50 +08:00
GET /api/v1/map/static/named/{name}/{width}/{height}.{format}
2015-10-22 19:28:08 +08:00
```
#### Params
2015-10-28 23:55:11 +08:00
Param | Description
--- | ---
2016-03-22 21:19:50 +08:00
name | the name of the Named Map
width | the width in pixels for the output image
height | the height in pixels for the output image
2015-10-28 23:55:11 +08:00
2016-03-22 21:19:50 +08:00
format | the format for the image, supported types: `png` , `jpg`
2015-10-28 23:55:11 +08:00
--- | ---
| _ jpg | will have a default quality of 85.
2015-10-22 19:28:08 +08:00
2016-05-26 22:22:52 +08:00
A Named Maps static image will get its constraints from the [`view` argument of the Create Named Map function ](http://docs.carto.com/carto-engine/maps-api/named-maps/#arguments ). If `view` is not defined, it will estimate the extent based on the involved tables, otherwise it fallbacks to `"zoom": 1` , `"lng": 0` and `"lat": 0` .
2015-10-22 19:28:08 +08:00
2015-10-28 23:55:11 +08:00
#### Layers
2015-10-22 19:28:08 +08:00
The Static Maps API allows for multiple layers of incorporation into the `MapConfig` to allow for maximum versatility in creating a static map. The examples below were used to generate the static image example in the next section, and appear in the specific order designated.
**Basemaps**
```javascript
2015-10-29 00:00:22 +08:00
{
"type": "http",
"options": {
"urlTemplate": "http://{s}.basemaps.cartocdn.com/dark_nolabels/{z}/{x}/{y}.png",
"subdomains": [
"a",
"b",
"c"
]
}
}
2015-10-22 19:28:08 +08:00
```
By manipulating the `"urlTemplate"` custom basemaps can be used in generating static images. Supported map types for the Static Maps API are:
2015-10-29 00:02:16 +08:00
```javascript
2015-10-28 23:55:11 +08:00
'http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png',
'http://{s}.basemaps.cartocdn.com/dark_nolabels/{z}/{x}/{y}.png',
'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png',
'http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png',
```
2015-10-22 19:28:08 +08:00
**Mapnik**
```javascript
2015-10-28 23:55:11 +08:00
{
"type": "mapnik",
"options": {
"sql": "select null::geometry the_geom_webmercator",
"cartocss": "#layer {\n\tpolygon-fill: #FF3300 ;\n\tpolygon-opacity: 0;\n\tline-color: #333 ;\n\tline-width: 0;\n\tline-opacity: 0;\n}",
"cartocss_version": "2.2.0"
}
},
2015-10-22 19:28:08 +08:00
```
2016-06-01 01:40:41 +08:00
**CARTO**
2015-10-22 19:28:08 +08:00
2016-06-01 01:40:41 +08:00
As described in the [MapConfig File Format ](http://docs.carto.com/carto-engine/maps-api/mapconfig/ ), a "cartodb" type layer is now just an alias to a "mapnik" type layer as above, intended for backwards compatibility.
2015-10-22 19:28:08 +08:00
```javascript
2015-10-28 23:55:11 +08:00
{
2016-06-01 01:40:41 +08:00
"type": "cartodb",
2015-10-28 23:55:11 +08:00
"options": {
"sql": "select * from park",
"cartocss": "/** simple visualization */\n\n#park {\n polygon-fill: #229A00 ;\n polygon-opacity: 0.7;\n line-color: #FFF ;\n line-width: 0;\n line-opacity: 1;\n}",
"cartocss_version": "2.1.1"
}
}
2015-10-22 19:28:08 +08:00
```
Additionally, static images from Torque maps and other map layers can be used together to generate highly customizable and versatile static maps.
### Caching
2016-06-01 01:40:41 +08:00
It is important to note that generated images are cached from the live data referenced with the `layergroupid token` on the specified CARTO account. This means that if the data changes, the cached image will also change. When linking dynamically, it is important to take into consideration the state of the data and longevity of the static image to avoid broken images or changes in how the image is displayed. To obtain a static snapshot of the map as it is today and preserve the image long-term regardless of changes in data, the image must be saved and stored locally.
2015-10-22 19:28:08 +08:00
### Limits
2017-01-18 00:21:23 +08:00
* While images can encompass an entirety of a map, the limit for pixel range is 8192 x 8192.
* Image resolution is set to 72 DPI
* JPEG quality is 85%
2016-11-02 21:34:55 +08:00
* Timeout limits for generating static maps are the same across CARTO Builder and CARTO Engine. It is important to ensure timely processing of queries.
2017-05-03 00:48:55 +08:00
* If you are publishing your map as a static image with the API, you must manually add [attributions ](https://carto.com/attribution ) for your static map image. For example, add the following attribution code:
2017-10-17 03:54:49 +08:00
{% highlight javascript %}
attribution: '© < a href = "http://www.openstreetmap.org/copyright" > OpenStreetMap< / a > contributors, © < a href = "https://carto.com/attributions" > CARTO< / a >
2017-05-03 00:48:55 +08:00
{% endhighlight %}
2015-10-22 19:28:08 +08:00
## Examples
2016-06-01 01:40:41 +08:00
After instantiating a map from a CARTO account:
2015-10-22 19:28:08 +08:00
2015-10-28 23:55:11 +08:00
#### Call
2015-10-22 19:28:08 +08:00
```bash
2016-03-22 23:23:04 +08:00
GET /api/v1/map/static/center/{layergroupid}/{z}/{x}/{y}/{width}/{height}.png
2015-10-22 19:28:08 +08:00
```
2015-10-28 23:55:11 +08:00
#### Response
2015-10-22 19:28:08 +08:00
< p class = "wrap-border" > < img src = "https://raw.githubusercontent.com/namessanti/Pictures/master/static_api.png" alt = "static-api" / > < / p >
### MapConfig
For this map, the multiple layers, order, and stylings are defined by the MapConfig.
```javascript
{
"version": "1.3.0",
"layers": [
{
"type": "http",
"options": {
"urlTemplate": "http://{s}.basemaps.cartocdn.com/dark_nolabels/{z}/{x}/{y}.png",
"subdomains": [
"a",
"b",
"c"
]
}
},
{
"type": "mapnik",
"options": {
"sql": "select null::geometry the_geom_webmercator",
"cartocss": "#layer {\n\tpolygon-fill: #FF3300 ;\n\tpolygon-opacity: 0;\n\tline-color: #333 ;\n\tline-width: 0;\n\tline-opacity: 0;\n}",
"cartocss_version": "2.2.0"
}
},
{
2016-06-01 01:40:41 +08:00
"type": "cartodb",
2015-10-22 19:28:08 +08:00
"options": {
"sql": "select * from park",
"cartocss": "/** simple visualization */\n\n#park {\n polygon-fill: #229A00 ;\n polygon-opacity: 0.7;\n line-color: #FFF ;\n line-width: 0;\n line-opacity: 1;\n}",
"cartocss_version": "2.1.1"
}
},
{
2016-06-01 01:40:41 +08:00
"type": "cartodb",
2015-10-22 19:28:08 +08:00
"options": {
"sql": "select * from residential_zoning_2009",
"cartocss": "/** simple visualization */\n\n#residential_zoning_2009 {\n polygon-fill: #c7eae5 ;\n polygon-opacity: 1;\n line-color: #FFF ;\n line-width: 0.2;\n line-opacity: 0.5;\n}",
"cartocss_version": "2.1.1"
}
},
{
2016-06-01 01:40:41 +08:00
"type": "cartodb",
2015-10-22 19:28:08 +08:00
"options": {
"sql": "select * from nycha_developments_july2011",
"cartocss": "/** simple visualization */\n\n#nycha_developments_july2011 {\n polygon-fill: #ef3b2c ;\n polygon-opacity: 0.7;\n line-color: #FFF ;\n line-width: 0;\n line-opacity: 1;\n}",
"cartocss_version": "2.1.1"
}
}
]
}
```