python - geopandas 无法识别多边形中的点
问题描述
我有两个数据框。一个有建筑物的多边形(大约 70K),另一个有可能在或不在多边形内的点(大约 100K)。我需要确定一个点是否在多边形内。
当我绘制两个数据框(下面的示例)时,该图显示一些点在多边形内,而其他点不在。但是,当我使用 .within() 时,结果显示没有一个点在多边形内。
我重新创建了“手动”创建一个多边形和一个点的示例,而不是导入数据,在这种情况下,.within() 确实识别出该点位于多边形中。因此,我认为我犯了一个错误,但我不知道在哪里。
示例:(为简单起见,我只发布对应于一个点和一个多边形的部分。在这种情况下,每个数据框都包含一个点或一个多边形)
1) 使用导入的数据。数据框 dmR 有点,数据框 dmf 有多边形
import pandas as pd
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
from shapely import wkt
from shapely.geometry import Point, Polygon
plt.style.use("seaborn")
# I'm skipping the data manipulation stage and
# going to the point where the data are used.
print(dmR)
geometry
35 POINT (-95.75207 29.76047)
print(dmf)
geometry
41964 POLYGON ((-95.75233 29.76061, -95.75194 29.760...
# Plot
fig, ax = plt.subplots(figsize=(5,5))
minx, miny, maxx, maxy = ([-95.7525, 29.7603, -95.7515, 29.761])
ax.set_xlim(minx, maxx)
ax.set_ylim(miny, maxy)
dmR.plot(ax=ax, c='Red')
dmf.plot(ax=ax, alpha=0.5)
plt.savefig('imported_data.png')
结果 显示该点在多边形内。然而,
print(dmR.within(dmf))
35 False
41964 False
dtype: bool
2)如果我尝试手动重新创建它,它将如下(可能有更好的方法来做到这一点,但我无法弄清楚):
# Get the vertices of the polygon to create it by hand
poly1 = dmf['geometry']
g = [i for i in poly1]
x,y = g[0].exterior.coords.xy
x,y
(array('d', [-95.752332508564, -95.75193554162979, -95.75193151831627, -95.75232848525047, -95.752332508564]),
array('d', [29.760606530637265, 29.760607694859385, 29.76044470363038, 29.76044237518235, 29.760606530637265]))
# Create the polygon by hand using the corresponding vertices
coords = [(-95.752332508564, 29.760606530637265),
(-95.75193554162979, 29.760607694859385),
(-95.75193151831627, 29.7604447036303),
(-95.75232848525047, 29.76044237518235),
(-95.752332508564, 29.760606530637265)]
poly = Polygon(coords)
# Create point by hand (just copy the point from 1) above
p1 = Point(-95.75207, 29.76047)
# Create the GeoPandas data frames from the point and polygon
ex = gpd.GeoDataFrame()
ex['geometry']=[poly]
ex = ex.set_geometry('geometry')
ex_p = gpd.GeoDataFrame()
ex_p['geometry'] = [p1]
ex_p = ex_p.set_geometry('geometry')
# Plot and print
fig, ax = plt.subplots(figsize=(5,5))
ax.set_xlim(minx, maxx)
ax.set_ylim(miny, maxy)
ex_p.plot(ax=ax, c='Red')
ex.plot(ax = ax, alpha=0.5)
plt.savefig('by_hand.png')
在这种情况下,结果也会显示多边形中的点。然而,
ex_p.within(ex)
0 True
dtype: bool
它认识到该点在多边形中。感谢所有关于做什么的建议!谢谢。
解决方案
我认为您的坐标参考系(crs)有些可疑。我无法说明,dmr
因为它没有提供,而是ex_p
一个幼稚的几何图形,因为您从点生成它而不指定 crs。您可以使用以下命令检查 crs:
dmr.crs
假设它在 4326 中,那么它将返回:
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
在这种情况下,您需要为ex_p
第一次使用设置 CRS:
ex_p = ex_p.set_crs(epsg=4326)
如果要dmr
动态继承 crs ,还可以使用:
ex_p = ex_p.set_crs(dmr.crs)
设置 crs 后,您可以使用以下命令从一个 crs 重新投影到另一个 crs:
ex_p = ex_p.to_crs(epsg=3395)
有关该主题的更多信息: https ://geopandas.org/projections.html
推荐阅读
- python - 如何在 Python 中实现可以解决奇数/偶数/素数数独难题的约束满足问题?
- java - Android java:如何创建 POJO 并将其转换为 Cloud Firestore REST API 可接受的 JSON
- c# - 管道缓冲区保留直到处理完成
- python - Python 检查两个列表
- .net - 在 .Net Core 中检测 Windows 代理设置
- symfony - 通过 phpspreadsheet 从 Excel 文件导入数据并检查数据库 symfony 4 中是否存在记录
- python-3.x - 如何计算连续字符?
- php - 如何返回找到的项目的结果。但也有一个未找到的项目列表
- kubernetes - 节点中的本地持久卷(尽管使用它的 pod 可以在另一个节点中重新调度,但仍然可以访问)
- php - 如何将目录字符串处理成更小的目录字符串