Skip to content

Commit

Permalink
Fix boundary_labels
Browse files Browse the repository at this point in the history
Use shape file because Tilemaker does not have a PointOnSurface
function.
  • Loading branch information
Nakaner committed Mar 17, 2022
1 parent 1f04fce commit dcc69af
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
48 changes: 48 additions & 0 deletions boundary_labels_shape_file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Boundary Labels Shape File

The *boundary_labels* layer uses a shape file of labelling points for administrative polygons as data source
because Tilemaker offers a centroid but no PointOnSurface function. This file explains how to create the shape file.

## Content of the Shape File

The shape file must have the following fields:

* `admin_leve`: value of OSM `admin_level=*` tag
* `name`: value of OSM `name=*` tag
* `name_de`: value of OSM `name:de=*` tag
* `name_en`: value of OSM `name:en=*` tag
* `way_area`: polygon area in ha in Web Mercator projection

The features must be sorted by `way_area` in descending order.

The shape file must contain features with `admin_level=2` and `admin_level=4` only.

## Create Shape File with Osmium, Osm2pgsql and PostGIS

You can create the shape file with Osmium, Osm2pgsql and PostGIS:

Filter the planet dump using Osmium:

```sh
osmium tags-filter -o admin.osm.pbf planet-latest.osm.pbf r/admin_level=2 r/admin_level=4
```

Create database and enable PostGIS and Hstore support (you might have to create the database superuser using `createuser` first):

```sh
createdb -E utf8 -O $USER adminpolygons
psql -d adminpolygons -c "CREATE EXTENSION postgis;"
psql -d adminpolygons -c "CREATE EXTENSION hstore;"
```

Import filtered planet dump:

```sh
osm2pgsql -d adminpolygons --hstore --multi-geometry --latlong admin.osm.pbf
```

Export shape file:

```sh
pgsql2shp -f admin-points.shp adminpolygons "SELECT admin_level, name, tags->'name:de' AS name_de, tags->'name:en' AS name_en, ST_Area(ST_Transform(way, 3857)) / 10000 AS way_area, ST_PointOnSurface(way) AS geom FROM planet_osm_polygon WHERE osm_id < 0 AND boundary = 'administrative' AND admin_level IN ('2', '4') ORDER BY way_area DESC;"
```
4 changes: 3 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
},
"boundary_labels": {
"minzoom": 2,
"maxzoom": 14
"maxzoom": 14,
"source": "data/admin-points-4326/admin_points.shp",
"source_columns": true
},
"addresses": {
"minzoom": 14,
Expand Down
19 changes: 16 additions & 3 deletions process.lua
Original file line number Diff line number Diff line change
Expand Up @@ -757,9 +757,6 @@ function way_function(way)
end

-- Layer boundaries
if way:Find("boundary") == "administrative" then
process_boundary_labels(way)
end
process_boundary_lines(way)

-- Layer streets, street_labels
Expand Down Expand Up @@ -824,5 +821,21 @@ function attribute_function(attr, layer)
attributes["y"] = 0
return attributes
end
if layer == "boundary_labels" then
attributes = {}
attributes["admin_level"] = attr["admin_leve"]
if attributes["admin_level"] == nil then
attributes["admin_level"] = attr["ADMIN_LEVE"]
end
keys = {"name", "name_de", "name_en", "way_area"}
for index, value in ipairs(keys) do
if attr[value] == nil then
attributes[value] = attr[string.upper(value)]
else
attributes[value] = attr[value]
end
end
return attributes
end
return attr
end

0 comments on commit dcc69af

Please sign in to comment.