1. 程式人生 > >使用python讀取txt坐標文件生成挖空礦山_探礦批量

使用python讀取txt坐標文件生成挖空礦山_探礦批量

http height 1.7 txt 空行 工作空間 ise 是否 mage

# -*-coding:utf-8-*- import arcpy import fileinput import os # 探礦權坐標格式舉例 # 111.0846,31.1530 # 111.1002,31.1530 # 111.1002,31.1500 # 111.1027,31.1500 # 111.1027,31.1344 # 111.1127,31.1344 # 111.1127,31.1300 # 111.1157,31.1300 # 111.1157,31.1225 # 111.0942,31.1225 # 111.0942,31.1400 # 111.0927,31.1400 # 111.0927,31.1500 # 111.0846,31.1500 # 0,0 # 生成的shp圖形存放目錄 arcpy.env.workspace = r"F:\shp" fc = "tk.shp" # 如果工作空間下不存在該FeatureClass,那麽新建FeatureClass isexist = arcpy.Exists(fc) if not isexist: print fc + " 要素類不存在!" exit() # 創建插入遊標,txt坐標文件名稱就是項目名稱 # cursor = arcpy.da.InsertCursor(fc, ["XMMC", "DKID", "DKMC", "MJ", "SHAPE@"]) cursor = arcpy.da.InsertCursor(fc, ["XMMC", "SHAPE@"]) # 遍歷所有坐標文件 dirpath = r"F:\2018tk\\" 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() # 坐標點的編號 bh = 0 # 遍歷單個txt坐標文件的坐標值 print txtname.decode("gbk") while rowstr: # 如果出現空行,則直接跳過 if rowstr.strip() == "" or rowstr.strip() == r"\n": rowstr = txt.readline() continue fieldlist = rowstr.split(",") # 如果以0,0或者-1,0開頭,標明該行是一個礦山(外圈或者內圈)結束 if rowstr.replace("\n", "") == "0,0": _xytemppolygon = arcpy.Polygon(_tempxylist) _xypolylist.append(_xytemppolygon) _tempxylist.removeAll() print rowstr.replace("\n", "") # 一行末尾有換行符 rowstr = txt.readline() bh = 0 continue # 挖空礦山的標識是-1 elif rowstr.replace("\n", "") == "-1,0": _wktemppolygon = arcpy.Polygon(_tempxylist) _wkpolylist.append(_wktemppolygon) _tempxylist.removeAll() print rowstr.replace("\n", "") # 一行末尾有換行符 rowstr = txt.readline() bh = 0 continue # 讀取坐標值 pnt = arcpy.Point() # 依次為坐標點編號、縱坐標、橫坐標 bh = bh + 1 x = fieldlist[0].format("000.0000") y = fieldlist[1].replace("\n", "").format("00.0000") degrex = x[0:3] minix = x[4:6] secdx = x[6:8] degrey = y[0:2] miniy = y[3:5] secdy = y[5:7] _x = ("%.3f" % (float(degrex) + float(minix) / 60 + float(secdx) / 3600)) _y = ("%.3f" % (float(degrey) + float(miniy) / 60 + float(secdy) / 3600)) pnt.ID = bh pnt.X = _x pnt.Y = _y _tempxylist.append(pnt) print "第{0}個坐標是:{1},{2}".format(bh, _x, _y) 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]) print txtname.decode("gbk") + " is finished!" continue # 對於多個外圈礦山,1個或者多個挖空礦山,無法判斷對哪個外圈礦山挖空 if xypolynum > 1 and wkpolynum >= 1: print (txtname + ",無法判斷挖空礦山!") continue # 遍歷形成外圈地塊 for i in range(xypolynum): cursor.insertRow([txtname, _xypolylist[i]]) print txtname.decode("gbk") + " is finished!" except Exception as err: # print (err.args[0]).decode("gbk") print "請檢查坐標格式是否正確,或者坐標存在自相交!" else: print "全部生成!" del cursor 效果如下: 技術分享圖片
測試數據如下: 111.1500,31.2537 111.1627,31.2617 111.1812,31.2301 111.1605,31.2300 0,0 111.1529,31.2501 111.1601,31.2501 111.1558,31.2530 111.1525,31.2526 -1,0 111.1648,31.2313 111.1738,31.2310 111.1735,31.2335 111.1648,31.2338 -1,0

使用python讀取txt坐標文件生成挖空礦山_探礦批量