Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I've checked the other answers here and none of the answers worked for me...

It's a "simple" matter of finding a layer by name in a map and changing it's datasource.

Here's my script:

    #-- New Source
    newSource = os.path.join(currDir, lyrZones + "_Dep" + dirNum + ".shp")

    #-- Validation
    if arcpy.Exists(newSource):
        print "The new source exists!"
    else:
        print "Not found!"

    #-- Find the layer and change its source            
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == lyrZones:
            print "Layer found!"
            print layers.dataSource
            print "  Replacing source..."
            lyr.replaceDataSource(currDir, "SHAPEFILE_WORKSPACE", newSource)
            print lyr.dataSource

I run it from a python script outside ArcGIS.

enter image description here

You can see that the source exists, the layer is found, I can access it's datasource by printing it, but the lyr.replaceDataSource causes an error (Unexpected error... doesn't help much...)

I've been working at it for 4-5 hours now and I'm short of solutions to try. Any idea what can cause the crash?

Thanks!!

share|improve this question
1  
On first glance, it looks like you've got some odd characters in your directory path. Try moving the shapefile to a test directory like D:\test and see if that works. –  Mintx Nov 20 '12 at 15:45
    
Never had any problem with that before. The character is "#" and it's only an old sorting trick so my projects will appears first. I tried putting my project in D:/TEMP, but it didn't work... –  fgcartographix Nov 20 '12 at 16:03
add comment

3 Answers

up vote 2 down vote accepted

I know this is pretty old, but I just encountered this same issue and was surprised by the lack of solutions out there. The solution is so simple, but you'd have no way of knowing it based on ESRI's practically non-existent help:

Don't include the shapefile extension. ...That's it.

newSource = os.path.join(currDir, lyrZones + "_Dep" + dirNum + ".shp")

Should read:

newSource = os.path.join(currDir, lyrZones + "_Dep" + dirNum)
share|improve this answer
add comment

Fun with debugging! Try this:

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name == lyrZones:
        if lyr.supports("DATASOURCE"):
            print lyr.dataSource
            print currDir
            print newSource
            lyr.replaceDataSource(currDir, "SHAPEFILE_WORKSPACE", newSource, TRUE)
            print "Did we crash yet?"
            print lyr.dataSource
        else:
            print "Layer isn't supported!"
share|improve this answer
    
At least I know Datasource is supported. Got the same error message. Crashed on "lyr.replaceDataSource(currDir, "SHAPEFILE_WORKSPACE", newSource, True)". Didn't get to "Did we crash yet?". Frustrating... –  fgcartographix Nov 20 '12 at 17:07
    
Argh, that sucks. What's the end goal you're trying to achieve? Maybe there's another way to go about this. –  Mintx Nov 20 '12 at 17:17
    
I have 5 templates and between 14 and 50 maps of each to do (128 maps total). I have two layers to update on each one. The first is only a Def. Query which I handled like a boss ;) (lyr.definitionQuery = defQueryLines). The other is a polygon layer that has a graduated color symbology, but the file has to change. I wrote a script that creates an output for each map and I need to update the source and change the symbology value (but I think it's 10.1 that do that. I have 10.0) so I could keep the symbology... –  fgcartographix Nov 20 '12 at 18:12
    
For now, I update the file manually (the first batch has 20 maps so it's not that tedious), but the next one will have 50 maps so... –  fgcartographix Nov 20 '12 at 18:13
    
Does the tool has to work with a .lyr file? Maybe I could work directly on a .lyr where I could change is datasource. I don't see any properties to change the symbology valye (the field symbolized though...) –  fgcartographix Nov 20 '12 at 18:34
show 2 more comments

I don't know if this helps you or not but I've encountered this error trying to switch SDE data sources for a layer. What I found is that when I'd run from PyScripter, it would spool up a 64-bit python.exe process. I'd get the ValueError. If I forced the script to run using the 32-bit python.exe via Command Prompt, I wouldn't get the error. I don't know if that would help your issue or not but it's worth a shot!

share|improve this answer
    
Thanks. It's all 32-bit, so it can't be the issue, but I'll keep that in mind when I'll try 64-bit geoprocessing. For now, I've left that part out of my script and replaced the source by hand... :( –  fgcartographix Mar 8 '13 at 12:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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