I am generating shapefiles from a geodatabase file with ogr2ogr via the spatialite dialect using the following command:
ogr2ogr -f "ESRI Shapefile" ./clipped.shp -dialect sqlite -sql "SELECT * FROM table_name WHERE ST_Intersects(SHAPE, GeomFromText('POLYGON ((46.93 -17.48, 46.91 -17.55, 47.01 -17.56, 47.01 -17.46, 46.93 -17.48))'))" ./geodatabase_file.gdb
The command itself works exactly as expected - returning only the records that are intersected by the given spatial query.
There are however, times when no records are returned by the intersection. When this happens empty shapefiles are generated that contain no entries. Is there a way to prevent ogr2ogr from generating files if no records are returned from a provided intersection?
I came to the conclusion that this is not possible as ogr2ogr
creates a shapefile first and then adds features to it as it finds them. Instead it is possible to use the ogrinfo
command to determine the Feature Count
of the shapefile and then act accordingly.
ogrinfo -so -al ./geodatabase_file.gdb
Information for this answer came from a comment posted on this question - https://gis.stackexchange.com/questions/366599/is-it-possible-to-prevent-ogr2ogr-from-generating-a-shapefile-if-no-records-are