How do you get/extract the points that define a shapely
polygon? Thanks!
Example of a shapely polygon
from shapely.geometry import Polygon # Create polygon from lists of points x = [list of x vals] y = [list of y vals] polygon = Polygon(x,y)
One of the simplest and most efficient ways of getting a list of points inside a polygon with Python is to rely on the Geopandas library and perform a spatial join. To do this, we simply create one geodataframe with points and another one with polygons, and join them with 'sjoin'.
Shapely is an offshoot of the GIS-Python project that provides spatial geometry functions independent of any geo-enabled database. In particular, it makes python point-in-polygon calculations very easy.
So, I discovered the trick is to use a combination of the Polygon
class methods to achieve this.
If you want geodesic coordinates, you then need to transform these back to WGS84 (via pyproj
, matplotlib
's basemap
, or something).
from shapely.geometry import Polygon #Create polygon from lists of points x = [list of x vals] y = [list of y vals] some_poly = Polygon(x,y) # Extract the point values that define the perimeter of the polygon x, y = some_poly.exterior.coords.xy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With