Skip to content
Permalink
Browse files

Merge pull request #5241 from nyalldawson/clean_code

[processing] Code cleanups
  • Loading branch information
nyalldawson committed Sep 23, 2017
2 parents e3f128b + 5a802b5 commit 53aba6109cce9f6df3d80b368e8ce3ed2e1edb2d
@@ -294,14 +294,6 @@ qgis:listuniquevalues: >

qgis:meanandstandarddeviationplot:


qgis:meancoordinates: >
This algorithm computes a point layer with the center of mass of geometries in an input layer.

An attribute can be specified as containing weights to be applied to each feature when computing the center of mass.

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.

qgis:mergevectorlayers: >
This algorithm combines multiple vector layers of the same geometry type into a single one.

@@ -32,9 +32,11 @@

from qgis.core import (QgsCoordinateTransform,
QgsField,
QgsFields,
QgsWkbTypes,
QgsFeatureSink,
QgsDistanceArea,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterEnum,
QgsProcessingParameterFeatureSink)
@@ -89,28 +91,23 @@ def processAlgorithm(self, parameters, context, feedback):
wkb_type = source.wkbType()
fields = source.fields()

new_fields = QgsFields()
if QgsWkbTypes.geometryType(wkb_type) == QgsWkbTypes.PolygonGeometry:
areaName = vector.createUniqueFieldName('area', fields)
fields.append(QgsField(areaName, QVariant.Double))
perimeterName = vector.createUniqueFieldName('perimeter', fields)
fields.append(QgsField(perimeterName, QVariant.Double))
new_fields.append(QgsField('area', QVariant.Double))
new_fields.append(QgsField('perimeter', QVariant.Double))
elif QgsWkbTypes.geometryType(wkb_type) == QgsWkbTypes.LineGeometry:
lengthName = vector.createUniqueFieldName('length', fields)
fields.append(QgsField(lengthName, QVariant.Double))
new_fields.append(QgsField('length', QVariant.Double))
else:
xName = vector.createUniqueFieldName('xcoord', fields)
fields.append(QgsField(xName, QVariant.Double))
yName = vector.createUniqueFieldName('ycoord', fields)
fields.append(QgsField(yName, QVariant.Double))
new_fields.append(QgsField('xcoord', QVariant.Double))
new_fields.append(QgsField('ycoord', QVariant.Double))
if QgsWkbTypes.hasZ(source.wkbType()):
self.export_z = True
zName = vector.createUniqueFieldName('zcoord', fields)
fields.append(QgsField(zName, QVariant.Double))
new_fields.append(QgsField('zcoord', QVariant.Double))
if QgsWkbTypes.hasM(source.wkbType()):
self.export_m = True
zName = vector.createUniqueFieldName('mvalue', fields)
fields.append(QgsField(zName, QVariant.Double))
new_fields.append(QgsField('mvalue', QVariant.Double))

fields = QgsProcessingUtils.combineFields(fields, new_fields)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, wkb_type, source.sourceCrs())

@@ -39,7 +39,8 @@
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsSpatialIndex,
QgsProcessingParameterField)
QgsProcessingParameterField,
QgsProcessingUtils)

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

fieldListB = vector.testForUniqueness(fieldListA, fieldListB)
for b in fieldListB:
fieldListA.append(b)
output_fields = QgsProcessingUtils.combineFields(fieldListA, fieldListB)

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fieldListA, geomType, sourceA.sourceCrs())
output_fields, geomType, sourceA.sourceCrs())

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

This file was deleted.

@@ -92,7 +92,6 @@
from .Intersection import Intersection
from .JoinAttributes import JoinAttributes
from .LinesToPolygons import LinesToPolygons
from .MeanCoords import MeanCoords
from .Merge import Merge
from .MinimumBoundingGeometry import MinimumBoundingGeometry
from .NearestNeighbourAnalysis import NearestNeighbourAnalysis
@@ -226,7 +225,6 @@ def getAlgs(self):
Intersection(),
JoinAttributes(),
LinesToPolygons(),
MeanCoords(),
Merge(),
MinimumBoundingGeometry(),
NearestNeighbourAnalysis(),
Binary file not shown.
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8" ?>
<ogr:FeatureCollection
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ogr.maptools.org/ mean_coordinates_unique_grouped.xsd"
xmlns:ogr="http://ogr.maptools.org/"
xmlns:gml="http://www.opengis.net/gml">
<gml:boundedBy>
<gml:Box>
<gml:coord><gml:X>3</gml:X><gml:Y>-1.2</gml:Y></gml:coord>
<gml:coord><gml:X>3.5</gml:X><gml:Y>2</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:mean_coordinates_unique_grouped fid="mean_coordinates_unique_grouped.0">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3.0,1.5</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:MEAN_X>3.000000000000000</ogr:MEAN_X>
<ogr:MEAN_Y>1.500000000000000</ogr:MEAN_Y>
<ogr:id2>2</ogr:id2>
</ogr:mean_coordinates_unique_grouped>
</gml:featureMember>
<gml:featureMember>
<ogr:mean_coordinates_unique_grouped fid="mean_coordinates_unique_grouped.1">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3.4,-1.2</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:MEAN_X>3.400000000000000</ogr:MEAN_X>
<ogr:MEAN_Y>-1.200000000000000</ogr:MEAN_Y>
<ogr:id2>0</ogr:id2>
</ogr:mean_coordinates_unique_grouped>
</gml:featureMember>
<gml:featureMember>
<ogr:mean_coordinates_unique_grouped fid="mean_coordinates_unique_grouped.2">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3.5,2.0</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:MEAN_X>3.500000000000000</ogr:MEAN_X>
<ogr:MEAN_Y>2.000000000000000</ogr:MEAN_Y>
<ogr:id2>1</ogr:id2>
</ogr:mean_coordinates_unique_grouped>
</gml:featureMember>
</ogr:FeatureCollection>
@@ -0,0 +1,46 @@
<?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="mean_coordinates_unique_grouped" type="ogr:mean_coordinates_unique_grouped_Type" substitutionGroup="gml:_Feature"/>
<xs:complexType name="mean_coordinates_unique_grouped_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="MEAN_X" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:totalDigits value="25"/>
<xs:fractionDigits value="15"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="MEAN_Y" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:totalDigits value="25"/>
<xs:fractionDigits value="15"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="id2" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:totalDigits value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>

0 comments on commit 53aba61

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