python - Python Shapely 似乎为多边形分配了错误的点
问题描述
我正在使用 shapely 将坐标映射到 geojson 文件,但似乎映射是错误的。在下图中(来自 geojson.io),您可以看到多边形,黄色是我要映射的点。在这种情况下,匀称地告诉我该点位于多边形内部,但正如您所见,这是错误的。
我的代码:
import json
from shapely.geometry import shape, Point
upzs = open('upzs.geojson', encoding='utf-8')
upzs = json.load(upzs)
point = Point(-74.09008026123047,4.719461869021348) # longitude, latitude
for feature in upzs['features']:
polygon = shape(feature['geometry'])
if point.within(polygon) == True:
print(feature['properties']['NOMBRE'])
if polygon.contains(point):
print(feature['properties']['NOMBRE'])
我的输出:
EL RINCON
EL RINCON
('EL RINCON' 是错误多边形的名称)
如果您想测试它,您可以从此链接下载 geojson 文件
解决方案
您确定您发布的图像真的是 GeoJson 文件中的 EL RINCON 吗?
当我在 jupyter notebook 上运行以下内容时,我得到了一个非常不同的形状。
import json
from shapely.geometry import shape, Point
upzs = open('pensionadosactivosupz.geojson', encoding='utf-8')
upzs = json.load(upzs)
point = Point(-74.09008026123047,4.719461869021348) # longitude, latitude
for feature in upzs['features']:
polygon = shape(feature['geometry'])
if point.within(polygon) == True:
print(feature['properties']['NOMBRE'])
if polygon.contains(point):
print(feature['properties']['NOMBRE'])
display(polygon)
此外,如果我映射该点(使用其他包,都在 pip 上可用),则该点包含在多边形中。找到底部的白色圆圈。
import matplotlib.pyplot as plt
import mplleaflet
import geopandas as gpd
from shapely.geometry import shape, Point
p = Point(-74.09008026123047,4.719461869021348)
x = gpd.read_file("pensionadosactivosupz.geojson")
fig, ax = plt.subplots()
x[x.NOMBRE=="EL RINCON"].plot(ax=ax)
ax.scatter([-74.09008026123047], [4.719461869021348], c="white")
mplleaflet.show()
推荐阅读
- ios - 是否可以使用 FaceID 单元 IR 传感器
- android-studio - Kotlin - 不重复的随机数
- reactjs - React、Apollo、GraphQL - 尝试从返回的查询中输出数据的问题
- google-kubernetes-engine - GKE 上的进程自行完成并重新启动
- html - 您可以在悬停时选择任何类还是只选择后代?
- perl - 我的 Perl 变量到变量替换不起作用
- r - 如何使用连接通过两个变量组合两个数据框并使用第二个变量保留不同的行
- r - R解释中的for循环
- javascript - Selection modify by sentence 选择2个句子
- azure - 如何在语言生成模板中使用动态值?