Wednesday, 28 January 2015

pyqgis - QGIS-Python Need to check if layer is in current extent and make it active


I'm putting together a plugin for QGIS. The user inputs a county and a coordinate pair. The plugin then adds in the county data and zooms to the given coordinates.


The problem is that there are multiple counties with the same name, when zoomed into the coord pair, the county data may be visible but not active because there are multiple layers with the same name added.


I need a way to activate the layer that is in view/visible/can-be-seen-at-the current-position in canvas. I've tried:


import qgis

import qgis.utils
canvas = qgis.utils.iface.mapCanvas()

layers = qgis.utils.iface.legendInterface().layers()
i = qgis.utils.iface
legend = i.legendInterface()

#iter through the legend layers
for layer in layers:
#if the name of the layer is the same as the what the user input

if layer.name().lower() == county.lower():
#and you can see it in the canvas/in the extent/if youre looking at it
if canvas.extent() == layer.extent():
#make the layer active so i can select, or identify things
iface.setActiveLayer(layer)

legend.isLayerVisible(layer) is not what im looking for, it toggles layers, the data is already visible, I need the layer in my current position active.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...