python map automation...changing data sources

857
3
04-11-2011 07:21 AM
RafiqBasaria
New Contributor II
Hi every one. My goal with this script is to be able to create new map documents with new sources for specific layers in the MXD. Im not sure what I am missing. So far, the script will create new MXD files with new names, but the sources will not change. Thanks for any help.

import arcpy

mxd = arcpy.mapping.MapDocument("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Projects/Orange/CommissionerDistricts_01.mxd")

mxd.saveACopy("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Projects/Orange/CommissionerDistricts_02.mxd")

mxd2 = arcpy.mapping.MapDocument("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Projects/Orange/CommissionerDistricts_02.mxd")

for lyr in arcpy.mapping.ListLayers(mxd2):
    if lyr.name == "OR_She_Dist1.shp":
        lyr.replaceDataSource("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/Orange/OR_She_Dist2.shp")
    if lyr.name == "OR_ADA_Dist1.shp":
        lyr.replaceDataSource("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/Orange/OR_ADA_Dist2.shp")
    if lyr.name == "OR_Rts_Dist1.shp":
        lyr.replaceDataSource("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/Orange/OR_Rts_Dist2.shp")
    if lyr.name == "CountyCommission_TriCounty":
        lyr.definitionQuery = '"DISTRICT" = \'2\' AND "COUNTY" = \'ORANGE\''

mxd2.save()
Tags (2)
0 Kudos
3 Replies
MathewCoyle
Frequent Contributor
Try this
import arcpy

mxd = arcpy.mapping.MapDocument("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Projects/Orange/CommissionerDistricts_01.mxd")

mxd2 = arcpy.mapping.MapDocument("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Projects/Orange/CommissionerDistricts_02.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name == "OR_She_Dist1.shp":
        lyr.replaceDataSource("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/Orange/OR_She_Dist2.shp")
    if lyr.name == "OR_ADA_Dist1.shp":
        lyr.replaceDataSource("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/Orange/OR_ADA_Dist2.shp")
    if lyr.name == "OR_Rts_Dist1.shp":
        lyr.replaceDataSource("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/Orange/OR_Rts_Dist2.shp")
    if lyr.name == "CountyCommission_TriCounty":
        lyr.definitionQuery = '"DISTRICT" = \'2\' AND "COUNTY" = \'ORANGE\''


mxd.saveACopy("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Projects/Orange/CommissionerDistricts_02.mxd")
0 Kudos
RafiqBasaria
New Contributor II
That was my original script, and it would do the same thing...save the new document, but not make any of the required changes in the new document. I figured it was making the changes, then saving a copy of the original document, rather than saving the document with the changes. So my thought process was to create the new document, then change the sources in the new document, and save the new document...However, I ended up with the same results.
0 Kudos
MathewCoyle
Frequent Contributor
Oh sorry, names are required for the replaceDataSource method.

replaceDataSource (workspace_path, workspace_type, dataset_name, {validate})

Also, testing for support helps too.

And are you sure the name of the layer ends in .shp? Those are the TOC names you are testing on, not dataSource names.

Try something like this.
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DATASOURCE") and lyr.supports("DATASETNAME") and lyr.supports("NAME"):
        if lyr.name == "OR_She_Dist1.shp":
            lyr.replaceDataSource("C:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/Orange", "SHAPEFILE_WORKSPACE, "OR_She_Dist2.shp")


This help page has all the required info
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/
0 Kudos