def clean_layer(layer): """Clean a vector layer. :param layer: The vector layer. :type layer: QgsVectorLayer :return: The buffered vector layer. :rtype: QgsVectorLayer """ output_layer_name = clean_geometry_steps['output_layer_name'] output_layer_name = output_layer_name % layer.keywords['layer_purpose'] # start editing layer.startEditing() count = 0 # iterate through all features request = QgsFeatureRequest().setSubsetOfAttributes([]) for feature in layer.getFeatures(request): geom = feature.geometry() was_valid, geometry_cleaned = geometry_checker(geom) if was_valid: # Do nothing if it was valid pass elif not was_valid and geometry_cleaned: # Update the geometry if it was not valid, and clean now layer.changeGeometry(feature.id(), geometry_cleaned, True) else: # Delete if it was not valid and not able to be cleaned count += 1 layer.deleteFeature(feature.id()) if count: LOGGER.critical( '%s features have been removed from %s because of invalid ' 'geometries.' % (count, layer.name())) else: LOGGER.info('No feature has been removed from the layer: %s' % layer.name()) # save changes layer.commitChanges() layer.keywords['title'] = output_layer_name check_layer(layer) return layer
def clean_layer(layer): """Clean a vector layer. :param layer: The vector layer. :type layer: QgsVectorLayer :return: The buffered vector layer. :rtype: QgsVectorLayer """ output_layer_name = clean_geometry_steps['output_layer_name'] output_layer_name = output_layer_name % layer.keywords['layer_purpose'] # start editing layer.startEditing() count = 0 # iterate through all features request = QgsFeatureRequest().setSubsetOfAttributes([]) for feature in layer.getFeatures(request): geom = feature.geometry() was_valid, geometry_cleaned = geometry_checker(geom) if was_valid: # Do nothing if it was valid pass elif not was_valid and geometry_cleaned: # Update the geometry if it was not valid, and clean now layer.changeGeometry(feature.id(), geometry_cleaned, True) else: # Delete if it was not valid and not able to be cleaned count += 1 layer.deleteFeature(feature.id()) if count: LOGGER.critical( '%s features have been removed from %s because of invalid ' 'geometries.' % (count, layer.name())) else: LOGGER.info( 'No feature has been removed from the layer: %s' % layer.name()) # save changes layer.commitChanges() layer.keywords['title'] = output_layer_name check_layer(layer) return layer
def clean_layer(layer): """Clean a vector layer. :param layer: The vector layer. :type layer: qgis.core.QgsVectorLayer :return: The buffered vector layer. :rtype: qgis.core.QgsVectorLayer """ output_layer_name = clean_geometry_steps['output_layer_name'] output_layer_name = output_layer_name % layer.keywords['layer_purpose'] count = layer.featureCount() parameters = {'INPUT': layer, 'OUTPUT': 'memory:'} initialize_processing() feedback = create_processing_feedback() context = create_processing_context(feedback=feedback) result = processing.run('qgis:fixgeometries', parameters, context=context) if result is None: raise ProcessingInstallationError cleaned = result['OUTPUT'] cleaned.setName(output_layer_name) removed_count = count - cleaned.featureCount() if removed_count: LOGGER.critical( '{removed_count} features have been removed from {layer_name} ' 'because of invalid geometries.'.format( removed_count=removed_count, layer_name=layer.name())) else: LOGGER.info('No feature has been removed from the layer: ' '{layer_name}'.format(layer_name=layer.name())) cleaned.keywords = layer.keywords.copy() cleaned.keywords['title'] = output_layer_name check_layer(cleaned) return cleaned
def clean_layer(layer, callback=None): """Clean a vector layer. :param layer: The vector layer. :type layer: QgsVectorLayer :param callback: A function to all to indicate progress. The function editing should accept params 'current' (int), 'maximum' (int) and 'step' (str). Defaults to None. :type callback: function :return: The buffered vector layer. :rtype: QgsVectorLayer """ output_layer_name = clean_geometry_steps['output_layer_name'] processing_step = clean_geometry_steps['step_name'] # NOQA output_layer_name = output_layer_name % layer.keywords['layer_purpose'] # start editing layer.startEditing() count = 0 # iterate through all features for feature in layer.getFeatures(): geom = feature.geometry() geometry_cleaned = geometry_checker(geom) if geometry_cleaned: feature.setGeometry(geometry_cleaned) else: count += 1 layer.deleteFeature(feature.id()) LOGGER.critical( '%s features have been removed from %s because of invalid geometries.' % (count, layer.name())) # save changes layer.commitChanges() layer.keywords['title'] = output_layer_name check_layer(layer) return layer