批量生成采矿_挖空矿区_xls字符串

# -*-coding:utf-8-*-
import arcpy
import xlrd
import re
 
# 采矿权坐标格式举例
# 1,4,1,4145796.42,37500324.70,2,4145822.42,37500429.70,3,4145717.42,37500429.70,4,4145713.42,37500349.70,1167,1090,,1,
#2,4,1,3950410.72,19466190.96,2,3950212.72,19466470.96,3,3950144.72,19466415.96,4,3950344.72,19466140.96,750,550,,1,4,1,3949944.71,19466065.96,2,3949804.71,19466265.96,3,3949254.71,19465995.96,4,3949304.71,19465715.95,750,550,,1,
 
# 读取xls文件
xlsfile = xlrd.open_workbook(r"E:\ck.xls", "r")
mysheet1 = xlsfile.sheet_by_name("ck")
rownum = mysheet1.nrows
colnum = mysheet1.ncols
 
# 生成的shp图形存放目录
arcpy.env.workspace = r"E:\shp"
 
# 2000坐标系
fc_2000_19 = "ck2000_19.shp"
fc_2000_37 = "ck2000_37.shp"
fc_2000_38 = "ck2000_38.shp"
 
# 80坐标系
fc_80_19 = "ck80_19.shp"
fc_80_37 = "ck80_37.shp"
fc_80_38 = "ck80_38.shp"
 
# 54坐标系
fc_54_19 = "ck54_19.shp"
fc_54_37 = "ck54_37.shp"
fc_54_38 = "ck54_38.shp"
 
# 未知坐标系
fc_unknown_19 = "unknown_19.shp"
fc_unknown_37 = "unknown_37.shp"
fc_unknown_38 = "unknown_38.shp"
 
# 检查工作空间下是否存在2000坐标系的要素类
isexist_fc_2000_19 = arcpy.Exists(fc_2000_19)
isexist_fc_2000_37 = arcpy.Exists(fc_2000_37)
isexist_fc_2000_38 = arcpy.Exists(fc_2000_38)
 
# 检查工作空间下是否存在80坐标系的要素类
isexist_fc_80_19 = arcpy.Exists(fc_80_19)
isexist_fc_80_37 = arcpy.Exists(fc_80_37)
isexist_fc_80_38 = arcpy.Exists(fc_80_38)
 
# 检查工作空间下是否存在54坐标系的要素类
isexist_fc_54_19 = arcpy.Exists(fc_54_19)
isexist_fc_54_37 = arcpy.Exists(fc_54_37)
isexist_fc_54_38 = arcpy.Exists(fc_54_38)
 
# 检查工作空间下是否存在未知坐标系的要素类
isexist_fc_unknown_19 = arcpy.Exists(fc_unknown_19)
isexist_fc_unknown_37 = arcpy.Exists(fc_unknown_37)
isexist_fc_unknown_38 = arcpy.Exists(fc_unknown_38)
 
# 记录没有匹配的记录
str_notmatch = ""
 
if not (
        isexist_fc_2000_19 and isexist_fc_2000_37 and isexist_fc_2000_38 and isexist_fc_80_19 and isexist_fc_80_37 and isexist_fc_80_38 and isexist_fc_54_19 and isexist_fc_54_37 and isexist_fc_54_38 and isexist_fc_unknown_19 and isexist_fc_unknown_37 and isexist_fc_unknown_38):
    print "要素类不存在!"
    exit()
 
# 创建2000坐标系各要素类的插入游标,许可证号xkzh,名称name,区域坐标coord
cursor_2000_19 = arcpy.da.InsertCursor(fc_2000_19, ["xkzh", "name", "coord", "SHAPE@"])
cursor_2000_37 = arcpy.da.InsertCursor(fc_2000_37, ["xkzh", "name", "coord", "SHAPE@"])
cursor_2000_38 = arcpy.da.InsertCursor(fc_2000_38, ["xkzh", "name", "coord", "SHAPE@"])
 
