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 am attempting to update a layer's data source in Arcmap using python

for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
    if lyr.name == "project_whl":
        lyr.replaceDataSource("F:\\Jobs2012\\05665\\EPASS\\", "SHAPEFILE_WORKSPACE","1205992_whl")

This WORKS!

HOWEVER I would like to use a wildcard instead of explicitly stating the dataset_name

Example below:

for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
        if lyr.name == "project_whl":
            lyr.replaceDataSource("F:\\Jobs2012\\05665\\EPASS\\", "SHAPEFILE_WORKSPACE","*whl")

Yet does not work, I have tried *, ?, %, _, & none of these work...

Just get the following error message

<type 'exceptions.ValueError'>: Layer: Unexpected error

Your thoughts, ideas?

share|improve this question
add comment

3 Answers 3

Yeah - I wouldn't expect any sort of wildcard specification to work due to the risk of returning multiple possible candidate shapefile datasources. I think you'll have to add a function to get the shapefile name via the Python OS namespace. Something like this should work:

def getFileName(dir):
 import os
 for root, dirs, files in os.walk(dir):
  for f in files:
   if "shp" in f:
    return f.replace('.shp','')

# And then you're loop will look like this:
for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
 if lyr.name == "project_whl":
  myFile = getFileName("SHAPEFILE_WORKSPACE")
  lyr.replaceDataSource("F:\\Jobs2012\\05665\\EPASS\\", "SHAPEFILE_WORKSPACE",myFile)

Note that this method will return the first shapefile encountered in SHAPEFILE_WORKSPACE

share|improve this answer
add comment

In general, unless the documentation says something should work, don't expect it to. :)

Here's what the Layer documentation has on the dataset_name parameter:

A string that represents the name of the dataset the way it appears in the new workspace (not the name of the layer in the TOC). If dataset_name is not provided, the replaceDataSource method will attempt to replace the dataset by finding a table with a the same name as the layer's current dataset property."

So you might try leaving it blank if you want it to locate it based on the previous dataset name.

share|improve this answer
    
Thanks, did try leaving blank still gave me error. Also will try implementing os.path.splittext. –  TForward Jun 21 '12 at 20:08
add comment

I ended up doing this.

Credit goes to user blah238 for suggesting using os.path.splitext

import os
import arcpy

# Function to remove ".shp" from shapefile
def delExt (shpFile):
    nPath = os.path.splitext(shpFile)
    return nPath[0]

# Update files with correct data source
os.chdir(ePassPath)
for files in os.listdir("."):
    if files.endswith("_whl.shp"):
        whlPath = files
    elif files.endswith("_aoa.shp"):
        aoaPath = files
    elif files.endswith("_ws.shp"):
        wsPath = files
    elif files.endswith("_route.shp"):
        routePath = files

#=== Loop throught layers find the one to be replaced ===

for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
     if lyr.name == "project_whl":
        lyr.replaceDataSource(ePassPath, "SHAPEFILE_WORKSPACE",delExt(whlPath))
    if lyr.name == "project_aoa":
        lyr.replaceDataSource(ePassPath, "SHAPEFILE_WORKSPACE",delExt(aoaPath))
    if lyr.name == "project_ws":
        lyr.replaceDataSource(ePassPath, "SHAPEFILE_WORKSPACE",delExt(wsPath))
    if lyr.name == "project_route":
        lyr.replaceDataSource(ePassPath, "SHAPEFILE_WORKSPACE",delExt(routePath))
share|improve this answer
1  
Better to use os.path.splitext than string slicing for removing the file extension IMO! –  blah238 Jun 21 '12 at 18:13
    
Sorry, Valvelondon did not think of you as was already using os in my code. for files in os.listdir("."): if files.endswith("_whl.shp"): whlPath = files –  TForward Jun 21 '12 at 22:01
    
I did not include it before as did not feel it was related to my question on wildcards within replaceDataSource. However, credit goes to you for confirming was on the right track. –  TForward Jun 21 '12 at 22:23
    
Fair enough. we can delete this thread then. –  valveLondon Jun 21 '12 at 22:34
    
Would it not be more useful to share our results with the community? –  TForward Jun 21 '12 at 22:42
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.