Another option is to use Shapely (a Python library based on GEOS, an engine for PostGIS) and Fiona (which is mainly intended for reading / writing files):
import fiona import shapely with fiona.open("path/to/shapefile.shp") as fiona_collection:
Please note that performing point-to-polygon tests can be expensive if the polygon is large / complex (for example, shapefiles for some countries with extremely irregular coastlines). In some cases, this can help to use bounding boxes to quickly disable a situation before performing a more intense test:
minx, miny, maxx, maxy = shape.bounds bounding_box = shapely.geometry.box(minx, miny, maxx, maxy) if bounding_box.contains(point): ...
Finally, keep in mind that downloading and analyzing large / irregular shapefiles takes some time (unfortunately, these types of polygons are often expensive to store in memory too).
Clint Harris Sep 11 '13 at 19:11 2013-09-11 19:11
source share