osm-rvb/db/sql/scripts/railway.sql

92 lines
3.4 KiB
SQL

-- colon before variable: for a prepared statement using named placeholders, this will be a parameter name of the form :name
CREATE SCHEMA IF NOT EXISTS :schema;
SET search_path to :schema, public;
DROP TABLE IF EXISTS :schema.tbl_railway;
DROP VIEW IF EXISTS :schema.vw_railway;
CREATE OR REPLACE VIEW :schema.vw_railway AS
SELECT foo.id,
foo.osm_id,
foo.railway,
foo.name,
foo.operator,
foo.st_x,
foo.st_y,
foo.osm_typ,
lk.name as landkreis,
CASE WHEN gem.name IS NULL THEN lk.name ELSE gem.name END as gemeinde,
st_geomfromtext('POINT(' || foo.st_x || ' ' || foo.st_y ||')',4326) as the_geom
FROM (
-- Block fuer den Bereich NODE
SELECT 'ON'||cast(p.osm_id as text) as id,
p.osm_id,
p.railway,
p.name AS name,
p.operator,
st_x(st_transform(p.way, 4326)) AS st_x,
st_y(st_transform(p.way, 4326)) AS st_y,
'node'::text AS osm_typ
FROM planet_osm_point p
WHERE
( p.railway = 'halt' OR p.railway = 'station')
AND ( p.usage IS NULL OR p.usage NOT LIKE '%tourism%')
AND p.proposed is null --- damit keine geplanten Gebäude etc übernommen werden
UNION
--- der Block für die Zuordnung WAY/POLYGON(ohne Relation d.h. OSM_id > 0)
SELECT 'OW'||cast(p.osm_id as text) as id,
p.osm_id,
p.railway,
p.name,
p.operator,
st_x(st_centroid(st_transform(p.way, 4326))) AS st_x,
st_y(st_centroid(st_transform(p.way, 4326))) AS st_y,
CASE
WHEN (p.osm_id < 0) THEN 'relation' else 'way' END as osm_typ
---, st_transform(st_centroid(p.way), 4326) AS the_geom
FROM planet_osm_polygon p
WHERE
( p.railway = 'halt' OR p.railway = 'station')
AND ( p.usage IS NULL OR p.usage NOT LIKE '%tourism%')
AND p.proposed is null
AND p.osm_id > 0 -- Ausschluss der Relationen
UNION
--- der Block für die Zuordnung Relation aus POLYGON/WAY (OSM_id < 0)
SELECT rel.id,
rel.osm_id,
rel.railway,
rel.name,
rel.operator,
avg(st_x) as st_x,
avg(st_y) as st_y,
rel.osm_typ
FROM
(SELECT 'OR'||cast(p.osm_id * -1 as text) as id,
p.osm_id,
p.railway,
p.name,
p.operator,
st_x(st_centroid(st_transform(p.way, 4326))) AS st_x,
st_y(st_centroid(st_transform(p.way, 4326))) AS st_y,
CASE
WHEN (p.osm_id < 0) THEN 'relation' else 'way' END as osm_typ
FROM planet_osm_polygon p
WHERE
( p.railway = 'halt' OR p.railway = 'station')
AND ( p.usage IS NULL OR p.usage NOT LIKE '%tourism%')
AND p.proposed is null
AND p.osm_id < 0 -- Relationen
) as rel
GROUP BY
rel.id, rel.osm_id, rel.railway, rel.name, rel.operator, rel.osm_typ
) AS foo
LEFT JOIN vw_counties lk ON st_contains(lk.way, st_transform(st_geomfromtext('POINT(' || foo.st_x || ' ' || foo.st_y ||')',4326),3857))
--- die Zuordnung erfolgt zu den Mitgliedsgemeinden (Admin 8)
LEFT JOIN vw_municipalities gem ON st_contains(gem.way, st_transform(st_geomfromtext('POINT(' || foo.st_x || ' ' || foo.st_y ||')',4326),3857))
WHERE (lk.name = ANY (ARRAY['Braunschweig','Salzgitter','Wolfsburg','Gifhorn','Landkreis Goslar','Landkreis Helmstedt','Landkreis Peine','Landkreis Wolfenbüttel'])) AND foo.name IS NOT NULL
ORDER BY lk.name, gem.name, foo.name;
CREATE TABLE IF NOT EXISTS :schema.tbl_railway AS SELECT * FROM :schema.vw_railway;