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.

Hi I'm trying to do something quite simple but its not working. I want my python script to change all of the layers that point to the same datasource (a point shapefile) to another point shapefile. replaceDataSource seems to be the methods but when I run this:

for lyr in arcpy.mapping.ListLayers(mxd):
    #print lyr.name
    if lyr.dataSource == r"X:\filepath\folder\points.shp":
            print lyr.dataSource
            lyr.replaceDataSource(r"\\main.glb.corp.local\E-GB$\Home\AB\4\J022414\Documents\ArcGIS", 'SHAPEFILE_WORKSPACE', "ismage_template_P.shp")
            print "data replaced"
mxd.saveACopy(mxd)

i get "Runtime error type 'exceptions.ValueError'>: Layer: Unexpected error" when I run this. It prints the data source but doesnt print "data replaced" so it looks like the problem is on the lyr.replacedatasource line

Has anyone had this problem before, or should I be using a different method?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Your lyr.replaceDataSource already knows that it is working with a SHAPEFILE_WORKSPACE and so it does not need ".shp " (which instead upsets it).

If you leave it off then I think your code will work.

share|improve this answer
    
Thanks, that seems to have worked, I have run into another problem thought. I dont know if its related "Runtime error <type 'exceptions.AttributeError'>: LayerObject: Get attribute dataSource does not exist". Any idea? –  whatahitson Mar 28 at 12:49
    
Oh never mind I fixed it by adding "if lyr.supports("DATASOURCE"):" –  whatahitson Mar 28 at 13:07

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.