Changes from review comments

- Shorten algorithm names (KNearestNeighbour and ConcaveHull)
- Check for feedback cancellation
- remove unnecessary try/except blocks
This commit is contained in:
Rudi von Staden 2018-09-14 09:10:40 +02:00
parent 5076fb6313
commit f4cab1780a
2 changed files with 36 additions and 29 deletions

View File

@ -75,7 +75,10 @@ class ConcaveHull(QgisAlgorithm):
return 'concavehull' return 'concavehull'
def displayName(self): def displayName(self):
return self.tr('Concave hull (using alpha shapes algorithm)') return self.tr('Concave hull (alpha shapes)')
def shortDescription(self):
return self.tr('Creates a concave hull using the alpha shapes algorithm.')
def icon(self): def icon(self):
return QgsApplication.getThemeIcon("/algorithms/mAlgorithmConcaveHull.svg") return QgsApplication.getThemeIcon("/algorithms/mAlgorithmConcaveHull.svg")

View File

@ -63,7 +63,10 @@ class KNearestConcaveHull(QgisAlgorithm):
return 'knearestconcavehull' return 'knearestconcavehull'
def displayName(self): def displayName(self):
return self.tr('Concave hull (using k-nearest neighbour algorithm)') return self.tr('Concave hull (k-nearest neighbour)')
def shortDescription(self):
return self.tr('Creates a concave hull using the k-nearest neighbour algorithm.')
def icon(self): def icon(self):
return QgsApplication.getThemeIcon("/algorithms/mAlgorithmConcaveHull.svg") return QgsApplication.getThemeIcon("/algorithms/mAlgorithmConcaveHull.svg")
@ -134,28 +137,28 @@ class KNearestConcaveHull(QgisAlgorithm):
filter = QgsExpression.createFieldEqualityExpression(field_name, unique) filter = QgsExpression.createFieldEqualityExpression(field_name, unique)
request = QgsFeatureRequest().setFilterExpression(filter) request = QgsFeatureRequest().setFilterExpression(filter)
request.setSubsetOfAttributes([]) request.setSubsetOfAttributes([])
features = source.getFeatures(request) # Get features with the grouping attribute equal to the current grouping value # Get features with the grouping attribute equal to the current grouping value
features = source.getFeatures(request)
for in_feature in features: for in_feature in features:
points.extend(extract_points(in_feature.geometry())) # Either points or vertices of more complex geometry if feedback.isCanceled():
break
# Add points or vertices of more complex geometry
points.extend(extract_points(in_feature.geometry()))
current += 1 current += 1
feedback.setProgress(int(current * total)) feedback.setProgress(int(current * total))
# A minimum of 3 points is necessary to proceed # A minimum of 3 points is necessary to proceed
if len(points) >= 3: if len(points) >= 3:
out_feature = QgsFeature() out_feature = QgsFeature()
try: the_hull = concave_hull(points, kneighbors)
the_hull = concave_hull(points, kneighbors) if the_hull:
if the_hull: vertex = [QgsPointXY(point[0], point[1]) for point in the_hull]
vertex = [QgsPointXY(point[0], point[1]) for point in the_hull] poly = QgsGeometry().fromPolygonXY([vertex])
poly = QgsGeometry().fromPolygonXY([vertex])
out_feature.setGeometry(poly) out_feature.setGeometry(poly)
out_feature.setAttributes([fid, unique]) # Give the polygon the same attribute as the point grouping attribute # Give the polygon the same attribute as the point grouping attribute
sink.addFeature(out_feature, QgsFeatureSink.FastInsert) out_feature.setAttributes([fid, unique])
success = True # at least one polygon created sink.addFeature(out_feature, QgsFeatureSink.FastInsert)
except: success = True # at least one polygon created
feedback.reportError('Exception while computing concave hull.')
raise QgsProcessingException('Exception while computing concave hull.')
fid += 1 fid += 1
if not success: if not success:
raise QgsProcessingException('No hulls could be created. Most likely there were not at least three unique points in any of the groups.') raise QgsProcessingException('No hulls could be created. Most likely there were not at least three unique points in any of the groups.')
@ -178,25 +181,26 @@ class KNearestConcaveHull(QgisAlgorithm):
features = source.getFeatures(request) # Get all features features = source.getFeatures(request) # Get all features
total = 100.0 / source.featureCount() if source.featureCount() else 0 total = 100.0 / source.featureCount() if source.featureCount() else 0
for in_feature in features: for in_feature in features:
points.extend(extract_points(in_feature.geometry())) # Either points or vertices of more complex geometry if feedback.isCanceled():
break
# Add points or vertices of more complex geometry
points.extend(extract_points(in_feature.geometry()))
current += 1 current += 1
feedback.setProgress(int(current * total)) feedback.setProgress(int(current * total))
# A minimum of 3 points is necessary to proceed # A minimum of 3 points is necessary to proceed
if len(points) >= 3: if len(points) >= 3:
out_feature = QgsFeature() out_feature = QgsFeature()
try: the_hull = concave_hull(points, kneighbors)
the_hull = concave_hull(points, kneighbors) if the_hull:
if the_hull: vertex = [QgsPointXY(point[0], point[1]) for point in the_hull]
vertex = [QgsPointXY(point[0], point[1]) for point in the_hull] poly = QgsGeometry().fromPolygonXY([vertex])
poly = QgsGeometry().fromPolygonXY([vertex])
out_feature.setGeometry(poly) out_feature.setGeometry(poly)
out_feature.setAttributes([0]) out_feature.setAttributes([0])
sink.addFeature(out_feature, QgsFeatureSink.FastInsert) sink.addFeature(out_feature, QgsFeatureSink.FastInsert)
except: else:
feedback.reportError('Exception while computing concave hull.') raise QgsProcessingException('Error while creating concave hull.')
raise QgsProcessingException('Exception while computing concave hull.')
else: else:
raise QgsProcessingException('At least three unique points are required to create a concave hull.') raise QgsProcessingException('At least three unique points are required to create a concave hull.')