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.
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.
Checkout http://geospatialpython.com/2011/01/point-in-polygon.html and http://geospatialpython.com/2011/08/point-in-polygon-2-on-line.html
Another option is to use Shapely (a Python library based on GEOS, the engine for PostGIS) and Fiona (which is basically for reading/writing files):
Note that doing point-in-polygon tests can be expensive if the polygon is large/complicated (e.g., shapefiles for some countries with extremely irregular coastlines). In some cases it can help to use bounding boxes to quickly rule things out before doing the more intensive test:
Lastly, keep in mind that it takes some time to load and parse large/irregular shapefiles (unfortunately, those types of polygons are often expensive to keep in memory, too).
This is an adaptation of yosukesabai's answer.
I wanted to ensure that the point I was searching for was in the same projection system as the shapefile, so I've added code for that.
I couldn't understand why he was doing a contains test on
ply = feat_in.GetGeometryRef()
(in my testing things seemed to work just as well without it), so I removed that.I've also improved the commenting to better explain what's going on (as I understand it).
This site, this site, and this site were helpful regarding the projection check. EPSG:4326
One way to do this is to read the ESRI Shape file using the OGR library http://www.gdal.org/ogr and then use the GEOS geometry library http://trac.osgeo.org/geos/ to do the point-in-polygon test. This requires some C/C++ programming.
There is also a python interface to GEOS at http://sgillies.net/blog/14/python-geos-module/ (which I have never used). Maybe that is what you want?
Another solution is to use the http://geotools.org/ library. That is in Java.
I also have my own Java software to do this (which you can download from http://www.mapyrus.org plus
jts.jar
from http://www.vividsolutions.com/products.asp ). You need only a text command fileinside.mapyrus
containing the following lines to check if a point lays inside the first polygon in the ESRI Shape file:And run with:
It will print a 1 if the point is inside, 0 otherwise.
You might also get some good answers if you post this question on https://gis.stackexchange.com/
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!
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:
i did almost exactly what you are doing yesterday using gdal's ogr with python binding. It looked like this.