tecznotes

Michal Migurski's notebook, listening post, and soapbox. Subscribe to this blog. Check out the rest of my site as well.

Apr 25, 2012 6:25pm

polygonize it

If you need to make a bag of polygons from a selection of OpenStreetMap data, you would think that simply running shapely.ops.polygonize on the selection of linestrings is enough. In fact, polygonize needs for the line endings to coincide, and may not work as you would expect with OSM data. I scratched my head over this a bit after successfully using polygonize to handle TIGER data, until it occurred to me that TIGER is built to have lots and lot of little lines.

The trick is to split each linestring input to polygonize() into all its constituent segments, and then let the function do the reassembly work for you. Creating the polygons of Amsterdam above from metropolitan extract roads took just a few seconds. Here’s the polygonize.py script that does it, expecting GeoJSON input and producing GeoJSON output.

Comments (2)

  1. Very cool Mike! Also, if you wanted to go the Quantum route, there is a tool called "Lines To Polygon" (Vector->Geometry Tools-> Lines to Polygon). Although it works great, I doubt it could run all of Amsterdam in a couple of seconds. Just another option.

    Posted by Bobby S. on Thursday, April 26 2012 10:45am EDT

  2. Hi Bobby, I actually tried that function in QGIS and it gave me the same unexpected results as PostGIS and Shapely's polygonize without pre-processing the lines. I think they all use the same GEOS function under the hood, so it makes sense. The reason my function works where those don't is that it breaks T intersections from two lines into three, so that the polygonizer can do its work. I think it would have the same result if I saved out a GeoJSON file of the shattered linestrings and then polygonized them in QGIS.

    Posted by Michal Migurski on Thursday, April 26 2012 11:17am EDT

Sorry, no new comments on old posts.

December 2017
Su M Tu W Th F Sa
     
      

Recent Entries

  1. planscore: a project to score gerrymandered district plans
  2. blog all dog-eared pages: human transit
  3. the levity of serverlessness
  4. three open data projects: openstreetmap, openaddresses, and who’s on first
  5. building up redistricting data for North Carolina
  6. district plans by the hundredweight
  7. baby steps towards measuring the efficiency gap
  8. things I’ve recently learned about legislative redistricting
  9. oh no
  10. landsat satellite imagery is easy to use
  11. openstreetmap: robots, crisis, and craft mappers
  12. quoted in the news
  13. dockering address data
  14. blog all dog-eared pages: the best and the brightest
  15. five-minute geocoder for openaddresses
  16. notes on debian packaging for ubuntu
  17. guyana trip report
  18. openaddresses population comparison
  19. blog all oft-played tracks VII
  20. week 1,984: back to the map

Archives