Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

Various updates for recent re-import

Russ Garrett6e078376db6e
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..81a2b47
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,42 @@
+volumes:
+ postgres_data:
+ imposm_cache:
+ imposm_expiry:
+ imposm_diff:
+
+services:
+ db:
+ image: postgis/postgis:17-3.4-alpine
+ shm_size: 128mb
+ environment:
+ POSTGRES_PASSWORD: postgres
+ volumes:
+ - postgres_data:/var/lib/postgresql/data
+ - ./schema/dev-init.sql:/docker-entrypoint-initdb.d/90-dev-init.sql
+ - ./schema/functions.sql:/docker-entrypoint-initdb.d/91-functions.sql
+
+ imposm:
+ build: imposm
+ volumes:
+ - imposm_cache:/imposm-cache
+ - imposm_expiry:/imposm-expiry
+ - imposm_diff:/imposm-diff
+ command:
+ - run
+ - -expiretiles-dir
+ - /imposm-expiry
+ - -cachedir
+ - /imposm-cache
+ - -diffdir
+ - /imposm-diff
+ - -mapping
+ - /mapping.json
+ - -connection
+ - "postgis://osm:osm@db/osm"
+
+ tegola:
+ build: tegola
+ environment:
+ DB_URI: "postgres://osm:osm@db:5432/osm"
+ ports:
+ - "8080:80"
\ No newline at end of file
diff --git a/imposm/README.md b/imposm/README.md
new file mode 100644
index 0000000..b0bfff5
--- /dev/null
+++ b/imposm/README.md
@@ -0,0 +1,58 @@
+# OpenInfraMap Database
+
+The OpenInfraMap database is a subset of OSM replicated by [imposm3](https://imposm.org).
+
+## DB Setup
+### Development
+There's a Docker Compose file in the root of the repository which is primarily intended for testing
+new Imposm configuration. If you're just testing changes to the live website or styles, there's no
+need to run this - you can just use the live tileserver.
+
+You'll need an OSM export file in PBF format. You're probably best off using a Geofabrik subset
+rather than the full planet for testing.
+
+First, start the DB container:
+
+ docker compose up db
+
+This will also create the "osm" database and import [functions.sql](../schema/functions.sql).
+
+Now you can run imposm to import the data:
+
+ docker compose run --rm --build -v $PWD/greater-london-latest.osm.pbf:/data.osm.pbf imposm import -connection postgis://osm:osm@db/osm -mapping /mapping.json -read /data.osm.pbf -write -optimize -deployproduction
+
+Now create the views from [views.sql](../schema/views.sql) - you can open the postgres console with:
+
+ docker compose exec db psql -U osm osm
+
+### Production
+Production config is likely to be more complex and I can't provide support for it. (The live
+OpenInfraMap instance is run as containers using Kubernetes.)
+
+I suggest creating separate Postgres user accounts for imposm and for the tile server.
+
+## Additional data
+The [web backend](../web-backend) requires country EEZ boundaries to be imported into the `countries`
+schema in the same database as the OSM data. These are sourced from the marineregions.org
+[Marine and Land Zones](https://marineregions.org/sources.php#unioneezcountry) dataset.
+
+ shp2pgsql -s 4326 -d ./EEZ_Land_v3_202030.shp countries.country_eez > ./country_eez.sql
+
+We create a materialized view from this, using `ST_Subdivide` to improve indexing performance:
+
+ CREATE MATERIALIZED VIEW countries.country_eez_sub AS
+ SELECT country_eez.gid,
+ country_eez."union",
+ country_eez.mrgid_eez,
+ country_eez.territory1,
+ country_eez.mrgid_ter1,
+ country_eez.iso_ter1,
+ country_eez.iso_sov1,
+ country_eez.pol_type,
+ ST_Subdivide(ST_Transform(country_eez.geom, 3857)) AS geom
+ FROM countries.country_eez
+ WHERE country_eez."union"::text <> 'Antarctica'::text;
+
+ CREATE INDEX country_eez_sub_geom ON countries.country_eez_sub USING GIST (geom);
+ CREATE INDEX country_eez_sub_iso_sov1 ON countries.country_eez_sub(iso_sov1);
+ CREATE INDEX country_eez_sub_iso_ter1 ON countries.country_eez_sub(iso_ter1);
\ No newline at end of file
diff --git a/schema/dev-init.sql b/schema/dev-init.sql
new file mode 100644
index 0000000..3b1183f
--- /dev/null
+++ b/schema/dev-init.sql
@@ -0,0 +1,2 @@
+CREATE ROLE osm PASSWORD 'osm' LOGIN;
+CREATE DATABASE osm OWNER osm;
\ No newline at end of file
diff --git a/schema/functions.sql b/schema/functions.sql
index 7d21f27..d4cf6dd 100644
--- a/schema/functions.sql
+++ b/schema/functions.sql
@@ -1,3 +1,6 @@
+\c osm osm
+
+CREATE EXTENSION IF NOT EXISTS postgis;
CREATE EXTENSION IF NOT EXISTS intarray;
CREATE EXTENSION IF NOT EXISTS hstore;
@@ -119,7 +122,7 @@ DECLARE
BEGIN
parts = string_to_array(a, ';')::INT[];
parts = array_cat(parts, string_to_array(b, ';')::INT[]);
- RETURN array_to_string(uniq(sort_desc(parts)), ';');
+ RETURN array_to_string(public.uniq(public.sort_desc(parts)), ';');
END
$$ LANGUAGE plpgsql;
@@ -249,13 +252,13 @@ $$ LANGUAGE sql;
-- Generate the outline of a distributed power plant
-- ST_ConcaveHull can fail on some geometries. This function tries it, but falls back to a simple buffer otherwise.
-CREATE OR REPLACE FUNCTION simplify_boundary(g1 geometry)
- RETURNS geometry
+CREATE OR REPLACE FUNCTION simplify_boundary(g1 public.geometry)
+ RETURNS public.geometry
LANGUAGE plpgsql
IMMUTABLE STRICT
PARALLEL UNSAFE AS $$
begin
- return st_buffer(st_concavehull(g1, 0.95), 10);
+ return ST_Buffer(ST_ConcaveHull(g1, 0.95), 10);
EXCEPTION
WHEN SQLSTATE 'XX000' THEN
RETURN st_buffer(g1, 10);
diff --git a/schema/prod-init.sql b/schema/prod-init.sql
new file mode 100644
index 0000000..25eca2e
--- /dev/null
+++ b/schema/prod-init.sql
@@ -0,0 +1,12 @@
+CREATE ROLE imposm PASSWORD 'imposm' LOGIN;
+CREATE ROLE tegola PASSWORD 'tegola' LOGIN;
+CREATE ROLE web_backend PASSWORD 'web_backend' LOGIN;
+CREATE DATABASE openinframap OWNER imposm;
+
+GRANT SELECT ON ALL TABLES IN SCHEMA public TO tegola;
+GRANT ALL ON SCHEMA countries TO tegola;
+GRANT SELECT ON ALL TABLES IN SCHEMA countries TO tegola;
+
+GRANT SELECT ON ALL TABLES IN SCHEMA public TO web_backend;
+GRANT ALL ON SCHEMA countries TO web_backend;
+GRANT SELECT ON ALL TABLES IN SCHEMA countries TO web_backend;
\ No newline at end of file
diff --git a/schema/views.sql b/schema/views.sql
index 94a8a5b..9c700e5 100644
--- a/schema/views.sql
+++ b/schema/views.sql
@@ -24,7 +24,7 @@ CREATE OR REPLACE VIEW substation AS
FROM power_substation_relation;
CREATE MATERIALIZED VIEW power_plant_relation AS
- SELECT rel.osm_id, simplify_boundary(ST_Collect(mem.geometry)) AS geometry,
+ SELECT rel.osm_id, ST_Buffer(ST_ConcaveHull(ST_Collect(mem.geometry), 0.95), 10) AS geometry,
(rel.tags -> 'name') AS name, rel.output, rel.source, rel.tags, rel.construction
FROM osm_power_plant_relation as rel, osm_power_plant_relation_member as mem
WHERE mem.osm_id = rel.osm_id