I have a shapefile containing polygons and a shapefile containing points. Now I would like to select the polygons that contain points.
How can I do this from the command line using ogr2ogr
?
gdalogrogr2ogrsqlite
I have a shapefile containing polygons and a shapefile containing points. Now I would like to select the polygons that contain points.
How can I do this from the command line using ogr2ogr
?
Best Answer
At the following link all SQL functions that test spatial relationships are described: http://www.gaia-gis.it/spatialite-2.4.0/spatialite-sql-2.4.html#p12.
The following spatial relationships can be used within a query
ST_Equals
-ST_Disjoint
-ST_Touches
-ST_Within
-ST_Overlaps
-ST_Crosses
-ST_Intersects
-ST_Contains
-ST_Relate
For this type of request you can do this using the
ST_Containts
query ofsqlite
. Which can be adopted in aogr2ogr
command as follows:Example
Picture of
shapefile_polygon.shp
andshapefile_point.shp
: And picture ofselection_polygon.shp
andshapefile_point.shp
:Expanded options of the spatial relationships:
ST_Equals
Returns true if the interior and the boundary of the two geometries are spatially equalST_Disjoint
Returns true if the boundaries and interior do not intersectST_Touches
Returns true if the boundaries intersect but the interiors do notST_Within
Returns true if the interior of the given geometry does not intersect with the exterior of another geometryST_Overlaps
Returns true if the interiors of two geometries have non-empty intersectionST_Crosses
Returns true if the interiors of the geometries intersect but the boundaries do notST_Intersects
Returns true if the interiors of the geometries intersectST_Contains
Tests if the given geometry contains another geometryST_Relate
Returns true if this geometry is spatially related to another geometry