Check if a geopoint with latitude and longitude is within a shapefile

前端 未结 7 1431
迷失自我
迷失自我 2020-12-02 06:08

How can I check if a geopoint is within the area of a given shapefile?

I managed to load a shapefile in python, but can\'t get any further.

相关标签:
7条回答
  • 2020-12-02 06:46

    Here is a simple solution based on pyshp and shapely.

    Let's assume that your shapefile only contains one polygon (but you can easily adapt for multiple polygons):

    import shapefile
    from shapely.geometry import shape, Point
    
    # read your shapefile
    r = shapefile.Reader("your_shapefile.shp")
    
    # get the shapes
    shapes = r.shapes()
    
    # build a shapely polygon from your shape
    polygon = shape(shapes[0])    
    
    def check(lon, lat):
        # build a shapely point from your geopoint
        point = Point(lon, lat)
    
        # the contains function does exactly what you want
        return polygon.contains(point)
    
    0 讨论(0)
提交回复
热议问题