Skip to content
Permalink
Browse files

[processing] removed unnecessary code to check validity of geometries

  • Loading branch information
volaya authored and alexbruy committed Jan 9, 2017
1 parent e8c149e commit 7b68c77f4c8c81dfc7798e6f1e423e4ee4fbd6cf
@@ -45,7 +45,6 @@ def buffering(progress, writer, distance, field, useField, layer, dissolve,

# With dissolve
if dissolve:
first = True
buffered_geometries = []
for inFeat in features:
attrs = inFeat.attributes()
@@ -55,12 +54,7 @@ def buffering(progress, writer, distance, field, useField, layer, dissolve,
value = distance

inGeom = inFeat.geometry()
if not inGeom:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING, 'Feature {} has empty geometry. Skipping...'.format(inFeat.id()))
continue
if not inGeom.isGeosValid():
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING, 'Feature {} has invalid geometry. Skipping...'.format(inFeat.id()))
continue

buffered_geometries.append(inGeom.buffer(float(value), segments, endCapStyle, joinStyle, mitreLimit))

current += 1
@@ -80,14 +74,8 @@ def buffering(progress, writer, distance, field, useField, layer, dissolve,
value = distance
inGeom = inFeat.geometry()
outFeat = QgsFeature()
if inGeom.isEmpty() or inGeom.isGeosEmpty():
pass
elif not inGeom.isGeosValid():
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING, 'Feature {} has invalid geometry. Skipping...'.format(inFeat.id()))
continue
else:
outGeom = inGeom.buffer(float(value), segments, endCapStyle, joinStyle, mitreLimit)
outFeat.setGeometry(outGeom)
outGeom = inGeom.buffer(float(value), segments, endCapStyle, joinStyle, mitreLimit)
outFeat.setGeometry(outGeom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
current += 1
@@ -119,16 +119,7 @@ def processAlgorithm(self, progress):
if new_geom.wkbType() == QgsWkbTypes.Unknown or QgsWkbTypes.flatType(new_geom.geometry().wkbType()) == QgsWkbTypes.GeometryCollection:
int_com = in_feat.geometry().combine(new_geom)
int_sym = in_feat.geometry().symDifference(new_geom)
if not int_com or not int_sym:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('GEOS geoprocessing error: One or more '
'input features have invalid geometry.'))
else:
new_geom = int_com.difference(int_sym)
if new_geom.isGeosEmpty() or not new_geom.isGeosValid():
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('GEOS geoprocessing error: One or more '
'input features have invalid geometry.'))
new_geom = int_com.difference(int_sym)
else:
# clip geometry totally contains feature geometry, so no need to perform intersection
new_geom = in_feat.geometry()
@@ -58,16 +58,13 @@ def defineCharacteristics(self):
self.tr('Input layer')))
self.addParameter(ParameterVector(Difference.OVERLAY,
self.tr('Difference layer')))
self.addParameter(ParameterBoolean(Difference.IGNORE_INVALID,
self.tr('Ignore invalid input features'), False, True))
self.addOutput(OutputVector(Difference.OUTPUT, self.tr('Difference')))

def processAlgorithm(self, progress):
layerA = dataobjects.getObjectFromUri(
self.getParameterValue(Difference.INPUT))
layerB = dataobjects.getObjectFromUri(
self.getParameterValue(Difference.OVERLAY))
ignoreInvalid = self.getParameterValue(Difference.IGNORE_INVALID)

geomType = QgsWkbTypes.multiType(layerA.wkbType())
writer = self.getOutputFromName(
@@ -80,7 +77,6 @@ def processAlgorithm(self, progress):
selectionA = vector.features(layerA)
total = 100.0 / len(selectionA)
for current, inFeatA in enumerate(selectionA):
add = True
geom = inFeatA.geometry()
diff_geom = QgsGeometry(geom)
attrs = inFeatA.attributes()
@@ -91,27 +87,15 @@ def processAlgorithm(self, progress):
tmpGeom = inFeatB.geometry()
if diff_geom.intersects(tmpGeom):
diff_geom = QgsGeometry(diff_geom.difference(tmpGeom))
if diff_geom.isGeosEmpty():
ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
self.tr('Feature with NULL geometry found.'))
if not diff_geom.isGeosValid():
if ignoreInvalid:
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('GEOS geoprocessing error: One or more input features have invalid geometry.'))
add = False
else:
raise GeoAlgorithmExecutionException(self.tr('Features with invalid geometries found. Please fix these errors or specify the "Ignore invalid input features" flag'))
break

