Bug #6074

Using QgsDataSourceURI to load a PostGIS table causes error if query returns empty and Geometry type is GEOMETRY

Added by Ian Packham 10 months ago. Updated 10 months ago.

Status:Closed Start Date:07/20/2012
Priority:Normal Due date:
Assigned to:- % Done:

0%

Category:PostGIS Data Provider
Target version:-
Platform: Patch supplied:No
Platform version: Affected version:master
Status info: Causes crash or corruption:No
Resolution:invalid

Description

I am using the following code to load a vector layer from a PostGIS table:

uri = QgsDataSourceURI()
uri.setConnection(self.host, str(self.port), self.database, self.username, self.password)
uri.setDataSource(schema, postGisTable, "the_geom",sqlstmt,'gid')
try:
    #if sql does not return data - get PostGIS error, but not exception?
    vlayer = QgsVectorLayer(uri.uri(), layerName, "postgres")
except:
    QMessageBox.critical(self.iface.mainWindow(), "Error", "Problem setting QgsVectorLayer")
if not vlayer.isValid():
    QMessageBox.critical(self.iface.mainWindow(),"Error","Layer failed to load!")

If the sqlstmt returns an empty query, in some cases my plugin shows the "Layer failed to load!" message meaning the layer is invalid (but is never caught by the previous try block).
Error in the General Log Messages is:

Geometry type and srid for empty column the_geom of "cmt"."subbasinsoilsdrainage_eastern_union" undefined.

On investigation I have discovered that if the Geometry type of the PostGIS layer is the generic GEOMETRY type, then I get this error. If the Geometry type is MultiPolygon or MultiPoint, then an empty layer works OK.

If this is expected behaviour then should we avoid the GEOMETRY type in our PostGIS tables?

History

Updated by Jürgen Fischer 10 months ago

  • Status changed from New to Closed
  • Resolution set to invalid

with empty tables that don't have a specific geometry type and/or you have to specify the expected geometry type with QgsDataSource::setWkbType() and expected srid with QgsDataSource::setSrid()

Also available in: Atom PDF