import re import math import json import arcpy import os #基准点txt,位移list,夸张系数,输出txt(?),输出文件夹 def drawline(shppath,linelist,ran1,outpath,txtpath,ran22): ran0 = float(ran1) ran2 = float(ran22) mm = 0 nameA = '' #结果点(?) resultp = [] #坐标系文件 prjpath = r'D:\4work_now\20240226TZL\CGCS2000_3_Degree_GK_Zone_34.prj' prjpath1 = r'D:\4work_now\20240226TZL\GCS_WGS_1984.prj' #大概是先把计算结果写上 with open(outpath,'w') as files: #循环列表 while mm < len(linelist): kk = 0 #读基准点文件 with open(shppath,'r') as filesA: for ppstr1 in filesA: #基准点的信息 pname = ppstr1.split(',',-1)[1] #这里加一点,识别角度值 angleA = readangle(pname) xP = float(ppstr1.split(',',-1)[2]) yP = float(ppstr1.split(',',-1)[3]) zP = float(ppstr1.split(',',-1)[4]) #读位移是否有和这个匹配的 str0 = linelist[mm] #把一些数据反过来写 nameA = str0.split(',',-1)[0] nameP = nameA.split('-',-1)[-1] if nameP == 'BPL': xA1= float(str0.split(',',-1)[2]) yA1= float(str0.split(',',-1)[1]) xA= float(str0.split(',',-1)[4]) yA= float(str0.split(',',-1)[3]) xA122= float(str0.split(',',-1)[2]) yA122= float(str0.split(',',-1)[1]) xA22= float(str0.split(',',-1)[4]) yA22= float(str0.split(',',-1)[3]) elif nameP == 'KHP': xA1= float(str0.split(',',-1)[1])* (-1) yA1= float(str0.split(',',-1)[2])* (-1) xA= float(str0.split(',',-1)[3])* (-1) yA= float(str0.split(',',-1)[4])* (-1) xA122= float(str0.split(',',-1)[1]) yA122= float(str0.split(',',-1)[2]) xA22= float(str0.split(',',-1)[3]) yA22= float(str0.split(',',-1)[4]) elif nameP == 'RFH': xA1= float(str0.split(',',-1)[1]) yA1= float(str0.split(',',-1)[2]) xA= float(str0.split(',',-1)[3]) yA= float(str0.split(',',-1)[4]) xA122= float(str0.split(',',-1)[1]) yA122= float(str0.split(',',-1)[2]) xA22= float(str0.split(',',-1)[3]) yA22= float(str0.split(',',-1)[4]) elif nameP == 'BPR': xA1= float(str0.split(',',-1)[2])*(-1) yA1= float(str0.split(',',-1)[1]) xA= float(str0.split(',',-1)[4])*(-1) yA= float(str0.split(',',-1)[3]) xA122= float(str0.split(',',-1)[2]) yA122= float(str0.split(',',-1)[1]) xA22= float(str0.split(',',-1)[4]) yA22= float(str0.split(',',-1)[3]) elif nameP == 'THP': xA1= float(str0.split(',',-1)[2])*(-1) yA1= float(str0.split(',',-1)[1]) xA= float(str0.split(',',-1)[4])*(-1) yA= float(str0.split(',',-1)[3]) xA122= float(str0.split(',',-1)[2]) yA122= float(str0.split(',',-1)[1]) xA22= float(str0.split(',',-1)[4]) yA22= float(str0.split(',',-1)[3]) else: xA1= float(str0.split(',',-1)[1]) yA1= float(str0.split(',',-1)[2]) xA= float(str0.split(',',-1)[3]) yA= float(str0.split(',',-1)[4]) xA122= float(str0.split(',',-1)[1]) yA122= float(str0.split(',',-1)[2]) xA22= float(str0.split(',',-1)[3]) yA22= float(str0.split(',',-1)[4]) if nameA == pname: #一些距离参数 xB = (xA * ran0) yB = (yA * ran0) xB1 = (xA1 * ran2) yB1 = (yA1 * ran2) #总距离 xA2 = xA * xA yA2 = yA * yA xA21 = xA1 * xA1 yA21 = yA1 * yA1 disA = math.sqrt(xA2 + yA2) disA1 = math.sqrt(xA21 + yA21) #这里分别对应x方向和y方向 xpx = xP - xB xpy = yP ypx = xP ypy = yB + yP xpx1 = xP - xB1 xpy1 = yP ypx1 = xP ypy1 = yB1 + yP #一同计算得到第二个点(大概) xangle = float(angleA) yangle = float(angleA) arrpointx = rotatecordiateX(xangle,xpx,xpy,xP,yP,xB) arrpointy = rotatecordiateY(yangle,ypx,ypy,xP,yP,yB) arrpointx1 = rotatecordiateX(xangle,xpx1,xpy1,xP,yP,xB1) arrpointy1 = rotatecordiateY(yangle,ypx1,ypy1,xP,yP,yB1) pppointx = float(arrpointx[0]) + float(arrpointy[0]) - xP pppointy = float(arrpointx[1]) + float(arrpointy[1]) - yP pppointx1 = float(arrpointx1[0]) + float(arrpointy1[0]) - xP pppointy1 = float(arrpointx1[1]) + float(arrpointy1[1]) - yP #写入 files.write(pname + "," + "px," + str(arrpointx[0]) + "," + str(arrpointx[1]) + "," + str(zP) + "," +str(xA22)+ "\n") files.write(pname + "," + "py," + str(arrpointy[0]) + "," + str(arrpointy[1]) + "," + str(zP) + "," +str(yA22)+ "\n") files.write(pname + "," + "pp," + str(pppointx) + "," + str(pppointy) + "," + str(zP) + ","+ str(disA) + "\n") files.write(pname + "," + "px1," + str(arrpointx1[0]) + "," + str(arrpointx1[1]) + "," + str(zP) + "," +str(xA122)+ "\n") files.write(pname + "," + "py1," + str(arrpointy1[0]) + "," + str(arrpointy1[1]) + "," + str(zP) + "," +str(yA122)+ "\n") files.write(pname + "," + "pp1," + str(pppointx1) + "," + str(pppointy1) + "," + str(zP) + ","+ str(disA1) + "\n") mm = mm + 1 kk = 1 break else: #意思是继续寻找和位移匹配的点 continue if kk == 0 : mm = mm + 1 # print(nameA) #最后输出 resultp.append(outpath) resultp.append(prjpath) resultp.append(prjpath1) resultp.append(txtpath) return resultp def rotatecordiateX(rang,xpx,xpy,xP,yP,zf): rx = float(xpx) ry = float(xpy) ox = float(xP) oy = float(yP) aa = 1 dis = math.sqrt((rx - ox)*(rx-ox) + (ry - oy)*(ry-oy)) if aa == 0: if zf > 0: if rang <= 90: angle = math.radians(rang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n = ox +x0 y0n = oy +y0 elif rang > 90 and rang <= 180: ang = 180 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n = ox-x0 y0n = oy+y0 elif rang > 180 and rang <=270: ang = rang - 180 angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n =ox-x0 y0n = oy-y0 else: ang = 360 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n = ox + x0 y0n = oy-y0 else: if rang <= 90: angle = math.radians(rang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n = ox - x0 y0n = oy -y0 elif rang > 90 and rang <= 180: ang = 180 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n = ox + x0 y0n = oy - y0 elif rang > 180 and rang <=270: ang = rang - 180 angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n =ox + x0 y0n = oy + y0 else: ang = 360 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n = ox - x0 y0n = oy + y0 else: if zf < 0: if rang <= 90: angle = math.radians(rang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n = ox +x0 y0n = oy +y0 elif rang > 90 and rang <= 180: ang = 180 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n = ox-x0 y0n = oy+y0 elif rang > 180 and rang <=270: ang = rang - 180 angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n =ox-x0 y0n = oy-y0 else: ang = 360 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n = ox + x0 y0n = oy-y0 else: if rang <= 90: angle = math.radians(rang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n = ox - x0 y0n = oy -y0 elif rang > 90 and rang <= 180: ang = 180 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n = ox + x0 y0n = oy - y0 elif rang > 180 and rang <=270: ang = rang - 180 angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n =ox + x0 y0n = oy + y0 else: ang = 360 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * cosA y0 = dis * sinA x0n = ox - x0 y0n = oy + y0 return [x0n,y0n] def rotatecordiateY(rang,xpx,xpy,xP,yP,zf): rx = float(xpx) ry = float(xpy) ox = float(xP) oy = float(yP) bb = 1 dis = math.sqrt((rx - ox)*(rx-ox) + (ry - oy)*(ry-oy)) if bb == 0: if zf > 0: if rang <= 90: angle = math.radians(rang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox+x0 y0n1 = oy-y0 elif rang > 90 and rang <= 180: ang = 180 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox+x0 y0n1 = oy+y0 elif rang > 180 and rang <=270: ang = rang - 180 angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox-x0 y0n1 = oy+y0 else: ang = 360 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox-x0 y0n1 = oy-y0 else: if rang <= 90: angle = math.radians(rang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox-x0 y0n1 = oy+y0 elif rang > 90 and rang <= 180: ang = 180 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox-x0 y0n1 = oy-y0 elif rang > 180 and rang <=270: ang = rang - 180 angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox+x0 y0n1 = oy-y0 else: ang = 360 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox+x0 y0n1 = oy+y0 else: if zf < 0: if rang <= 90: angle = math.radians(rang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox+x0 y0n1 = oy-y0 elif rang > 90 and rang <= 180: ang = 180 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox+x0 y0n1 = oy+y0 elif rang > 180 and rang <=270: ang = rang - 180 angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox-x0 y0n1 = oy+y0 else: ang = 360 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox-x0 y0n1 = oy-y0 else: if rang <= 90: angle = math.radians(rang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox-x0 y0n1 = oy+y0 elif rang > 90 and rang <= 180: ang = 180 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox-x0 y0n1 = oy-y0 elif rang > 180 and rang <=270: ang = rang - 180 angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox+x0 y0n1 = oy-y0 else: ang = 360 - rang angle = math.radians(ang) cosA = math.cos(angle) sinA = math.sin(angle) x0 = dis * sinA y0 = dis * cosA x0n1 = ox+x0 y0n1 = oy+y0 return [x0n1,y0n1] def readangle(pname): nameA = pname.split('-',-1)[-1] #作个键值对 dict = {'BD':101, 'BDZ':101, 'BDY':101,'KHP':191, 'THP':191, 'BPR':11,'RFH':11,'BPL':210,'DQ':101} ang1 = dict[nameA] if nameA == 'THP' or nameA == 'BPR': ang2 = 360 - ang1 elif nameA == 'KHP': ang2 = 90 - ang1 else: ang2 = 270 - ang1 return ang2 def readtxt(listp): #输出和两个转换坐标文件 txtpath = listp[0] prjpath = listp[1] prjpath1 = listp[2] outpath = listp[3] #创建需要用的 gdbpath = outpath + '\\bd.mdb' gdbpath1 = outpath + '\\result.mdb' mdbpath = gdbpath1 + '\\BDp' foutpath = gdbpath + '\\BDp' try: arcpy.CreatePersonalGDB_management (outpath, 'bd.mdb') except: arcpy.Delete_management(gdbpath) arcpy.CreatePersonalGDB_management (outpath, 'bd.mdb') try: arcpy.CreatePersonalGDB_management (outpath, 'result.mdb') except: arcpy.Delete_management(gdbpath1) arcpy.CreatePersonalGDB_management (outpath, 'result.mdb') try: arcpy.CreateFeatureclass_management(gdbpath,'BDp', 'POINT') except: arcpy.Delete_management(foutpath) arcpy.CreateFeatureclass_management(gdbpath,'BDp', 'POINT') arcpy.MakeFeatureLayer_management(foutpath, 'bd') #cgcs2000的坐标系文件(务必放入输出文件夹) arcpy.DefineProjection_management('bd', prjpath) arcpy.AddField_management('bd','pname','TEXT') arcpy.AddField_management('bd','pxyp','TEXT') arcpy.AddField_management('bd','elevation','FLOAT') arcpy.AddField_management('bd','dis','FLOAT') with arcpy.da.InsertCursor('bd',['SHAPE@X','SHAPE@Y','elevation','pname','pxyp','dis']) as cursorZ: with open(txtpath,'r') as filesA: for ppstr1 in filesA: str1 = ppstr1.split(',',-1)[0] str2 = ppstr1.split(',',-1)[1] str3 = float(ppstr1.split(',',-1)[2]) str4 = float(ppstr1.split(',',-1)[3]) str5 = float(ppstr1.split(',',-1)[4]) str61 = ppstr1.split(',',-1)[5] str62 = str61.replace('\n','') str6 = float(str62) listp1 = [] listp1.append(str3) listp1.append(str4) listp1.append(str5) listp1.append(str1) listp1.append(str2) listp1.append(str6) cursorZ.insertRow(listp1) # print('3') listp2 = [] listp2.append(foutpath) listp2.append(mdbpath) listp2.append(prjpath1) return listp2 def transzb(listp): #原始点,转换点,转换文件 shppath = listp[0] mdbpath = listp[1] prjpath = listp[2] try: arcpy.Project_management(shppath, mdbpath, prjpath) except: #检查空几何 arcpy.MakeFeatureLayer_management(shppath, 'SHP2') with arcpy.da.UpdateCursor('SHP2',['SHAPE@']) as cursorD: for curD in cursorD: shp1 = curD[0] if shp1 == None: cursorD.deleteRow() arcpy.Project_management(shppath, mdbpath, prjpath) # print('4') return mdbpath def rejson(inpath,outpath): #这里的点就是位移两个点 foutpath = outpath + '\\Result.json' arcpy.MakeFeatureLayer_management(inpath, 'bdlayer') #根据属性归类 mm = 0 pxstr = '' pystr = '' ppstr = '' pxstr1 = '' pystr1 = '' ppstr1 = '' pname = '' lx = '' ly = '' lp = '' lx1 = '' ly1 = '' lp1 = '' nameA = '' resultp = [] with arcpy.da.SearchCursor('bdlayer',['SHAPE@X','SHAPE@Y','elevation','pname','pxyp','dis']) as cursorZ: for curZ in cursorZ: if mm == 0: #px pname = curZ[3] nameA = pname.split('-',-1)[-1] if nameA =='BPR' or nameA == 'THP': pystr = str(curZ[0]) + " ," + str(curZ[1]) + " ," + str(curZ[2]) ly = str(round(curZ[5],1)) # pxstr = str(round(curZ[0],2)) + " ," + str(round(curZ[1],2)) + " ," + str(round(curZ[2])) # lx = str(round(curZ[5],2)) else: pxstr = str(curZ[0]) + " ," + str(curZ[1]) + " ," + str(curZ[2]) lx = str(round(curZ[5],1)) mm = mm + 1 elif mm == 1 and curZ[3] == pname: if nameA =='BPR' or nameA == 'THP': pxstr = str(curZ[0]) + " ," + str(curZ[1]) + " ," + str(curZ[2]) lx = str(round(curZ[5],1)) else: #py pystr = str(curZ[0]) + " ," + str(curZ[1]) + " ," + str(curZ[2]) ly = str(round(curZ[5],1)) mm = mm + 1 elif mm == 2 and curZ[3] == pname: #pp ppstr = str(curZ[0]) + " ," + str(curZ[1]) + " ," + str(curZ[2]) lp = str(round(curZ[5],1)) mm = mm + 1 elif mm == 3 and curZ[3] == pname: if nameA =='BPR' or nameA == 'THP': pystr1 = str(curZ[0]) + " ," + str(curZ[1]) + " ," + str(curZ[2]) ly1 = str(round(curZ[5],1)) else: pxstr1 = str(curZ[0]) + " ," + str(curZ[1]) + " ," + str(curZ[2]) lx1 = str(round(curZ[5],1)) mm = mm + 1 elif mm == 4 and curZ[3] == pname: if nameA =='BPR' or nameA == 'THP': pxstr1 = str(curZ[0]) + " ," + str(curZ[1]) + " ," + str(curZ[2]) lx1 = str(round(curZ[5],1)) else: pystr1 = str(curZ[0]) + " ," + str(curZ[1]) + " ," + str(curZ[2]) ly1 = str(round(curZ[5],1)) mm = mm + 1 elif mm == 5 and curZ[3] == pname: ppstr1 = str(curZ[0]) + " ," + str(curZ[1]) + " ," + str(curZ[2]) lp1 = str(round(curZ[5],1)) #此时加入item item = { "name": pname, "px": pxstr, "lx": lx, "py": pystr, "ly": ly, "pp": ppstr, "lp": lp, "px1": pxstr1, "lx1": lx1, "py1": pystr1, "ly1": ly1, "pp1": ppstr1, "lp1": lp1 } resultp.append(item) mm = 0 # print('5') with open(foutpath, 'w') as dump_f: json.dump(resultp,dump_f) def linew(bppath,inpath,outpath,ranA,ranB): # ang1 = 270 - float(angle1) ran = float(ranA) ran1 = float(ranB) #存储位移array txtarr = [] #获取位移量文件名字 txtfilename = inpath.split('\\',-1)[-1] txtfile = txtfilename.split('.',-1)[0] #读的是位移量文件 with open(inpath,'r+') as fileA: for lineA in fileA: #直接读 if lineA == '\n': continue elif 'end' in lineA: fulloutpath = outpath + '\\Resulttxt.txt' #进入第二步 # print('1') #这里把所有的都理出来了 resultp = drawline(bppath,txtarr,ran,fulloutpath,outpath,ran1) #这里读取 list2 = readtxt(resultp) #坐标转换 mdbpath = transzb(list2) #再输出为json rejson(mdbpath,outpath) # print('finish') break else: txtarr.append(lineA) if __name__ == '__main__': # 第一个是基准点,第二个是位移量,第三个是输出文件夹,夸张系数 linew(r"D:\4work_now\20240226TZL\BP2024_cgcs2000.txt",r"D:\4work_now\20240226TZL\TZL2023.txt",r"D:\4work_now\20240226TZL\result",2,2)