6

I have been trying to dissolve a shapefile with more than 38000 rows. I used query as follows

ogr2ogr -f "ESRI Shapefile" dissolved.shp Barangays.shp -dialect sqlite -sql "select ST_union(Geometry),* from input  GROUP BY concated"

It gave me error that geometry is invalid because of self-intersecting polygons.

I found out there are 86 features which have invalid geometries using QGIS. I tried Make Valid tool as suggested in this question. But this is also not working, giving error undefined symbol, which I couldn't understand. So I decided to validate using ogr2ogr and after searching through google I tried this query

ogr2ogr -f "ESRI Shapefile" valid_barangays.shp Barangays.shp -dialect sqlite -sql "select Geometry.MakeValid(),* from Barangays"

It gave me this error

ERROR 1: In ExecuteSQL(): sqlite3_prepare(select Geometry.MakeValid(),* from Barangays):
  near "(": syntax error

How can I solve this problem?

I also imported my shapefile using ogr2ogr in phpmyadmin mysql. It saved geomtry column as [geometry some_number B]. I am not able to query. I always give error function not exist.

AMahajan
  • 188
  • 2
  • 7
  • What about using QGIS ? – Shiko Jul 30 '16 at 15:39
  • @shiko As an automated process? – Fábio Dias May 01 '18 at 18:41
  • @FábioDias in QGIS you can do both, either manually or programmatically using python (Inside QGIS as a plugin or as a standalone application). check this https://gis.stackexchange.com/questions/159699/how-to-find-the-irregular-polygons-in-qgis-using-python – Shiko May 01 '18 at 20:41

2 Answers2

6

Based on the post: https://www.gaia-gis.it/fossil/libspatialite/wiki?name=liblwgeom-4.0

SpatiaLite should support the ST_MakeValid argument. So this should work. Without your data it is hard to test. But it should work if you specify your geometry within the ST_MakeValid statement

So if you run:

ogr2ogr -f "ESRI Shapefile" valid_barangays.shp Barangays.shp -dialect sqlite -sql "select ST_MakeValid(geometry) as geometry, * from Barangays"

If that does not work, you will likely have to specify each other column instead of selecting *.

HeikkiVesanto
  • 16,433
  • 2
  • 46
  • 68
3

Using makevalid parameter could fix your problem:

-makevalid

Run the OGRGeometry::MakeValid() operation, followed by OGRGeometryFactory::removeLowerDimensionSubGeoms(), on geometries to ensure they are valid regarding the rules of the Simple Features specification.

Try:

ogr2ogr valid_barangays.shp Barangays.shp -makevalid
julien
  • 10,186
  • 6
  • 55
  • 93
  • 2
    this is available departing gdal/ogr > 3.2, which was released in october 2020: https://github.com/OSGeo/gdal/blob/v3.2.0/gdal/NEWS – Elio Diaz Nov 08 '20 at 18:07