Select to view content in your preferred language

Repair Multiple Raster Data Paths

1385
6
04-23-2012 06:21 AM
JeremyJones1
Emerging Contributor
Hello Everyone,

I have a mxd that has probably 100 or more rasters with broken data paths.  I'm trying to use python to fix all the datapaths.  I had the rasters grouped but have since removed them from the groupings trying to make this work.  Below is the script I have been trying to run in the python window in my mxd. 

>>> import arcpy
... mxd=arcpy.mapping.MapDocument(r"JeremyJones_Enbridge2012.mxd")
... for lyr in arcpy.mapping.ListLayers(mxd):
... if lyr.supports("DATASOURCE"):
...     mxd.findAndReplaceWorkspacePaths(r"C:\ENBRIDGE2\GIS_DATA\Imagery\July2011Aerials\", r"E:\Enbridge\GIS_DATA\Imagery\2011Aerials")
... mxd.saveACopy(r"Temp2.mxd")

I keep getting an message of "Parsing error <type 'exceptions.IndentationError'>: expected an indented block (line 4)

Any help would be greatly appreciated.

Jeremy
0 Kudos
6 Replies
KevinBurke
Esri Contributor
Hello, according to the current script:
>>> import arcpy
... mxd=arcpy.mapping.MapDocument(r"JeremyJones_Enbridge2012.mxd")
... for lyr in arcpy.mapping.ListLayers(mxd):
... if lyr.supports("DATASOURCE"):
... mxd.findAndReplaceWorkspacePaths(r"C:\ENBRIDGE2\GIS_DATA\Imagery\July2011Aerials\", r"E:\Enbridge\GIS_DATA\Imagery\2011Aerials")
... mxd.saveACopy(r"Temp2.mxd")

The reason why it is failing, is because according to Python syntax, there needs to be an indentation after the for loop and after the if statement. For example:

import arcpy
mxd=arcpy.mapping.MapDocument(r"JeremyJones_Enbridge2012.mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DATASOURCE"):
        mxd.findAndReplaceWorkspacePaths(r"C:\ENBRIDGE2\GIS_DATA\Imagery\July2011Aerials\", r"E:\Enbridge\GIS_DATA\Imagery\2011Aerials")
        mxd.saveACopy(r"Temp2.mxd")

Give this a try and see if it works for you.

Thank you.
0 Kudos
JeremyJones1
Emerging Contributor
Kevin,

It didn't show up with an indentation so should it look like this?

import arcpy
mxd=arcpy.mapping.MapDocument(r"JeremyJones_Enbridge2012.mxd")
.......for lyr in arcpy.mapping.ListLayers(mxd):
...........if lyr.supports("DATASOURCE"):
...............mxd.findAndReplaceWorkspacePaths(r"C:\ENBRIDGE2\GIS_DATA\Imagery\July2011Aerials\", r"E:\Enbridge\GIS_DATA\Imagery\2011Aerials")
..................mxd.saveACopy(r"Temp2.mxd")


Ran it with the indentations and am now getting this error:

Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 5)



Thanks

Jeremy
0 Kudos
KevinBurke
Esri Contributor
Hey Jeremy,

Here is that script again, which is similar to what you tested:

import arcpy
mxd=arcpy.mapping.MapDocument(r"JeremyJones_Enbridge2012.mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
.....if lyr.supports("DATASOURCE"):
.......mxd.findAndReplaceWorkspacePaths(r"C:\ENBRIDGE2\GIS_DATA\Imagery\July2011Aerials\", r"E:\Enbridge\GIS_DATA\Imagery\2011Aerials")
.......mxd.saveACopy(r"Temp2.mxd")

Where are you running this code from? ArcMap, PythonWin, Python IDLE?

After the for loop there should be one tab indent and also, after the if statement, there should be another tab indent.

Have you taken a look at http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s30000004p000000

Thank you.
0 Kudos
JeremyJones1
Emerging Contributor
Kevin,

I've tried running it in arcMap in the python window, and in the Python IDLE window.  When I run it in the IDLE window I'm getting an error that it doesn't like the syntax for the new data path.  It looks to error out at the E. 

I have tried looking at the link you send me.  That's where I started, however they don't use any examples of drive letter changes, with new folders etc.  It seems like I have it set up right, but there must be something wrong.

Thanks

Jeremy
0 Kudos
KevinBurke
Esri Contributor
Hi Jeremy,

Try putting the full path to the existing mxd document in following line:mxd=arcpy.mapping.MapDocument(r"JeremyJones_Enbridge2012.mxd")

Or, if this mxd is currently opened you could use the following: mxd=arcpy.mapping.MapDocument("CURRENT") and run the code within the Python Window in ArcMap.

Thank you.
0 Kudos
JeremyJones1
Emerging Contributor
Kevin,

Thanks for trying to help.  When I run it in the python IDLE mode i get a syntax error at the point of where the new data path is entered.  For some reason it doesn't like what I have there.  In theory it looks correct, I've even double checked my paths to make sure I had it correct. 

import arcpy
mxd=arcpy.mapping.MapDocument(r"E:\Enbridge\JeremyJones_Enbridge2012.mxd")
....for lyr in arcpy.mapping.ListLayers(mxd):
......if lyr.supports("DATASOURCE"):
........mxd.findAndReplaceWorkspacePaths(r"C:\ENBRIDGE2\GIS_DATA\Imagery\July2011Aerials\", r"E:\Enbridge\GIS_DATA\Imagery\2011Aerials")
........mxd.saveACopy(r"Temp2.mxd")
0 Kudos