Skip to content
Permalink
Browse files

[processing] make service area outputs optional

  • Loading branch information
alexbruy committed Jul 17, 2017
1 parent bee5683 commit 4a6ceffc543750be890066e705298eb72c2b265f
@@ -151,10 +151,12 @@ def initAlgorithm(self, config=None):

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT_POINTS,
self.tr('Service area (boundary nodes)'),
QgsProcessing.TypeVectorPoint))
QgsProcessing.TypeVectorPoint,
optional=True))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT_POLYGON,
self.tr('Service area (convex hull)'),
QgsProcessing.TypeVectorPolygon))
QgsProcessing.TypeVectorPolygon,
optional=True))

def name(self):
return 'serviceareafromlayer'
@@ -184,12 +186,6 @@ def processAlgorithm(self, parameters, context, feedback):
feat = QgsFeature()
feat.setFields(fields)

(sinkPoints, pointsId) = self.parameterAsSink(parameters, self.OUTPUT_POINTS, context,
fields, QgsWkbTypes.MultiPoint, layer.crs())

(sinkPoygon, polygonId) = self.parameterAsSink(parameters, self.OUTPUT_POLYGON, context,
fields, QgsWkbTypes.Polygon, layer.crs())

directionField = -1
if directionFieldName:
directionField = layer.fields().lookupField(directionFieldName)
@@ -239,6 +235,18 @@ def processAlgorithm(self, parameters, context, feedback):
feedback.pushInfo(self.tr('Calculating service areas...'))
graph = builder.graph()

results = {}
(sinkPoints, pointsId) = self.parameterAsSink(parameters, self.OUTPUT_POINTS, context,
fields, QgsWkbTypes.MultiPoint, layer.crs())

(sinkPolygon, polygonId) = self.parameterAsSink(parameters, self.OUTPUT_POLYGON, context,
fields, QgsWkbTypes.Polygon, layer.crs())

if sinkPoints:
results[self.OUTPUT_POINTS] = pointsId
if sinkPolygon:
results[self.OUTPUT_POLYGON] = polygonId

vertices = []
upperBoundary = []
lowerBoundary = []
@@ -261,44 +269,42 @@ def processAlgorithm(self, parameters, context, feedback):
upperBoundary.append(graph.vertex(graph.edge(tree[j]).inVertex()).point())
lowerBoundary.append(graph.vertex(graph.edge(tree[j]).outVertex()).point())

geomUpper = QgsGeometry.fromMultiPoint(upperBoundary)
geomLower = QgsGeometry.fromMultiPoint(lowerBoundary)

feat.setGeometry(geomUpper)
feat['type'] = 'upper'
feat['start'] = origPoint
sinkPoints.addFeature(feat, QgsFeatureSink.FastInsert)

feat.setGeometry(geomLower)
feat['type'] = 'lower'
feat['start'] = origPoint
sinkPoints.addFeature(feat, QgsFeatureSink.FastInsert)

upperBoundary.append(origPoint)
lowerBoundary.append(origPoint)
geomUpper = QgsGeometry.fromMultiPoint(upperBoundary)
geomLower = QgsGeometry.fromMultiPoint(lowerBoundary)

geom = geomUpper.convexHull()
feat.setGeometry(geom)
feat['type'] = 'upper'
feat['start'] = origPoint
sinkPolygons.addFeature(feat, QgsFeatureSink.FastInsert)

geom = geomLower.convexHull()
feat.setGeometry(geom)
feat['type'] = 'lower'
feat['start'] = origPoint
sinkPolygons.addFeature(feat, QgsFeatureSink.FastInsert)
if sinkPoints:
geomUpper = QgsGeometry.fromMultiPoint(upperBoundary)
geomLower = QgsGeometry.fromMultiPoint(lowerBoundary)

feat.setGeometry(geomUpper)
feat['type'] = 'upper'
feat['start'] = origPoint
sinkPoints.addFeature(feat, QgsFeatureSink.FastInsert)

feat.setGeometry(geomLower)
feat['type'] = 'lower'
feat['start'] = origPoint
sinkPoints.addFeature(feat, QgsFeatureSink.FastInsert)

if sinkPolygon:
upperBoundary.append(origPoint)
lowerBoundary.append(origPoint)
geomUpper = QgsGeometry.fromMultiPoint(upperBoundary)
geomLower = QgsGeometry.fromMultiPoint(lowerBoundary)

