Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#1462 closed bug (fixed)

PostGIS layer loaded via Python is not saved correct in qgs project file

Reported by: hdus Owned by: jef
Priority: critical: causes crash or data corruption Milestone:
Component: Project Loading / Saving Version: Trunk
Keywords: Cc:
Must Fix for Release: No Platform: All
Platform Version: Awaiting user input: no

Description

I load a PostGIS layer via Python with:

...
uri = QgsDataSourceURI()
uri.setConnection(self.DBHOST, self.DBPORT, self.DBNAME, self.DBUSER, self.DBPASSWD)
uri.setDataSource(str(schema), str(table), "wkb_geometry", "archive=0")
vlayer = QgsVectorLayer(uri.uri(), layerName, "postgres")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)    
...

The layer is added to mapcanvas and it is displayed correct. Save the project to testpython.qgs and reload this project-file QGIS reports: Unable to load layer.

Load the same layer with the postgis connector. Save the project to testconnector.qgs and reload this project-file everthing works fine.

I compare the two project files and I see a relevant difference, the following tags are missing in testpython.qgs:

            <edittypes>
                <edittype type="0" name="archive" />
                <edittype type="0" name="archive_date" />
                <edittype type="0" name="gem_bfs" />
                <edittype type="0" name="new_date" />
                <edittype type="0" name="nummer" />
                <edittype type="0" name="ogc_fid" />
            </edittypes>
            <displayfield>ogc_fid</displayfield>
            <label>0</label>
            <attributeactions/>
            <singlesymbol>
                <symbol>
                    <lowervalue></lowervalue>
                    <uppervalue></uppervalue>
                    <label></label>
                    <pointsymbol>hard:circle</pointsymbol>
                    <pointsize>2</pointsize>
                    <rotationclassificationfieldname></rotationclassificationfieldname>
                    <scaleclassificationfieldname></scaleclassificationfieldname>
                    <outlinecolor red="0" blue="0" green="0" />
                    <outlinestyle>SolidLine</outlinestyle>
                    <outlinewidth>0.26</outlinewidth>
                    <fillcolor red="153" blue="60" green="213" />
                    <fillpattern>SolidPattern</fillpattern>
                    <texturepath></texturepath>
                </symbol>
            </singlesymbol>
            <labelattributes>
                <label fieldname="" text="Label" />
                <family fieldname="" name="Sans Serif" />
                <size fieldname="" units="pt" value="12" />
                <bold fieldname="" on="0" />
                <italic fieldname="" on="0" />
                <underline fieldname="" on="0" />
                <color fieldname="" red="0" blue="0" green="0" />
                <x fieldname="" />
                <y fieldname="" />
                <offset x="0" y="0" units="pt" yfieldname="" xfieldname="" />
                <angle fieldname="" value="0" auto="0" />
                <alignment fieldname="" value="center" />
                <buffercolor fieldname="" red="255" blue="255" green="255" />
                <buffersize fieldname="" units="pt" value="1" />
                <bufferenabled fieldname="" on="" />
                <multilineenabled fieldname="" on="" />
            </labelattributes>

Change History (5)

comment:1 by jef, 15 years ago

I tried to run your code in the python console. When try to save I get

NotImplementedError: QgsVectorLayer.writeSymbology() is abstract and must be overridden

I still need to investigate why there's python code fired at all at that point, but if I replace

vlayer = QgsVectorLayer(uri.uri(), layerName, "postgres")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)  

with

iface.addVectorLayer(uri.uri(), layerName, "postgres")

everything is fine again.

in reply to:  1 ; comment:2 by hdus, 15 years ago

Replying to jef:

I tried to run your code in the python console. When try to save I get

NotImplementedError: QgsVectorLayer.writeSymbology() is abstract and must be overridden

I still need to investigate why there's python code fired at all at that point, but if I replace

vlayer = QgsVectorLayer(uri.uri(), layerName, "postgres")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)  

with

iface.addVectorLayer(uri.uri(), layerName, "postgres")

everything is fine again.

Your workaround works fine for me too. But how do I load a namedStyle to this layer? With my version I can prepare a vLayer object and I'm able to add a symbolisation with vLayer.loadNamedStyle() before I register the vLayer. I tried

self.iface.addVectorLayer(uri.uri(), layerName, "postgres")
vLayer = self.iface.mapCanvas().currentLayer()
vLayer.loadNamedStyle(Path to qml-file)

in this case the symbolisation in mapcanvas is updated, but the legend symbolisation is not changed.

in reply to:  2 comment:3 by hdus, 15 years ago

Replying to hdus:

Replying to jef:

I tried to run your code in the python console. When try to save I get

NotImplementedError: QgsVectorLayer.writeSymbology() is abstract and must be overridden

I still need to investigate why there's python code fired at all at that point, but if I replace

vlayer = QgsVectorLayer(uri.uri(), layerName, "postgres")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)  

with

iface.addVectorLayer(uri.uri(), layerName, "postgres")

everything is fine again.

Your workaround works fine for me too. But how do I load a namedStyle to this layer? With my version I can prepare a vLayer object and I'm able to add a symbolisation with vLayer.loadNamedStyle() before I register the vLayer. I tried

self.iface.addVectorLayer(uri.uri(), layerName, "postgres")
vLayer = self.iface.mapCanvas().currentLayer()
vLayer.loadNamedStyle(Path to qml-file)

in this case the symbolisation in mapcanvas is updated, but the legend symbolisation is not changed.

OK I got it with self.iface.refreshLegend(vLayer).

comment:4 by jef, 15 years ago

Resolution: fixed
Status: newclosed

fixed in r9805

comment:5 by (none), 15 years ago

Milestone: Version 1.0.0

Milestone Version 1.0.0 deleted

Note: See TracTickets for help on using tickets.