0

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 Sep 28 '18 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 Sep 28 '18 at 18:58
  • @enolan yes, I would use ArcCatalog to do this if it is an occasional task. – danak Sep 28 '18 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/… – user3523583 Sep 30 '18 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 Oct 2 '18 at 16:14
1

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.