问题描述
我正在使用 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
如果你想测试它,你可以从这个 link 下载 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()
,
我不确定,也许你显示了错误的多边形?
这是我的代码
import json
from shapely.geometry import shape,Point
import folium
upzs = open('upzs.geojson',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'])
break
m=folium.Map(location=(5,5),zoom_start=6)
folium.Marker([4.719461869021348,-74.09008026123047],popup=folium.Popup('hello',max_width=1000),tooltip='click here').add_to(m)
folium.GeoJson(polygon).add_to(m)
m