Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract points/coordinates from a polygon in Shapely

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) 
like image 478
ryanjdillon Avatar asked Dec 09 '13 15:12

ryanjdillon


People also ask

How do you get a list of points in a polygon in Python?

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'.

Is Point in polygon shapely?

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.


1 Answers

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 
like image 96
ryanjdillon Avatar answered Sep 29 '22 23:09

ryanjdillon