How empty geometries be deleted from shapefile using the command-line GDAL utilities?
1 Answer
The GDAL tool to use is perhaps surprisingly ogrinfo with SQLite dialect. The description on the manual page mentions only that the program can be used for listing information.
The ogrinfo program lists various information about an OGR supported data source to stdout (the terminal).
However, it is also possible to edit data with ogrinfo in place without making a new copy of the layer with ogr2ogr. This is done be sending SQL commands with one of the suppportes SQL dialects SQLite dialect or native OGR SQL
For your use case the SQLite dialect is needed because it exposes the SQL DELETE FROM functionality. Start with shapefile with one point and one empty geometry. Ogrinfo reports that layer has 2 features:
Layer name: shp_with_empty_point
Geometry: Point
Feature Count: 2
Extent: (715.000000, 479.000000) - (715.000000, 479.000000)
Layer SRS WKT:
(unknown)
OGRFeature(shp_with_empty_point):0
POINT (715 479)
OGRFeature(shp_with_empty_point):1
Update with SQL
ogrinfo -dialect sqlite -sql "delete from shp_with_empty_point where geometry is null" shp_with_empty_point.shp
Check the result with ogrinfo and find out that there is only one feature left on the layer.
Layer name: shp_with_empty_point
Geometry: Point
Feature Count: 1
Extent: (715.000000, 479.000000) - (715.000000, 479.000000)
Layer SRS WKT:
(unknown)
OGRFeature(shp_with_empty_point):0
POINT (715 479)