Skip to content

Instantly share code, notes, and snippets.

@mvexel
Created April 11, 2012 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvexel/2356599 to your computer and use it in GitHub Desktop.
Save mvexel/2356599 to your computer and use it in GitHub Desktop.
problematic postgis query
SET search_path TO us,postgis,hstore;
DROP TABLE IF EXISTS candidates;
CREATE TABLE candidates AS
WITH agg_intersections AS
(
WITH intersection_nodes_wayrefs AS
(
WITH intersection_nodes AS
(
SELECT
a.id AS node_id,
b.way_id,
a.geom
FROM
nodes a,
way_nodes b
WHERE
a.id = b.node_id AND
a.id IN
(
SELECT
DISTINCT node_id
FROM
way_nodes
GROUP BY
node_id
HAVING
COUNT(1) = 2
)
)
SELECT
DISTINCT a.node_id AS node_id,
b.id AS way_id,
b.tags->'highway' AS osm_highway,
a.geom AS geom,
b.tags->'ref' AS osm_ref
FROM
intersection_nodes a,
ways b
WHERE
a.way_id = b.id
)
SELECT
node_id,
array_agg(way_id) AS way_ids,
array_agg(osm_highway) AS osm_highways,
array_agg(osm_ref) AS osm_refs
FROM
intersection_nodes_wayrefs
GROUP BY
node_id
)
SELECT
a.* ,
b.geom AS node_geom,
c.linestring AS way_geom
FROM
agg_intersections a,
nodes b,
ways c
WHERE
(
'motorway' = ANY(osm_highways)
AND NOT
(
'motorway_link' = ANY(osm_highways)
OR
'service' = ANY(osm_highways)
OR
'motorway' = ALL(osm_highways)
OR
'construction' = ANY(osm_highways)
)
)
AND
a.node_id = b.id
AND
c.id = ANY(a.way_ids);
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment