0

I'm trying to save a WFS layer to a Postgis database with no success. Somehow modifying the connection string I can get QgsVectorLayerExporter to write either the geometry column or the attribute table, never both.

So with these commands...

con_string = """dbname='gis' host='localhost' port='25432' user='qgis' password='qgis'
key='id' type='CURVEPOLYGON' table="public"."my_layer_new" (geom) """

QgsVectorLayerExporter.exportLayer(my_layer, con_string, 'postgres', project.crs())

... I see the geometry column in the views.geometry_columns table and the my_layer_new table is created but it is empty.

However if I remove the (geom) from the connection string...

con_string = """dbname='gis' host='localhost' port='25432' user='qgis' password='qgis'
key='id' type='CURVEPOLYGON' table="public"."my_layer_new" """

QgsVectorLayerExporter.exportLayer(my_layer, con_string, 'postgres', project.crs())

... no geometry column is in the views.geometry_columns table but on the otherhand the my_layer_new table is created and populated with the attributes of each feature.

This really feels like it's an just add this string to X type of situation.

EDIT:

After changing the last lines to

    err = QgsVectorLayerExporter.exportLayer(my_layer, con_string, 'postgres', project.crs())
    print(err)

I discovered that the actual problem is ERROR: Geometry type (MultiSurface) does not match column type (CurvePolygon)

1

In the end creating an instance of QgsVectorLayerExporter and passing it the desired geometry type helped.

con_string = """dbname='gis' host='localhost' port='25432' user='qgis' password='qgis'
key='id' table="public"."my_layer_new" (geom)"""
exporter = QgsVectorLayerExporter(con_string, 'postgres', fields=my_layer.fields(), crs=project.crs(), overwrite=True,
                              geometryType=QgsWkbTypes.MultiSurface)
exporter.addFeatures(my_layer.getFeatures())

I wonder if this would have been possible with QgsVectorLayerExporter.exportLayer() and passing the desired geometry type through options, but unfortunately this was not documented so did not give it a go.

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.