Другой способ получить points in polygon
может быть таким:
Давайте создадим файл port_au_princ.csv
со следующим содержимым:
index,geometry
0,POINT(-72.09513801999077 18.6526410972226)
1,POINT(-72.21044508038457 19.61039786418674)
2,POINT(-72.27903674995586 18.52939294725028)
Вот код, которыйвыполняет все необходимые операции.
import pandas as pd
import geopandas as gpd
from geopandas.tools import sjoin
import osmnx as ox
from shapely.wkt import loads
# get polygon of the city
city_pgon = ox.gdf_from_place('Port-au-Prince', which_result = 2)
# create a geoDataFrame out of it, set appropriate CRS
city_pgon = gpd.GeoDataFrame(city_pgon, crs={'init': 'epsg:4326'})
# read the file
pap = pd.read_csv('port_au_princ.csv', sep=',')
geometry = [loads(pt) for pt in pap['geometry']] # prep geometry
# create geoDataFrame from `pap` dataframe
geo_pap = gpd.GeoDataFrame(pap, geometry=geometry, crs={'init': 'epsg:4326'})
pts_in_poly = gpd.sjoin(geo_pap, city_pgon, op='within', how='inner') # do spatial join
ax1 = city_pgon.plot() # plot the polygon
geo_pap.plot(ax=ax1, color='red', zorder=5) # plot red points
pts_in_poly.plot(ax=ax1, color='yellow', zorder=6) # plot yellow point
Сюжет будет:
Распечатайте некоторую информацию pts_in_poly
сэтот код:
print(pts_in_poly.index, pts_in_poly.geometry)
выход,
Int64Index([2], dtype='int64') 2 POINT (-72.27903674995586 18.52939294725028)
Name: geometry, dtype: object