Created
December 17, 2013 10:06
-
-
Save jsanz/8002690 to your computer and use it in GitHub Desktop.
Compute the distance between selected or all geoms on QGis 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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