One of the great things about learning to script (I wont dare to say I know enough to consider myself a programmer) is being to automate tasks or add functionality to software you are using. A few months ago one of the core developers of QGIS, Nathan Woodrow made a command plugin that lets you interact with the layers and tables similar to how some CAD software work. The plugins is called command bar and you can read the docs at http://qgiscommand.readthedocs.org/en/latest/

command_bar

Now one of the cool things this plugin has is the possibility to add your own user commands (http://qgiscommand.readthedocs.org/en/latest/usercommands/)

Reading through the issue list someone asked for the possibility of getting the bounding box of a layer or selected features, I figure I could try that out. So I made a gist for the bbox albeit very crude and add it using the instructions of the plugin. Below is the python code

[code language=”python”]
from qgis.utils import iface
from qgis.core import QGis
from qgiscommand.command import command

@command()
def bbox():
layer = iface.activeLayer()

def pbounds (Lextent,s):
e = Lextent
if s ==’no’:
iface.messageBar().pushInfo("bbox layer\n",’x,y\n%s,%s\n%s,%s’ %(e.xMinimum(), e.yMinimum(), e.xMaximum(), e.yMaximum()))
else:
iface.messageBar().pushInfo("bbox selected features\n",’x,y\n%s,%s\n%s,%s’ %(e.xMinimum(), e.yMinimum(), e.xMaximum(), e.yMaximum()))

if layer.wkbType()== QGis.WKBPolygon or layer.wkbType() == QGis.WKBMultiPolygon or layer.wkbType() == QGis.WKBLineString:
if layer.selectedFeatureCount() < 1:
s = ‘no’
e = layer.extent()
else:
s = ‘yes’
e = layer.boundingBoxOfSelected()
pbounds(e, s)

else:
if layer.featureCount() <1:
print ‘less than 2 points’
else:
if layer.selectedFeatureCount() <2:
s = ‘no’
e = layer.extent()
pbounds(e, s)
else:
s = ‘yes’
e = layer.boundingBoxOfSelected()
pbounds(e, s)
[/code]

End result here for selected features, provides the coordinates in the message bar (probably not the best solution I must say)

selected_bbox

So go ahead, try it out. Got suggestions for new features or ideas add them to the github page for the plugin, know how to code? even better fork and make a pull request!

Happy Qgising!


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.

%d bloggers like this: