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 10: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 2:45pm UTC

  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 3:17pm UTC

Sorry, no new comments on old posts.

April 2024
Su M Tu W Th F Sa
 
    

Recent Entries

  1. Mapping Remote Roads with OpenStreetMap, RapiD, and QGIS
  2. How It’s Made: A PlanScore Predictive Model for Partisan Elections
  3. Micromobility Data Policies: A Survey of City Needs
  4. Open Precinct Data
  5. Scoring Pennsylvania
  6. Coming To A Street Near You: Help Remix Create a New Tool for Street Designers
  7. planscore: a project to score gerrymandered district plans
  8. blog all dog-eared pages: human transit
  9. the levity of serverlessness
  10. three open data projects: openstreetmap, openaddresses, and who’s on first
  11. building up redistricting data for North Carolina
  12. district plans by the hundredweight
  13. baby steps towards measuring the efficiency gap
  14. things I’ve recently learned about legislative redistricting
  15. oh no
  16. landsat satellite imagery is easy to use
  17. openstreetmap: robots, crisis, and craft mappers
  18. quoted in the news
  19. dockering address data
  20. blog all dog-eared pages: the best and the brightest

Archives