使用python读取txt坐标文件生成挖空矿山_探矿批量

# -*-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
posted @ 2019-01-28 16:31  kkqq8860928  阅读(565)  评论(1编辑  收藏  举报