Select to view content in your preferred language

unable to change lyrx source using arcpy

593
2
Jump to solution
07-14-2022 12:56 AM
Labels (1)
justin77
New Contributor

Hi,

I have lyrx files in a template directory. The lyrx files displaydata from shapefiles form various folders (all in the same path).

I want to

  1. copy the lyrx's across to a new project directory (no problems here)
  2. change the source of the shapefiles the lyrx's reference.

I'm using Python3, and arcpy.mp

Method #1: I have tried to do achieve #2in the map document using .updateConnectionProperties, the script runs but there's no source changes.

Method #2 (Preferred): I've also tried to manipulate the lyrx files directly - this is my preferred method as there is no MXD required (I think?) but still no luck changing the source. And no errors, everything seems to be running fine.

The lyrx I'm working with are groups; ie BareMap.lyrx contains a number of layers itself. I don't know if I should be separating them, manipulating the data and then grouping them?

justin77_0-1657785276131.png

The code I'm using is modified slightly from this post Solved: Change Data Source of .lyrx files in bulk and perm... - Esri Community

 

 

 

filesDirectory = path.join(destination_dir, "new_layers")
fileExt = r".lyrx"
files = [path.join(filesDirectory, f) for f in listdir(filesDirectory) if f.endswith(fileExt)]
ticker = 0
fileExt = r".lyrx" #do not change it
files = [path.join(filesDirectory, f) for f in listdir(filesDirectory) if f.endswith(fileExt)] #do not change it
for file in files:
    lyrFile = arcpy.mp.LayerFile(file)
    lyrFile.updateConnectionProperties(r"C:\Users\pc9Code\ndc\toolbox\Data", r"C:\Users\pc9\newcode") #path to old database and path to new database (.sde file in your case)
    lyrFile.saveACopy(file.split(".")[0]+ "New.lyrx")
    ticker += 1

 

 

 

 Would love any suggestions it's doing my head in 😄

0 Kudos
1 Solution

Accepted Solutions
RichardHowe
Occasional Contributor III

As lyrx files are just json, you could just change the source in the line of json and save the file, without even touching arcpy or an aprx.

Just fill in the path to the layer file for filename and the path to the new folder location for the data for newPath

import json
filename = r''
newPath = r''

with open(filename, "r") as file:
    data = json.load(file)
    data["layerDefinitions"][0]["featureTable"]["dataConnection"]["workspaceConnectionString"] = newPath

with open(filename, "w") as file:
    json.dump(data, file)

 

View solution in original post

2 Replies
RichardHowe
Occasional Contributor III

As lyrx files are just json, you could just change the source in the line of json and save the file, without even touching arcpy or an aprx.

Just fill in the path to the layer file for filename and the path to the new folder location for the data for newPath

import json
filename = r''
newPath = r''

with open(filename, "r") as file:
    data = json.load(file)
    data["layerDefinitions"][0]["featureTable"]["dataConnection"]["workspaceConnectionString"] = newPath

with open(filename, "w") as file:
    json.dump(data, file)

 

justin77
New Contributor

thankyou!