我试图在shapefile的多边形内找到一个点。
我需要编写一个循环,该循环可以在多边形上循环并返回该点所在的多边形的索引。
如何编写循环以找出该点位于哪个多边形中?
到目前为止,这是我写的内容:
import pandas as pd
import pylab as pl
import os
import zipfile
import geopandas as gp
import shapely
%pylab inline
# Read in the shapefile
ct_shape = gp.read_file(path)
# Segmented the file so it only contains Brooklyn data & set projection
ct_latlon = ct_shape[ct_shape.BoroName == 'Brooklyn']
ct_latlon = ct_latlon.to_crs({'init': 'epsg:4326'})
ct_latlon.head()
# Dataframe image
[Head of the dataframe image][1]: https://i.stack.imgur.com/xAl6m.png
# Created a point that I need to look for within the shapefile
CUSP = shapely.geometry.Point(40.693217, -73.986403)
输出可能是这样的:“ 3001100”(正确多边形的BCTCB2010)
最佳答案
我用一行代码解决了它。无需循环。
发布给其他可能感兴趣的人:
# Setting the coordinates for the point
CUSP = shapely.geometry.Point((-73.986403, 40.693217,)) # Longitude & Latitude
# Printing a list of the coords to ensure iterable
list(CUSP.coords)
# Searching for the geometry that intersects the point. Returning the index for the appropriate polygon.
index = ct_latlon[ct_latlon.geometry.intersects(CUSP)].BCTCB2010.values[0]