This post is another Spatial Thoughts Academy Weekly Challenge solutions. The challenge is to find the edge of the polygon in a set of building footprints whose center point is closest to a street and then connect it with the nearest point on the street. Shapely and Fiona were of course up to the challenge. The basic algorithm is to grab the exterior-most ring of the building footprints, break out the edges, determine the midpoint of each edge, measure the distance to each road until the shortest distance is found, then grab the nearest point on the road, and finally draw a line between the edge midpoint and the nearest road point. Shapely's nearest_points() method is really versatile here because you can throw any two geometries at it even if they are different types. Fiona is used to read and write geopackages which I'm really liking lately as more an more people are using them finally. Here's the code:
Friday, August 20, 2021
Thursday, August 12, 2021
Extracting MultiPolygons with Holes using Shapely and Fiona
SpatialThoughts.com recently posted a challenge on LinkedIn to extract only building footprints with holes from a city-wide dataset.
What made the challenge interesting is the building footprints are MultiPolygons instead of simple Polygons which stumped a few people's attempts. But this challenge is very straight-forward Shapely to select the polygons and Fiona to read and write the data.
The correct feature count if you extract all of the building footprints with holes or polygon inner rings is 45. The challenge author later demonstrated how to do it in QGIS using a series of expressions. I think I had the only Python solution. One guy posted a successful answer in R.
The challenge data is a city extracted from OSM: https://lnkd.in/g7dVqzMx
Here is my solution:
Subscribe to:
Posts (Atom)