1. 程式人生 > 其它 >ArcPy按屬性批量匯出CAD檔案

ArcPy按屬性批量匯出CAD檔案

工作:批量匯出戶型圖及標註到CAD

GIS版本:10.2

原本構建了批量匯出模型,但因資料量大,每次執行1000餘就卡住了 最終想起還是自己寫更靠譜,執行速度也更快一些。

程式部分:

# -*- coding: utf-8 -*-

# Import arcpy module
import arcpy

aa=0
zrz_RH="E:/大英戶型圖/資料庫/資料庫處理中.gdb/戶型融合/ZRZ_2層融合"
zrz_line="E:/大英戶型圖/資料庫/資料庫處理中.gdb/戶型line/ZRZ_2層line"
zrz_lineA="E:/大英戶型圖/資料庫/資料庫處理中.gdb/戶型line/ZRZ_2層line註記"

#建立要素圖層,按屬性選擇的前提必須是圖層或檢視,故先建立圖層


arcpy.MakeFeatureLayer_management(zrz_line, "zrz_line_ly")
arcpy.MakeFeatureLayer_management(zrz_lineA, "zrz_lineA_ly")

path_DC="E:/大英戶型圖/GIS匯出/層2/"
cursor=arcpy.da.SearchCursor(zrz_RH,["BDCDYH"])
for row in cursor:

aa=aa+1
if aa%1000==1:
print "正在處理條數:"+str(aa)

# Process: 按屬性選擇圖層
arcpy.SelectLayerByAttribute_management("zrz_line_ly", "NEW_SELECTION", "BDCDYH ='"+str(row[0])+"'")


arcpy.SelectLayerByAttribute_management("zrz_lineA_ly", "NEW_SELECTION", "BDCDY ='"+str(row[0])+"'")


#匯出CAD
arcpy.ExportCAD_conversion("zrz_line_ly;zrz_lineA_ly", "DWG_R2004", path_DC+str(row[0])+".dwg", "Ignore_Filenames_in_Tables", "Overwrite_Existing_Files", "")

del row
del cursor

以上程式能完美執行,但資料量過大(我的資料分兩部分,一部分50000條,另一部分250000條),查詢速度變的很慢,預計2天,故後期建模型先將資料按欄位批量分開,再改進如下:

# -*- coding: utf-8 -*-

# Import arcpy module
import arcpy
import sys

aa=0
path_DC="D:/daying/DC/"
zrz_DJ="D:/daying/ZRZ1.gdb/zrzall/ZRZ_1RHok"
cursor_DJ=arcpy.da.SearchCursor(zrz_DJ,["DJZQ"],'"OBJECTID"<11 and "OBJECTID">0')
for row_DJ in cursor_DJ:
print "正在處理地籍子區:"+str(row_DJ[0])
zrz_RH="D:/daying/ZRZ1.gdb/ZRZ_RH/H"+str(row_DJ[0])
zrz_line="D:/daying/ZRZ1.gdb/ZRZline/L"+str(row_DJ[0])
zrz_lineA="D:/daying/ZRZ1.gdb/ZRZzj/T"+str(row_DJ[0])


#建立要素圖層
arcpy.MakeFeatureLayer_management(zrz_line, "zrzline"+str(row_DJ[0]) )
arcpy.MakeFeatureLayer_management(zrz_lineA, "zrzzj"+str(row_DJ[0]) )

cursor=arcpy.da.SearchCursor(zrz_RH,["BDCDYH"])
for row in cursor:

aa=aa+1
if aa%100==1:
print "正在處理條數:"+str(aa)

# Process: 按屬性選擇圖層
arcpy.SelectLayerByAttribute_management("zrzline"+str(row_DJ[0]), "NEW_SELECTION", "BDCDYH ='"+str(row[0])+"'")
arcpy.SelectLayerByAttribute_management("zrzzj"+str(row_DJ[0]), "NEW_SELECTION", "BDCDY ='"+str(row[0])+"'")


#匯出CAD
arcpy.ExportCAD_conversion("zrzline"+str(row_DJ[0])+";zrzzj"+str(row_DJ[0]), "DWG_R2004", path_DC+str(row[0])+".dwg", "Ignore_Filenames_in_Tables", "Overwrite_Existing_Files", "")
del row
del cursor

print "處理完成!"
del row_DJ
del cursor_DJ

經以上修改,速度提升4倍以上!