Files
yongestreet/duckdb/metro.sql
T
2025-01-02 13:12:38 -05:00

51 lines
1.9 KiB
SQL

INSTALL spatial;
LOAD spatial;
CREATE OR REPLACE VIEW osm_quebec AS (
SELECT
*,
'montreal' AS city
FROM
ST_READOSM (
'/nix/store/j56yncmjh7qgwa5k631vl70mdny7c5y0-data-dir/canada/quebec/montreal_quebec.osm.pbf'
)
);
CREATE OR REPLACE VIEW roads AS (
WITH
interresting_ways AS (SELECT * FROM osm1_quebec
WHERE (
tags['highway'][1] = 'residential'
OR tags['highway'][1] = 'primary'
OR tags['highway'][1] = 'secondary'
OR tags['highway'][1] = 'tertiary'
)
AND city = 'montreal'),
relations AS (SELECT id,
tags,
UNNEST(refs) AS refs,
UNNEST(RANGE(LEN(refs))) as index
FROM interresting_ways),
road_points AS (SELECT osm_quebec.id AS id,
osm_quebec.lon AS lon,
osm_quebec.lat AS lat,
relations.tags AS tags,
relations.index AS relation_index,
relations.id AS relation_id
FROM relations
JOIN osm_quebec
ON osm_quebec.id = relations.refs
WHERE
osm_quebec.kind = 'node'
ORDER BY relation_id, relation_index)
SELECT
relation_id,
LIST([road_points.lat, road_points.lon]) AS points,
ANY_VALUE(tags) AS tags
FROM road_points
GROUP BY relation_id
);
COPY roads TO 'static/data/montreal.csv';
COPY roads TO 'static/data/montreal.json';