使用python添加矢量数据到mxd

1、添加shp数据到mxd 

# -*-coding:utf-8-*-
import arcpy

mxd = arcpy.mapping.MapDocument(r"E:\testmxd\testmxd.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
theShape = r"E:\testmxd\江夏区.shp"
addLayer = arcpy.mapping.Layer(theShape)
arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
#Save to a new map document and clear variable references
mxd.saveACopy(r"E:\testmxd\testmxd1.mxd")
del mxd

2、添加sde中的要素类到mxd

# -*- coding: utf-8 -*-
import arcpy
from arcpy import env
import os
env.overwriteOutput = True
def CopyDatabase(InputSDE,OutputSDE):
    print("Input SDE file Path "+InputSDE)
    print("Output SDE file Path"+OutputSDE)
    print("Open SDE")
    arcpy.env.workspace=InputSDE
    print("Start mxd...")
    for fea in arcpy.ListFeatureClasses():
        try:
            if (fea=="SDE.jiangxia"):
                print "Copying table " + fea + " to " + OutputSDE
                mxd = arcpy.mapping.MapDocument(r"E:\testmxd\testmxd1.mxd")
                df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

                addLayer = arcpy.mapping.Layer(fea)
                arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
                # Save to a new map document and clear variable references
                mxd.saveACopy(r"E:\testmxd\testmxd2.mxd")
                print "ok"
        except Exception:
            print("Filed Copy table "+fea)
            continue


inputsde=r'C:\Users\admin\AppData\Roaming\Esri\Desktop10.6\ArcCatalog\Connection to orcl.sde'
outputsde=r'C:\Users\admin\AppData\Roaming\Esri\Desktop10.6\ArcCatalog\Connection to orcl.sde'
CopyDatabase(inputsde,outputsde)


效果图:

使用python添加矢量数据到mxd_第1张图片

使用python添加矢量数据到mxd_第2张图片

参考资料:

http://desktop.arcgis.com/zh-cn/arcmap/latest/analyze/arcpy-mapping/addlayer.htm 

你可能感兴趣的:(Python)