Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
qgib opened this issue Jul 20, 2012 · 1 comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Data Provider Related to specific vector, raster or mesh data providers

Comments

@qgib
Copy link
Contributor

qgib commented Jul 20, 2012

Author Name: Ian Packham (Ian Packham)
Original Redmine Issue: 6074
Affected QGIS version: master
Redmine category:data_provider/postgis


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?

@qgib
Copy link
Contributor Author

qgib commented Jul 20, 2012

Author Name: Jürgen Fischer (@jef-n)


with empty tables that don't have a specific geometry type and/or you have to specify the expected geometry type with "QgsDataSource::setWkbType()":http://www.qgis.org/api/classQgsDataSourceURI.html#afa25bc980ca958fb1dece4129b72f590 and expected srid with "QgsDataSource::setSrid()":http://www.qgis.org/api/classQgsDataSourceURI.html#aa659f9bcfdcf5fc394566926df0f6ebe


  • resolution was changed from to invalid
  • status_id was changed from Open to Closed

@qgib qgib added Bug Either a bug report, or a bug fix. Let's hope for the latter! Data Provider Related to specific vector, raster or mesh data providers labels May 24, 2019
@qgib qgib closed this as completed May 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! Data Provider Related to specific vector, raster or mesh data providers
Projects
None yet
Development

No branches or pull requests

1 participant