Select to view content in your preferred language

Layer Invalid Data Source ?

5153
1
Jump to solution
10-10-2013 10:42 AM
MaryM
by
Deactivated User
I wrote a script tool where a map is generated, a checkbox of layers can be checked to add the layers to the map, and when I run it some layers will add to the new map and some layers, throw "Object: CreateObject Layer invalid data source." 

I did notice all of the layers that error have a space in their names.  They are valid layers as in when AddData is used manually they can add to a map.  I know the list of the layers is a Python multiValue list, not a Python list, taking out one if the items and testing it's type yields that the item is a unicode object.  I also noticed when I print the mutliValues out the ones with spaces have single quotes surrounding them and the ones w/o spaces do not have single quotes surrounding them...
'file path here' Object: CreateObject Layer invalid data source 'file path here' Object: CreateObject Layer invalid data source filePathHere Added: FilePath.lyr




I convert them to layers in the script when I iterate through the multiValue list like so:
try:     if countyLayers[0]:         for item in countyLayers.split(';'):             try:                 item1=arcpy.mapping.Layer(item)                 arcpy.mapping.AddLayer(dataFrame,item1,"AUTO_ARRANGE")                 item2=str(item.split("\\")[-1])                 arcpy.AddMessage("Added: " + item2)                 arcpy.RefreshTOC()                 del item1, item2             except Exception as e:                 arcpy.AddMessage(e.message)                 pass except:     arcpy.AddMessage("No county data specified to add")     pass
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MaryM
by
Deactivated User
After much trial and error I found a post that helped...

http://forums.arcgis.com/threads/58298-Python-Script-Help-Spaces-in-filename

From my expereince, if I made a function with loop to remove the " ' "s from the layers with spaces (the Layers with spaces got these " ' "s unlike the other layers) it was set in an infinite loop b/c it would not remove.  The forum above did not work for doing this:
arcpy.mapping.AddLayer(dataFrame,Layer.strip("'"),"AUTO_ARRANGE")

as Layer has no method .strip.  Although, when I tried it when I defined the Layer it worked and took out the " ' "s when it was filled as a parameter: Layer=arcpy.mapping.Layer(item.strip("'"))

View solution in original post

0 Kudos
1 Reply
MaryM
by
Deactivated User
After much trial and error I found a post that helped...

http://forums.arcgis.com/threads/58298-Python-Script-Help-Spaces-in-filename

From my expereince, if I made a function with loop to remove the " ' "s from the layers with spaces (the Layers with spaces got these " ' "s unlike the other layers) it was set in an infinite loop b/c it would not remove.  The forum above did not work for doing this:
arcpy.mapping.AddLayer(dataFrame,Layer.strip("'"),"AUTO_ARRANGE")

as Layer has no method .strip.  Although, when I tried it when I defined the Layer it worked and took out the " ' "s when it was filled as a parameter: Layer=arcpy.mapping.Layer(item.strip("'"))
0 Kudos