Skip to content

Commit 53aba61

Browse files
authored
Merge pull request #5241 from nyalldawson/clean_code
[processing] Code cleanups
2 parents e3f128b + 5a802b5 commit 53aba61

16 files changed

+539
-293
lines changed

python/plugins/processing/algs/help/qgis.yaml

-8
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,6 @@ qgis:listuniquevalues: >
294294

295295
qgis:meanandstandarddeviationplot:
296296

297-
298-
qgis:meancoordinates: >
299-
This algorithm computes a point layer with the center of mass of geometries in an input layer.
300-
301-
An attribute can be specified as containing weights to be applied to each feature when computing the center of mass.
302-
303-
If an attribute is selected in the <Unique ID field> parameters, features will be grouped according to values in this field. Instead of a single point with the center of mass of the whole layer, the output layer will contain a center of mass for the features in each category.
304-
305297
qgis:mergevectorlayers: >
306298
This algorithm combines multiple vector layers of the same geometry type into a single one.
307299

python/plugins/processing/algs/qgis/ExportGeometryInfo.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232

3333
from qgis.core import (QgsCoordinateTransform,
3434
QgsField,
35+
QgsFields,
3536
QgsWkbTypes,
3637
QgsFeatureSink,
3738
QgsDistanceArea,
39+
QgsProcessingUtils,
3840
QgsProcessingParameterFeatureSource,
3941
QgsProcessingParameterEnum,
4042
QgsProcessingParameterFeatureSink)
@@ -89,28 +91,23 @@ def processAlgorithm(self, parameters, context, feedback):
8991
wkb_type = source.wkbType()
9092
fields = source.fields()
9193

94+
new_fields = QgsFields()
9295
if QgsWkbTypes.geometryType(wkb_type) == QgsWkbTypes.PolygonGeometry:
93-
areaName = vector.createUniqueFieldName('area', fields)
94-
fields.append(QgsField(areaName, QVariant.Double))
95-
perimeterName = vector.createUniqueFieldName('perimeter', fields)
96-
fields.append(QgsField(perimeterName, QVariant.Double))
96+
new_fields.append(QgsField('area', QVariant.Double))
97+
new_fields.append(QgsField('perimeter', QVariant.Double))
9798
elif QgsWkbTypes.geometryType(wkb_type) == QgsWkbTypes.LineGeometry:
98-
lengthName = vector.createUniqueFieldName('length', fields)
99-
fields.append(QgsField(lengthName, QVariant.Double))
99+
new_fields.append(QgsField('length', QVariant.Double))
100100
else:
101-
xName = vector.createUniqueFieldName('xcoord', fields)
102-
fields.append(QgsField(xName, QVariant.Double))
103-
yName = vector.createUniqueFieldName('ycoord', fields)
104-
fields.append(QgsField(yName, QVariant.Double))
101+
new_fields.append(QgsField('xcoord', QVariant.Double))
102+
new_fields.append(QgsField('ycoord', QVariant.Double))
105103
if QgsWkbTypes.hasZ(source.wkbType()):
106104
self.export_z = True
107-
zName = vector.createUniqueFieldName('zcoord', fields)
108-
fields.append(QgsField(zName, QVariant.Double))
105+
new_fields.append(QgsField('zcoord', QVariant.Double))
109106
if QgsWkbTypes.hasM(source.wkbType()):
110107
self.export_m = True
111-
zName = vector.createUniqueFieldName('mvalue', fields)
112-
fields.append(QgsField(zName, QVariant.Double))
108+
new_fields.append(QgsField('mvalue', QVariant.Double))
113109

110+
fields = QgsProcessingUtils.combineFields(fields, new_fields)
114111
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
115112
fields, wkb_type, source.sourceCrs())
116113

python/plugins/processing/algs/qgis/Intersection.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
QgsProcessingParameterFeatureSource,
4040
QgsProcessingParameterFeatureSink,
4141
QgsSpatialIndex,
42-
QgsProcessingParameterField)
42+
QgsProcessingParameterField,
43+
QgsProcessingUtils)
4344

4445
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
4546
from processing.tools import vector
@@ -122,12 +123,10 @@ def processAlgorithm(self, parameters, context, feedback):
122123
fieldListB = sourceB.fields()
123124
field_indices_b = [i for i in range(0, fieldListB.count())]
124125

125-
fieldListB = vector.testForUniqueness(fieldListA, fieldListB)
126-
for b in fieldListB:
127-
fieldListA.append(b)
126+
output_fields = QgsProcessingUtils.combineFields(fieldListA, fieldListB)
128127

129128
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
130-
fieldListA, geomType, sourceA.sourceCrs())
129+
output_fields, geomType, sourceA.sourceCrs())
131130

132131
outFeat = QgsFeature()
133132
indexB = QgsSpatialIndex(sourceB.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(sourceA.sourceCrs())), feedback)

python/plugins/processing/algs/qgis/MeanCoords.py

-177
This file was deleted.

