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

前端 未结 7 1429
迷失自我
迷失自我 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:27

    If you want to find out which polygon (from a shapefile full of them) contains a given point (and you have a bunch of points as well), the fastest way is using postgis. I actually implemented a fiona based version, using the answers here, but it was painfully slow (I was using multiprocessing and checking bounding box first). 400 minutes of processing = 50k points. Using postgis, that took less than 10seconds. B tree indexes are efficient!

    shp2pgsql -s 4326 shapes.shp > shapes.sql
    

    That will generate a sql file with the information from the shapefiles, create a database with postgis support and run that sql. Create a gist index on the geom column. Then, to find the name of the polygon:

    sql="SELECT name FROM shapes WHERE ST_Contains(geom,ST_SetSRID(ST_MakePoint(%s,%s),4326));"
    cur.execute(sql,(x,y))
    

提交回复
热议问题