0

Im loading some layers from postgis using pyqgis in my standalone qgis app. For each layer i also load a style using:

lyr.loadNamedStyle("style.qml")

I've setup 2 versions of styles in qgis desktop, one with and one without scale range visibility settings for the layer. The problem is, when i use the style with scale visibility (no matter if i set a really large range from 1:100 000 000 to 1:1) my features are not shown, not ever. If i use no style at all, or style without scale visibility settings, the layers are visible normaly.

I should add, styles work in QGIS desktop as expected.

What bothers me slightly is that when i print the current scale property of the map canvas in my app, the value has an insanely large value, for example 10e14 to begin with, even though the scale should be somewhere around 4000000 (1:4000000). What is even wirder is, this scale value (canvas.scale()) changes significantly when i pan (not zoom) the map.

Any ideas what is going on?

Because i thought the mapCanvas srid is not setup correctly and the scale would never fit into my scale range, I tried setting map canvas srid explicitly, did that on layers, stil no luck. I am using QGIS 2.8.1

UPDATE: It seems in any case i have to set the CRS explicitly

canvas.mapRenderer().setDestinationCrs(QgsCoordinateReferenceSystem(32632))

but it doesn't change anything. I just thought that layers will implicitly enforce my local CRS but they don't.

  • Well, i havve to investigate a bit further first ;) – U2ros Apr 13 '15 at 5:05
  • Yes, the scale reported from the map canvas is the problem. If i set the scale range to a large enough number (order of e05 larger than it actually is), then the scale visibility style settings work as excpected! – U2ros Apr 13 '15 at 8:04
  • Doing this... yields the same result both times - degrees: print self.map.mapUnits() self.map.mapRenderer().setDestinationCrs(QgsCoordinateReferenceSystem(32632)) print self.map.mapUnits() – U2ros Apr 13 '15 at 8:16
2

Okay. Solved it. There were 2 things i had to do, to get the styles working.

  1. You must set the coordinate system explicitly. Loading Postgis layers with a preset CRS did not do that automatically (as we are used from QGis desktop). You can do so by:

    map.setDestinationCrs(QgsCoordinateReferenceSystem(srid))

  2. You must explicitly set map units to meters, or whatever is used in your projected CRS. The scale is then calculated correctly:

    map.setMapUnits(QGis.UnitTypes.Meters)

1

Adding onto above (not enough points to comment)

map.setMapUnits(QGis.UnitTypes.Meters)

Does not work for me (version 2.14). I must reference meters with the index for that unit type, noted here in the API Docs. Using this works for me (0 = meters).

map.setMapUnits(0)
1

With QGIS 3, extract from https://qgis.org/api/api_break.html

setMapUnits() was removed. The map units are dictated by the units for the destination CRS.

You now only need map.setDestinationCrs(QgsCoordinateReferenceSystem(srid))

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.