python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
from .Intersection import Intersection
9393
from .JoinAttributes import JoinAttributes
9494
from .LinesToPolygons import LinesToPolygons
95-
from .MeanCoords import MeanCoords
9695
from .Merge import Merge
9796
from .MinimumBoundingGeometry import MinimumBoundingGeometry
9897
from .NearestNeighbourAnalysis import NearestNeighbourAnalysis
@@ -226,7 +225,6 @@ def getAlgs(self):
226225
Intersection(),
227226
JoinAttributes(),
228227
LinesToPolygons(),
229-
MeanCoords(),
230228
Merge(),
231229
MinimumBoundingGeometry(),
232230
NearestNeighbourAnalysis(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://ogr.maptools.org/ mean_coordinates_unique_grouped.xsd"
5+
xmlns:ogr="http://ogr.maptools.org/"
6+
xmlns:gml="http://www.opengis.net/gml">
7+
<gml:boundedBy>
8+
<gml:Box>
9+
<gml:coord><gml:X>3</gml:X><gml:Y>-1.2</gml:Y></gml:coord>
10+
<gml:coord><gml:X>3.5</gml:X><gml:Y>2</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:mean_coordinates_unique_grouped fid="mean_coordinates_unique_grouped.0">
16+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3.0,1.5</gml:coordinates></gml:Point></ogr:geometryProperty>
17+
<ogr:MEAN_X>3.000000000000000</ogr:MEAN_X>
18+
<ogr:MEAN_Y>1.500000000000000</ogr:MEAN_Y>
19+
<ogr:id2>2</ogr:id2>
20+
</ogr:mean_coordinates_unique_grouped>
21+
</gml:featureMember>
22+
<gml:featureMember>
23+
<ogr:mean_coordinates_unique_grouped fid="mean_coordinates_unique_grouped.1">
24+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3.4,-1.2</gml:coordinates></gml:Point></ogr:geometryProperty>
25+
<ogr:MEAN_X>3.400000000000000</ogr:MEAN_X>
26+
<ogr:MEAN_Y>-1.200000000000000</ogr:MEAN_Y>
27+
<ogr:id2>0</ogr:id2>
28+
</ogr:mean_coordinates_unique_grouped>
29+
</gml:featureMember>
30+
<gml:featureMember>
31+
<ogr:mean_coordinates_unique_grouped fid="mean_coordinates_unique_grouped.2">
32+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3.5,2.0</gml:coordinates></gml:Point></ogr:geometryProperty>
33+
<ogr:MEAN_X>3.500000000000000</ogr:MEAN_X>
34+
<ogr:MEAN_Y>2.000000000000000</ogr:MEAN_Y>
35+
<ogr:id2>1</ogr:id2>
36+
</ogr:mean_coordinates_unique_grouped>
37+
</gml:featureMember>
38+
</ogr:FeatureCollection>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
4+
<xs:element name="FeatureCollection" type="ogr:FeatureCollectionType" substitutionGroup="gml:_FeatureCollection"/>
5+
<xs:complexType name="FeatureCollectionType">
6+
<xs:complexContent>
7+
<xs:extension base="gml:AbstractFeatureCollectionType">
8+
<xs:attribute name="lockId" type="xs:string" use="optional"/>
9+
<xs:attribute name="scope" type="xs:string" use="optional"/>
10+
</xs:extension>
11+
</xs:complexContent>
12+
</xs:complexType>
13+
<xs:element name="mean_coordinates_unique_grouped" type="ogr:mean_coordinates_unique_grouped_Type" substitutionGroup="gml:_Feature"/>
14+
<xs:complexType name="mean_coordinates_unique_grouped_Type">
15+
<xs:complexContent>
16+
<xs:extension base="gml:AbstractFeatureType">
17+
<xs:sequence>
18+
<xs:element name="geometryProperty" type="gml:PointPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
19+
<xs:element name="MEAN_X" nillable="true" minOccurs="0" maxOccurs="1">
20+
<xs:simpleType>
21+
<xs:restriction base="xs:decimal">
22+
<xs:totalDigits value="25"/>
23+
<xs:fractionDigits value="15"/>
24+
</xs:restriction>
25+
</xs:simpleType>
26+
</xs:element>
27+
<xs:element name="MEAN_Y" nillable="true" minOccurs="0" maxOccurs="1">
28+
<xs:simpleType>
29+
<xs:restriction base="xs:decimal">
30+
<xs:totalDigits value="25"/>
31+
<xs:fractionDigits value="15"/>
32+
</xs:restriction>
33+
</xs:simpleType>
34+
</xs:element>
35+
<xs:element name="id2" nillable="true" minOccurs="0" maxOccurs="1">
36+
<xs:simpleType>
37+
<xs:restriction base="xs:integer">
38+
<xs:totalDigits value="10"/>
39+
</xs:restriction>
40+
</xs:simpleType>
41+
</xs:element>
42+
</xs:sequence>
43+
</xs:extension>
44+
</xs:complexContent>
45+
</xs:complexType>
46+
</xs:schema>

0 commit comments

Comments
 (0)