Re: [QGIS-Developer] PyQGIS zoom to layer not working
Hello all, I just wanted to return to my problem and show my solution. Like I already said, I tried back and forth, to no avail. Like I already described, zoom to layer never worked, and I still believe my earlier observation is true, that loading multiple layers at once changes the map extent everytime a layer gets loaded/added. What helped in the end was this single line of code, between the layer loading code and the final zoom to layer code ... # wait for application to process pending tasks QCoreApplication.processEvents() Maybe this is helpful for others as well. Have a nice day, Frank -Ursprüngliche Nachricht- Von: QGIS-Developer Im Auftrag von Frank Broniewski via QGIS-Developer Gesendet: Donnerstag, 22. Juni 2023 10:56 An: rdmaili...@duif.net; qgis-developer@lists.osgeo.org Betreff: Re: [QGIS-Developer] PyQGIS zoom to layer not working Hey Richard, thanks for the good wishes! I feel, I completely jinxed it 😃. I will continue to investigate, as soon as I find a solution, I'll post it here ... -Ursprüngliche Nachricht- Von: Richard Duivenvoorde Gesendet: Dienstag, 20. Juni 2023 15:33 An: Frank Broniewski ; qgis-developer@lists.osgeo.org Betreff: Re: AW: [QGIS-Developer] PyQGIS zoom to layer not working On 6/20/23 14:56, Frank Broniewski wrote: > I did set the QGIS default CRS to 2169 in the options, but this did not help > unfortunately. Mmm, that should work: Settings / CRS and Transforms/ CRS for projects / Use a default CRS AND I would maybe also set the CRS for layer to epsg:2169 Another option is to create a 'Default Project' (empty, BUT crs set to epsg:2169) Settings / General / Set Current Project As Default and Checkbox 'Create new project from default project' fiddling with those I'm pretty sure it should be possible to start QGIS in the right crs.. Suc6 Richard Duivenvoorde ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [QGIS-Developer] PyQGIS zoom to layer not working
Hey Richard, thanks for the good wishes! I feel, I completely jinxed it 😃. I will continue to investigate, as soon as I find a solution, I'll post it here ... -Ursprüngliche Nachricht- Von: Richard Duivenvoorde Gesendet: Dienstag, 20. Juni 2023 15:33 An: Frank Broniewski ; qgis-developer@lists.osgeo.org Betreff: Re: AW: [QGIS-Developer] PyQGIS zoom to layer not working On 6/20/23 14:56, Frank Broniewski wrote: > I did set the QGIS default CRS to 2169 in the options, but this did not help > unfortunately. Mmm, that should work: Settings / CRS and Transforms/ CRS for projects / Use a default CRS AND I would maybe also set the CRS for layer to epsg:2169 Another option is to create a 'Default Project' (empty, BUT crs set to epsg:2169) Settings / General / Set Current Project As Default and Checkbox 'Create new project from default project' fiddling with those I'm pretty sure it should be possible to start QGIS in the right crs.. Suc6 Richard Duivenvoorde ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [QGIS-Developer] PyQGIS zoom to layer not working
On 6/20/23 14:56, Frank Broniewski wrote: I did set the QGIS default CRS to 2169 in the options, but this did not help unfortunately. Mmm, that should work: Settings / CRS and Transforms/ CRS for projects / Use a default CRS AND I would maybe also set the CRS for layer to epsg:2169 Another option is to create a 'Default Project' (empty, BUT crs set to epsg:2169) Settings / General / Set Current Project As Default and Checkbox 'Create new project from default project' fiddling with those I'm pretty sure it should be possible to start QGIS in the right crs.. Suc6 Richard Duivenvoorde ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Re: [QGIS-Developer] PyQGIS zoom to layer not working
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: mapCanvas extent: 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 Gesendet: Dienstag, 20. Juni 2023 13:37 An: Frank Broniewski ; qgis-developer@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: # 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@lists.osgeo.org > List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
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: # 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@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer ___ QGIS-Developer mailing list QGIS-Developer@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
[QGIS-Developer] PyQGIS zoom to layer not working
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@lists.osgeo.org List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer