2

I wanted to find the length of features in a road layer. So I go through each feature in the layer and used,

line = feature.geometry().asPolyline()

to find the points which particular feature is created and then used haversine formula to calculate the length. But there are some features(roads) which are not as continued lines (shown in image-yellow coloured), so the line returns an empty list.

Is there any way to find length of those type of features?

enter image description here

2

Perhaps you could create a new attribute column and calculate the length of each feature from there using:

$length
2
  • Thank You very much. I have used this code to find the length. nathanw.net/2011/02/23/… def getLength(): layer = qgis.utils.iface.mapCanvas().currentLayer() total = 0 for feature in layer.selectedFeatures(): geom = feature.geometry() total = total + geom.length() return total print getLength() But how to convert the length I got in degrees to meters programmatically?
    – Dil
    Oct 26 '14 at 3:57
  • 1
    You need convert change the layer's crs. Then get the length with Nathan's length. Note that with his function you need to have all geometry in the same layer and this layer need to be active.
    – SIGIS
    Nov 9 '17 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.