Saturday, 22 September 2018

Calculate line lengths with Python in QGIS


I found this article which describes the "QGIS GUI way" for calculating the line lengths. http://www.qgistutorials.com/en/docs/calculating_line_lengths.html


How can I calculate the length of one line (feature) in a vector layer with Python code?


As an example, I want to know the length of the yellow line.


enter image description here



Answer



From the QGIS Documents: Geometry Handling, you can use the following code to get the length of any selected line(s):


layer = qgis.utils.iface.activeLayer()
features = layer.selectedFeatures()
for f in features:

geom = f.geometry()
print "Length:", geom.length()

Hope this helps!


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...