forked from yurunsang/qgis_location_analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRyersonGeo_-_Distance_Matrix_-_Euclidean.py
84 lines (61 loc) · 2.96 KB
/
RyersonGeo_-_Distance_Matrix_-_Euclidean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
##RyersonGeo - Distance matrix - euclidean=name
##Consumer_Centroid_Layer=vector
##Consumer_Centroid_Layer_ID_Field=field Consumer_Centroid_Layer
##Centre_Point_Layer=vector
##Centre_Point_Layer_ID_Field=field Centre_Point_Layer
##Output_Layer=output file
# Script: RyersonGeo_-_Distance_Matrix_-_Euclidean.py
# Author: Michael Morrish
# Date: December 18, 2016
#
# This script takes in two input shapefiles and two field specifications and
# produces a euclidean distance matrix.
# Imports.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
# Get the layers.
lyrConsumer = processing.getObject(Consumer_Centroid_Layer)
lyrCentre = processing.getObject(Centre_Point_Layer)
# Get the fields.
fldConsumerID_index = lyrConsumer.fieldNameIndex(Consumer_Centroid_Layer_ID_Field)
fldCentreID_index = lyrCentre.fieldNameIndex(Centre_Point_Layer_ID_Field)
# Need to prepare output layer and add new fields.
# New fields are simply the ID of the Centre.
# Loop through each Centre to construct new field names.
lyrOutput = processing.getObject(Output_Layer)
provider = lyrOutput.dataProvider()
# Optimize feature request for this loop.
request1 = QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry).setSubsetOfAttributes([Centre_Point_Layer_ID_Field], lyrCentre.fields() )
# The loop.
for centreFeature in lyrCentre.getFeatures(request1):
# Capture value of fldCentreID_index (current ID).
currentCentreID = centreFeature[fldCentreID_index]
# Add and name the field. Double type for distances.
new_field_name = currentCentreID
provider.addAttributes([QgsField(new_field_name, QVariant.Double)])
lyrOutput.updateFields()
# Set the output layer for editing.
lyrOutput.startEditing()
# Optimize feature request for outer nested loop.
request2 = QgsFeatureRequest().setSubsetOfAttributes([Consumer_Centroid_Layer_ID_Field], lyrConsumer.fields() )
# Loop through each Consumer feature.
for consumerFeature in lyrConsumer.getFeatures(request2):
# Capture value of fldConsumerID_index (current ID).
currentConsumerID = consumerFeature[fldConsumerID_index]
# Loop through each Centre feature.
for centreFeature in lyrCentre.getFeatures():
# Capture value of fldCentreID_index (current ID).
currentCentreID = centreFeature[fldCentreID_index]
# Create a measurement object.
mObject = QgsDistanceArea()
# Measure the euclidean distance.
eDistance = mObject.measureLine(consumerFeature.geometry().asPoint(), centreFeature.geometry().asPoint())
# Set the euclidean distance value of the new Centre field for
# the current Consumer and Centre.
current_distmatrix_field = currentCentreID
distmatrix_field_index = lyrOutput.fieldNameIndex(current_distmatrix_field)
lyrOutput.changeAttributeValue(consumerFeature.id(), distmatrix_field_index,eDistance)
# Commit the changes to the layer.
lyrOutput.commitChanges()