![](/img/trans.png)
[英]How do I serialize msiexec.exe installation using multiprocessing or any other module via python scripting?
[英]How do I add a shapefile in ArcGIS via python scripting?
我正在嘗試使用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
但是,我的原始數據始終是形狀文件,因此我需要能夠打開它們。 (等效地:在不打開形狀文件的情況下將其轉換為圖層文件,但我不希望這樣做)。
變量“ 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
最近,我為類似的任務而苦苦掙扎,最初使用了識別地圖文檔,識別數據框,創建圖層並將該圖層添加到地圖文檔中的方法。 有趣的是,只要在當前地圖文檔中調用以下命令,就可以使用以下方法完成所有操作。
# 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方法訪問結果對象的輸出,但是也可以使用您感興趣的result屬性的索引來訪問它,在這種情況下,輸出位於索引0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.