簡體   English   中英

Python Shapely 似乎為多邊形分配了錯誤的點

[英]Python Shapely seems to assign wrong point to polygon

我正在使用 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()

在此處輸入圖片說明

我不確定,也許你顯示了錯誤的多邊形?

這是我的代碼

import json
from shapely.geometry import shape, Point
import folium

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'])
        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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM