首页 文章

arcpy将shp文件保存到mxd

提问于
浏览
1

使用arcpy,如何将shapefile保存到映射文档(mxd)文件?在ArcGis中,您只需提交文件 - >保存副本,但到目前为止,它已显示出更复杂的arcpy .

import os, sys, string, arcpy, arcpy.mapping, glob, arcgisscripting, time
from arcpy import env

workspace = "C:/users/Documents/maps/"
curmap= workspace + "current.shp"

我已经浏览了ArcGIS Help 10.1文本,一旦你有了mxd文件就会启动它,我仍然需要创建mxd文件 . 这个问题是我找到的最接近潜在答案的问题 . 在保存到mxd之前,我是否需要将文件从shp文件更改为gdb或要素图层? https://gis.stackexchange.com/questions/129713/arcpy-saveacopy-method-saving-copy-of-mxd-to-wrong-path

2 回答

  • 1

    我假设您想要将shapefile添加到新的mxd . 首先,您需要通过打开ArcMap和另存为创建空白mxd来创建空白mxd . 然后创建shapefile的要素图层,然后将其添加到mxd .

    import arcpy
    from arcpy import mapping
    
    blank_mxd_path = r"C:\blank_mxd.mxd"
    mxd = arcpy.mapping.MapDocument(blank_mxd_path)
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    
    shapefile_path = r"C:\path\to\file.shp"
    arcpy.MakeFeatureLayer_management(shapefile_path, "nameinTOC")
    layer = arcpy.mapping.Layer("nameinTOC")
    arcpy.mapping.AddLayer(df, layer, "AUTO_ARRANGE")
    
    mxd.saveACopy(r"C:\location\of\your\new\mapDoc.mxd")
    
    del mxd
    
  • 1

    对于初学者,您需要将shapefile保存为.MXD . 您设置工作区,但实际上您将shapefile保存到此路径,而不是MXD .

    你需要的是:

    import arcpy
    
    workspace = "C:/users/Documents/maps/"
    

    如果您想使用当前打开的MXD,请使用以下代码:

    arcpy.mapping.MapDocument("CURRENT")
    

    否则,您将需要一个 Map 文档名称,即您尝试将此特定shapefile保存到的MXD . 此外,您还要设置当前数据框 .

    mxd = arcpy.mapping.MapDocument(path + r"\YOUR MXD NAME HERE")
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    

    然后,取你的shapefile(整个路径)和:

    shp_path = r"SHAPEFILE PATH.shp"
    arcpy.MakeFeatureLayer_management(shp_path, "NAME AS SHOWN IN TABLE OF CONTENTS")
    lyr = arcpy.mapping.Layer("name in the Table of Contents")
    arcpy.mapping.AddLayer(df, layer, "AUTO_ARRANGE")
    

    这会将图层添加到您设置的数据框中 .

    最后,您需要删除您创建的mxd变量以释放空间:

    del mxd
    

相关问题