Mapbox Tiling Service
Beta

Census block unioning

This example uses United States Census data to show how to union features differently at different zoom levels. It unions the blocks into larger geographies as you zoom out, so the size of features is appropriate to each zoom level. Within each feature resulting from the union, it sums the land area attribute from the source features.

It starts from a tileset source of Census blocks and initially simplifies them outward (outward_only: true) with a small simplification distance (distance: 1) so there will not be any simplification artifacts later in the union operation. It uses set to dynamically create a new attribute that will be used in the union to cause different levels of aggregation of the geography at different zoom levels, using concat in the set expression to combine attributes from the original feature. It starts with only the state ID (STATEFP10) at low zoom levels and, as the zoom level increases, adds the county (COUNTYFP10), tract (TRACTCE10), and block (BLOCKCE10) IDs. The union then specifies to group_by this generated attribute. It also uses sum in the aggregate specification to sum the land area (ALAND10) attribute of the source features into the output feature. Once the union is complete, it applies bidirectional (outward_only: false) simplification with the standard simplification distance (distance: 4) to reduce the tile size.

{
"version": 1,
"layers": {
"census": {
"minzoom": 0,
"maxzoom": 11,
"source": "mapbox://tileset-source/{username}/census-blocks",
"features": {
"simplification": {
"outward_only": true,
"distance": 1
},
"attributes": {
"set": {
"key": [ "concat",
[ "case", [ ">=", [ "zoom" ], 0 ], [ "get", "STATEFP10" ], "" ],
[ "case", [ ">=", [ "zoom" ], 5 ], [ "get", "COUNTYFP10" ], "" ],
[ "case", [ ">=", [ "zoom" ], 8 ], [ "get", "TRACTCE10" ], "" ],
[ "case", [ ">=", [ "zoom" ], 11 ], [ "get", "BLOCKCE10" ], "" ]
]
}
}
},
"tiles": {
"union": [
{
"group_by": [ "key" ],
"aggregate": {
"ALAND10": "sum"
},
"simplification": {
"distance": 4,
"outward_only": false
}
}
]
}
}
}
}
FieldDescriptionData type
sourceThe source data to use for this layer. Tileset sources are created with the Create a tileset source endpoint of Mapbox Tiling Service (MTS).String
minzoomSpecify the minimum zoom at which your tileset will be available. A value of 0 here ensures that the census blocks will be visible at a global level.Integer
maxzoomSpecify the maximum zoom at which your tileset will be available. A value of 11 here ensures that the census blocks will be visible as the user zooms to block level, and will be magnified as they zoom deeper.Integer
features.simplification.outward_onlySpecifies that features will be simplified only outward, so they will overlap slightly and union correctly rather than leaving gaps.Boolean expression
features.simplification.distanceSpecifies that features will be simplified only by a single unit of distance, so even at low zoom levels small blocks will not be simplified away, so they will union correctly rather than leaving gaps.Integer expression
features.attributes.setCreates a new feature attribute from the conditional concatenation of existing attributesObject mapping String to Expression
features.tiles.union.group_bySpecifies that features will be unioned if the key attribute created above matchesArray of String
features.tiles.union.aggregateSpecifies that the land area (ALAND10) attribute of features will be summed as they are unionedObject mapping String to String
features.tiles.union.simplification.distanceSpecifies that after unioning, the features will be simplified to the normal distance of 4.Integer Expression
features.tiles.union.simplification.outward_onlySpecifies that after unioning, the features will may simplified either inward or outward, allowing the smallest final tilesBoolean Expression
Was this page helpful?