[GIS] Error: Geometry constraint geom-type or SRID not allowed. How to ensure the correct geom-type

qgisspatialitesqlsqlite

Following the top answer on a previous question about storing geometries in a spatialite database, I am trying to update a spatialite table using:

engine.execute("SELECT AddGeometryColumn('your_table', 'geog', 27700, 'polygon', 'XY');")
# add a Spatialite geometry column called 'geog' to the table, using ESPG 27700, data type POLYGON and 2 dimensions
    # (x, y)
engine.execute("UPDATE your_table SET geog = GeomFromWKB(geometry, 27700);")

The code worked fine when I was using point data. However, when I use vector data I keep getting the error
your_table.geog violates Geometry constraint [geom-type or SRID not allowed]
I know the SRID is ok, so from the error I assume it is the geom-type that is incorrect. I have checked the geom-type in a couple ways: 1. opening the layer in QGIS and checking the porperties; and 2. opening the database in spatialite-gui and 'check geometries'. I have also tried using polygon, multipolygon and multipolygonz as the 4th argument in the AddGeometryColumn selection, but I get the same error. So, what is the best way to check which geometry it should be?

Best Answer

Using @user30184's comment, I initialized the table for multipolygons, using the CastToMultiPolygon function found here. The second part of my above code was changed to:

    # Spatialite geometry objects
    engine.execute("UPDATE your_table SET geom = CastToMultiPolygon(ST_GeomFromWKB(geometry, 27700));")```

Related Question