Select to view content in your preferred language

UpdateLayer/AddLayer lose join properties, field map (.lyr with arcServer souce)

409
1
01-30-2014 02:57 PM
JasonBlack
New Contributor
Hello, I will try to be brief and give all the background - skip down to the bold *** to see the crux of the issue...

I am running Arc 10.2 Standard License on a WinServer2008R2Standard sp1 os, accessed through remote desktop
This is through an EDN license under which we also have an ArcServer installed on this same machine
My DB connection is to SQL Server where we have the SDE featureclasses, and a view based on some sql table(s) in another (SQL Server) database.  (hopefully all inconsequential - everything to do with this stuff seems to be fine)

The whole point of this is to test our possible move to arc server at 10.2 (and SQL server on the IT side) from our current 9.3.1 desktop (and MSAccess) environment.  My goal is to prove we can use python to update the layers in our many existing maps to make this changeover relatively painless - nobody in GIS wants this if we have to rebuild these maps manually.  Everything I see in the documentation tells me I can do this using UpdateLayer. 

I have saved a few template layer files onto a network drive and when added manually to any of the test .mxds below they were as they should have been - they all source one of the SDE featuresclasses and all have a join to the view which adds a couple of fields...but I hit a wall here:
***
...with the file on disk being a template .lyr file sourced to a featureclass in arc server, with a join to a view based on tables in sql server saved in the layer...
in all test map .mxd save-a-copy versions from 9.3.1 to 10.2...
UpdateLayer from the file on disk doesn't carry over join properties and creates a corrupted field map with duplicate field names;
AddLayer from the file on disk loses the join properties and creates a corrupted field map with duplicate field names;
But adding the layer manually from the same file on disk with the map open in ArcMap  - no issues and everything comes in fine.
***
The code is below if anyone want to see but it's really very basic and it runs fine - no errors and it's fast - but when I check the saved maps, the layers are not updated *correctly*, and when I tried (on a hunch) to programmatically add and then update from the added layer instead of from the .lyr on disk, I found that AddLayer had already lost the mentioned properties adding the .lyr...

All the code does is identify the layers to update based on their data sources and then sends them off to the appropriate function which uses UpdateLayer and the appropriate saved .lyr file...So it seems this issue is not really with my code but with THE code???

Any help or pointers with this would be appreciated - I'm not really new to GIS, but pretty new to everything I'm dealing with here.

PS: if anyone wonders why I would test this from maps at all different versions I found earlier that if I tried to save the maps above 10.0 at the end of the code a lot of the test maps were corrupted after running it on them and wouldn't open... so I thought it might make a difference.  Still having to do that in this scenario and they will get resaved by the mapper in 10.2 who opens them - if we get there lol

import arcpy, arcpy.mapping, sys, os

def main():
        mxdlist = list(arcpy.GetParameter(0))
        for mxditem in mxdlist:
                mxd = arcpy.mapping.MapDocument(str(mxditem))
                dataframelist = arcpy.mapping.ListDataFrames (mxd)
                dataframelistCount = len(dataframelist) -1
                print dataframelistCount +1, "dataframe(s)"
                for dataframe in dataframelist:
                        layerlist = arcpy.mapping.ListLayers (mxd,"",dataframe)
                        layerlistCount = len(layerlist) -1
                        print layerlistCount +1, "layers in dataframe named", dataframe.name
                        x = 0
                        while x <= layerlistCount:
                                lyr = layerlist
                                try:
                                        print lyr.dataSource
                                        if lyr.dataSource == (r"C:\MapData\AB Base Map.mdb\Features\Grazing_Reserves_AB"):
                                                lyrUpdate_GRZ (lyr, dataframe)
                                        elif lyr.dataSource == (r"C:\MapData\AB Base Map.mdb\Features\MD_County_AB"):
                                                lyrUpdate_MD (lyr, dataframe)
                                        else:
                                                print "No Action"
                                except NameError:
                                        print "Skipping Group layer..."
                                x = x+1
                        print "Done looping through layers in", dataframe.name
                print "Done with this map document:", mxd.filePath
                mxd.saveACopy (mxd.filePath[:-4]+"_Updated.mxd",'10.0')
                print "A copy has been saved, named", mxd.filePath[:-4]+"_Updated"+mxd.filePath[-4:]

def lyrUpdate_GRZ (lyr, dataframe):
    templateLayer = arcpy.mapping.Layer(r"H:\Z - Staff Stuff\J_Black\ArcServerTesting\GISbox.DBO.Grazing_Reserves_AB.lyr")
    print "Layer is FeatureLayer: ", lyr.isFeatureLayer
    print "Updating...", lyr.dataSource, "in... ", dataframe.name, "to...", templateLayer.dataSource, "Which is also a FeatureLayer?:", templateLayer.isFeatureLayer
    arcpy.mapping.UpdateLayer (dataframe, lyr, templateLayer, False)#templateLayer

def lyrUpdate_MD (lyr, dataframe):
    templateLayer = arcpy.mapping.Layer(r"H:\Z - Staff Stuff\J_Black\ArcServerTesting\GISbox.DBO.MD_County_AB.lyr")
    print "Layer is FeatureLayer: ", lyr.isFeatureLayer
    print "Updating...", lyr.dataSource, "in... ", dataframe.name, "to...", templateLayer.dataSource, "Which is also a FeatureLayer?:", templateLayer.isFeatureLayer
    arcpy.mapping.UpdateLayer (dataframe, lyr, templateLayer, False)

if __name__ == '__main__':
    main()
Tags (2)
0 Kudos
1 Reply
JeffBarrette
Esri Regular Contributor
Update layer is NOT the ideal function for managing data sources.  It was orginally designed to update layer symbology and other layer properties.  The doc says that if you set symbology_only = False, then all other properties get overwritten, including the data source.

There are several other methods designed specifically for updating data sources.  Please review the following help topic.

http://resources.arcgis.com/en/help/main/10.2/#/Updating_and_fixing_data_sources_with_arcpy_mapping/...

I hope this helps you,
Jeff
0 Kudos