简体   繁体   中英

Saving out a generated vector layer QGIS 3.6 python script

I'm trying to run some tools on a lines shapefile in QGIS 3.6 in order to remove geometry errors and identify self-intersections. I have the script working so that it outputs 2 layers: "Cleaned_Lines": the lines layer with duplicate and invalid geometries removed, and "Intersections": a points shapefile which indicates areas where the lines intersect themselves. These layers are created and loaded into QGIS as temporary layers. I want the "Cleaned_Lines" layer to automatically save out to the same directory as the input lines layer.

 # Check validity
            alg_params = {
                'IGNORE_RING_SELF_INTERSECTION': False,
                'INPUT_LAYER': parameters['lines'],
                'METHOD': 2,
                'VALID_OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
            }

            in_path = self.parameterDefinition('lines').valueAsPythonString(parameters['lines'], context)
            directory = os.path.dirname(in_path)
            output_path = os.path.join(os.path.dirname(in_path), "CleanedLines.shp'")

...

# Delete duplicate geometries
        alg_params = {
            'INPUT': outputs['CheckValidity']['VALID_OUTPUT'],
            'OUTPUT': parameters['CleanedLines']
        }

        cleaned = processing.run('qgis:deleteduplicategeometries', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        outputs['DeleteDuplicateGeometries'] = processing.run('qgis:deleteduplicategeometries', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['CleanedLines'] = outputs['DeleteDuplicateGeometries']['OUTPUT']

        cleaned_lines_layer = results['CleanedLines']
        print(cleaned_lines_layer)

        layer = QgsVectorLayer(cleaned_lines_layer, 'CleanedLines', 'ogr')

        if not os.path.isfile(output_path) or Overwrite_Results_Files:
            writer = QgsVectorFileWriter.writeAsVectorFormat(layer, output_path, 'CP1250', layer.crs(), 'ESRI Shapefile')

            cleaned_lines_layer = output_path

        layer = None

I excepted the file to write out to the directory indicated by the variable "output_path" but it simply creates it as a temporary layer and the script finishes. Any insight would be appreciated!

from qgis.core import QgsVectorFileWriter
QgsVectorFileWriter.writeAsVectorFormat(layer, "output_path", "UTF-8", layer.crs(), "ESRI Shapefile")

I used it without writer and it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM