Skip to content
Permalink
Browse files

Merge pull request #5427 from alexbruy/processing-distance

[processing] keep column names in standard distance matrix (fix #17150)
  • Loading branch information
alexbruy committed Oct 24, 2017
2 parents 5261a9a + 04a8418 commit d34c09b3fe61a4baf55362ec0bc29b79d462e36b
@@ -16,9 +16,6 @@
* *
***************************************************************************
"""
from builtins import next
from builtins import str
from builtins import range

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
@@ -207,14 +204,15 @@ def linearMatrix(self, parameters, context, source, inField, target_source, targ

def regularMatrix(self, parameters, context, source, inField, target_source, targetField,
nPoints, feedback):

index = QgsSpatialIndex(target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(source.sourceCrs())), feedback)
inIdx = source.fields().lookupField(inField)

distArea = QgsDistanceArea()
distArea.setSourceCrs(source.sourceCrs())
distArea.setEllipsoid(context.project().ellipsoid())

inIdx = source.fields().lookupField(inField)
targetIdx = target_source.fields().lookupField(targetField)

index = QgsSpatialIndex(target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(source.sourceCrs())), feedback)

first = True
sink = None
dest_id = None
@@ -225,27 +223,28 @@ def regularMatrix(self, parameters, context, source, inField, target_source, tar
break

inGeom = inFeat.geometry()
inID = str(inFeat.attributes()[inIdx])
featList = index.nearestNeighbor(inGeom.asPoint(), nPoints)
if first:
featList = index.nearestNeighbor(inGeom.asPoint(), nPoints)
first = False
fields = QgsFields()
input_id_field = source.fields()[inIdx]
input_id_field.setName('ID')
fields.append(input_id_field)
for i in range(len(featList)):
fields.append(QgsField('DIST_{0}'.format(i + 1), QVariant.Double))
for f in target_source.getFeatures(QgsFeatureRequest().setFilterFids(featList).setSubsetOfAttributes([targetIdx]).setDestinationCrs(source.sourceCrs())):
fields.append(QgsField(str(f[targetField]), QVariant.Double))

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, source.wkbType(), source.sourceCrs())

data = [inID]
data = [inFeat[inField]]
for target in target_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setFilterFids(featList).setDestinationCrs(source.sourceCrs())):
if feedback.isCanceled():
break
outGeom = target.geometry()
dist = distArea.measureLine(inGeom.asPoint(),
outGeom.asPoint())
data.append(float(dist))
data.append(dist)

out_feature = QgsFeature()
out_feature.setGeometry(inGeom)
out_feature.setAttributes(data)

Large diffs are not rendered by default.

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://ogr.maptools.org/" xmlns:ogr="http://ogr.maptools.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" elementFormDefault="qualified" version="1.0">
<xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
<xs:element name="FeatureCollection" type="ogr:FeatureCollectionType" substitutionGroup="gml:_FeatureCollection"/>
<xs:complexType name="FeatureCollectionType">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureCollectionType">
<xs:attribute name="lockId" type="xs:string" use="optional"/>
<xs:attribute name="scope" type="xs:string" use="optional"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="linear_matrix" type="ogr:linear_matrix_Type" substitutionGroup="gml:_Feature"/>
<xs:complexType name="linear_matrix_Type">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureType">
<xs:sequence>
<xs:element name="geometryProperty" type="gml:MultiPointPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
<xs:element name="InputID" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="255"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="TargetID" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="255"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Distance" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
@@ -0,0 +1,149 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ogr.maptools.org/ standard_matrix.xsd"
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>0</gml:X><gml:Y>-5</gml:Y></gml:coord>
<gml:coord><gml:X>8</gml:X><gml:Y>3</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:standard_matrix fid="standard_matrix.0">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>1,1</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:ID>points.0</ogr:ID>
<ogr:points.7>6.32455532033676</ogr:points.7>
<ogr:points.6>7.28010988928052</ogr:points.6>
<ogr:points.5>6.08276253029822</ogr:points.5>
<ogr:points.4>3</ogr:points.4>
<ogr:points.8>2.23606797749979</ogr:points.8>
<ogr:points.3>4.12310562561766</ogr:points.3>
<ogr:points.2>1.4142135623731</ogr:points.2>
<ogr:points.1>2.82842712474619</ogr:points.1>
<ogr:points.0>0</ogr:points.0>
</ogr:standard_matrix>
</gml:featureMember>
<gml:featureMember>
<ogr:standard_matrix fid="standard_matrix.1">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3,3</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:ID>points.1</ogr:ID>
<ogr:points.7>5.65685424949238</ogr:points.7>
<ogr:points.6>6.40312423743285</ogr:points.6>
<ogr:points.5>8.54400374531753</ogr:points.5>
<ogr:points.4>2.23606797749979</ogr:points.4>
<ogr:points.8>5</ogr:points.8>
<ogr:points.3>2.23606797749979</ogr:points.3>
<ogr:points.2>1.4142135623731</ogr:points.2>
<ogr:points.1>0</ogr:points.1>
<ogr:points.0>2.82842712474619</ogr:points.0>
</ogr:standard_matrix>
</gml:featureMember>
<gml:featureMember>
<ogr:standard_matrix fid="standard_matrix.2">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>2,2</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:ID>points.2</ogr:ID>
<ogr:points.7>5.8309518948453</ogr:points.7>
<ogr:points.6>6.70820393249937</ogr:points.6>
<ogr:points.5>7.28010988928052</ogr:points.5>
<ogr:points.4>2.23606797749979</ogr:points.4>
<ogr:points.8>3.60555127546399</ogr:points.8>
<ogr:points.3>3</ogr:points.3>
<ogr:points.2>0</ogr:points.2>
<ogr:points.1>1.4142135623731</ogr:points.1>
<ogr:points.0>1.4142135623731</ogr:points.0>
</ogr:standard_matrix>
</gml:featureMember>
<gml:featureMember>
<ogr:standard_matrix fid="standard_matrix.3">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>5,2</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:ID>points.3</ogr:ID>
<ogr:points.7>3.60555127546399</ogr:points.7>
<ogr:points.6>4.24264068711928</ogr:points.6>
<ogr:points.5>8.60232526704263</ogr:points.5>
<ogr:points.4>1.4142135623731</ogr:points.4>
<ogr:points.8>5.8309518948453</ogr:points.8>
<ogr:points.3>0</ogr:points.3>
<ogr:points.2>3</ogr:points.2>
<ogr:points.1>2.23606797749979</ogr:points.1>
<ogr:points.0>4.12310562561766</ogr:points.0>
</ogr:standard_matrix>
</gml:featureMember>
<gml:featureMember>
<ogr:standard_matrix fid="standard_matrix.4">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>4,1</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:ID>points.4</ogr:ID>
<ogr:points.7>3.60555127546399</ogr:points.7>
<ogr:points.6>4.47213595499958</ogr:points.6>
<ogr:points.5>7.21110255092798</ogr:points.5>
<ogr:points.4>0</ogr:points.4>
<ogr:points.8>4.47213595499958</ogr:points.8>
<ogr:points.3>1.4142135623731</ogr:points.3>
<ogr:points.2>2.23606797749979</ogr:points.2>
<ogr:points.1>2.23606797749979</ogr:points.1>
<ogr:points.0>3</ogr:points.0>
</ogr:standard_matrix>
</gml:featureMember>
<gml:featureMember>
<ogr:standard_matrix fid="standard_matrix.5">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>0,-5</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:ID>points.5</ogr:ID>
<ogr:points.7>8.06225774829855</ogr:points.7>
<ogr:points.6>8.94427190999916</ogr:points.6>
<ogr:points.5>0</ogr:points.5>
<ogr:points.4>7.21110255092798</ogr:points.4>
<ogr:points.8>4</ogr:points.8>
<ogr:points.3>8.60232526704263</ogr:points.3>
<ogr:points.2>7.28010988928052</ogr:points.2>
<ogr:points.1>8.54400374531753</ogr:points.1>
<ogr:points.0>6.08276253029822</ogr:points.0>
</ogr:standard_matrix>
</gml:featureMember>
<gml:featureMember>
<ogr:standard_matrix fid="standard_matrix.6">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>8,-1</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:ID>points.6</ogr:ID>
<ogr:points.7>1</ogr:points.7>
<ogr:points.6>0</ogr:points.6>
<ogr:points.5>8.94427190999916</ogr:points.5>
<ogr:points.4>4.47213595499958</ogr:points.4>
<ogr:points.8>8</ogr:points.8>
<ogr:points.3>4.24264068711928</ogr:points.3>
<ogr:points.2>6.70820393249937</ogr:points.2>
<ogr:points.1>6.40312423743285</ogr:points.1>
<ogr:points.0>7.28010988928052</ogr:points.0>
</ogr:standard_matrix>
</gml:featureMember>
<gml:featureMember>
<ogr:standard_matrix fid="standard_matrix.7">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>7,-1</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:ID>points.7</ogr:ID>
<ogr:points.7>0</ogr:points.7>
<ogr:points.6>1</ogr:points.6>
<ogr:points.5>8.06225774829855</ogr:points.5>
<ogr:points.4>3.60555127546399</ogr:points.4>
<ogr:points.8>7</ogr:points.8>
<ogr:points.3>3.60555127546399</ogr:points.3>
<ogr:points.2>5.8309518948453</ogr:points.2>
<ogr:points.1>5.65685424949238</ogr:points.1>
<ogr:points.0>6.32455532033676</ogr:points.0>
</ogr:standard_matrix>
</gml:featureMember>
<gml:featureMember>
<ogr:standard_matrix fid="standard_matrix.8">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>0,-1</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:ID>points.8</ogr:ID>
<ogr:points.7>7</ogr:points.7>
<ogr:points.6>8</ogr:points.6>
<ogr:points.5>4</ogr:points.5>
<ogr:points.4>4.47213595499958</ogr:points.4>
<ogr:points.8>0</ogr:points.8>
<ogr:points.3>5.8309518948453</ogr:points.3>
<ogr:points.2>3.60555127546399</ogr:points.2>
<ogr:points.1>5</ogr:points.1>
<ogr:points.0>2.23606797749979</ogr:points.0>
</ogr:standard_matrix>
</gml:featureMember>
</ogr:FeatureCollection>
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://ogr.maptools.org/" xmlns:ogr="http://ogr.maptools.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" elementFormDefault="qualified" version="1.0">
<xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
<xs:element name="FeatureCollection" type="ogr:FeatureCollectionType" substitutionGroup="gml:_FeatureCollection"/>
<xs:complexType name="FeatureCollectionType">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureCollectionType">
<xs:attribute name="lockId" type="xs:string" use="optional"/>
<xs:attribute name="scope" type="xs:string" use="optional"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="standard_matrix" type="ogr:standard_matrix_Type" substitutionGroup="gml:_Feature"/>
<xs:complexType name="standard_matrix_Type">
<xs:complexContent>
<xs:extension base="gml:AbstractFeatureType">
<xs:sequence>
<xs:element name="geometryProperty" type="gml:PointPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
<xs:element name="ID" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="255"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="points.7" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="points.6" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="points.5" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="points.4" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="points.8" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="points.3" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="points.2" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="points.1" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="points.0" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>

0 comments on commit d34c09b

Please sign in to comment.
You can’t perform that action at this time.