Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

SQL tweaks

Russ Garrett859ed8a19c05
diff --git a/schema/functions.sql b/schema/functions.sql
index 83a6765..b0ca7f4 100644
--- a/schema/functions.sql
+++ b/schema/functions.sql
@@ -200,4 +200,18 @@ create or replace function osm_url (tags HSTORE)
returns null on null input AS $$
SELECT COALESCE(tags -> 'website', tags -> 'contact:website', tags -> 'url');
$$ 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 (geometry geometry)
+ returns geometry
+ immutable
+ returns null on null input as $$
+begin
+ return st_buffer(st_concavehull(geometry, 0.95), 10);
+EXCEPTION
+ WHEN SQLSTATE 'XX000' THEN
+ RETURN st_buffer(geometry, 10);
+end
+$$ language plpgsql;
diff --git a/schema/views.sql b/schema/views.sql
index e3c973b..26041f1 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, ST_Buffer(ST_ConcaveHull(ST_Collect(mem.geometry), 0.99), 10) AS geometry,
+ SELECT rel.osm_id, simplify_boundary(ST_Collect(mem.geometry)) 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