Skip to content

Commit

Permalink
[BUG] run eliminate with non-editable input layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Ströbl committed Mar 16, 2016
1 parent 082b575 commit 62a4db8
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions python/plugins/processing/algs/qgis/Eliminate.py
Original file line number Diff line number Diff line change
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.dataProvider().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)

0 comments on commit 62a4db8

Please sign in to comment.