Skip to content

Commit

Permalink
[FEATURE][processing] Add non-joinable output to Join by Location alg
Browse files Browse the repository at this point in the history
Allows unjoinable features to be saved to a separate optional layer
  • Loading branch information
nyalldawson committed Aug 17, 2018
1 parent 773371a commit 0d20062
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 13 deletions.
61 changes: 48 additions & 13 deletions python/plugins/processing/algs/qgis/SpatialJoin.py
Expand Up @@ -41,7 +41,8 @@
QgsProcessingParameterEnum,
QgsProcessingParameterField,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterString)
QgsProcessingParameterString,
QgsProcessingOutputNumber)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import vector
Expand All @@ -58,6 +59,8 @@ class SpatialJoin(QgisAlgorithm):
DISCARD_NONMATCHING = "DISCARD_NONMATCHING"
PREFIX = "PREFIX"
OUTPUT = "OUTPUT"
NON_MATCHING = "NON_MATCHING"
JOINED_COUNT = "JOINED_COUNT"

def group(self):
return self.tr('Vector general')
Expand Down Expand Up @@ -120,7 +123,19 @@ def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterString(self.PREFIX,
self.tr('Joined field prefix'), optional=True))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT,
self.tr('Joined layer')))
self.tr('Joined layer'),
QgsProcessing.TypeVectorAnyGeometry,
defaultValue=None, optional=True, createByDefault=True))

non_matching = QgsProcessingParameterFeatureSink(self.NON_MATCHING,
self.tr('Unjoinable features from first layer'),
QgsProcessing.TypeVectorAnyGeometry,
defaultValue=None, optional=True, createByDefault=False)
# TODO GUI doesn't support advanced outputs yet
# non_matching.setFlags(non_matching.flags() | QgsProcessingParameterDefinition.FlagAdvanced )
self.addParameter(non_matching)

self.addOutput(QgsProcessingOutputNumber(self.JOINED_COUNT, self.tr("Number of joined features from input table")))

def name(self):
return 'joinattributesbylocation'
Expand Down Expand Up @@ -170,9 +185,14 @@ def processAlgorithm(self, parameters, context, feedback):

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
out_fields, source.wkbType(), source.sourceCrs())
if sink is None:
if self.OUTPUT in parameters and parameters[self.OUTPUT] is not None and sink is None:
raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))

(non_matching_sink, non_matching_dest_id) = self.parameterAsSink(parameters, self.NON_MATCHING, context,
source.fields(), source.wkbType(), source.sourceCrs())
if self.NON_MATCHING in parameters and parameters[self.NON_MATCHING] is not None and non_matching_sink is None:
raise QgsProcessingException(self.invalidSinkError(parameters, self.NON_MATCHING))

# do the join

# build a list of 'reversed' predicates, because in this function
Expand All @@ -182,7 +202,7 @@ def processAlgorithm(self, parameters, context, feedback):
self.parameterAsEnums(parameters, self.PREDICATE, context)]

remaining = set()
if not discard_nomatch:
if not discard_nomatch or non_matching_sink is not None:
remaining = set(source.allFeatureIds())

added_set = set()
Expand All @@ -191,6 +211,9 @@ def processAlgorithm(self, parameters, context, feedback):
features = join_source.getFeatures(request)
total = 100.0 / join_source.featureCount() if join_source.featureCount() else 0

joined_count = 0
unjoined_count = 0

for current, f in enumerate(features):
if feedback.isCanceled():
break
Expand Down Expand Up @@ -221,21 +244,33 @@ def processAlgorithm(self, parameters, context, feedback):
if getattr(engine, predicate)(test_feat.geometry().constGet()):
added_set.add(test_feat.id())

# join attributes and add
attributes = test_feat.attributes()
attributes.extend(join_attributes)
output_feature = test_feat
output_feature.setAttributes(attributes)
sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
if sink is not None:
# join attributes and add
attributes = test_feat.attributes()
attributes.extend(join_attributes)
output_feature = test_feat
output_feature.setAttributes(attributes)
sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
break

feedback.setProgress(int(current * total))

if not discard_nomatch:
if not discard_nomatch or non_matching_sink is not None:
remaining = remaining.difference(added_set)
for f in source.getFeatures(QgsFeatureRequest().setFilterFids(list(remaining))):
if feedback.isCanceled():
break
sink.addFeature(f, QgsFeatureSink.FastInsert)
if sink is not None:
sink.addFeature(f, QgsFeatureSink.FastInsert)
if non_matching_sink is not None:
non_matching_sink.addFeature(f, QgsFeatureSink.FastInsert)

result = {}
if sink is not None:
result[self.OUTPUT] = dest_id
if non_matching_sink is not None:
result[self.NON_MATCHING] = non_matching_dest_id

result[self.JOINED_COUNT] = len(added_set)

return {self.OUTPUT: dest_id}
return result
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ogr.maptools.org/ join_by_location_unjoinable.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>2</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:join_by_location_unjoinable fid="points.3">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>5,2</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:id>4</ogr:id>
<ogr:id2>2</ogr:id2>
</ogr:join_by_location_unjoinable>
</gml:featureMember>
<gml:featureMember>
<ogr:join_by_location_unjoinable fid="points.5">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>0,-5</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:id>6</ogr:id>
<ogr:id2>0</ogr:id2>
</ogr:join_by_location_unjoinable>
</gml:featureMember>
<gml:featureMember>
<ogr:join_by_location_unjoinable fid="points.6">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>8,-1</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:id>7</ogr:id>
<ogr:id2>0</ogr:id2>
</ogr:join_by_location_unjoinable>
</gml:featureMember>
</ogr:FeatureCollection>
@@ -0,0 +1,37 @@
<?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="join_by_location_unjoinable" type="ogr:join_by_location_unjoinable_Type" substitutionGroup="gml:_Feature"/>
<xs:complexType name="join_by_location_unjoinable_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:long">
<xs:totalDigits value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="id2" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:long">
<xs:totalDigits value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
19 changes: 19 additions & 0 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -4738,6 +4738,25 @@ tests:
fid: skip
fid_2: skip

- algorithm: qgis:joinattributesbylocation
name: Join by location, unjoinable
params:
DISCARD_NONMATCHING: false
INPUT:
name: custom/points.shp
type: vector
JOIN:
name: polys.gml
type: vector
METHOD: 0
PREDICATE:
- 0
PREFIX: ''
results:
NON_MATCHING:
name: expected/join_by_location_unjoinable.gml
type: vector

- algorithm: qgis:joinbylocationsummary
name: Join by location (summary), intersects
params:
Expand Down

0 comments on commit 0d20062

Please sign in to comment.