Created
June 22, 2020 20:38
-
-
Save gacarrillor/9ffdb819f937d7a0fab4c492ac38908b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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