0

Is it possible to change Datum Transformation in QGIS by QGIS?

enter image description here

For example add new Transformation or change actual.

2

2 Answers 2

2

You can set datum transformations using the project QgsCoordinateTransformContext accessible via QgsProject.instance().transformContext()

For Proj 5, you get existing list from

QgsProject.instance().transformContext().sourceDestinationDatumTransforms()

For Proj 6+, you get existing list from

QgsProject.instance().transformContext().coordinateOperations()

Then, use methods from QgsCoordinateTransformContext to add new datum transformations e.g addSourceDestinationDatumTransform (Proj 5) or addCoordinateOperation (Proj 6+)

To be able to get a generic solution depending of Proj version, you may want to use a condition with statement QgsProjUtils.projVersionMajor()

You may also look at related classes QgsDatumTransform (in particular to get "candidate" datums between projections) and QgsCoordinateTransform (if interested by reprojecting coordinates using a datum transformation)

0

My solution is:

# -*- coding: utf-8 -*-

from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
                       QgsFeatureSink,
                       QgsProject,
                       QgsDatumTransform,
                       QgsCoordinateTransformContext,
                       QgsCoordinateTransform,
                       QgsProcessingException,
                       QgsProcessingAlgorithm,
                       QgsCoordinateReferenceSystem,
                       QgsProcessingParameterFeatureSource,
                       QgsProcessingParameterFeatureSink)
from qgis import processing


class ExampleDatumTransformationAlgorithm(QgsProcessingAlgorithm):
    def tr(self, string):
        return QCoreApplication.translate('Processing', string)

    def createInstance(self):
        return ExampleDatumTransformationAlgorithm()

    def name(self):
        return 'Example Datum Transformation'

    def displayName(self):
        return self.tr('Transform Datum Projection')

    def group(self):
        return self.tr('Example scripts')

    def groupId(self):
        return 'example'

    def shortHelpString(self):
        return self.tr("Example algorithm short description")

    def initAlgorithm(self, config=None):
        pass
        
    def _clear_datum_settings(self):
        QgsProject.instance().setTransformContext(QgsCoordinateTransformContext().clear())
        
    def is_right_krovak_transformation(self, name, proj):
        if '(1)' in name and '570.8' in proj:
            self.feedback.pushInfo('[INFO]   Selected Transformation Datum: {0}'.format(proj))
            return True
        else:
            return False
        
    def get_aviable_datum_transformations(self, source_epsg, dest_epsg):
        possible_transformation = QgsDatumTransform.operations(QgsCoordinateReferenceSystem(source_epsg), QgsCoordinateReferenceSystem(dest_epsg), True)
        for tr in possible_transformation:
            if self.is_right_krovak_transformation(tr.name, tr.proj):
                return tr.proj
        
    def add_new_datum_transformation(self, settings, dest_epsg):     
        project = QgsProject.instance()
        context = QgsCoordinateTransformContext()
        for key, value in settings.items():
            context.addCoordinateOperation(QgsCoordinateReferenceSystem(key), QgsCoordinateReferenceSystem(dest_epsg), value, True)
        project.setTransformContext(context)
        
    def add_new_datum_transformation_by_epsg(self, source_epsg_list, dest_epsg):
        settings = {}
        for source_epsg in source_epsg_list:
            settings[source_epsg] = self.get_aviable_datum_transformations(source_epsg, dest_epsg)
        self.add_new_datum_transformation(settings, dest_epsg)

    def processAlgorithm(self, parameters, context, feedback):
        self.feedback = feedback
        self.add_new_datum_transformation_by_epsg(["EPSG:3857", "EPSG:4326"], "EPSG:5514")
        
        return {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.