Skip to content

Instantly share code, notes, and snippets.

@jsanz
Created October 30, 2019 16:40
Show Gist options
  • Save jsanz/64d08d8ff2d07f619d45e17087d07573 to your computer and use it in GitHub Desktop.
Save jsanz/64d08d8ff2d07f619d45e17087d07573 to your computer and use it in GitHub Desktop.
Python - QGIS - Fix geometries
"""
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!")
@beginor
Copy link

beginor commented Apr 13, 2021

This is the exact gist I am looking for!

@jsanz
Copy link
Author

jsanz commented Apr 13, 2021

This is the exact gist I am looking for!

🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment