如何使用geojson和shapely确定一个点是否在多边形内部

Jos*_*osh 7 python polygon geojson shapely

我希望在地图上创建一个区域,并能够自动确定点(坐标)是否在该区域内。在本示例中,我使用整个美国的 geojson 文件和纽约市的坐标。

Geojson: https: //github.com/johan/world.geo.json

我已经阅读了 shapely 文档,只是不明白为什么我的结果返回 False。任何帮助将非常感激。

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

with open('USA.geo.json', 'r') as f:
    js = json.load(f)

point = Point(40.712776, -74.005974)

for feature in js['features']:

    polygon = shape(feature['geometry'])

    if polygon.contains(point):
        print ('Found containing polygon:', feature)
Run Code Online (Sandbox Code Playgroud)

我希望打印包含的坐标,但没有打印任何内容。

blu*_*lub 7

您需要交换周围的值Point()

point = Point(-74.005974, 40.712776)
Run Code Online (Sandbox Code Playgroud)

您使用的数据集的坐标首先是经度,其次是纬度。