if add:
try:
outFeat.setGeometry(diff_geom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
except:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
continue

try:
outFeat.setGeometry(diff_geom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
except:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
continue

progress.setPercentage(int(current * total))

@@ -83,7 +83,6 @@ def processAlgorithm(self, progress):
count = 0

for featA in featuresA:
add = True
geom = featA.geometry()
diffGeom = QgsGeometry(geom)
attrs = featA.attributes()
@@ -93,31 +92,22 @@ def processAlgorithm(self, progress):
tmpGeom = featB.geometry()
if diffGeom.intersects(tmpGeom):
diffGeom = QgsGeometry(diffGeom.difference(tmpGeom))
if not diffGeom.isGeosValid():
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('GEOS geoprocessing error: One or '
'more input features have invalid '
'geometry.'))
add = False
break

if add:
try:
outFeat.setGeometry(diffGeom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
except:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
continue

try:
outFeat.setGeometry(diffGeom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
except:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
continue

count += 1
progress.setPercentage(int(count * total))

length = len(layerA.fields())

for featA in featuresB:
add = True
geom = featA.geometry()
diffGeom = QgsGeometry(geom)
attrs = featA.attributes()
@@ -128,23 +118,15 @@ def processAlgorithm(self, progress):
tmpGeom = featB.geometry()
if diffGeom.intersects(tmpGeom):
diffGeom = QgsGeometry(diffGeom.difference(tmpGeom))
if not diffGeom.isGeosValid():
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('GEOS geoprocessing error: One or '
'more input features have invalid '
'geometry.'))
add = False
break

if add:
try:
outFeat.setGeometry(diffGeom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
except:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
continue

try:
outFeat.setGeometry(diffGeom)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)
except:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
self.tr('Feature geometry error: One or more output features ignored due to invalid geometry.'))
continue

count += 1
progress.setPercentage(int(count * total))
@@ -161,9 +161,6 @@ def processAlgorithm(self, progress):
if len(lstIntersectingB) != 0:
intB = QgsGeometry.unaryUnion(lstIntersectingB)
diff_geom = diff_geom.difference(intB)
if diff_geom.isGeosEmpty() or not diff_geom.isGeosValid():
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('GEOS geoprocessing error: One or more input features have invalid geometry.'))

if diff_geom.wkbType() == 0 or QgsWkbTypes.flatType(diff_geom.geometry().wkbType()) == QgsWkbTypes.GeometryCollection:
temp_list = diff_geom.asGeometryCollection()
@@ -214,9 +211,6 @@ def processAlgorithm(self, progress):
if engine.intersects(tmpGeom.geometry()):
add = True
diff_geom = QgsGeometry(diff_geom.difference(tmpGeom))
if diff_geom.isGeosEmpty() or not diff_geom.isGeosValid():
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR,
self.tr('GEOS geoprocessing error: One or more input features have invalid geometry.'))
else:
try:
# Ihis only happends if the bounding box
@@ -122,7 +122,7 @@ def filterFeature(f, ignoreInvalid):
if ignoreInvalid:
return False
else:
raise GeoAlgorithmExecutionException(self.tr('Features with invalid geometries found. Please fix these errors or specify the "Ignore invalid input features" flag'))
raise GeoAlgorithmExecutionException(self.tr('Features with invalid geometries found. Please fix these geometries or specify the "Ignore invalid input features" flag'))
return True

if invalidFeaturesMethod == self.IGNORE:

0 comments on commit 7b68c77

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