def mask_geometry(self): if not self.parameters.geometry: geom = QgsGeometry() return geom, QgsRectangle() geom = QgsGeometry(self.parameters.geometry) # COPY !! if self.parameters.do_simplify: if hasattr(self.canvas, "mapSettings"): tol = (self.parameters.simplify_tolerance * self.canvas.mapSettings().mapUnitsPerPixel()) else: tol = (self.parameters.simplify_tolerance * self.canvas.mapRenderer().mapUnitsPerPixel()) if tol in list(self.simplified_geometries.keys()): geom, bbox = self.simplified_geometries[tol] else: if self.has_simplifier: simplifier = QgsMapToPixelSimplifier( QgsMapToPixelSimplifier.SimplifyGeometry, tol) geom = simplifier.simplify(geom) if not geom.isGeosValid(): # make valid geom = geom.buffer(0.0, 1) bbox = geom.boundingBox() self.simplified_geometries[tol] = ( QgsGeometry(geom), QgsRectangle(bbox), ) else: bbox = geom.boundingBox() return geom, bbox
def prepareAlgorithm(self, parameters, context, feedback): self.tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context) self.method = self.parameterAsEnum(parameters, self.METHOD, context) if self.method != 0: self.simplifier = QgsMapToPixelSimplifier(QgsMapToPixelSimplifier.SimplifyGeometry, self.tolerance, self.method) return True
def mask_geometry(self): if not self.parameters.geometry: geom = QgsGeometry() return geom, QgsRectangle() geom = QgsGeometry(self.parameters.geometry) # COPY !! if self.parameters.do_simplify: if hasattr(self.canvas, 'mapSettings'): tol = self.parameters.simplify_tolerance * \ self.canvas.mapSettings().mapUnitsPerPixel() else: tol = self.parameters.simplify_tolerance * \ self.canvas.mapRenderer().mapUnitsPerPixel() if tol in list(self.simplified_geometries.keys()): geom, bbox = self.simplified_geometries[tol] else: if self.has_simplifier: simplifier = QgsMapToPixelSimplifier(QgsMapToPixelSimplifier.SimplifyGeometry, tol) geom = simplifier.simplify(geom) if not geom.isGeosValid(): # make valid geom = geom.buffer(0.0, 1) bbox = geom.boundingBox() self.simplified_geometries[tol] = (QgsGeometry(geom), QgsRectangle(bbox)) else: bbox = geom.boundingBox() return geom, bbox
def processAlgorithm(self, parameters, context, feedback): source = self.parameterAsSource(parameters, self.INPUT, context) tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context) method = self.parameterAsEnum(parameters, self.METHOD, context) pointsBefore = 0 pointsAfter = 0 (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context, source.fields(), source.wkbType(), source.sourceCrs()) features = source.getFeatures() total = 100.0 / source.featureCount() if source.featureCount() else 0 if method != 0: simplifier = QgsMapToPixelSimplifier( QgsMapToPixelSimplifier.SimplifyGeometry, tolerance, method) for current, input_feature in enumerate(features): if feedback.isCanceled(): break out_feature = input_feature if input_feature.geometry(): input_geometry = input_feature.geometry() pointsBefore += input_geometry.geometry().nCoordinates() if method == 0: # distance output_geometry = input_geometry.simplify(tolerance) else: output_geometry = simplifier.simplify(input_geometry) pointsAfter += output_geometry.geometry().nCoordinates() out_feature.setGeometry(output_geometry) sink.addFeature(out_feature, QgsFeatureSink.FastInsert) feedback.setProgress(int(current * total)) QgsMessageLog.logMessage( self. tr('Simplify: Input geometries have been simplified from {0} to {1} points' ).format(pointsBefore, pointsAfter), self.tr('Processing'), QgsMessageLog.INFO) return {self.OUTPUT: dest_id}
class SimplifyGeometries(QgisFeatureBasedAlgorithm): TOLERANCE = 'TOLERANCE' METHOD = 'METHOD' def icon(self): return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'simplify.png')) def group(self): return self.tr('Vector geometry tools') def __init__(self): super().__init__() self.tolerance = None self.method = None self.simplifier = None def initParameters(self, config=None): self.methods = [self.tr('Distance (Douglas-Peucker)'), 'Snap to grid', 'Area (Visvalingam)'] self.addParameter(QgsProcessingParameterEnum( self.METHOD, self.tr('Simplification method'), self.methods, defaultValue=0)) self.addParameter(QgsProcessingParameterNumber(self.TOLERANCE, self.tr('Tolerance'), minValue=0.0, maxValue=10000000.0, defaultValue=1.0)) def name(self): return 'simplifygeometries' def displayName(self): return self.tr('Simplify geometries') def outputName(self): return self.tr('Simplified') def prepareAlgorithm(self, parameters, context, feedback): self.tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context) self.method = self.parameterAsEnum(parameters, self.METHOD, context) if self.method != 0: self.simplifier = QgsMapToPixelSimplifier(QgsMapToPixelSimplifier.SimplifyGeometry, self.tolerance, self.method) return True def processFeature(self, feature, feedback): if feature.hasGeometry(): input_geometry = feature.geometry() if self.method == 0: # distance output_geometry = input_geometry.simplify(self.tolerance) else: output_geometry = self.simplifier.simplify(input_geometry) feature.setGeometry(output_geometry) return feature
def processAlgorithm(self, feedback): layer = dataobjects.getObjectFromUri(self.getParameterValue( self.INPUT)) tolerance = self.getParameterValue(self.TOLERANCE) method = self.getParameterValue(self.METHOD) pointsBefore = 0 pointsAfter = 0 writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( layer.fields().toList(), layer.wkbType(), layer.crs()) features = vector.features(layer) total = 100.0 / len(features) if method != 0: simplifier = QgsMapToPixelSimplifier( QgsMapToPixelSimplifier.SimplifyGeometry, tolerance, method) for current, input_feature in enumerate(features): out_feature = input_feature if input_feature.geometry(): input_geometry = input_feature.geometry() pointsBefore += input_geometry.geometry().nCoordinates() if method == 0: # distance output_geometry = input_geometry.simplify(tolerance) else: output_geometry = simplifier.simplify(input_geometry) pointsAfter += output_geometry.geometry().nCoordinates() out_feature.setGeometry(output_geometry) writer.addFeature(out_feature) feedback.setProgress(int(current * total)) del writer ProcessingLog.addToLog( ProcessingLog.LOG_INFO, self. tr('Simplify: Input geometries have been simplified from {0} to {1} points' ).format(pointsBefore, pointsAfter))
def processAlgorithm(self, parameters, context, feedback): layer = QgsProcessingUtils.mapLayerFromString( self.getParameterValue(self.INPUT), context) tolerance = self.getParameterValue(self.TOLERANCE) method = self.getParameterValue(self.METHOD) pointsBefore = 0 pointsAfter = 0 writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( layer.fields(), layer.wkbType(), layer.crs(), context) features = QgsProcessingUtils.getFeatures(layer, context) total = 100.0 / QgsProcessingUtils.featureCount(layer, context) if method != 0: simplifier = QgsMapToPixelSimplifier( QgsMapToPixelSimplifier.SimplifyGeometry, tolerance, method) for current, input_feature in enumerate(features): out_feature = input_feature if input_feature.geometry(): input_geometry = input_feature.geometry() pointsBefore += input_geometry.geometry().nCoordinates() if method == 0: # distance output_geometry = input_geometry.simplify(tolerance) else: output_geometry = simplifier.simplify(input_geometry) pointsAfter += output_geometry.geometry().nCoordinates() out_feature.setGeometry(output_geometry) writer.addFeature(out_feature) feedback.setProgress(int(current * total)) del writer QgsMessageLog.logMessage( self. tr('Simplify: Input geometries have been simplified from {0} to {1} points' ).format(pointsBefore, pointsAfter), self.tr('Processing'), QgsMessageLog.INFO)
def processAlgorithm(self, progress): layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT)) tolerance = self.getParameterValue(self.TOLERANCE) method = self.getParameterValue(self.METHOD) pointsBefore = 0 pointsAfter = 0 writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( layer.fields().toList(), layer.wkbType(), layer.crs() ) features = vector.features(layer) total = 100.0 / len(features) if method != 0: simplifier = QgsMapToPixelSimplifier(QgsMapToPixelSimplifier.SimplifyGeometry, tolerance, method) for current, input_feature in enumerate(features): out_feature = input_feature if input_feature.geometry(): input_geometry = input_feature.geometry() pointsBefore += input_geometry.geometry().nCoordinates() if method == 0: # distance output_geometry = input_geometry.simplify(tolerance) else: output_geometry = simplifier.simplify(input_geometry) pointsAfter += output_geometry.geometry().nCoordinates() out_feature.setGeometry(output_geometry) writer.addFeature(out_feature) progress.setPercentage(int(current * total)) del writer ProcessingLog.addToLog( ProcessingLog.LOG_INFO, self.tr( "Simplify: Input geometries have been simplified from %s to %s points" % (pointsBefore, pointsAfter) ), )
def processAlgorithm(self, parameters, context, feedback): source = self.parameterAsSource(parameters, self.INPUT, context) tolerance = self.parameterAsDouble(parameters, self.TOLERANCE, context) method = self.parameterAsEnum(parameters, self.METHOD, context) pointsBefore = 0 pointsAfter = 0 (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context, source.fields(), source.wkbType(), source.sourceCrs()) features = source.getFeatures() total = 100.0 / source.featureCount() if source.featureCount() else 0 if method != 0: simplifier = QgsMapToPixelSimplifier(QgsMapToPixelSimplifier.SimplifyGeometry, tolerance, method) for current, input_feature in enumerate(features): if feedback.isCanceled(): break out_feature = input_feature if input_feature.geometry(): input_geometry = input_feature.geometry() pointsBefore += input_geometry.geometry().nCoordinates() if method == 0: # distance output_geometry = input_geometry.simplify(tolerance) else: output_geometry = simplifier.simplify(input_geometry) pointsAfter += output_geometry.geometry().nCoordinates() out_feature.setGeometry(output_geometry) sink.addFeature(out_feature, QgsFeatureSink.FastInsert) feedback.setProgress(int(current * total)) QgsMessageLog.logMessage(self.tr('Simplify: Input geometries have been simplified from {0} to {1} points').format(pointsBefore, pointsAfter), self.tr('Processing'), QgsMessageLog.INFO) return {self.OUTPUT: dest_id}
def processAlgorithm(self, parameters, context, feedback): layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context) tolerance = self.getParameterValue(self.TOLERANCE) method = self.getParameterValue(self.METHOD) pointsBefore = 0 pointsAfter = 0 writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(), context) features = QgsProcessingUtils.getFeatures(layer, context) total = 100.0 / QgsProcessingUtils.featureCount(layer, context) if method != 0: simplifier = QgsMapToPixelSimplifier(QgsMapToPixelSimplifier.SimplifyGeometry, tolerance, method) for current, input_feature in enumerate(features): out_feature = input_feature if input_feature.geometry(): input_geometry = input_feature.geometry() pointsBefore += input_geometry.geometry().nCoordinates() if method == 0: # distance output_geometry = input_geometry.simplify(tolerance) else: output_geometry = simplifier.simplify(input_geometry) pointsAfter += output_geometry.geometry().nCoordinates() out_feature.setGeometry(output_geometry) writer.addFeature(out_feature) feedback.setProgress(int(current * total)) del writer QgsMessageLog.logMessage(self.tr('Simplify: Input geometries have been simplified from {0} to {1} points').format(pointsBefore, pointsAfter), self.tr('Processing'), QgsMessageLog.INFO)