【问题标题】:Add multiple shapefiles to MXD with arcpy使用 arcpy 将多个 shapefile 添加到 MXD
【发布时间】:2013-03-07 06:02:39
【问题描述】:

我正在尝试使用此处找到的一些代码将多个文件添加到 MXD 文件: How do I add a shapefile in ArcGIS via python scripting?

下面的代码没有返回任何错误,但是似乎没有任何 shapefile 被添加到空白 mxd 文档中。

任何关于为什么这不起作用的帮助将不胜感激。

import arcpy
import arcpy.mapping
from shutil import copyfile
from os import listdir
from os.path import isfile, join

def AddAllShapeFilesToNewMXD(source_directory):
    # Source file is the template that the will be copied to the directory with
    # All the shape files in it.
    source_file = 'M:\Ops Field Map\Blank Map.mxd'

    # Output file is the name of the file that will have the shape files added to it
    output_file = 'GPS_Map'
    rev_count = 0
    while isfile(join(source_directory, output_file + '.mxd')):
        #Make sure a unique file is created
        print ('File ' + output_file + '.mxd exists.'),
        rev_count += 1
        output_file = output_file + '_rev' + str(rev_count)
        print ('Trying ' + output_file + '.mxd ...')

    # Create the destination file. This is the file the shape files are added to
    destination_file = join(source_directory, output_file + '.mxd')
    copyfile(source_file, destination_file)
    print 'MXD file created: ' + destination_file

    # Get the map doccument
    mxd = arcpy.mapping.MapDocument(destination_file)
    # Get the data frame
    data_frame = arcpy.mapping.ListDataFrames(mxd, "*")[0]

    # Get a list of all the shape files
    shp_files = [ f for f in listdir(source_directory) if isfile(join(source_directory, f)) and f.endswith('.shp') ]

    # Add all the shapefiles to the mxd file
    for s in shp_files:
        new_layer_full_path = join(source_directory, s)
        new_layer = arcpy.mapping.Layer(new_layer_full_path)
        arcpy.mapping.AddLayer(data_frame, new_layer, "BOTTOM")
        print 'Layer added ' + new_layer_full_path
        del new_layer

    return True

directory = 'C:\Users\gps\Desktop\dd test'
AddAllShapeFilesToNewMXD(directory)

【问题讨论】:

  • 打印语句是否显示(即“添加图层”)?如果是这样,您可以发布该输出吗?

标签: python arcgis arcpy


【解决方案1】:

很难知道没有文件可玩,但上面的代码可能没有给出错误但没有显示任何内容的一个原因是,对于许多 arcgis 地图显示操作,您必须确保 arcgis 地理处理选项地理处理>地理处理选项下的“将地理处理操作的结果添加到显示中”已打开。

【讨论】:

    【解决方案2】:

    可能是您遗漏了这两行:

    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()
    

    【讨论】:

      【解决方案3】:

      看起来您快到了,如果您的代码在活动会话中运行,Lucas 和BelowZero 都提供了很好的建议。如果它正在创建一个 *.mxd 供以后使用,我看不到结果的保存位置。这是一些更简单的示例代码,请注意最后一行:

      mxd = arcpy.mapping.MapDocument(srcdir+'/data_bin/Untitled.mxd')
      data_frame = arcpy.mapping.ListDataFrames(mxd)[0]
      mxd.activeView = data_frame.name
      
      flowlinesLyr=arcpy.mapping.Layer('..\\NHDPlus\\nhdflowline_en')
      flowlinesLyr.name='NHDPlus Flowlines'
      arcpy.mapping.AddLayer (data_frame, flowlinesLyr,'TOP')
      
      gagesEventLyr=arcpy.mapping.Layer('..\\NHDPlus\\StreamGageEvent')
      gagesEventLyr.name='Original stream gage locations'
      arcpy.mapping.AddLayer (data_frame, gagesEventLyr,'TOP')
      
      mxd.saveACopy(datadir+'\NHDPlus'+Region+'_Gage_QAQC.mxd')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-06
        • 2016-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-24
        • 2015-05-27
        相关资源
        最近更新 更多