mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-26 00:04:03 -04:00
Port Join Attributes alg to new API
Improvements: - don't fetch unused geometry for joined table
This commit is contained in:
parent
572dadab01
commit
adda744576
@ -30,14 +30,13 @@ import os
|
|||||||
|
|
||||||
from qgis.core import (QgsFeature,
|
from qgis.core import (QgsFeature,
|
||||||
QgsFeatureSink,
|
QgsFeatureSink,
|
||||||
QgsApplication,
|
QgsFeatureRequest,
|
||||||
QgsProcessingUtils)
|
QgsProcessingParameterFeatureSource,
|
||||||
|
QgsProcessingUtils,
|
||||||
|
QgsProcessingParameterField,
|
||||||
|
QgsProcessingParameterFeatureSink)
|
||||||
|
|
||||||
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
||||||
from processing.core.parameters import ParameterVector
|
|
||||||
from processing.core.parameters import ParameterTable
|
|
||||||
from processing.core.parameters import ParameterTableField
|
|
||||||
from processing.core.outputs import OutputVector
|
|
||||||
from processing.tools import vector
|
from processing.tools import vector
|
||||||
|
|
||||||
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
||||||
@ -45,11 +44,11 @@ pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|||||||
|
|
||||||
class JoinAttributes(QgisAlgorithm):
|
class JoinAttributes(QgisAlgorithm):
|
||||||
|
|
||||||
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
OUTPUT = 'OUTPUT'
|
||||||
INPUT_LAYER = 'INPUT_LAYER'
|
INPUT = 'INPUT'
|
||||||
INPUT_LAYER_2 = 'INPUT_LAYER_2'
|
INPUT_2 = 'INPUT_2'
|
||||||
TABLE_FIELD = 'TABLE_FIELD'
|
FIELD = 'FIELD'
|
||||||
TABLE_FIELD_2 = 'TABLE_FIELD_2'
|
FIELD_2 = 'FIELD_2'
|
||||||
|
|
||||||
def group(self):
|
def group(self):
|
||||||
return self.tr('Vector general tools')
|
return self.tr('Vector general tools')
|
||||||
@ -58,16 +57,15 @@ class JoinAttributes(QgisAlgorithm):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def initAlgorithm(self, config=None):
|
def initAlgorithm(self, config=None):
|
||||||
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
||||||
self.tr('Input layer')))
|
self.tr('Input layer')))
|
||||||
self.addParameter(ParameterTable(self.INPUT_LAYER_2,
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_2,
|
||||||
self.tr('Input layer 2'), False))
|
self.tr('Input layer 2')))
|
||||||
self.addParameter(ParameterTableField(self.TABLE_FIELD,
|
self.addParameter(QgsProcessingParameterField(self.FIELD,
|
||||||
self.tr('Table field'), self.INPUT_LAYER))
|
self.tr('Table field'), parentLayerParameterName=self.INPUT))
|
||||||
self.addParameter(ParameterTableField(self.TABLE_FIELD_2,
|
self.addParameter(QgsProcessingParameterField(self.FIELD_2,
|
||||||
self.tr('Table field 2'), self.INPUT_LAYER_2))
|
self.tr('Table field 2'), parentLayerParameterName=self.INPUT_2))
|
||||||
self.addOutput(OutputVector(self.OUTPUT_LAYER,
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Joined layer')))
|
||||||
self.tr('Joined layer')))
|
|
||||||
|
|
||||||
def name(self):
|
def name(self):
|
||||||
return 'joinattributestable'
|
return 'joinattributestable'
|
||||||
@ -76,26 +74,27 @@ class JoinAttributes(QgisAlgorithm):
|
|||||||
return self.tr('Join attributes table')
|
return self.tr('Join attributes table')
|
||||||
|
|
||||||
def processAlgorithm(self, parameters, context, feedback):
|
def processAlgorithm(self, parameters, context, feedback):
|
||||||
input = self.getParameterValue(self.INPUT_LAYER)
|
input = self.parameterAsSource(parameters, self.INPUT, context)
|
||||||
input2 = self.getParameterValue(self.INPUT_LAYER_2)
|
input2 = self.parameterAsSource(parameters, self.INPUT_2, context)
|
||||||
output = self.getOutputFromName(self.OUTPUT_LAYER)
|
field = self.parameterAsString(parameters, self.FIELD, context)
|
||||||
field = self.getParameterValue(self.TABLE_FIELD)
|
field2 = self.parameterAsString(parameters, self.FIELD_2, context)
|
||||||
field2 = self.getParameterValue(self.TABLE_FIELD_2)
|
|
||||||
|
|
||||||
layer = QgsProcessingUtils.mapLayerFromString(input, context)
|
joinField1Index = input.fields().lookupField(field)
|
||||||
joinField1Index = layer.fields().lookupField(field)
|
joinField2Index = input2.fields().lookupField(field2)
|
||||||
|
|
||||||
layer2 = QgsProcessingUtils.mapLayerFromString(input2, context)
|
outFields = vector.combineFields(input.fields(), input2.fields())
|
||||||
joinField2Index = layer2.fields().lookupField(field2)
|
|
||||||
|
|
||||||
outFields = vector.combineVectorFields(layer, layer2)
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
||||||
writer = output.getVectorWriter(outFields, layer.wkbType(), layer.crs(), context)
|
outFields, input.wkbType(), input.sourceCrs())
|
||||||
|
|
||||||
# Cache attributes of Layer 2
|
# Cache attributes of input2
|
||||||
cache = {}
|
cache = {}
|
||||||
features = QgsProcessingUtils.getFeatures(layer2, context)
|
features = input2.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry))
|
||||||
total = 100.0 / layer2.featureCount() if layer2.featureCount() else 0
|
total = 100.0 / input2.featureCount() if input2.featureCount() else 0
|
||||||
for current, feat in enumerate(features):
|
for current, feat in enumerate(features):
|
||||||
|
if feedback.isCanceled():
|
||||||
|
break
|
||||||
|
|
||||||
attrs = feat.attributes()
|
attrs = feat.attributes()
|
||||||
joinValue2 = str(attrs[joinField2Index])
|
joinValue2 = str(attrs[joinField2Index])
|
||||||
if joinValue2 not in cache:
|
if joinValue2 not in cache:
|
||||||
@ -104,14 +103,18 @@ class JoinAttributes(QgisAlgorithm):
|
|||||||
|
|
||||||
# Create output vector layer with additional attribute
|
# Create output vector layer with additional attribute
|
||||||
outFeat = QgsFeature()
|
outFeat = QgsFeature()
|
||||||
features = QgsProcessingUtils.getFeatures(layer, context)
|
features = input.getFeatures()
|
||||||
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
total = 100.0 / input.featureCount() if input.featureCount() else 0
|
||||||
for current, feat in enumerate(features):
|
for current, feat in enumerate(features):
|
||||||
|
if feedback.isCanceled():
|
||||||
|
break
|
||||||
|
|
||||||
outFeat.setGeometry(feat.geometry())
|
outFeat.setGeometry(feat.geometry())
|
||||||
attrs = feat.attributes()
|
attrs = feat.attributes()
|
||||||
joinValue1 = str(attrs[joinField1Index])
|
joinValue1 = str(attrs[joinField1Index])
|
||||||
attrs.extend(cache.get(joinValue1, []))
|
attrs.extend(cache.get(joinValue1, []))
|
||||||
outFeat.setAttributes(attrs)
|
outFeat.setAttributes(attrs)
|
||||||
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
||||||
feedback.setProgress(int(current * total))
|
feedback.setProgress(int(current * total))
|
||||||
del writer
|
|
||||||
|
return {self.OUTPUT: dest_id}
|
||||||
|
|||||||
@ -82,6 +82,7 @@ from .HubLines import HubLines
|
|||||||
from .ImportIntoPostGIS import ImportIntoPostGIS
|
from .ImportIntoPostGIS import ImportIntoPostGIS
|
||||||
from .ImportIntoSpatialite import ImportIntoSpatialite
|
from .ImportIntoSpatialite import ImportIntoSpatialite
|
||||||
from .Intersection import Intersection
|
from .Intersection import Intersection
|
||||||
|
from .JoinAttributes import JoinAttributes
|
||||||
from .LinesIntersection import LinesIntersection
|
from .LinesIntersection import LinesIntersection
|
||||||
from .LinesToPolygons import LinesToPolygons
|
from .LinesToPolygons import LinesToPolygons
|
||||||
from .MeanCoords import MeanCoords
|
from .MeanCoords import MeanCoords
|
||||||
@ -151,7 +152,6 @@ from .ZonalStatistics import ZonalStatistics
|
|||||||
# from .StatisticsByCategories import StatisticsByCategories
|
# from .StatisticsByCategories import StatisticsByCategories
|
||||||
# from .FieldsCalculator import FieldsCalculator
|
# from .FieldsCalculator import FieldsCalculator
|
||||||
# from .FieldPyculator import FieldsPyculator
|
# from .FieldPyculator import FieldsPyculator
|
||||||
# from .JoinAttributes import JoinAttributes
|
|
||||||
# from .PointsDisplacement import PointsDisplacement
|
# from .PointsDisplacement import PointsDisplacement
|
||||||
# from .PointsFromPolygons import PointsFromPolygons
|
# from .PointsFromPolygons import PointsFromPolygons
|
||||||
# from .PointsFromLines import PointsFromLines
|
# from .PointsFromLines import PointsFromLines
|
||||||
@ -189,7 +189,6 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
|
|||||||
# ExtractByLocation(),
|
# ExtractByLocation(),
|
||||||
# SpatialJoin(),
|
# SpatialJoin(),
|
||||||
# GeometryConvert(), FieldsCalculator(),
|
# GeometryConvert(), FieldsCalculator(),
|
||||||
# JoinAttributes(),
|
|
||||||
# FieldsPyculator(),
|
# FieldsPyculator(),
|
||||||
# StatisticsByCategories(),
|
# StatisticsByCategories(),
|
||||||
# RasterLayerStatistics(), PointsDisplacement(),
|
# RasterLayerStatistics(), PointsDisplacement(),
|
||||||
@ -249,6 +248,7 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
|
|||||||
ImportIntoPostGIS(),
|
ImportIntoPostGIS(),
|
||||||
ImportIntoSpatialite(),
|
ImportIntoSpatialite(),
|
||||||
Intersection(),
|
Intersection(),
|
||||||
|
JoinAttributes(),
|
||||||
LinesIntersection(),
|
LinesIntersection(),
|
||||||
LinesToPolygons(),
|
LinesToPolygons(),
|
||||||
MeanCoords(),
|
MeanCoords(),
|
||||||
|
|||||||
@ -2265,21 +2265,21 @@ tests:
|
|||||||
- expected/points_to_path_grouped2.gml
|
- expected/points_to_path_grouped2.gml
|
||||||
type: vector
|
type: vector
|
||||||
|
|
||||||
# - algorithm: qgis:joinattributestable
|
- algorithm: qgis:joinattributestable
|
||||||
# name: join the attribute table by common field
|
name: join the attribute table by common field
|
||||||
# params:
|
params:
|
||||||
# INPUT_LAYER:
|
INPUT:
|
||||||
# name: points.gml
|
name: points.gml
|
||||||
# type: vector
|
type: vector
|
||||||
# INPUT_LAYER_2:
|
INPUT_2:
|
||||||
# name: table.dbf
|
name: table.dbf
|
||||||
# type: table
|
type: table
|
||||||
# TABLE_FIELD: id
|
FIELD: id
|
||||||
# TABLE_FIELD_2: ID
|
FIELD_2: ID
|
||||||
# results:
|
results:
|
||||||
# OUTPUT_LAYER:
|
OUTPUT:
|
||||||
# name: expected/join_attribute_table.gml
|
name: expected/join_attribute_table.gml
|
||||||
# type: vector
|
type: vector
|
||||||
|
|
||||||
- algorithm: qgis:convexhull
|
- algorithm: qgis:convexhull
|
||||||
name: Simple convex hull
|
name: Simple convex hull
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user