Using replaceDataSource to change geotiff file source?

3135
7
10-12-2010 07:09 AM
DanSlayback
New Contributor II
I'm trying to take an existing mxd file and update it, automatically, with an updated raster layer. The rasters are geotiffs. However, I only get errors when I try to use replaceDataSource. Here is an example call, after I've setup "lyr" to point to the correct layer in the correct dataframe of the mxd:

>>> lyr.replaceDataSource("E:/Projects", "RASTER_WORKSPACE", "NewLayer_2010.tif")

Traceback (most recent call last):
  File "<pyshell#20>", line 1, in <module>
    floodlayer.replaceDataSource("E:/Projects", "RASTER_WORKSPACE", "NewLayer_2010.tif")
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\utils.py", line 179, in fn_
    return fn(*args, **kw)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\_mapping.py", line 559, in replaceDataSource
    return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))
ValueError: Layer: Unexpected error


I have also tried using a Raster object, as returned by arcpy.Raster(filename), as the third parameter to this call, but also with no luck.

I am new to map automation in ArcGIS and arcpy - is what I'm trying to do simply not possible? Or is my syntax wrong, or I'm missing a critical step?

As an alternative, I also tried inserting (or adding) a new Layer (arcpy.mapping.AddLayer). This works, but requires a layer object to be added/inserted -- it wont work on a geotiff file. And I cant figure out how to make a geotiff file into a layer object automatically. I can do so interactively in the ArcMap session (Save As Layer File), but I need to automate this. Is there a way to do this automatically?

Or a better way all around to do what I'm trying to do? (Update one geotiff layer in mxd with a different one, where both have same size/extent/georeference etc just different values).

thanks for any tips or hints.
0 Kudos
7 Replies
JeffBarrette
Esri Regular Contributor
Hello Daniel,

Is your new raster in the same workspace (i.e., folder) as the old raster?

Jeff
0 Kudos
JeffBarrette
Esri Regular Contributor
Daniel,

Try removing the extension from the file name.  Try:

lyr.replaceDataSource("E:/Projects", "RASTER_WORKSPACE", "NewLayer_2010")


This issue was addressed for SP1.  Please let me know if this does not work for you.  At SP1 you'll be able to use your original line of code.

Thanks again,
Jeff
0 Kudos
DanSlayback
New Contributor II
Hi Jeff,

Yes, the raster was in the same directory - is that required?

I wont have a chance to try it without the .tif until Monday, but will get back to you then if it works (or doesnt).

When is the SP1 expected out?

thanks for the help!

Dan
0 Kudos
JeffBarrette
Esri Regular Contributor
It should not matter if the raster is in a different folder.  I was going to provide an alternative approach but then learned about the SP1 fix concerning the extension as part of the string.

Please keep us posted,
Jeff
0 Kudos
SeanRaffuse
New Contributor
Aha!  This was driving me insane for hours.  I'm glad I stumbled upon this.  I was using 'name.img' with no success.  Changing it to 'name' fixed the problem.  Thanks!
0 Kudos
DanSlayback
New Contributor II
Ok, yes, that does it - Thank you!

However, one additional issue: the replacement filename apparently cannot have any periods in it, except for the one indicating filetype. So, a file named test.tif will work (using simply "test" in replaceDataSource), but a file named test.abc.tif will not work (with "test.abc" in replaceDataSource).

Is that expected behavior? Eg the only period in a filename should be the one indicating file type?

Dan
JianLiu
New Contributor III

I have the same problem too. My tif file name has a "." in it ("MN_Marine_on_St._Croix_20130807_TM_geo.tif"), the replacedatasource won't work! So I have to manually rename the file to make it work? Imagine I have hundreds of files... I am using 10.3.This seems to be an easy fix. ESRI, please. 

0 Kudos