使用python讀取txt坐標文件生成挖空礦山_采礦批量
阿新 • • 發佈:2019-01-28
執行 Coding app 檢查 bubuko 圈地 lac temp 全部 # -*-coding:utf-8-*-
import arcpy
import fileinput
import os
# 采礦權坐標格式舉例
# 1,3509447.04,37493933.70
# 2,3509447.05,37495583.71
# 3,3508597.04,37495583.72
# 4,3508597.04,37494783.71
# 5,3508336.97,37494583.71
# 6,3508247.03,37493933.70
# *,1300,-400,,1
#生成的shp圖形存放目錄
arcpy.env.workspace = r"F:\shp"
fc = "ck.shp"
# 如果工作空間下不存在該FeatureClass,那麽新建FeatureClass
isexist = arcpy.Exists(fc)
if not isexist:
print fc + " 要素類不存在!"
# 創建插入遊標,txt坐標文件名稱就是項目名稱
# cursor = arcpy.da.InsertCursor(fc, ["XMMC", "DKID", "DKMC", "MJ", "SHAPE@"])
cursor = arcpy.da.InsertCursor(fc, ["XMMC", "SHAPE@"])
# 遍歷所有坐標文件
dirpath = r"F:\ck\\"
txtlist = os.listdir(dirpath)
txtname = ""
try:
# 遍歷文件夾目錄下所有的txt文件
for txtpath in txtlist:
txtname = txtpath.split(".")[0]
txt = fileinput.input(dirpath + txtpath)
rowstr = txt.readline()
fieldlist = []
# 外圈坐標,即外圈礦山
_xypolylist = []
# 挖空坐標,即挖空礦山
_wkpolylist = []
# 臨時坐標
_tempxylist = arcpy.Array()
# 遍歷單個txt坐標文件的坐標值
while rowstr:
fieldlist = rowstr.split(",")
fieldcount = len(fieldlist)
# 跳過txt坐標文件前面部門的屬性描述
if fieldcount == 5:
isend = fieldlist[0]
iswk = fieldlist[4].replace("\n", "")
# 如果以*開頭,標明該行是一個礦山(外圈或者內圈)結束
if isend == r"*" and (iswk == "1" or iswk == "0" or iswk == ""):
_xytemppolygon = arcpy.Polygon(_tempxylist)
_xypolylist.append(_xytemppolygon)
_tempxylist.removeAll()
print rowstr.replace("\n", "") # 一行末尾有換行符
rowstr = txt.readline()
continue
# 挖空礦山的標識是-1
elif isend == r"*" and iswk == "-1":
_wktemppolygon = arcpy.Polygon(_tempxylist)
_wkpolylist.append(_wktemppolygon)
_tempxylist.removeAll()
print rowstr.replace("\n", "") # 一行末尾有換行符
rowstr = txt.readline()
continue
# 讀取坐標值
pnt = arcpy.Point()
# 依次為坐標點編號、縱坐標、橫坐標
bh = fieldlist[0]
x = fieldlist[2].replace("\n", "")
y = fieldlist[1]
pnt.ID = bh
pnt.X = x
pnt.Y = y
_tempxylist.append(pnt)
print "第{0}個坐標是:{1},{2}".format(bh, y, x)
rowstr = txt.readline()
xypolynum = len(_xypolylist)
wkpolynum = len(_wkpolylist)
# 如果外圈礦山只有1個,挖空礦山1個或者多個,則執行裁剪,即交集取反
if xypolynum == 1 and wkpolynum >= 1:
poly = _xypolylist[0]
for j in range(wkpolynum):
poly = poly.symmetricDifference(_wkpolylist[j])
cursor.insertRow([txtname, poly])
continue
# 對於多個外圈礦山,1個或者多個挖空礦山,無法判斷對哪個外圈礦山挖空
if xypolynum > 1 and wkpolynum >= 1:
print txtpath.decode("gbk") + ",無法判斷挖空礦山!"
continue
# 遍歷形成外圈地塊
for i in range(xypolynum):
cursor.insertRow([txtname, _xypolylist[i]])
print txtpath.decode("gbk") + " is finished!"
except Exception as err:
# print (err.args[0]).decode("gbk")
print "請檢查坐標格式是否正確,或者坐標存在自相交!"
else:
print "全部生成!"
del cursor
效果圖如下:
測試數據:
1,3271945.18,38561194.99
2,3272376.19,38562246.99
3,3271873.19,38562717.99
4,3271443.19,38561634.99
*,400,0,,1
5,3271928.18,38561662.99
6,3271928.18,38561782.99
7,3271808.18,38561783.00
8,3271808.18,38561662.99
*,400,0,,-1
使用python讀取txt坐標文件生成挖空礦山_采礦批量