Point in Polygon with geoJSON in Python

前端 未结 3 683
夕颜
夕颜 2021-01-30 09:03

I have a geoJSON database with lots of polygons (census tracts specifically) and I have lots of long,lat points.

I am hoping that there would exist an efficient Python c

3条回答
  •  天涯浪人
    2021-01-30 09:33

    I found an interesting article describing how to do exactly what you are looking to do.

    TL;DR: Use Shapely

    You will find this code at the end of the article:

    import json
    from shapely.geometry import shape, Point
    # depending on your version, use: from shapely.geometry import shape, Point
    
    # load GeoJSON file containing sectors
    with open('sectors.json') as f:
        js = json.load(f)
    
    # construct point based on lon/lat returned by geocoder
    point = Point(-122.7924463, 45.4519896)
    
    # check each polygon to see if it contains the point
    for feature in js['features']:
        polygon = shape(feature['geometry'])
        if polygon.contains(point):
            print 'Found containing polygon:', feature
    

提交回复
热议问题