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
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?
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 😄
Solved! Go to Solution.
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)
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)
thankyou!