Select to view content in your preferred language

replaceDataSource

2276
2
03-05-2013 09:15 AM
TonyAlmeida
MVP Regular Contributor
I have mxd i work on for different cases, but have the the same layers. The script i am working on needs to update the "Subject_Property" layer data source. I am having trouble getting this to work.

The script will search for a text in the "DXF_TEXT" filed and export it out to the location of the mxd. The mxd already has the layer "SUBJECT_PROPERTY" but i need to get it to update the mxd's "SUBJECT_PROPERTY" with the one that was searched and exported,  don't needed added to the current mxd. hopefully this makes sense.

Here is the code i have so far. Any help would be gratefully appreciated.
Thanks.
import arcpy, string, os

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "TAXLOTS")[0]

arcpy.env.workspace = os.path.dirname(mxd.filePath)
wp = os.path.dirname(mxd.filePath)



value = arcpy.GetParameterAsText(0)
if value == "#" or value == "":
    value = None
if value:
    arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "\"DXF_TEXT\" = '" + str(value) +"'")
else:
    arcpy.AddError("no value")
    
df.extent = lyr.getSelectedExtent()
df.scale = 7000



if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:

   arcpy.Select_analysis("TAXLOTS", "SUBJECT_PROPERTY")

#del mxd
#mxd = arcpy.mapping.MapDocument("CURRENT")
#lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0]
lyrpath = lyr.workspacePath
data_path = os.path.dirname(lyrpath)

SP = "SUBJECT_PROPERTY" 
lyr.replaceDataSource(data_path, "SHAPEFILE_WORKSPACE", SP, True )

Tags (2)
0 Kudos
2 Replies
TonyAlmeida
MVP Regular Contributor
I have something that works but i am not sure if this is correct or if there is an easier way of doing this.
Any input would be gratefully appreciated please.


import arcpy, string, os

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "TAXLOTS")[0]

arcpy.env.workspace = os.path.dirname(mxd.filePath)
wp = os.path.dirname(mxd.filePath)



value = arcpy.GetParameterAsText(0)
if value == "#" or value == "":
    value = None
if value:
    arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "\"DXF_TEXT\" = '" + str(value) +"'")
else:
    arcpy.AddError("no value")
    
df.extent = lyr.getSelectedExtent()
df.scale = 24000



if int(arcpy.GetCount_management("TAXLOTS").getOutput(0)) > 0:

   arcpy.Select_analysis("TAXLOTS", "SUBJECT_PROPERTY")

del mxd, wp, 
mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0]
lyrpath = lyr.workspacePath
#data_path = os.path.dirname(lyrpath)
wp = os.path.dirname(mxd.filePath)

SP = "SUBJECT_PROPERTY" 
lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", SP, True )

arcpy.RefreshActiveView()
arcpy.RefreshTOC()
0 Kudos
JeffBarrette
Esri Regular Contributor
I'm not clear about your concern (if it is working).  As long as your wp variable is pointing to the correct location, you have the workspace type correct and you have a valid data source, it should just work.

Jeff
0 Kudos