Commit 2d8df9fc authored by Adrien Pavie's avatar Adrien Pavie
Browse files

Improve processing of boundaries in DB

parent 8420c25f
......@@ -13,9 +13,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed
- Added contact information in various themes (cinema, hosting, public_service, restaurant, shop_craft_office, toilets)
- Improve performance of dump exports (single table + several ogr2ogr calls)
- Post-process operations on database moved from `db/run.sh` to `db/post_process.sql`
### Fixed
- URL in metadata file for dump exports
- Exclude cities along national border (side-effect of raw PBF dump borders)
## 0.2.2 - 2020-06-25
......
......@@ -96,7 +96,7 @@ tables.line = osm2pgsql.define_table{
ids = { type = 'way', id_column = 'osm_id' },
columns = {
{ column = 'boundary', type = 'text' },
{ column = 'admin_level', type = 'text' },
{ column = 'admin_level', type = 'int4' },
{ column = 'leisure', type = 'text' },
{ column = 'amenity', type = 'text' },
{ column = 'tourism', type = 'text' },
......@@ -120,7 +120,7 @@ tables.polygon = osm2pgsql.define_table{
ids = { type = 'area', id_column = 'osm_id' },
columns = {
{ column = 'boundary', type = 'text' },
{ column = 'admin_level', type = 'text' },
{ column = 'admin_level', type = 'int4' },
{ column = 'leisure', type = 'text' },
{ column = 'amenity', type = 'text' },
{ column = 'tourism', type = 'text' },
......@@ -139,6 +139,7 @@ tables.polygon = osm2pgsql.define_table{
{ column = 'toilets', type = 'text' },
{ column = 'drinking_water', type = 'text' },
{ column = 'waterway', type = 'text' },
{ column = 'ref:INSEE', type = 'text' },
{ column = 'tags', type = 'hstore' },
{ column = 'rel_tags', type = 'hstore' },
{ column = 'way', type = 'geometry' }
......
--
-- Post process of SQL database
--
-- Add relation tags on nodes
CREATE INDEX planet_osm_relation_node_node_id_idx ON planet_osm_relation_node(node_id);
CREATE AGGREGATE array_agg_flat(anyarray) (sfunc = array_cat, stype = anyarray, initcond = '{}');
UPDATE planet_osm_point
SET rel_tags = a.rel_tags
FROM (
SELECT node_id, hstore(array_agg_flat(hstore_to_array(rel_tags))) AS rel_tags
FROM planet_osm_relation_node
GROUP BY node_id
) a WHERE a.node_id = osm_id;
-- Add centroid for geometries
ALTER TABLE planet_osm_line ADD COLUMN centroid GEOMETRY(Point, 3857);
ALTER TABLE planet_osm_polygon ADD COLUMN centroid GEOMETRY(Point, 3857);
UPDATE planet_osm_line SET centroid = ST_Centroid(way);
UPDATE planet_osm_polygon SET centroid = ST_Centroid(way);
CREATE INDEX planet_osm_line_centroid_idx ON planet_osm_line USING GIST(centroid);
CREATE INDEX planet_osm_polygon_centroid_idx ON planet_osm_polygon USING GIST(centroid);
-- Add boundaries for API requests
CREATE EXTENSION pg_trgm;
CREATE EXTENSION unaccent;
CREATE TABLE boundary AS
SELECT
osm_id,
COALESCE(tags->'ref:INSEE', tags->'ref:FR:SIREN', tags->'ref:NUTS', tags->'ref') AS ref,
CASE WHEN boundary = 'administrative' THEN 'admin_'||admin_level ELSE boundary END AS type,
tags->'name' AS name,
lower(unaccent(tags->'name')) AS simple_name,
ST_SimplifyPreserveTopology(way, 10) AS way,
ST_Centroid(way) AS center
FROM planet_osm_polygon
WHERE
(boundary IN ('administrative', 'political') AND "ref:INSEE" IS NOT NULL)
OR boundary = 'local_authority';
CREATE INDEX boundary_osm_id_idx ON boundary(osm_id);
CREATE INDEX boundary_way_idx ON boundary USING GIST(way);
CREATE INDEX boundary_center_idx ON boundary USING GIST(center);
CREATE INDEX boundary_ref_trgm_idx ON boundary USING GIST(ref gist_trgm_ops);
CREATE INDEX boundary_simple_name_trgm_idx ON boundary USING GIST(name gist_trgm_ops);
CREATE INDEX boundary_type_idx ON boundary(type);
......@@ -106,14 +106,8 @@ for col in ${used_tags}; do
done
set -e
# Add relation tags on nodes
psql -h "${PG_HOST}" -U "${PG_USER}" -p "${PG_PORT}" -d "${PG_DB_TMP}" -c "CREATE INDEX planet_osm_relation_node_node_id_idx ON planet_osm_relation_node(node_id); CREATE AGGREGATE array_agg_flat(anyarray) (sfunc = array_cat, stype = anyarray, initcond = '{}'); UPDATE planet_osm_point SET rel_tags = a.rel_tags FROM (SELECT node_id, hstore(array_agg_flat(hstore_to_array(rel_tags))) AS rel_tags FROM planet_osm_relation_node GROUP BY node_id) a WHERE a.node_id = osm_id;"
# Add centroid for geometries
psql -h "${PG_HOST}" -U "${PG_USER}" -p "${PG_PORT}" -d "${PG_DB_TMP}" -c "ALTER TABLE planet_osm_line ADD COLUMN centroid GEOMETRY(Point, 3857); ALTER TABLE planet_osm_polygon ADD COLUMN centroid GEOMETRY(Point, 3857); UPDATE planet_osm_line SET centroid = ST_Centroid(way); UPDATE planet_osm_polygon SET centroid = ST_Centroid(way); CREATE INDEX planet_osm_line_centroid_idx ON planet_osm_line USING GIST(centroid); CREATE INDEX planet_osm_polygon_centroid_idx ON planet_osm_polygon USING GIST(centroid);"
# Add boundaries for API requests
psql -h "${PG_HOST}" -U "${PG_USER}" -p "${PG_PORT}" -d "${PG_DB_TMP}" -c "CREATE EXTENSION pg_trgm; CREATE EXTENSION unaccent; CREATE TABLE boundary AS SELECT osm_id, COALESCE(tags->'ref:INSEE', tags->'ref:FR:SIREN', tags->'ref:NUTS', tags->'ref') AS ref, CASE WHEN boundary = 'administrative' THEN 'admin_'||admin_level ELSE boundary END AS type, tags->'name' AS name, lower(unaccent(tags->'name')) AS simple_name, ST_SimplifyPreserveTopology(way, 10) AS way, ST_Centroid(way) AS center FROM planet_osm_polygon WHERE boundary IN ('administrative', 'local_authority', 'political') AND (admin_level IS NULL OR (admin_level::int >= 6 AND admin_level::int <= 8)); CREATE INDEX boundary_osm_id_idx ON boundary(osm_id); CREATE INDEX boundary_way_idx ON boundary USING GIST(way); CREATE INDEX boundary_center_idx ON boundary USING GIST(center); CREATE INDEX boundary_ref_trgm_idx ON boundary USING GIST(ref gist_trgm_ops); CREATE INDEX boundary_simple_name_trgm_idx ON boundary USING GIST(name gist_trgm_ops); CREATE INDEX boundary_type_idx ON boundary(type);"
# Launch post-process script
psql -h "${PG_HOST}" -U "${PG_USER}" -p "${PG_PORT}" -d "${PG_DB_TMP}" -f post_process.sql
# JSON boundary version for API
bounds_json="
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment