4

I'm running ogr2ogr converting from Shape -> Shape to clean up Shapefiles before they are added to PostGIS via shp2pgsql.

The problem I'm encountering is when the Shapefile contains records that have no geometry which is causing problems with my system.

Does anyone know a way using ogr2ogr to exclude any records with no geometry?

Also is there anyway to remove invalid geometry. E.g. two vertices at the same location?

6

You can do both with the SQLite dialect http://www.gdal.org/ogr_sql_sqlite.html. SQLite dialect gives you all the possibilities of SQLite http://www.sqlite.org/lang.html and SpatiaLite http://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html. However, I am nor sure if two vertices at same location makes geometries invalid.

For removing the empty geometries:

ogr2ogr -f "ESRI Shapefile" -dialect sqlite -sql "select * from input where geometry is not null" output.shp input.shp

For correcting invalid geometries at the same the -sql part gets a bit longer because you must pick the attributes one by one to the query

ogr2ogr -f "ESRI Shapefile" -dialect sqlite -sql "select ST_MakeValid(geometry) as geometry, attr_1, attr_2, attr_3, attr_4 from input where geometry is not null" output.shp input.shp

If it happens to be so that repeated vertices are not considered as errors you can get rid of those by simplifying the geometries with a tolerance of zero

ogr2ogr -f "ESRI Shapefile" -dialect sqlite -sql "select ST_Simplify(geometry,0) as geometry, attr_1, attr_2, attr_3, attr_4 from input where geometry is not null" output.shp input.shp

Commands are not tested but I will edit the answer if they do not work for you.

| improve this answer | |
  • Nice answer--do you think the OP could use the -simplify option to remove duplicate points? I wonder if -simplify 0 would remove duplicate points without modifying the shape..? – elrobis Oct 17 '14 at 18:06
  • ^ the answer as to whether simplify 0 would remove duplicate points is unfortunately "no, it won't." I just tried it. I created a simple four point Polygon WKT, duplicated the second point, exported it to shapefile, used the QGIS plugin "GetWKT" to confirm the duplicate point remained, tried ogr2ogr with -simplify 0, and then used GetWKT again to see if the point remained in the new feature. Sure enough, the point remained. – elrobis Oct 17 '14 at 18:26
  • Try with a very small tolerance then, like 0.01 meter. Simplifying with zero or close to zero tolerance is a very common trick. Also my original ogr2org "ST_Simplify(geom, 0) does just the same but with another algorithm. – user30184 Oct 17 '14 at 19:31
  • Ha! Yeah I tried eleven zeroes (0.000000000001) and it did remove the duplicate point without changing the other vertices. But I couldn't get the ST_Simplify trick to work. Maybe my ogr2ogr is too old, or maybe I wasn't referencing the geom field properly, not sure. – elrobis Oct 17 '14 at 19:46
  • It may be that your GDAL is built with an older Spatialite. In older versions function was called "simplify" and it is still used as a synonym. – user30184 Oct 17 '14 at 21:03
0

I didn't have sqlite compiled in my ogr2ogr but you can get around this by using standard sql dialect and adding "where ogr_geom_wkt not like '%EMPTY%'"

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.