简体   繁体   中英

Python Shapely seems to assign wrong point to polygon

I'm doing some mapping of coordinates to a geojson file using shapely, but it seems that the mapping is wrong. In the image below (from geojson.io) you see the polygon and in yellow the point I want to map. In this case shapely tell me that the point is inside the polygon, but as you see this is false.

在此处输入图片说明

My code:

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

My output:

EL RINCON
EL RINCON

('EL RINCON' is the name of the wrong polygon)

You can donwload the geojson file from this link if you want to test it

Are you sure that your posted image is really EL RINCON in the GeoJson file?

I got a very different shape when I run the below on 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)

在此处输入图片说明

Also, the point is included in the polygon if I map it (using other packages, all available on pip). Find the white circle towards the bottom.

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()

在此处输入图片说明

I'm not sure, maybe you show a wrong polygon ?

This is my code

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM