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[0], 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.

  • Have you tried the repoint manually? To clarify, the package is unpacked, right? This sounds real useful, please keep us updated.
    – danak
    Commented Sep 28, 2018 at 17:27
  • I'm sure that you are aware that if you right click an .mxd in ArcCatalog that you can use "Set Datasource Sources(s)..." and replace all in there, with a find and replace? You can also save this as a new .mxd so as to not overwrite the existing mxd.
    – enolan
    Commented Sep 28, 2018 at 18:58
  • @enolan yes, I would use ArcCatalog to do this if it is an occasional task.
    – danak
    Commented Sep 28, 2018 at 19:01
  • I repointed them manually for one .lyr to create the template, but again the manual way is not feasible for the number of .lyr files to repoint (hundreds), hence the question here on how to do it with arcpy. Probably in the question is not clear but I’m not talking about repointing a layer package, I am talking about the geopackage format .gpkg, when @danak you ask me if the geopackage is unpacked what do you mean? Can a geopackage be unpacked? esri.com/arcgis-blog/products/arcgis-desktop/analytics/… Commented Sep 30, 2018 at 23:00
  • The ArcCatalog method suggested by @enolan can handle hundreds of repoints in a map at once; I do it all the time. You just set up the search and replaces. I have not used gpkgs, so I am not sure how this will wor,.
    – danak
    Commented Oct 2, 2018 at 16:14

2 Answers 2


I have been in touch directly with ESRI and this is an excerpt of the response I get to the same question posted here.

[...] After carrying out extensive research I'm afraid to report that the lack of support surrounding the use of Geopackages means that your desired workflow is currently not feasible in a programmatic sense. As you know, however, it is possible to manually open ArcMap and utilise Layer files with Geopackages. I understand this is not ideal, apologies for this.[...]

[...]I think it would be very useful to log this enhancement idea on the GeoNet Ideas page. Esri Inc Developers regularly scan this site for ideas on how to enhance future releases.[...]


Sorry to resurrect this but here are at least some thoughts and pseudocode that might allow you to accomplish this.

With ArcGIS Pro, a layer file is in a .lyrx format and can be read as JSON. I am not sure if a .lyr is the same as I do not have one accessible to me right now, but I do know it is still a text format not binary. Therefore editing either is straightforward.

Looking at a .lyrx in notepad as a template, I see this under the layerDefinitions element:

"featureTable" : {
    "type" : "CIMFeatureTable",
    "displayField" : "name",
    "editable" : true,
    "dataConnection" : {
      "type" : "CIMStandardDataConnection",
      "workspaceConnectionString" : "DATABASE=..\\Documents\\ArcGIS\\Projects\\First\\First.gdb",
      "workspaceFactory" : "FileGDB",
      "dataset" : "test_point_fc",
      "datasetType" : "esriDTFeatureClass"

The way I would go about this is editing the workspaceConnectionString value to reflect the path to your geopackage. Let's say I had a geopackage I wanted to update this example layer with. Assuming that my dataset name is consistent between the two, I would

  • set the workspaceConnectionString to the path of my geopackage
  • try to set the workspaceFactory element to LocalDatabase (this is only a guess, because arcpy.Describe().workspaceType yields LocalDatabase when used on a geopackage)
  • set the dataset element to main.datasetname (e.g., main.test_point_fc)
  • You should be able to leave datasetType alone, as (once again, arcpy.Describe() is your friend) a geopackage vector table is read as a FeatureClass object.

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.