Tuesday 17 October 2017

qgis - How to debug 'Layer is not valid error' from Python plugin?


I'm trying to understand what goes wrong with a QGIS python plugin. Basically I'm trying to render a series of POINT.


The query works fine, at least giving it by hand from inside PostGIS DB:


"SELECT  gid, CAST (gid as text)|| '_' || (ST_DumpPoints(the_geom)).path[2] as key,  (ST_DumpPoints(the_geom)).geom as points FROM ways"

I concat gid and path[2] because after splitting the_geom (MULTILINE) field into several POINT, gid are duplicated and I think they are no longer usable as key (with this trick key should be unique, but it seems not solve my problem).



I tried to create the layer this way:


def createPointLayer(self):
uri = self.db.getURI()
query = "SELECT gid, CAST (gid as text)|| '_' || (ST_DumpPoints(the_geom)).path[2] as key, (ST_DumpPoints(the_geom)).geom as points FROM ways"

uri.setDataSource("", "(" + query + ")", "points","","key")
layerName = "NodeLayer"
aLayer = self.iface.addVectorLayer(uri.uri(), layerName, self.db.getProviderName())

I think the query works fine (QGIS takes a long time before display the error. I guess something goes wrong after the query has been executed).



When the plugins run QGIS output the following error:



Layer is not valid:


The layer dbname='pgrouting' user='user' password='user' key='gid' table="(SELECT gid, CAST (gid as text)|| '_' || (ST_DumpPoints(the_geom)).path[2] as key, (ST_DumpPoints(the_geom)).geom as points FROM ways)" (points) sql= is not a valid layer and can not be added to the map



I already had to struggle against the Layer is not valid error, but I don't know how to debug it. So I have 2 questions:



  1. Could anyone tell me what's my mistake in this specific case?

  2. How can I debug 'Layer not valid error'? How can I get more information on which is the problem?



EDIT:


QGIS log console display the following message:



No key field for query given





No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...