我有一个shapefile
的新加坡,我用这种方式可视化:
import shapefile
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
### Plot shapefile
sf = shapefile.Reader("myShape.shp")
recs = sf.records()
shapes = sf.shapes()
Nshp = len(shapes)
cns = []
for nshp in xrange(Nshp):
cns.append(recs[nshp][1])
cns = array(cns)
cm = get_cmap('Dark2')
cccol = cm(1.*arange(Nshp)/Nshp)
# -- plot --
fig,ax = plt.subplots(figsize=(20,10))
for nshp in xrange(Nshp):
ptchs = []
pts = array(shapes[nshp].points)
prt = shapes[nshp].parts
par = list(prt) + [pts.shape[0]]
for pij in xrange(len(prt)):
ptchs.append(Polygon(pts[par[pij]:par[pij+1]]))
ax.add_collection(PatchCollection(ptchs,facecolor=cccol[nshp,:],edgecolor='k', linewidths=.1))
ax.set_xlim(103.6,104.1)
ax.set_ylim(1.15,1.48)
现在我有一个坐标为dataframe
的点列表,我想检查一下这些点的shapefile区域。
mypoints
lon lat
0 103.619740 1.280485
1 103.622632 1.268944
2 103.622632 1.274714
3 103.622632 1.277600
4 103.622632 1.280485
特别是从shapefile
我可以提取特定区域的信息,例如name
。事实上,在recs
我有这方面的信息。例如:
recs[0]:
[42,
1,
'STRAITS VIEW',
'SVSZ01',
'Y',
'STRAITS VIEW',
'SV',
'CENTRAL REGION',
'CR',
'21451799CA1AB6EF',
datetime.date(2016, 5, 11),
30832.9017,
28194.0843,
5277.76082729,
1127297.23737]
在这种情况下,STRAITS VIEW
是该区域的名称。最后,我想要dataframe
喜欢:
mypoints
lon lat name
0 103.619740 1.280485 STRAITS VIEW
1 103.622632 1.268944 STRAITS VIEW
2 103.622632 1.274714 DOWNTOWN
3 103.622632 1.277600 BEDOK
4 103.622632 1.280485 CHANGI
答案 0 :(得分:0)
要检查坐标是否位于某些Polygon内,您可以使用GDAL中的Intersects方法。此方法比较两个OGRGeometry
对象并返回一个布尔值,指示它们是否拦截。
使用此功能,您可以迭代您的点数,并为每个测试点Intersects()
显示所有多边形区域。它应该看起来像:
for point_geom in your_points:
#iterate over your area polygons and check
for area_geom in your_areas:
if area_geom.Intersects(point_geom):
#then you have a match, save or do as you want
注意:如果您的积分不 OGRGeometry
(您的多边形很可能就像您从Shapefile中读取它们一样),您可以创建建议使用的几何图形here:
from osgeo import ogr
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(1198054.34, 648493.09)
以及多边形的情况:
ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(1179091.1646903288, 712782.8838459781)
ring.AddPoint(1161053.0218226474, 667456.2684348812)
#etc...add all your points, in a loop would be better
# Create polygon
poly = ogr.Geometry(ogr.wkbPolygon)
poly.AddGeometry(ring)