3

I have a dump of OSM data in my city and wrote a query to polygonize it. In the first attempt, the result looked like this:

enter image description here

The problem was that OSM lines were not split and I guessed that the polygonizer only made polygons of entire linestrings:

enter image description here

^^ here, the green nodes are used for polygons, the blue ones are ignored, together with line 3.

Since st_split supports slice by multiple points only in PostGIS 2.2, I decided to write a Python script to do this. Together with some SQL code, it splits lines into segments that respect intersections. Every road joining an intersection is unique.

The way I understand it now:

enter image description here

The SQL code:

CREATE VIEW nodes AS
SELECT ROW_NUMBER() OVER () id, st_node(way) nd
FROM rd_sliced_roads;


DROP VIEW IF EXISTS pg;
CREATE VIEW pg AS
WITH   
    polygroup AS (SELECT ST_Polygonize(way) initial FROM nodes),
    dumped AS (SELECT (st_dump(initial)).geom result FROM polygroup)
SELECT ROW_NUMBER() OVER () id, result
FROM dumped;

The result is still quite similar: polygons are ignored, even though lines join each other. Red lines are both source and st_nodified lines. Why does st_polygonize ignore these small quarters?

enter image description here

What am I missing here?

3

Turned out, doing st_union instead of st_node did the trick. I still wonder what st_polygonize is so sensitive to?

enter image description here

So, the algorythm to make polygons is very simple:

with united as (select st_union(way) w from planet_osm_lines /* filter highways here */),
     polied as (select st_polygonize(w) w from united)
select (st_dump(w)).geom poly from polied;
  • 1
    Duplicated segments can throw it off; these would be removed by ST_Union. – dbaston Apr 11 '16 at 12:37
  • @dbaston I'll check that, but don't think that was the case. – culebrón Apr 11 '16 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.