geom = geomUpper.convexHull()
feat.setGeometry(geom)
feat['type'] = 'upper'
feat['start'] = origPoint
sinkPolygon.addFeature(feat, QgsFeatureSink.FastInsert)

geom = geomLower.convexHull()
feat.setGeometry(geom)
feat['type'] = 'lower'
feat['start'] = origPoint
sinkPolygon.addFeature(feat, QgsFeatureSink.FastInsert)

vertices[:] = []
upperBoundary[:] = []
lowerBoundary[:] = []

feedback.setProgress(int(i * total))

results = {}
results[self.OUTPUT_POINTS] = pointsId
results[self.OUTPUT_POLYGON] = polygonId

return results
@@ -149,10 +149,12 @@ def initAlgorithm(self, config=None):

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT_POINTS,
self.tr('Service area (boundary nodes)'),
QgsProcessing.TypeVectorPoint))
QgsProcessing.TypeVectorPoint,
optional=True))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT_POLYGON,
self.tr('Service area (convex hull)'),
QgsProcessing.TypeVectorPolygon))
QgsProcessing.TypeVectorPolygon,
optional=True))

def name(self):
return 'serviceareafrompoint'
@@ -238,37 +240,40 @@ def processAlgorithm(self, parameters, context, feedback):
(sinkPoints, pointsId) = self.parameterAsSink(parameters, self.OUTPUT_POINTS, context,
fields, QgsWkbTypes.MultiPoint, layer.crs())

feat.setGeometry(geomUpper)
feat['type'] = 'upper'
feat['start'] = startPoint.toString()
sinkPoints.addFeature(feat, QgsFeatureSink.FastInsert)

feat.setGeometry(geomLower)
feat['type'] = 'lower'
feat['start'] = startPoint.toString()
sinkPoints.addFeature(feat, QgsFeatureSink.FastInsert)

upperBoundary.append(startPoint)
lowerBoundary.append(startPoint)
geomUpper = QgsGeometry.fromMultiPoint(upperBoundary)
geomLower = QgsGeometry.fromMultiPoint(lowerBoundary)

(sinkPoygon, polygonId) = self.parameterAsSink(parameters, self.OUTPUT_POLYGON, context,
fields, QgsWkbTypes.Polygon, layer.crs())
geom = geomUpper.convexHull()
feat.setGeometry(geom)
feat['type'] = 'upper'
feat['start'] = startPoint.toString()
sinkPoygon.addFeature(feat, QgsFeatureSink.FastInsert)

geom = geomLower.convexHull()
feat.setGeometry(geom)
feat['type'] = 'lower'
feat['start'] = startPoint.toString()
sinkPoygon.addFeature(feat, QgsFeatureSink.FastInsert)

(sinkPolygon, polygonId) = self.parameterAsSink(parameters, self.OUTPUT_POLYGON, context,
fields, QgsWkbTypes.Polygon, layer.crs())
results = {}
results[self.OUTPUT_POINTS] = pointsId
results[self.OUTPUT_POLYGON] = polygonId
if sinkPoints:
feat.setGeometry(geomUpper)
feat['type'] = 'upper'
feat['start'] = startPoint.toString()
sinkPoints.addFeature(feat, QgsFeatureSink.FastInsert)

feat.setGeometry(geomLower)
feat['type'] = 'lower'
feat['start'] = startPoint.toString()
sinkPoints.addFeature(feat, QgsFeatureSink.FastInsert)

upperBoundary.append(startPoint)
lowerBoundary.append(startPoint)
geomUpper = QgsGeometry.fromMultiPoint(upperBoundary)
geomLower = QgsGeometry.fromMultiPoint(lowerBoundary)

results[self.OUTPUT_POINTS] = pointsId

if sinkPolygon:
geom = geomUpper.convexHull()
feat.setGeometry(geom)
feat['type'] = 'upper'
feat['start'] = startPoint.toString()
sinkPolygon.addFeature(feat, QgsFeatureSink.FastInsert)

geom = geomLower.convexHull()
feat.setGeometry(geom)
feat['type'] = 'lower'
feat['start'] = startPoint.toString()
sinkPolygon.addFeature(feat, QgsFeatureSink.FastInsert)

results[self.OUTPUT_POLYGON] = polygonId

return results

0 comments on commit 4a6ceff

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