I have hundreds of lyr files with a complex tree structure containing both layer groups and nested layers. What I would like to achieve is to grab each lyr files, iterate through every single layer (also within a layer group), change the data source and the name of the dataset it points to and most importantly change the support from ArcSDE (underlying sql server DB) to a gpkg file that already contains the tables which the lyr will point to.
I produced a lyr file with all layers pointing to the wanted gpkg file and table manually, it works fine; I used it as a template for the other lyr files I want to change, so that I only have to change the source with the replaceDataSource method.
def update_from_template(self): # adds the template lyr to the temp mxd source_main_lyr = arcpy.mapping.Layer(self.template_lyr) arcpy.mapping.AddLayer(self.updt_df, source_main_lyr, "BOTTOM") for upd_lyr in self.layers_list: if not upd_lyr.isGroupLayer: # keeps track of the original datasetname datasetname = upd_lyr.datasetName # tracks down the source lyr (pointing to SDE) source_lyr = arcpy.mapping.ListLayers(self.updt_mxd, upd_lyr.name, self.updt_df) if len(source_lyr) > 0: # change the source lyr properties to the same of the template (pointing to GEOPACKAGE) arcpy.mapping.UpdateLayer(self.df, upd_lyr, source_lyr, False) # changes the name of the source back to its original newName = datasetname.replace("SDE_LIVE.DBO.GLOBAL_", "main.") # I use SDE again here (there is no GEOPACKAGE OPTION) I tried also NONE but I get the same # results servProp = upd_lyr.replaceDataSource(self.repointed_GPKG_location, "SDE_WORKSPACE", newName, False) upd_lyr.DataSource = servProp
But result of the lyr file are broken links, pointing to the old (template) dataset. I imagine what I am trying to do is not immediatly supported by ESRI but I was wondering there is anyone out there who had tried this, and maybe give advice. I am using ArcGIS desktop 10.4 and arcpy python 2.x but could update if required.