Summary
Changes the data source on layers within a map document (MXD).
If you are using one of the standard Defense Mapping templates, you can use this tool to reset the sources of all of the layers in the TOC to the new data source.
Usage
The Document parameter must be the path to a .mxd on disk and not a map object created with arcpy.mapping.MapDocument().
The Data Layer Mapping parameter is a value table that consists of the layer name from the map document and the dataset to which this layer will be resourced.
Syntax
SetDataSource_defense (document, datalayer_mapping, {active_data_frame_only})
Parameter | Explanation | Data Type |
document | The path to a valid ArcMap document (.mxd) that contains layers for which the source needs to be set. This option doesn't work with the CURRENT keyword or a Map Object, so you will need to specify the path. | File |
datalayer_mapping ["layer_name", "dataset_path"] | Specifies the layer name and the dataset that corresponds to the layer. | Value Table |
active_data_frame_only (Optional) | Determines if the active data frame or all the data frames in the current map should be used to update the sources.
| Boolean |
Code sample
SetDataSource example 1
This example uses the specific names of the data layers and the proper paths to the datasets.
"""
THIS EXAMPLE ASSUMES THAT YOU KNOW THE SPECIFIC NAMES OF THE DATA
LAYERS AND THE PROPER PATHS TO THE DATASETS.
"""
# Importing necessary modules
import arcpy
# Checking out defense mapping extension
arcpy.CheckOutExtension('defense')
# Setting path to mxd
map_document = r'C:\DATA\Example.mxd'
# Calling Set Data Source tool
arcpy.defense.SetDataSource(map_document, r"'ZD040_Named_Location_Point - TextP' C:\TRD4.gdb\MGCP\TextP")
SetDataSource example 2
This example resets the sources for several layers from a map document to the same dataset.
"""
THIS EXAMPLE ASSUMES THAT YOU ARE RESOURCING SEVERAL LAYERS FROM A MAP
DOCUMENT TO THE SAME DATABASE, AND THAT THERE ARE NO GROUPED LAYERS IN THE
MAP DOCUMENT.
"""
# Importing necessary modules
import arcpy, os
# Checking out defense mapping extension
arcpy.CheckOutExtension('defense')
# Setting path to mxd and database
map_document = r'C:\DATA\Example.mxd'
database = r'C:\DATA\Example.gdb'
# Getting the layers from the proper dataframes
map_object = arcpy.mapping.MapDocument(map_document)
data_frame = arcpy.mapping.ListDataFrames(map_object, "BaseMap")[0]
resource_layers = arcpy.mapping.ListLayers(map_object, "*", data_frame)
# Creating an empty string that we can build onto with each layer in the dataframe
source_string = r""
for layer in resource_layers:
layer_name = layer.name
feature_class = os.path.split(layer.dataSource)[1]
dataset = os.path.split(os.path.split(layer.dataSource)[0])[1]
new_source = r'{0}\{1}\{2}'.format(database, dataset, feature_class)
source_string = source_string + "'{0}' {1};".format(layer_name, new_source)
# Saving and closing the map document objec to remove any locks
map_object.save()
del map_object
# Running Set Data Source on all layers in the map documents data frame
arcpy.defense.SetDataSource(map_document, source_string)
Environments
This tool does not use any geoprocessing environments
Licensing information
- ArcGIS Desktop Basic: No
- ArcGIS Desktop Standard: Requires Defense Mapping
- ArcGIS Desktop Advanced: Requires Defense Mapping