Select to view content in your preferred language

Following samples in Help and it STILL isn't working!

807
6
07-27-2011 07:44 AM
LorindaGilbert
Frequent Contributor
Hi,
I am in the process of creating a blended script that will have python scripting and data models (exported to python to be incorporated) that can be double clicked from an icon on the desktop that will do the following:
1.  Open mxd
2.  Remove a specific layer - the last geocoding results
3.  Geocode the newest data from the txt file that opens in the mxd
4.  Have the user review the unmatched data and match as necessary
5.  Update the geocoded results to a preexisting layer file symbology definition
6.  Save the geocoding results as a new feature class in the filegeodatabase
7.  Create maps based on certain predefined queries where date ranges and certain items change
8.  Eventually have the geocoded results appended to a master feature class for archive

I can get the name of the mxd and the layer listing in the mxd using python.  However, when I attempt to either remove the geocoding results layer or update the layer using the resaved layer file, I get
"Runtime error <type 'exceptions.AssertionError'>
I looked that up and it seems to refer to the illegal characters in the name - ":" - changed that and it STILL doesn't work.

import arcpy
arcpy.mapping.UpdateLayer('Layers','Featureclass name','path and layerfile name')
arcpy.mapping.RemoveLayer('Layers','Featureclass name')

I was under the impression that the python window in ArcMap was to be used for testing the scripting.

I'd sure appreciate some help on this cause the script needs to be done within the next week to two weeks 😞
Thanks
Tags (2)
0 Kudos
6 Replies
JakeSkinner
Esri Esteemed Contributor
Would you be able to copy/paste in your python code?  Be sure to wrap CODE tags around your python code to maintain the correct format/indentations.
0 Kudos
LorindaGilbert
Frequent Contributor
I'm currently attempting testing in the Python window in ArcMap so I am typing in the code there.  I have done the following so far - if it doesn't work there it won't work in a script:

import arcpy
mxd = arcpy.mapping.MapDocument (j:\mxdfiles\mxdname.mxd)
arcpy.mapping.UpdateLayer('Layers','TestFC','j:\test\test.lyr)
arcpy.mapping.RemoveLayer('Layers','TestFC')

Runtime error <type 'exceptions.AssertionError'>:  on either the Remove or the Update
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Try the following:

mxd = arcpy.mapping.MapDocument(r"j:\mxdfiles\mxdname.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

updateLayer = arcpy.mapping.ListLayers(mxd, "TestFC", df)[0]
sourceLayer = arcpy.mapping.Layer(r"j:\test\test.lyr")

arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer)

mxd.save()


I believe the problem is that you are not defining the data frame correctly.
0 Kudos
LorindaGilbert
Frequent Contributor
Thank you, that got that part working.  And I got the part working that saves the last geocoding result to a feature class in the filegeodatabase - now to get to NOT add the copied layer into the mxd.  Also, even with arcpy.RefreshTOC and arcpy.RefreshActiveView the updated layer doesn't show the new symbology in the TOC until I open the properties menu.
Now to get the remove layer and geocoding sections to run.
Why is it that the [0] was added to the command?  Is it because by the very nature of the TOC having 'usually' more than one feature class that it becomes an array?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
If you want the map document to update use "CURRENT" for the mxd.  Ex:

mxd = arcpy.mapping.MapDocument("CURRENT")


Then add at the bottom of your code:

arcpy.RefreshTOC()
arcpy.RefreshActiveView()


ListLayers always returns a Python list object even if only one layer is returned. In order to return a Layer object, an index value must be used on the list (e.g., lyr = arcpy.mapping.ListLayers(mxd)[0]).  More information can be found here.
0 Kudos
LorindaGilbert
Frequent Contributor
Thanks a bunch for the help, I'm new at Python and haven't finished the class yet (still on the first chapter) but still need to get this running soon - grrrr.
I also found that using
arcpy.ApplySymbologyFromLayer_management(layer2manipulate,sourcelayerfile)
visably updated the TOC.  But I will also add the other ones in too.  Got the remove to work.  Seems that rather than typing in the values for the functions/processes it works better if you have defined variables and then use them instead.  And I very vaguely remember reading or hearing something like that in one of the classes that I took.
I think that rather than using the "CURRENT" mxd function, I will use a variable, hardcode it and then validate that they are in the proper mxd before running any of this stuff.
Now I'm working on the geocoding section.
I figure the best way is to run in pieces, then put it all together in a somewhat coherent fashion.
0 Kudos