# 创建80坐标系各要素类的插入游标,许可证号xkzh,名称name,区域坐标coord
cursor_80_19 = arcpy.da.InsertCursor(fc_80_19, ["xkzh", "name", "coord", "SHAPE@"])
cursor_80_37 = arcpy.da.InsertCursor(fc_80_37, ["xkzh", "name", "coord", "SHAPE@"])
cursor_80_38 = arcpy.da.InsertCursor(fc_80_38, ["xkzh", "name", "coord", "SHAPE@"])
 
# 创建54坐标系各要素类的插入游标,许可证号xkzh,名称name,区域坐标coord
cursor_54_19 = arcpy.da.InsertCursor(fc_54_19, ["xkzh", "name", "coord", "SHAPE@"])
cursor_54_37 = arcpy.da.InsertCursor(fc_54_37, ["xkzh", "name", "coord", "SHAPE@"])
cursor_54_38 = arcpy.da.InsertCursor(fc_54_38, ["xkzh", "name", "coord", "SHAPE@"])
 
# 创建未知坐标系各要素类的插入游标,许可证号xkzh,名称name,区域坐标coord
cursor_unknown_19 = arcpy.da.InsertCursor(fc_unknown_19, ["xkzh", "name", "coord", "SHAPE@"])
cursor_unknown_37 = arcpy.da.InsertCursor(fc_unknown_37, ["xkzh", "name", "coord", "SHAPE@"])
cursor_unknown_38 = arcpy.da.InsertCursor(fc_unknown_38, ["xkzh", "name", "coord", "SHAPE@"])
 
