Skip to content

Commit

Permalink
Merge pull request #2920 from bstroebl/testEliminate
Browse files Browse the repository at this point in the history
[processing] Tests for eliminate
  • Loading branch information
m-kuhn committed Mar 17, 2016
2 parents 09f6b91 + cd267b0 commit 9e6866e
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 59 deletions.
35 changes: 18 additions & 17 deletions python/plugins/processing/algs/qgis/Eliminate.py
Expand Up @@ -41,7 +41,7 @@
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterSelection
from processing.core.outputs import OutputVector
from processing.tools import dataobjects
from processing.tools import dataobjects, vector

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

Expand Down Expand Up @@ -101,15 +101,16 @@ def processAlgorithm(self, progress):
boundary = self.getParameterValue(self.MODE) == self.MODE_BOUNDARY
smallestArea = self.getParameterValue(self.MODE) == self.MODE_SMALLEST_AREA
keepSelection = self.getParameterValue(self.KEEPSELECTION)
processLayer = vector.duplicateInMemory(inLayer)

if not keepSelection:
# Make a selection with the values provided
attribute = self.getParameterValue(self.ATTRIBUTE)
comparison = self.comparisons[self.getParameterValue(self.COMPARISON)]
comparisonvalue = self.getParameterValue(self.COMPARISONVALUE)

selectindex = inLayer.dataProvider().fieldNameIndex(attribute)
selectType = inLayer.dataProvider().fields()[selectindex].type()
selectindex = vector.resolveFieldIndex(processLayer, attribute)
selectType = processLayer.fields()[selectindex].type()
selectionError = False

if selectType == 2:
Expand Down Expand Up @@ -166,7 +167,7 @@ def processAlgorithm(self, progress):
raise GeoAlgorithmExecutionException(
self.tr('Error in selection input: %s' % msg))
else:
for feature in inLayer.getFeatures():
for feature in processLayer.getFeatures():
aValue = feature.attributes()[selectindex]

if aValue is None:
Expand Down Expand Up @@ -205,20 +206,20 @@ def processAlgorithm(self, progress):
if match:
selected.append(feature.id())

inLayer.setSelectedFeatures(selected)
processLayer.setSelectedFeatures(selected)

if inLayer.selectedFeatureCount() == 0:
if processLayer.selectedFeatureCount() == 0:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
self.tr('%s: (No selection in input layer "%s")' % (self.commandLineName(), self.getParameterValue(self.INPUT))))

# Keep references to the features to eliminate
featToEliminate = []
for aFeat in inLayer.selectedFeatures():
for aFeat in processLayer.selectedFeatures():
featToEliminate.append(aFeat)

# Delete all features to eliminate in inLayer (we won't save this)
inLayer.startEditing()
inLayer.deleteSelectedFeatures()
# Delete all features to eliminate in processLayer (we won't save this)
processLayer.startEditing()
processLayer.deleteSelectedFeatures()

