forked from qgis/QGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request qgis#3385 from nyalldawson/centrods
[processing] Rework centroid algorithm to handle non-polygon layers
- Loading branch information
Showing
18 changed files
with
595 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
*************************************************************************** | ||
PolygonCentroids.py | ||
--------------------- | ||
Date : August 2012 | ||
Copyright : (C) 2012 by Victor Olaya | ||
Email : volayaf at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
*************************************************************************** | ||
""" | ||
|
||
__author__ = 'Victor Olaya' | ||
__date__ = 'August 2012' | ||
__copyright__ = '(C) 2012, Victor Olaya' | ||
|
||
# This will get replaced with a git SHA1 when you do a git archive323 | ||
|
||
__revision__ = '$Format:%H$' | ||
|
||
import os | ||
|
||
from qgis.PyQt.QtGui import QIcon | ||
|
||
from qgis.core import Qgis, QgsGeometry, QgsFeature, QgsWkbTypes | ||
|
||
from processing.core.GeoAlgorithm import GeoAlgorithm | ||
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException | ||
from processing.core.parameters import ParameterVector | ||
from processing.core.outputs import OutputVector | ||
from processing.tools import dataobjects, vector | ||
|
||
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0] | ||
|
||
|
||
class PolygonCentroids(GeoAlgorithm): | ||
|
||
INPUT_LAYER = 'INPUT_LAYER' | ||
OUTPUT_LAYER = 'OUTPUT_LAYER' | ||
|
||
def __init__(self): | ||
GeoAlgorithm.__init__(self) | ||
# this algorithm is deprecated - use Centroids instead | ||
self.showInToolbox = False | ||
|
||
def getIcon(self): | ||
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'centroids.png')) | ||
|
||
def defineCharacteristics(self): | ||
self.name, self.i18n_name = self.trAlgorithm('Polygon centroids') | ||
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools') | ||
|
||
self.addParameter(ParameterVector(self.INPUT_LAYER, | ||
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_POLYGON])) | ||
|
||
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Centroids'))) | ||
|
||
def processAlgorithm(self, progress): | ||
layer = dataobjects.getObjectFromUri( | ||
self.getParameterValue(self.INPUT_LAYER)) | ||
|
||
writer = self.getOutputFromName( | ||
self.OUTPUT_LAYER).getVectorWriter( | ||
layer.fields(), | ||
QgsWkbTypes.Point, | ||
layer.crs()) | ||
|
||
outFeat = QgsFeature() | ||
|
||
features = vector.features(layer) | ||
total = 100.0 / len(features) | ||
for current, feat in enumerate(features): | ||
inGeom = feat.geometry() | ||
attrs = feat.attributes() | ||
|
||
if inGeom.isEmpty(): | ||
outGeom = QgsGeometry(None) | ||
else: | ||
outGeom = QgsGeometry(inGeom.centroid()) | ||
if not outGeom: | ||
raise GeoAlgorithmExecutionException( | ||
self.tr('Error calculating centroid')) | ||
|
||
outFeat.setGeometry(outGeom) | ||
outFeat.setAttributes(attrs) | ||
writer.addFeature(outFeat) | ||
progress.setPercentage(int(current * total)) | ||
|
||
del writer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
python/plugins/processing/tests/testdata/expected/centroid_lines.gfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<GMLFeatureClassList> | ||
<GMLFeatureClass> | ||
<Name>centroid_lines</Name> | ||
<ElementPath>centroid_lines</ElementPath> | ||
<GeometryType>1</GeometryType> | ||
<SRSName>EPSG:4326</SRSName> | ||
<DatasetSpecificInfo> | ||
<FeatureCount>7</FeatureCount> | ||
<ExtentXMin>0.00000</ExtentXMin> | ||
<ExtentXMax>8.75520</ExtentXMax> | ||
<ExtentYMin>-3.00000</ExtentYMin> | ||
<ExtentYMax>2.90165</ExtentYMax> | ||
</DatasetSpecificInfo> | ||
</GMLFeatureClass> | ||
</GMLFeatureClassList> |
48 changes: 48 additions & 0 deletions
48
python/plugins/processing/tests/testdata/expected/centroid_lines.gml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ogr:FeatureCollection | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="" | ||
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>-3</gml:Y></gml:coord> | ||
<gml:coord><gml:X>8.755203820042828</gml:X><gml:Y>2.901650429449553</gml:Y></gml:coord> | ||
</gml:Box> | ||
</gml:boundedBy> | ||
|
||
<gml:featureMember> | ||
<ogr:centroid_lines fid="lines.0"> | ||
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>8.755203820042828,2.901650429449553</gml:coordinates></gml:Point></ogr:geometryProperty> | ||
</ogr:centroid_lines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:centroid_lines fid="lines.1"> | ||
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>0,-1</gml:coordinates></gml:Point></ogr:geometryProperty> | ||
</ogr:centroid_lines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:centroid_lines fid="lines.2"> | ||
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>2.375,1.625</gml:coordinates></gml:Point></ogr:geometryProperty> | ||
</ogr:centroid_lines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:centroid_lines fid="lines.3"> | ||
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>4,1</gml:coordinates></gml:Point></ogr:geometryProperty> | ||
</ogr:centroid_lines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:centroid_lines fid="lines.4"> | ||
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>8.5,-3.0</gml:coordinates></gml:Point></ogr:geometryProperty> | ||
</ogr:centroid_lines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:centroid_lines fid="lines.5"> | ||
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>8,-1</gml:coordinates></gml:Point></ogr:geometryProperty> | ||
</ogr:centroid_lines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:centroid_lines fid="lines.6"> | ||
</ogr:centroid_lines> | ||
</gml:featureMember> | ||
</ogr:FeatureCollection> |
15 changes: 15 additions & 0 deletions
15
python/plugins/processing/tests/testdata/expected/centroid_multilines.gfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<GMLFeatureClassList> | ||
<GMLFeatureClass> | ||
<Name>centroid_multilines</Name> | ||
<ElementPath>centroid_multilines</ElementPath> | ||
<GeometryType>1</GeometryType> | ||
<SRSName>EPSG:4326</SRSName> | ||
<DatasetSpecificInfo> | ||
<FeatureCount>4</FeatureCount> | ||
<ExtentXMin>0.00000</ExtentXMin> | ||
<ExtentXMax>4.41936</ExtentXMax> | ||
<ExtentYMin>-1.00000</ExtentYMin> | ||
<ExtentYMax>2.68756</ExtentYMax> | ||
</DatasetSpecificInfo> | ||
</GMLFeatureClass> | ||
</GMLFeatureClassList> |
33 changes: 33 additions & 0 deletions
33
python/plugins/processing/tests/testdata/expected/centroid_multilines.gml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ogr:FeatureCollection | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="" | ||
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>-1</gml:Y></gml:coord> | ||
<gml:coord><gml:X>4.41935638121706</gml:X><gml:Y>2.687560818469874</gml:Y></gml:coord> | ||
</gml:Box> | ||
</gml:boundedBy> | ||
|
||
<gml:featureMember> | ||
<ogr:centroid_multilines fid="lines.1"> | ||
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>0,-1</gml:coordinates></gml:Point></ogr:geometryProperty> | ||
</ogr:centroid_multilines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:centroid_multilines fid="lines.2"> | ||
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>4.41935638121706,1.293104104489945</gml:coordinates></gml:Point></ogr:geometryProperty> | ||
</ogr:centroid_multilines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:centroid_multilines fid="lines.3"> | ||
</ogr:centroid_multilines> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:centroid_multilines fid="lines.4"> | ||
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3.423678244400056,2.687560818469874</gml:coordinates></gml:Point></ogr:geometryProperty> | ||
</ogr:centroid_multilines> | ||
</gml:featureMember> | ||
</ogr:FeatureCollection> |
19 changes: 19 additions & 0 deletions
19
python/plugins/processing/tests/testdata/expected/centroid_multipoint.gfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<GMLFeatureClassList> | ||
<GMLFeatureClass> | ||
<Name>centroid_multipoint</Name> | ||
<ElementPath>centroid_multipoint</ElementPath> | ||
<SRSName>EPSG:4326</SRSName> | ||
<DatasetSpecificInfo> | ||
<FeatureCount>5</FeatureCount> | ||
<ExtentXMin>2.00000</ExtentXMin> | ||
<ExtentXMax>4.50000</ExtentXMax> | ||
<ExtentYMin>-3.00000</ExtentYMin> | ||
<ExtentYMax>2.00000</ExtentYMax> | ||
</DatasetSpecificInfo> | ||
<PropertyDefn> | ||
<Name>d</Name> | ||
<ElementPath>d</ElementPath> | ||
<Type>Integer</Type> | ||
</PropertyDefn> | ||
</GMLFeatureClass> | ||
</GMLFeatureClassList> |
43 changes: 43 additions & 0 deletions
43
python/plugins/processing/tests/testdata/expected/centroid_multipoint.gml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ogr:FeatureCollection | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="" | ||
xmlns:ogr="http://ogr.maptools.org/" | ||
xmlns:gml="http://www.opengis.net/gml"> | ||
<gml:boundedBy> | ||
<gml:Box> | ||
<gml:coord><gml:X>2</gml:X><gml:Y>-3</gml:Y></gml:coord> | ||
<gml:coord><gml:X>4.5</gml:X><gml:Y>2</gml:Y></gml:coord> | ||
</gml:Box> | ||
</gml:boundedBy> | ||
|
||
<gml:featureMember> | ||
<ogr:centroid_multipoint fid="points.9"> | ||
<ogr:d>5</ogr:d> | ||
</ogr:centroid_multipoint> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:centroid_multipoint fid="points.0"> | ||
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>2,2</gml:coordinates></gml:Point></ogr:geometryProperty> | ||
<ogr:d>1</ogr:d> | ||
</ogr:centroid_multipoint> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:centroid_multipoint fid="points.3"> | ||
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>4.5,1.5</gml:coordinates></gml:Point></ogr:geometryProperty> | ||
<ogr:d>2</ogr:d> | ||
</ogr:centroid_multipoint> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:centroid_multipoint fid="points.5"> | ||
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>4,-3</gml:coordinates></gml:Point></ogr:geometryProperty> | ||
<ogr:d>3</ogr:d> | ||
</ogr:centroid_multipoint> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:centroid_multipoint fid="points.7"> | ||
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3.5,-1.0</gml:coordinates></gml:Point></ogr:geometryProperty> | ||
<ogr:d>4</ogr:d> | ||
</ogr:centroid_multipoint> | ||
</gml:featureMember> | ||
</ogr:FeatureCollection> |
Oops, something went wrong.