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

查看:33
本文介绍了如何通过 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"是要添加的shape文件的路径.

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天全站免登陆