try:
    # 遍历xls文件中所有行
    for i in range(rownum):
        row = mysheet1.row_values(i)
 
        # 读取基本信息:许可证号、名称、坐标系类型、区域坐标,这些基本信息主要根据xls文件来定
        xkzh = row[0]
        name = row[1]
        xytype = str(row[2])
        coord = row[3]
        # print (name + ",is beginging...")
 
        # 调试代码用
        if i == -1:
            print "!!!"
 
        # 外圈坐标,即外圈矿山
        _xypolylist = []
        # 挖空坐标,即挖空矿山
        _wkpolylist = []
        # 临时坐标
        _tempxylist = arcpy.Array()
        # 坐标点的编号
        bh = 0
        # 投影坐标的带号
        dh = 0
 
        # 获取区块数num_qk,字符串第1个逗号前的数字表示区块数
        index_qk = coord.find(",")
        num_qk = int(coord[:index_qk])
 
        # 除过区块数,剩余的字符串
        coordlist = coord[index_qk + 1:]
 
        # 根据正则表达式,获取各区块结束的字符串标识       
        pattern = re.compile(
            r"([-]{0,1}[0-9]{1,4}[\.]{0,1}[0-9]{0,3}[,][-]{0,1}[0-9]{1,4}[\.]{0,1}[0-9]{0,3}[,][.]{0,}[\d]{0,}[\u4E00-\u9FFF]{0,}[,][-]{0,1}[0-1],)",
            re.U)
        reglist = re.findall(pattern, coordlist)       
 
        # 如果没有匹配出坐标结束标识,则做出记录
        num_reglist = len(reglist)
        if num_reglist == 0:
            # print name + " is not matching..."
            print xkzh
            str_notmatch = r"'" + xkzh + "'," + str_notmatch
            continue
 
        # 如果匹配出的坐标结束标识的数量与区块数不一致,则做出记录
        if num_reglist != num_qk:
            # print name + " qks is not equal ..."
            print xkzh
            str_notmatch = r"'" + xkzh + "'," + str_notmatch
            continue
 
        for j in range(num_qk):
 
            # 获得区块结束的字符串标识
            str_reg = str(reglist[j])
 
            # 如果匹配出的标识符不是4个,则说明匹配错了
            num_dot = str_reg.count(",")
            if num_dot != 4:
                break
 
            # 获得索引值index_regend
            index_regend = coordlist.find(str_reg)
            qk_coordlist = coordlist[:index_regend]
 
            # 获取坐标点数num_coord,字符串中第1个逗号前的数值表示坐标点数。(已经去掉了区块数)
            index_coord = qk_coordlist.find(",")
            num_coord = int(qk_coordlist[:index_coord])
 
            # 获取是否挖空的标识
            wk_info = str_reg.split(",")
            wk_mark = wk_info[3]
 
            # 获得坐标点
            qk_coordlist = qk_coordlist[index_coord + 1:]
            xylist = qk_coordlist.split(",")
 
            # 编号、投影带号、横坐标、纵坐标
            bh = 0
            dh = 0
            _x = 0
            _y = 0
 
            for k in range(num_coord * 3):
 
                # 坐标点标识,纵坐标(7位整数)、横坐标(8位整数)
                if k % 3 == 1:
 
                    # 检查纵坐标是否满足7位
                    y = xylist[k].format("0000000.###")
                    yindex_dot = y.find(".")
                    if yindex_dot == -1:
                        if len(y) != 7:
                            print xkzh + " ylength is not enough..."
                            break
                    else:
                        if len(y[:yindex_dot]) != 7:
                            print xkzh + " ylength is not enough..."
                            break
                    _y = float(y)
 
                elif k % 3 == 2:
 
                    # 检查横坐标是否满足8位
                    x = xylist[k].format("00000000.###")
                    xindex_dot = x.find(".")
                    if xindex_dot == -1:
                        if len(x) != 8:
                            print xkzh + " xlength is not enough..."
                            break
                    else:
                        if len(x[:xindex_dot]) != 8:
                            print xkzh + " xlength is not enough..."
                            break
                    _x = float(x)
 
                    # 获取投影带号
                    dh = str(x[:2])
 
                    # 读取坐标值
                    pnt = arcpy.Point()
                    # 依次为坐标点编号、纵坐标、横坐标
                    bh = bh + 1
                    pnt.ID = bh
                    pnt.X = _x
                    pnt.Y = _y
                    _tempxylist.append(pnt)
                else:
                    continue
 
            # 如果坐标点数为0,则说明该坐标有问题(不满足前7后8),继续一个矿权
            if len(_tempxylist) == 0:
                break
 
            # 根据挖空标识不同,将外圈、内圈坐标存放在不同的列表中
            _xytemppolygon = arcpy.Polygon(_tempxylist)
            if wk_mark == "1" or wk_mark == "0":
                _xypolylist.append(_xytemppolygon)
            elif wk_mark == "-1":
                _wkpolylist.append(_xytemppolygon)
            else:
                print name + "is error..."
 
            # 清楚临时列表
            _tempxylist.removeAll()
 
            # 剩余的坐标串
            len_reg = len(str_reg)
            coordlist = coordlist[index_regend + len_reg:]
 
        # 获取外圈的数量,如果等于0则继续下一个循环
        xypolynum = len(_xypolylist)
        if xypolynum == 0:
            continue
 
        wkpolynum = len(_wkpolylist)
        # 如果外圈矿山只有1个,挖空矿山1个或者多个,则执行裁剪,即交集取反
        if xypolynum == 1 and wkpolynum >= 1:
            poly = _xypolylist[0]
            for p in range(wkpolynum):
                poly = poly.symmetricDifference(_wkpolylist[p])
 
            # 2000坐标系
            if xytype == "3":
                if dh == "37":
                    cursor_2000_37.insertRow([xkzh, name, coord, poly])
                elif dh == "38":
                    cursor_2000_38.insertRow([xkzh, name, coord, poly])
                elif dh == "19":
                    cursor_2000_19.insertRow([xkzh, name, coord, poly])
                else:
                    print xkzh + " dh is error..."
 
            # 80坐标
            elif xytype == "2":
                if dh == "37":
                    cursor_80_37.insertRow([xkzh, name, coord, poly])
                elif dh == "38":
                    cursor_80_38.insertRow([xkzh, name, coord, poly])
                elif dh == "19":
                    cursor_80_19.insertRow([xkzh, name, coord, poly])
                else:
                    print xkzh + " dh is error..."
 
            # 54坐标系
            elif xytype == "1":
                if dh == "37":
                    cursor_54_37.insertRow([xkzh, name, coord, poly])
                elif dh == "38":
                    cursor_54_38.insertRow([xkzh, name, coord, poly])
                elif dh == "19":
                    cursor_54_19.insertRow([xkzh, name, coord, poly])
                else:
                    print xkzh + " dh is error..."
 
            # 坐标系未知
            else:
                if dh == "37":
                    cursor_unknown_37.insertRow([xkzh, name, coord, poly])
                elif dh == "38":
                    cursor_unknown_38.insertRow([xkzh, name, coord, poly])
                elif dh == "19":
                    cursor_unknown_19.insertRow([xkzh, name, coord, poly])
                else:
                    print xkzh + " dh is error..."
            # print name + " is finished!"
            continue
 
        # 对于多个外圈矿山,1个或者多个挖空矿山,无法判断对哪个外圈矿山挖空
        if xypolynum > 1 and wkpolynum >= 1:
            print (name + ",无法判断挖空矿山!")
            continue
 
        # 遍历形成外圈地块,一个矿山无论是否存在多个区块,只能是一种坐标系
        for q in range(xypolynum):
 
            # 2000坐标系
            if xytype == "3":
                if dh == "37":
                    cursor_2000_37.insertRow([xkzh, name, coord, _xypolylist[q]])
                elif dh == "38":
                    cursor_2000_38.insertRow([xkzh, name, coord, _xypolylist[q]])
                elif dh == "19":
                    cursor_2000_19.insertRow([xkzh, name, coord, _xypolylist[q]])
                else:
                    print xkzh + " dh is error..."
 
            # 80坐标
            elif xytype == "2":
                if dh == "37":
                    cursor_80_37.insertRow([xkzh, name, coord, _xypolylist[q]])
                elif dh == "38":
                    cursor_80_38.insertRow([xkzh, name, coord, _xypolylist[q]])
                elif dh == "19":
                    cursor_80_19.insertRow([xkzh, name, coord, _xypolylist[q]])
                else:
                    print xkzh + " dh is error..."
 
            # 54坐标系
            elif xytype == "1":
                if dh == "37":
                    cursor_54_37.insertRow([xkzh, name, coord, _xypolylist[q]])
                elif dh == "38":
                    cursor_54_38.insertRow([xkzh, name, coord, _xypolylist[q]])
                elif dh == "19":
                    cursor_54_19.insertRow([xkzh, name, coord, _xypolylist[q]])
                else:
                    print xkzh + " dh is error..."
 
            # 坐标系未知
            else:
                if dh == "37":
                    cursor_unknown_37.insertRow([xkzh, name, coord, _xypolylist[q]])
                elif dh == "38":
                    cursor_unknown_38.insertRow([xkzh, name, coord, _xypolylist[q]])
                elif dh == "19":
                    cursor_unknown_19.insertRow([xkzh, name, coord, _xypolylist[q]])
                else:
                    print xkzh + " dh is error..."
 
        # print "It is finished!"
except Exception as err:
    # print (err.args[0]).decode("gbk")
    print err.message
else:
    print "全部完成!"
    print str_notmatch
    del cursor_2000_37
    del cursor_2000_38
    del cursor_2000_19
    del cursor_80_37
    del cursor_80_38
    del cursor_80_19
    del cursor_54_37
    del cursor_54_38
    del cursor_54_19
    del cursor_unknown_37
    del cursor_unknown_38
    del cursor_unknown_19
 
posted @ 2019-09-10 11:21  kkqq8860928  阅读(303)  评论(0编辑  收藏  举报