Skip to content

Instantly share code, notes, and snippets.

@gacarrillor
Created June 22, 2020 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gacarrillor/9ffdb819f937d7a0fab4c492ac38908b to your computer and use it in GitHub Desktop.
Save gacarrillor/9ffdb819f937d7a0fab4c492ac38908b to your computer and use it in GitHub Desktop.
from qgis.utils import iface
from qgis.core import QgsProject, QgsVectorFileWriter, QgsCoordinateReferenceSystem
path = "/tmp/gpx/sample.gpx"
layer = QgsProject.instance().mapLayer("[% @layer_id %]")
if layer.selectedFeatureCount():
options = QgsVectorFileWriter.SaveVectorOptions()
options.ct = QgsCoordinateTransform(layer.crs(), QgsCoordinateReferenceSystem(4326), QgsProject.instance())
options.driverName = "GPX"
options.datasourceOptions = ["GPX_USE_EXTENSIONS=ON"]
options.fileEncoding = "utf-8"
options.layerOptions=["FORCE_GPX_ROUTE=YES"]
options.onlySelectedFeatures = True
result, error_string = QgsVectorFileWriter.writeAsVectorFormatV2(
layer,
path,
layer.transformContext(),
options)
if result == QgsVectorFileWriter.NoError:
iface.messageBar().pushSuccess(
"Export2GPX",
"Exported {count} lines to <a href='file://{path}'>{path}</a>".format(
count=layer.selectedFeatureCount(),
path=path))
else:
iface.messageBar().pushWarning("Export2GPX", "Ouch! Errors exporting: {}".format(error_string))
else:
iface.messageBar().pushInfo("Export2GPX", "First select some features...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment