Skip to content

Instantly share code, notes, and snippets.

@beginor
Forked from jsanz/fix-geometries.py
Created April 13, 2021 00:05
Show Gist options
  • Save beginor/074e2baf33ed8cb3890d9a79f518fbd1 to your computer and use it in GitHub Desktop.
Save beginor/074e2baf33ed8cb3890d9a79f518fbd1 to your computer and use it in GitHub Desktop.

Revisions

  1. @jsanz jsanz created this gist Oct 30, 2019.
    41 changes: 41 additions & 0 deletions fix-geometries.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    """
    Small script to fix geometries of the first file argument
    using the native QGIS processing algorithm. You may need
    to adjust the path to you installation.
    """

    import sys
    sys.path.append('/usr/share/qgis/python/plugins')

    from processing.core.Processing import Processing
    import processing
    from qgis.core import (
    QgsApplication,
    QgsProcessingFeedback,
    QgsVectorLayer
    )
    from qgis.analysis import QgsNativeAlgorithms


    print("Initializing QGIS...")
    qgs = QgsApplication([], False)
    qgs.initQgis()
    Processing.initialize()
    QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())


    # Getting the file paths
    in_file = sys.argv[1]
    out_file = sys.argv[2]

    # Running the algorithm
    params = {
    'INPUT': QgsVectorLayer(in_file, 'layer1', 'ogr'),
    'OUTPUT': out_file
    }
    feedback = QgsProcessingFeedback()

    print("Running the fix geometries algorithm...")
    res = processing.run("native:fixgeometries", params, feedback=feedback)

    print("Done!")