[QGIS-Developer] PyQGIS zoom to layer not working
Richard Duivenvoorde
rdmailings at duif.net
Tue Jun 20 04:36:49 PDT 2023
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