如何通过 python 脚本在 ArcGIS 中添加 shapefile?

发布于 2024-09-29 13:43:49 字数 549 浏览 6 评论 0原文

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

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

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

但是,我的原始数据始终是形状文件,所以我需要能够打开它们。 (同样:将形状文件转换为图层文件而不打开它,但我不想这样做)。

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).

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素染倾城色 2024-10-06 13:43:49

变量“theShape”是要添加的形状文件的路径。

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

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
無處可尋 2024-10-06 13:43:49

最近我在处理类似的任务,最初使用的是识别地图文档、识别数据框、创建图层并将图层添加到地图文档的方法。有趣的是,这一切都可以使用以下命令来完成,前提是从当前地图文档中调用它。

# import modules
import arcpy

# create layer in TOC and reference it in a variable for possible other actions
newLyr = arcpy.MakeFeatureLayer_managment(
    in_features, 
    out_layer
)[0]

创建要素图层需要两个输入,输入特征和输出层。输入要素可以是任何类型的要素类或图层。这包括 shapefile。输出图层是出现在内容列表中的图层的名称。

此外,创建要素图层可以接受 where 子句以在创建时创建定义查询。当需要快速创建大量具有不同定义查询的图层时,这通常是我实现它的方式。

最后,在上面的代码片段中,虽然没有必要,但我演示了如何使用工具输出的结果填充变量,以便可以使用 arcpy.mapping 在内容列表中操作图层(如果稍后在脚本中需要的话) 。每个工具都会返回一个结果对象。结果对象输出可以使用 getOutput 方法访问,但也可以使用您感兴趣的结果属性的索引访问,在本例中输出位于索引 0。

Recently I struggled with a similar task, and initially used the method of identifying the map document, identifying the data frame, creating a layer and adding the layer to the map document. Interestingly enough, this can all be accomplished using the following provided it is called from within the current map document.

# import modules
import arcpy

# create layer in TOC and reference it in a variable for possible other actions
newLyr = arcpy.MakeFeatureLayer_managment(
    in_features, 
    out_layer
)[0]

Make Feature Layer requires two inputs, the input features and the output layer. The input features can be any type of feature class or layer. This includes shapefiles. Output layer is the name of the layer to appear in the table of contents.

Also, Make Feature Layer can accept a where clause to create a definition query at creation time. This typically is how I implement it, when needing to create a lot of layers with different definition queries quickly.

Finally, in the above snippet, although it is not necessary, I demonstrated how to populate a variable with the result of the tool output so the layer could be manipulated in the table of contents using arcpy.mapping if this is necessary later in the script. Every tool returns a result object. The result object output can be accessed using the getOutput method, but it can also be accessed by using the index of the result property you are interested in, in this case the output located at index 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文