1

ここにあるコードの一部を使用して、複数のファイルを MXD ファイルに追加しようとしています: How do I add a shapefile in ArcGIS via python scripting?

以下のコードはエラーを返しませんが、空の 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)
4

3 に答える 3

0

コードがアクティブなセッションで実行されている場合、ルーカスと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')
于 2013-09-06T17:20:57.510 に答える
0

操作するファイルがないとわかりにくいですが、上記のコードでエラーが発生しないのに何も表示されない理由の 1 つは、多くの arcgis マップ表示操作で、「結果を追加する」の arcgis ジオプロセシング オプションを確認する必要があることです。 「ジオプロセシング > ジオプロセシング オプション」の下の「ディスプレイへのジオプロセシング操作の表示」がオンになっています。

于 2013-03-18T22:57:41.730 に答える
0

次の 2 つの重要な行が欠落している可能性があります。

arcpy.RefreshActiveView()
arcpy.RefreshTOC()
于 2013-03-27T20:36:57.833 に答える