Skip to content

Instantly share code, notes, and snippets.

@jsanz
Created December 17, 2013 10:06
Show Gist options
  • Save jsanz/8002690 to your computer and use it in GitHub Desktop.
Save jsanz/8002690 to your computer and use it in GitHub Desktop.
Compute the distance between selected or all geoms on QGis 2
import processing
layer = qgis.utils.iface.activeLayer()
# Get all or selected features
features = processing.getfeatures(layer)
# Store all the features on a list
list = [feat for feat in features]
# Get the index of a identifier field
id = layer.fieldNameIndex('ID')
# Double iterate over the list
print "id1;id2;dist"
for feature1 in list:
for feature2 in list:
id1 = feature1.attributes()[id]
id2 = feature2.attributes()[id]
# Print the distance if ids are not equal
if (id1!=id2):
dist = feature1.geometry().distance(feature2.geometry())
print "%s;%s;%.3f" % (id1,id2,dist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment