如何通过python脚本在ArcGIS中添加shapefile? [英] How do I add a shapefile in ArcGIS via python scripting?

查看:284
本文介绍了如何通过python脚本在ArcGIS中添加shapefile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python自动执行ArcGIS Desktop中的各种任务(通常使用ArcMap),并且一直需要一种将形状文件添加到当前地图的方法. (然后做一些事情,但这是另一回事了.)

I am trying to automate various tasks in ArcGIS Desktop (using ArcMap generally) with Python, and I keep needing a way to add a shape file to the current map. (And then do stuff to it, but that's another story).

到目前为止,我能做的最好的就是使用以下命令将 layer 文件添加到当前地图("addLayer"是图层文件对象):

The best I can do so far is to add a layer file to the current map, using the following ("addLayer" is a layer file object):

def AddLayerFromLayerFile(addLayer):
 import arcpy
 mxd = arcpy.mapping.MapDocument("CURRENT")
 df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
 arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
 arcpy.RefreshActiveView()
 arcpy.RefreshTOC()
 del mxd, df, addLayer

但是,我的原始数据始终是形状文件,因此我需要能够打开它们. (等效地:在不打开形状文件的情况下将其转换为图层文件,但我不希望这样做).

However, my raw data is always going be shape files, so I need to be able to open them. (Equivantly: convert a shape file to a layer file wiothout opening it, but I'd prefer not to do that).

推荐答案

变量"theShape"是要添加的形状文件的路径.

Variable "theShape" is the path of the shape file to be added.

import arcpy
import arcpy.mapping
# get the map document 
mxd = arcpy.mapping.MapDocument("CURRENT")  

# get the data frame 
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]  

# create a new layer 
newlayer = arcpy.mapping.Layer(theShape)  

# add the layer to the map at the bottom of the TOC in data frame 0 
arcpy.mapping.AddLayer(df, newlayer,"BOTTOM")

# Refresh things
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd, df, newlayer

这篇关于如何通过python脚本在ArcGIS中添加shapefile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