0

I have an ArcPy script that changes the connection parameters for every item in a layer file from a SQL Server connection to a PostGIS connection. This script works perfectly in ArcGIS Python version 2.7.

As I am migrating the scripts to use the new ArcPy library in Python 3.7 for ArcGIS Pro, I am finding it really hard to achieve this very simple task. The layer.findAndReplaceWorkspacePath and layer.replaceDataSource are not ported to the new mp module and I am using layer.updateConnectionProperties to replace the underlying .sde connection file. The action does not have any effect and even if I set the Validate parameter to False, the layer sde connection file is not repointed and still shows as a SQL Server connection.

import arcpy
import os

class RepointLyr:

    def __init__(self):
        self.current_path = r'C:\lyr_file.lyr'
        self.lyr = arcpy.mp.LayerFile(self.current_path)
        self.layer_list = self.lyr.listLayers()
        self.repoint_lyr()

    def repoint_lyr(self):
        for layer in self.layer_list:
            if not layer.isGroupLayer:
                try:
                    datasetName = layer.dataSource.split("\\")[-1].split(".")[-1]
                    workspace_path = os.path.dirname(layer.dataSource)
                    layer.updateConnectionProperties(workspace_path, r"C:\connection_to_POSTGRES.sde", validate=False)
                    layer.updateConnectionProperties({'dataset': datasetName}, {'dataset': "sde.sde.{0}".format(datasetName)})
                except Exception as err:
                    print(err)
                    continue
        self.lyr.saveACopy(r"C:\repointed_lyr_file.lyr")



if __name__ == "__main__":
    repoint_lyr = RepointLyr()

Has anyone already tried what I am trying to do, with successful results?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.