arcPy findAndReplaceWorkspacePath Won't Update Rasters

1275
4
Jump to solution
03-14-2017 10:54 AM
LloydBronn
Occasional Contributor II

I'm using ArcGIS 10.3. I have an MXD that contains symbolized global raster images. I have a GP tool that masks these rasters to a country or state and exports the images. I'm trying to update the source background layers and rasters each time a new country is chosen. The layer names are all generic, but the source files all have the country or state name tagged on.  findAndReplaceWorkspacePath works just fine with the background layers (shapefiles), but it won't update the rasters. I've confirmed the old and new sources with print statements, it's just not changing the source. The only time I get images is if I use the default state (Texas) set in the MXD. If I choose any other location, I just get a blank country or state outline image.

Here is my code snippet:

layer_list = ["Precip","High_Temp","Temp_Departure"]
for filename in layer_list:
layer_name = filename
for lyr in arcpy.mapping.ListLayers(mxd):
 if lyr.supports("DATASOURCE"):
 
 if layer_name == str(lyr.name):
 old_source = os.path.dirname(lyr.dataSource)
 ##print old_source + "\\" + layer_name
 new_source = country_temp_path
 ##print new_source + "\\" + layer_name
 
 lyr.findAndReplaceWorkspacePath(old_source, new_source, False)
return()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
LloydBronn
Occasional Contributor II

Just got it to work

for lyr in arcpy.mapping.ListLayers(mxd):
                if lyr.supports("DATASOURCE"):
                    new_source = country_temp_path
                    if  lyr.isRasterLayer:
                        lyr.replaceDataSource(new_source, "RASTER_WORKSPACE", input_country + "_" + layer_name, False)
                 

View solution in original post

0 Kudos
4 Replies
RebeccaStrauch__GISP
MVP Emeritus

do a test for    lyr.isRasterLayer   then use   lyr.replaceDataSource()

To see how I did it, if that doesn't help, look at my script   4_dataSourceRepairX.py , about line #269 in   /blogs/myAlaskaGIS/2015/08/31/python-addin-for-data-inventory-and-broken-link-repair?sr=search&searc...

0 Kudos
LloydBronn
Occasional Contributor II

I tried this and I'm still getting blank images 

layer_list = ["Precip","High_Temp","Temp_Departure"]
for layer in layer_list:
layer_name = layer
for lyr in arcpy.mapping.ListLayers(mxd):
 if lyr.supports("DATASOURCE"):
 old_source = os.path.dirname(lyr.dataSource)
 new_source = country_temp_path
 if lyr.dataSource == old_source:
 if lyr.isRasterLayer:
 lyr.replaceDataSource(new_source, "RASTER_WORKSPACE", layer_name, True)
0 Kudos
LloydBronn
Occasional Contributor II

Just got it to work

for lyr in arcpy.mapping.ListLayers(mxd):
                if lyr.supports("DATASOURCE"):
                    new_source = country_temp_path
                    if  lyr.isRasterLayer:
                        lyr.replaceDataSource(new_source, "RASTER_WORKSPACE", input_country + "_" + layer_name, False)
                 
0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I know you said you couldn't find the script for in the addin thread.  You needed to download the .zip file.

0 Kudos