# ANALYZE
if len(featToEliminate) > 0: # Prevent zero division
Expand All @@ -242,7 +243,7 @@ def processAlgorithm(self, progress):
feat = featToEliminate.pop()
geom2Eliminate = QgsGeometry(feat.geometry())
bbox = geom2Eliminate.boundingBox()
fit = inLayer.getFeatures(
fit = processLayer.getFeatures(
QgsFeatureRequest().setFilterRect(bbox))
mergeWithFid = None
mergeWithGeom = None
Expand Down Expand Up @@ -296,7 +297,7 @@ def processAlgorithm(self, progress):
# A successful candidate
newGeom = mergeWithGeom.combine(geom2Eliminate)

if inLayer.changeGeometry(mergeWithFid, newGeom):
if processLayer.changeGeometry(mergeWithFid, newGeom):
madeProgress = True
else:
raise GeoAlgorithmExecutionException(
Expand All @@ -314,18 +315,18 @@ def processAlgorithm(self, progress):
# End while

# Create output
provider = inLayer.dataProvider()
provider = processLayer.dataProvider()
output = self.getOutputFromName(self.OUTPUT)
writer = output.getVectorWriter(provider.fields(),
provider.geometryType(), inLayer.crs())
provider.geometryType(), processLayer.crs())

# Write all features that are left over to output layer
iterator = inLayer.getFeatures()
iterator = processLayer.getFeatures()
for feature in iterator:
writer.addFeature(feature)

# Leave inLayer untouched
inLayer.rollBack()
# Leave processLayer untouched
processLayer.rollBack()

for feature in featNotEliminated:
writer.addFeature(feature)
Binary file not shown.
1 change: 0 additions & 1 deletion python/plugins/processing/tests/testdata/custom/polys.prj

This file was deleted.

1 change: 0 additions & 1 deletion python/plugins/processing/tests/testdata/custom/polys.qpj

This file was deleted.

Binary file not shown.
Binary file not shown.
Expand Up @@ -16,22 +16,22 @@
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-1,-1 -1,3 3,3 3,2 6,1 6,-3 2,-1 -1,-1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>aaaaa</ogr:name>
<ogr:intval>33</ogr:intval>
<ogr:floatval>44.123455999999997</ogr:floatval>
<ogr:floatval>44.123456</ogr:floatval>
</ogr:eliminate_largest_area>
</gml:featureMember>
<gml:featureMember>
<ogr:eliminate_largest_area fid="polys.1">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>5,5 6,4 4,4 5,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>Aaaaa</ogr:name>
<ogr:intval>-33</ogr:intval>
<ogr:floatval>0.000000000000000</ogr:floatval>
<ogr:floatval>0</ogr:floatval>
</ogr:eliminate_largest_area>
</gml:featureMember>
<gml:featureMember>
<ogr:eliminate_largest_area fid="polys.2">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>2,5 2,6 3,6 3,5 2,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>bbaaa</ogr:name>
<ogr:floatval>0.123000000000000</ogr:floatval>
<ogr:floatval>0.123</ogr:floatval>
</ogr:eliminate_largest_area>
</gml:featureMember>
<gml:featureMember>
Expand All @@ -44,7 +44,7 @@
<gml:featureMember>
<ogr:eliminate_largest_area fid="polys.4">
<ogr:intval>120</ogr:intval>
<ogr:floatval>-100291.432130000001052</ogr:floatval>
<ogr:floatval>-100291.43213</ogr:floatval>
</ogr:eliminate_largest_area>
</gml:featureMember>
</ogr:FeatureCollection>
Expand Up @@ -33,8 +33,6 @@
<xs:element name="floatval" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:totalDigits value="24"/>
<xs:fractionDigits value="15"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Expand Down
Expand Up @@ -16,22 +16,22 @@
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-1,-1 -1,3 3,3 3,2 2,2 2,-1 -1,-1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>aaaaa</ogr:name>
<ogr:intval>33</ogr:intval>
<ogr:floatval>44.123455999999997</ogr:floatval>
<ogr:floatval>44.123456</ogr:floatval>
</ogr:eliminate_smallest_area>
</gml:featureMember>
<gml:featureMember>
<ogr:eliminate_smallest_area fid="polys.1">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>5,5 6,4 4,4 5,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>Aaaaa</ogr:name>
<ogr:intval>-33</ogr:intval>
<ogr:floatval>0.000000000000000</ogr:floatval>
<ogr:floatval>0</ogr:floatval>
</ogr:eliminate_smallest_area>
</gml:featureMember>
<gml:featureMember>
<ogr:eliminate_smallest_area fid="polys.2">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>2,5 2,6 3,6 3,5 2,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>bbaaa</ogr:name>
<ogr:floatval>0.123000000000000</ogr:floatval>
<ogr:floatval>0.123</ogr:floatval>
</ogr:eliminate_smallest_area>
</gml:featureMember>
<gml:featureMember>
Expand All @@ -44,7 +44,7 @@
<gml:featureMember>
<ogr:eliminate_smallest_area fid="polys.4">
<ogr:intval>120</ogr:intval>
<ogr:floatval>-100291.432130000001052</ogr:floatval>
<ogr:floatval>-100291.43213</ogr:floatval>
</ogr:eliminate_smallest_area>
</gml:featureMember>
</ogr:FeatureCollection>
Expand Up @@ -33,8 +33,6 @@
<xs:element name="floatval" nillable="true" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:totalDigits value="24"/>
<xs:fractionDigits value="15"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Expand Up @@ -10,18 +10,18 @@
<gml:coord><gml:X>8</gml:X><gml:Y>5.5</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:polys_centroid fid="polys.0">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>0.653846153846154,1.11538461538462</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>0.653846153846154,1.115384615384615</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:name>aaaaa</ogr:name>
<ogr:intval>33</ogr:intval>
<ogr:floatval>44.123456</ogr:floatval>
</ogr:polys_centroid>
</gml:featureMember>
<gml:featureMember>
<ogr:polys_centroid fid="polys.1">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>5.0,4.33333333333333</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>5.0,4.333333333333333</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:name>Aaaaa</ogr:name>
<ogr:intval>-33</ogr:intval>
<ogr:floatval>0</ogr:floatval>
Expand All @@ -47,4 +47,12 @@
<ogr:floatval>-100291.43213</ogr:floatval>
</ogr:polys_centroid>
</gml:featureMember>
<gml:featureMember>
<ogr:polys_centroid fid="polys.5">
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>4.080459770114943,-0.218390804597701</gml:coordinates></gml:Point></ogr:geometryProperty>
<ogr:name>elim</ogr:name>
<ogr:intval>2</ogr:intval>
<ogr:floatval>3.33</ogr:floatval>
</ogr:polys_centroid>
</gml:featureMember>
</ogr:FeatureCollection>
Expand Up @@ -10,18 +10,18 @@
<gml:coord><gml:X>10</gml:X><gml:Y>6</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:polys_deleteholes fid="polys.0">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-1,-1 2,-1 2,2 3,2 3,3 -1,3 -1,-1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-1,-1 -1,3 3,3 3,2 2,2 2,-1 -1,-1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>aaaaa</ogr:name>
<ogr:intval>33</ogr:intval>
<ogr:floatval>44.123456</ogr:floatval>
</ogr:polys_deleteholes>
</gml:featureMember>
<gml:featureMember>
<ogr:polys_deleteholes fid="polys.1">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>5,5 4,4 6,4 5,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>5,5 6,4 4,4 5,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>Aaaaa</ogr:name>
<ogr:intval>-33</ogr:intval>
<ogr:floatval>0</ogr:floatval>
Expand All @@ -47,4 +47,12 @@
<ogr:floatval>-100291.43213</ogr:floatval>
</ogr:polys_deleteholes>
</gml:featureMember>
<gml:featureMember>
<ogr:polys_deleteholes fid="polys.5">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>3,2 6,1 6,-3 2,-1 2,2 3,2</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>elim</ogr:name>
<ogr:intval>2</ogr:intval>
<ogr:floatval>3.33</ogr:floatval>
</ogr:polys_deleteholes>
</gml:featureMember>
</ogr:FeatureCollection>
7 changes: 3 additions & 4 deletions python/plugins/processing/tests/testdata/polys.gfs
@@ -1,12 +1,11 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>polys</Name>
<ElementPath>polys</ElementPath>
<!--POLYGON-->
<Name>polys2</Name>
<ElementPath>polys2</ElementPath>
<GeometryType>3</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>5</FeatureCount>
<FeatureCount>6</FeatureCount>
<ExtentXMin>-1.00000</ExtentXMin>
<ExtentXMax>10.00000</ExtentXMax>
<ExtentYMin>-3.00000</ExtentYMin>
Expand Down
36 changes: 22 additions & 14 deletions python/plugins/processing/tests/testdata/polys.gml
Expand Up @@ -10,41 +10,49 @@
<gml:coord><gml:X>10</gml:X><gml:Y>6</gml:Y></gml:coord>
</gml:Box>
</gml:boundedBy>

<gml:featureMember>
<ogr:polys fid="polys.0">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-1,-1 2,-1 2,2 3,2 3,3 -1,3 -1,-1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:polys2 fid="polys.0">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-1,-1 -1,3 3,3 3,2 2,2 2,-1 -1,-1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>aaaaa</ogr:name>
<ogr:intval>33</ogr:intval>
<ogr:floatval>44.123456</ogr:floatval>
</ogr:polys>
</ogr:polys2>
</gml:featureMember>
<gml:featureMember>
<ogr:polys fid="polys.1">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>5,5 4,4 6,4 5,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:polys2 fid="polys.1">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>5,5 6,4 4,4 5,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>Aaaaa</ogr:name>
<ogr:intval>-33</ogr:intval>
<ogr:floatval>0</ogr:floatval>
</ogr:polys>
</ogr:polys2>
</gml:featureMember>
<gml:featureMember>
<ogr:polys fid="polys.2">
<ogr:polys2 fid="polys.2">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>2,5 2,6 3,6 3,5 2,5</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>bbaaa</ogr:name>
<ogr:floatval>0.123</ogr:floatval>
</ogr:polys>
</ogr:polys2>
</gml:featureMember>
<gml:featureMember>
<ogr:polys fid="polys.3">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>6,1 10,1 10,-3 6,-3 6,1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs><gml:innerBoundaryIs><gml:LinearRing><gml:coordinates>7,0 9,0 9,-2 7,-2 7,0</gml:coordinates></gml:LinearRing></gml:innerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:polys2 fid="polys.3">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>6,1 10,1 10,-3 6,-3 6,1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs><gml:innerBoundaryIs><gml:LinearRing><gml:coordinates>7,0 7,-2 9,-2 9,0 7,0</gml:coordinates></gml:LinearRing></gml:innerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>ASDF</ogr:name>
<ogr:intval>0</ogr:intval>
</ogr:polys>
</ogr:polys2>
</gml:featureMember>
<gml:featureMember>
<ogr:polys fid="polys.4">
<ogr:polys2 fid="polys.4">
<ogr:intval>120</ogr:intval>
<ogr:floatval>-100291.43213</ogr:floatval>
</ogr:polys>
</ogr:polys2>
</gml:featureMember>
<gml:featureMember>
<ogr:polys2 fid="polys.5">
<ogr:geometryProperty><gml:Polygon srsName="EPSG:4326"><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>3,2 6,1 6,-3 2,-1 2,2 3,2</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>
<ogr:name>elim</ogr:name>
<ogr:intval>2</ogr:intval>
<ogr:floatval>3.33</ogr:floatval>
</ogr:polys2>
</gml:featureMember>
</ogr:FeatureCollection>
Expand Up @@ -122,6 +122,7 @@ tests:
compare:
geometry:
precision: 7

- algorithm: qgis:addautoincrementalfield
name: Add autoincremental field
params:
Expand All @@ -133,7 +134,6 @@ tests:
name: expected/autoincrement_field.gml
type: vector


# Eliminate sliver polygons
# case 1: merge with largest area
- algorithm: qgis:eliminatesliverpolygons
Expand All @@ -143,7 +143,7 @@ tests:
COMPARISON: '0'
COMPARISONVALUE: 'polys.5'
INPUT:
name: custom/polys.shp
name: polys.gml
type: vector
KEEPSELECTION: 'False'
MODE: '0'
Expand All @@ -160,7 +160,7 @@ tests:
COMPARISON: '0'
COMPARISONVALUE: 'polys.5'
INPUT:
name: custom/polys.shp
name: polys.gml
type: vector
KEEPSELECTION: 'False'
MODE: '1'
Expand All @@ -177,7 +177,7 @@ tests:
COMPARISON: '0'
COMPARISONVALUE: 'polys.5'
INPUT:
name: custom/polys.shp
name: polys.gml
type: vector
KEEPSELECTION: 'False'
MODE: '2'
Expand Down

0 comments on commit 9e6866e

Please sign in to comment.