使用python读取txt坐标文件生成挖空矿山_探矿批量
生活随笔
收集整理的這篇文章主要介紹了
使用python读取txt坐标文件生成挖空矿山_探矿批量
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
# -*-coding:utf-8-*- import arcpy import fileinput import os # 探礦權(quán)坐標(biāo)格式舉例 # 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() # 創(chuàng)建插入游標(biāo),txt坐標(biāo)文件名稱就是項(xiàng)目名稱 # cursor = arcpy.da.InsertCursor(fc, ["XMMC", "DKID", "DKMC", "MJ", "SHAPE@"]) cursor = arcpy.da.InsertCursor(fc, ["XMMC", "SHAPE@"]) # 遍歷所有坐標(biāo)文件 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 = [] # 外圈坐標(biāo),即外圈礦山 _xypolylist = [] # 挖空坐標(biāo),即挖空礦山 _wkpolylist = [] # 臨時(shí)坐標(biāo) _tempxylist = arcpy.Array() # 坐標(biāo)點(diǎn)的編號(hào) bh = 0 # 遍歷單個(gè)txt坐標(biāo)文件的坐標(biāo)值 print txtname.decode("gbk") while rowstr: # 如果出現(xiàn)空行,則直接跳過(guò) if rowstr.strip() == "" or rowstr.strip() == r"\n": rowstr = txt.readline() continue fieldlist = rowstr.split(",") # 如果以0,0或者-1,0開(kāi)頭,標(biāo)明該行是一個(gè)礦山(外圈或者內(nèi)圈)結(jié)束 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 # 挖空礦山的標(biāo)識(shí)是-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 # 讀取坐標(biāo)值 pnt = arcpy.Point() # 依次為坐標(biāo)點(diǎn)編號(hào)、縱坐標(biāo)、橫坐標(biāo) 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}個(gè)坐標(biāo)是:{1},{2}".format(bh, _x, _y) rowstr = txt.readline() xypolynum = len(_xypolylist) wkpolynum = len(_wkpolylist) # 如果外圈礦山只有1個(gè),挖空礦山1個(gè)或者多個(gè),則執(zhí)行裁剪,即交集取反 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 # 對(duì)于多個(gè)外圈礦山,1個(gè)或者多個(gè)挖空礦山,無(wú)法判斷對(duì)哪個(gè)外圈礦山挖空 if xypolynum > 1 and wkpolynum >= 1: print (txtname + ",無(wú)法判斷挖空礦山!") 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 "請(qǐng)檢查坐標(biāo)格式是否正確,或者坐標(biāo)存在自相交!" else: print "全部生成!" del cursor 效果如下: 測(cè)試數(shù)據(jù)如下: 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
轉(zhuǎn)載于:https://www.cnblogs.com/apromise/p/10330757.html
總結(jié)
以上是生活随笔為你收集整理的使用python读取txt坐标文件生成挖空矿山_探矿批量的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: innobackupex 数据库备份
- 下一篇: Linux CentOS 7 防火墙/端