[QGIS-Developer] PyQGIS zoom to layer not working
Frank Broniewski
broniewski at a-a.lu
Tue Jun 20 05:56:33 PDT 2023
Hi Richard,
thank you for your response, you may have already given me a good hint. QGIS is started from another app with an empty/no project, and the default CRS is in effect (WGS84 here). My layers are in epsg:2169 (Luxembourg), so I definitely need to correct this. Since all layers are in 2169, I could set the project's CRS in the plugin ...
But here are the extents, grabbed from the console
georecord_layer extent:
<QgsRectangle: 79099.45521477346483152 74612.53163364417559933, 79127.02656594294239767 74638.46575664929696359>
mapCanvas extent:
<QgsRectangle: -15656.28378378378693014 53925, 170656.28378378378693014 141075>
When I run setExtent in the console, it works ...
iface.mapCanvas().setExtent(plugin.georecord_layer.layer.extent())
I did set the QGIS default CRS to 2169 in the options, but this did not help unfortunately.
Many thanks,
Frank
-----Ursprüngliche Nachricht-----
Von: Richard Duivenvoorde <rdmailings at duif.net>
Gesendet: Dienstag, 20. Juni 2023 13:37
An: Frank Broniewski <broniewski at a-a.lu>; qgis-developer at lists.osgeo.org
Betreff: Re: [QGIS-Developer] PyQGIS zoom to layer not working
Hi Frank,
It really depends on:
- what is the crs of your project
- what is the crs of your layer
- if they match, it should work
- if they do not match you have to reproject the layer extent to the crs of the mapcanvas
I think the python console will be helpfull here.
While loaded, does the plugin have a handle to the actual QGIS layer?
If so, say it is called 'self.georecord_layer.layer' in the plugin.
Then you can do (in the console):
# first find out the name of your plugin qgis.utils.plugins # will show you all loaded layers AND there key/name # say your plugins name is 'franks_plugin'
# then
plugin = qgis.utils.plugins['franks_plugin']
# will give you a handle (in the console) to your plugin # then plugin.georecord_layer.layer.extent should give you an extent... eg:
<QgsRectangle: 103890.796875 492342.34375, 104287.453125 492643.46875> # NOTE that this is in the layers crs !
# you can also use (making the right layer active in legend):
iface.activeLayer().extent()
# you can also look at the extent of the mapcanvas from the console:
iface.mapCanvas().extent()
(you can off course also use logging see the extent somewhere)
Can you show us both the canvas and the layer extent?
Regards,
Richard Duivenvoorde
On 6/20/23 12:59, Frank Broniewski via QGIS-Developer wrote:
> Hi,
>
> I am writing a plugin, in which I am loading a bunch of layers, and I want to zoom on one of them. Layers are loaded through an action (click).
>
> def load_layer(self):
>
> self.project.addMapLayers(
>
> [
>
> self.addresses_layer.layer,
>
> self.adminunits_layer.layer,
>
> self.georecord_layer,
>
> ],
>
> addToLegend=False,
>
> )
>
> self.project.addMapLayers(
>
> [
>
> self.geolocation_point_layer.layer,
>
> self.geolocation_line_layer.layer,
>
> self.geolocation_poly_layer.layer,
>
> self.ortho_layer
>
> ]
>
> )
>
>
> self.iface.mapCanvas().setExtent(self.georecord_layer.layer.extent())
>
> self.iface.mapCanvas().refresh()
>
> but unfortunately, that does not work as expected. QGIS zooms just on the ortho_layer in the end. I have the suspicion, that the extent I set, gets overwritten by another layer that loads in in a later stage. Layers are from a PostGIS, and the ortho_layer is a WMS layer.
>
> I tried googleing, mapCanvas().waitWhileRendering() and whatnot, it just will not zoom. I am completely clueless now, so any hints are greatly appreciated.
>
> This is on OSGeo4W, QGIS 3.28 and Python , for explanation, the .layer
> attribute is the actual QGIS layer …
>
> Many thanks,
>
> Frank
>
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
More information about the QGIS-Developer
mailing list