控制网复测平面基准归算程序(包含控制网复测平面基准计算,平面控制网稳定性计算,水准测段高差稳定计算三个程序功能)
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

WD.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import openpyxl
  2. import xlrd
  3. import sqlite3
  4. import os
  5. from openpyxl import Workbook
  6. import tkinter as tk
  7. from tkinter import messagebox
  8. import math
  9. import numpy as np
  10. def to_utf8(text):
  11. str1 = text.encode('utf-8')
  12. return str1
  13. # 弹窗提示用户是否更新数据
  14. def ask_for_update(file_name):
  15. root = tk.Tk()
  16. root.withdraw() # 隐藏主窗口
  17. response = messagebox.askyesno("提示", f"检测到数据库中存在相同文件名 '{file_name}',是否更新数据?")
  18. return response
  19. # 读取Excel文件并计算公式结果
  20. def load_and_calculate_excel(file_path,cell0):
  21. # 加载Excel文件并计算单元格的公式结果。
  22. workbook = openpyxl.load_workbook(file_path, data_only=True)
  23. sheet = workbook.active
  24. h1_value = sheet[cell0].value
  25. return h1_value
  26. # 数据库插入函数
  27. def insert_into_database(database,pastname,newname,beforename,excelname,listname1,listpastx1,listpasty1,listcgcs1,pjbc,fxzwc,points,zbs,zfxs,sf,pjbcs,pjfxs,listbex,listbey,listnewx,listnewy):
  28. #先把输入的录入数据库
  29. db = sqlite3.connect(database)
  30. #获取游标
  31. cursor = db.cursor()
  32. #字符类的都需要转换成字节存进去
  33. utf_pan = to_utf8(pastname)
  34. utf_nn = to_utf8(newname)
  35. utf_bn = to_utf8(beforename)
  36. utf_tn = to_utf8(excelname)
  37. try:
  38. # 查询是否已存在相同的记录
  39. cursor.execute(
  40. "SELECT * FROM WD_Input_Param WHERE TableName = ?",
  41. (utf_tn,)
  42. )
  43. existing_record = cursor.fetchone()
  44. if existing_record:
  45. # 弹窗询问用户是否更新数据
  46. if not ask_for_update(excelname):
  47. print("用户选择不更新数据")
  48. return
  49. # 更新现有记录WD_Input_Param
  50. cursor.execute(
  51. """
  52. UPDATE WD_Input_Param
  53. SET First_ResultName = ?,Last_ResultName=?,New_ResultName=?,Avg_SL=?,Ms_Dir=?,Pt_Count=?,SL_Count=?,Dir_Count=?,Scale_Value=?,Avg_MSL=?,Avg_Dir=?
  54. WHERE TableName = ?
  55. """,
  56. (utf_bn,utf_pan,utf_nn,pjbc,fxzwc,points,zbs,zfxs,sf,pjbcs,pjfxs,utf_tn,)
  57. )
  58. # 更新现有记录WD_Input_Point
  59. #删了已有的数据,重新插入
  60. cursor.execute(
  61. """
  62. DELETE FROM WD_Input_Point WHERE TableName = ?
  63. """,
  64. (utf_tn,)
  65. )
  66. rr = 0
  67. while rr < len(listname1):
  68. rr1 = listname1[rr]
  69. rr2 = listpastx1[rr]
  70. rr3 = listpasty1[rr]
  71. rr4 = listcgcs1[rr]
  72. rr5 = listnewx[rr]
  73. rr6 = listnewy[rr]
  74. rr7 = listbex[rr]
  75. rr8 = listbey[rr]
  76. utf_rr1 = to_utf8(rr1)
  77. cursor.execute(
  78. """
  79. INSERT INTO WD_Input_Point(TableName,First_ResultName,Last_ResultName,New_ResultName,Last_X,Last_Y,New_X,New_Y,PointName,Wight,First_X,First_Y) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
  80. """,
  81. (utf_tn,utf_bn,utf_pan,utf_nn,rr2,rr3,rr5,rr6,utf_rr1,rr4,rr7,rr8,)
  82. )
  83. rr = rr + 1
  84. messagebox.showinfo("提示",
  85. f"文件 '{excelname}' 已成功更新到本地数据库中,点击确认以关闭此对话框。对话框关闭后,请点击“计算”以重新计算数据")
  86. else:
  87. # 插入新记录WD_Input_Param
  88. cursor.execute(
  89. """
  90. INSERT INTO WD_Input_Param (TableName,First_ResultName,Last_ResultName,New_ResultName,Avg_SL,Ms_Dir,Pt_Count,SL_Count,Dir_Count,Scale_Value,Avg_MSL,Avg_Dir)
  91. VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
  92. """,
  93. (utf_tn,utf_bn,utf_pan,utf_nn,pjbc,fxzwc,points,zbs,zfxs,sf,pjbcs,pjfxs,)
  94. )
  95. # 插入新记录WD_Input_Point
  96. rr = 0
  97. while rr < len(listname1):
  98. rr1 = listname1[rr]
  99. rr2 = listpastx1[rr]
  100. rr3 = listpasty1[rr]
  101. rr4 = listcgcs1[rr]
  102. rr5 = listnewx[rr]
  103. rr6 = listnewy[rr]
  104. rr7 = listbex[rr]
  105. rr8 = listbey[rr]
  106. utf_rr1 = to_utf8(rr1)
  107. cursor.execute(
  108. """
  109. INSERT INTO WD_Input_Point(TableName,First_ResultName,Last_ResultName,New_ResultName,Last_X,Last_Y,New_X,New_Y,PointName,Wight,First_X,First_Y) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
  110. """,
  111. (utf_tn,utf_bn,utf_pan,utf_nn,rr2,rr3,rr5,rr6,utf_rr1,rr4,rr7,rr8,)
  112. )
  113. rr = rr + 1
  114. messagebox.showinfo("提示",
  115. f"文件 '{excelname}' 已成功添加到本地数据库中,点击确认以关闭此对话框。对话框关闭后,请点击“计算”以计算数据")
  116. db.commit()
  117. except sqlite3.Error as e:
  118. print(f"插入或更新文件名时发生错误: {e}")
  119. except Exception as e:
  120. print(f"处理文件时发生错误: {e}")
  121. finally:
  122. db.close()
  123. #读取xls
  124. def xlrd_read(excelpath,dbpath):
  125. global gszbx
  126. global gszby
  127. listname1 = []
  128. listpastx1 = []
  129. listpasty1 = []
  130. listcgcs1 = []
  131. #excel表名就是表名
  132. excelname = os.path.basename(excelpath)
  133. # 读取excel
  134. xlr = xlrd.open_workbook(excelpath)
  135. tabler = xlr.sheets()[0]
  136. # 从第3行开始,3A为点号,3B为最新x,3C为最新y,3D为往期x
  137. # 3E为往期y,3F为成果次数,3G为最早x,3H最早y
  138. # 先看点数
  139. rows = tabler.nrows
  140. row = 2
  141. points = rows - 2
  142. # 收集每一列的数据,方便后期计算
  143. listname = []
  144. listnewx = []
  145. listnewy = []
  146. listpastx = []
  147. listpasty = []
  148. # 成果次数
  149. listcgcs = []
  150. # 最早的
  151. listbex = []
  152. listbey = []
  153. sumnewx = 0
  154. sumnewy = 0
  155. sumpastx = 0
  156. sumpasty = 0
  157. # 记录下最早的那几个开头
  158. # excelpath是输入表格,outpath是输出路径,pjbc是平均边长
  159. # fxzwc是方向中误差,pjfxs是平均方向数,sf是缩放值
  160. # 先计算位移判断值
  161. pjbc = float(tabler.cell(0, 9).value)
  162. fxzwc = float(tabler.cell(1, 9).value)
  163. zbs = float(tabler.cell(2, 9).value)
  164. zfxs = float(tabler.cell(3, 9).value)
  165. pjbcs = zbs / points
  166. pjfxs = zfxs / points
  167. sf = float(tabler.cell(4, 9).value)
  168. # 再拆细点
  169. wy1 = pjbc / 1000
  170. wy2 = wy1 + 1
  171. wypd1 = wy2 * wy2 / pjbcs
  172. # 好像206.265是常数系数
  173. wy3 = pjbc * fxzwc / 206.265
  174. wypd2 = wy3 * wy3 / pjfxs
  175. wypd3 = math.sqrt(wypd1 + wypd2)
  176. wypd = round((wypd3 * 2 * math.sqrt(2)), 9)
  177. wypd0 = wypd
  178. lastname = tabler.cell(0, 3).value
  179. finalname = tabler.cell(0, 1).value
  180. befname = tabler.cell(0, 6).value
  181. utf_ln = to_utf8(lastname)
  182. utf_fn = to_utf8(finalname)
  183. utf_bn = to_utf8(befname)
  184. while row < rows:
  185. pname = tabler.cell(row, 0).value
  186. pnewx = float(tabler.cell(row, 1).value)
  187. sumnewx = sumnewx + pnewx
  188. pnewy = float(tabler.cell(row, 2).value)
  189. sumnewy = sumnewy + pnewy
  190. ppastx = float(tabler.cell(row, 3).value)
  191. sumpastx = sumpastx + ppastx
  192. ppasty = float(tabler.cell(row, 4).value)
  193. sumpasty = sumpasty + ppasty
  194. pcgcs = float(tabler.cell(row, 5).value)
  195. pbex = float(tabler.cell(row, 6).value)
  196. pbey = float(tabler.cell(row, 7).value)
  197. listname.append(pname)
  198. listnewx.append(pnewx)
  199. listnewy.append(pnewy)
  200. listpastx.append(ppastx)
  201. listpasty.append(ppasty)
  202. listcgcs.append(pcgcs)
  203. listbex.append(pbex)
  204. listbey.append(pbey)
  205. row = row + 1
  206. # 最后返回这里输出
  207. rr = 0
  208. while rr < len(listname):
  209. rr1 = listname[rr]
  210. rr2 = listpastx[rr]
  211. rr3 = listpasty[rr]
  212. rr4 = listcgcs[rr]
  213. listname1.append(rr1)
  214. listpastx1.append(rr2)
  215. listpasty1.append(rr3)
  216. listcgcs1.append(rr4)
  217. rr = rr + 1
  218. #存入数据库
  219. insert_into_database(dbpath,lastname,finalname,befname,excelname,listname1,listpastx1,listpasty1,listcgcs1,pjbc,fxzwc,points,zbs,zfxs,sf,pjbcs,pjfxs,listbex,listbey,listnewx,listnewy)
  220. #读取xlsx
  221. def openpyxl_read(excelpath,dbpath):
  222. global gszbx
  223. global gszby
  224. listname1 = []
  225. listpastx1 = []
  226. listpasty1 = []
  227. listcgcs1 = []
  228. #excel表名就是表名
  229. excelname = os.path.basename(excelpath)
  230. # 读取excel
  231. xlr = openpyxl.load_workbook(excelpath)
  232. tabler = xlr.worksheets[0]
  233. # 从第3行开始,3A为点号,3B为最新x,3C为最新y,3D为往期x
  234. # 3E为往期y
  235. # 先看点数
  236. rows = tabler.max_row
  237. row = 3
  238. points = rows - 2
  239. # 收集每一列的数据,方便后期计算
  240. listname = []
  241. listnewx = []
  242. listnewy = []
  243. listpastx = []
  244. listpasty = []
  245. listcgcs = []
  246. listbex=[]
  247. listbey=[]
  248. sumnewx = 0
  249. sumnewy = 0
  250. sumpastx = 0
  251. sumpasty = 0
  252. # 记录下最早的那几个开头
  253. # excelpath是输入表格,outpath是输出路径,pjbc是平均边长
  254. # fxzwc是方向中误差,zrbzwc是最弱边边长相对中误差,pjbcs是平均边数,pjfxs是平均方向数,sf是缩放值
  255. # 新增总边数zbs,总方向数zfxs
  256. # 先计算位移判断值
  257. pjbc = float(tabler['J1'].value)
  258. fxzwc = float(tabler['J2'].value)
  259. try:
  260. zbs = float(tabler['J3'].value)
  261. except:
  262. zbs = load_and_calculate_excel(excelpath,'J3')
  263. try:
  264. zfxs = float(tabler['J4'].value)
  265. except:
  266. zfxs = load_and_calculate_excel(excelpath,'J4')
  267. pjbcs = zbs / points
  268. pjfxs = zfxs / points
  269. sf = float(tabler['J5'].value)
  270. # 再拆细点
  271. wy1 = pjbc / 1000
  272. wy2 = wy1 + 1
  273. wypd1 = wy2 * wy2 / pjbcs
  274. # 好像206.265是常数系数
  275. wy3 = pjbc * fxzwc / 206.265
  276. wypd2 = wy3 * wy3 / pjfxs
  277. wypd3 = math.sqrt(wypd1 + wypd2)
  278. wypd = round((wypd3 * 2 * math.sqrt(2)), 9)
  279. wypd0 = wypd
  280. befname=tabler['G1'].value
  281. lastname = tabler['D1'].value
  282. finalname = tabler['B1'].value
  283. while row <= rows:
  284. try:
  285. pname = tabler.cell(row, 1).value
  286. pnewx = float(tabler.cell(row, 2).value)
  287. sumnewx = sumnewx + pnewx
  288. pnewy = float(tabler.cell(row, 3).value)
  289. sumnewy = sumnewy + pnewy
  290. ppastx = float(tabler.cell(row, 4).value)
  291. sumpastx = sumpastx + ppastx
  292. ppasty = float(tabler.cell(row, 5).value)
  293. sumpasty = sumpasty + ppasty
  294. pcgcs = float(tabler.cell(row, 6).value)
  295. pbex = float(tabler.cell(row, 7).value)
  296. pbey = float(tabler.cell(row, 8).value)
  297. listname.append(pname)
  298. listnewx.append(pnewx)
  299. listnewy.append(pnewy)
  300. listpastx.append(ppastx)
  301. listpasty.append(ppasty)
  302. listcgcs.append(pcgcs)
  303. listbex.append(pbex)
  304. listbey.append(pbey)
  305. row = row + 1
  306. except:
  307. break
  308. # 最后返回这里输出
  309. rr = 0
  310. while rr < len(listname):
  311. rr1 = listname[rr]
  312. rr2 = listpastx[rr]
  313. rr3 = listpasty[rr]
  314. rr4 = listcgcs[rr]
  315. listname1.append(rr1)
  316. listpastx1.append(rr2)
  317. listpasty1.append(rr3)
  318. listcgcs1.append(rr4)
  319. rr = rr + 1
  320. #存入数据库
  321. insert_into_database(dbpath,lastname,finalname,befname,excelname,listname1,listpastx1,listpasty1,listcgcs1,pjbc,fxzwc,points,zbs,zfxs,sf,pjbcs,pjfxs,listbex,listbey,listnewx,listnewy)
  322. # 主函数 读取excel文件
  323. def main_function(file_path,dbpath):
  324. # 调用读取Excel文件的函数
  325. file_name = os.path.basename(file_path)
  326. #两种加载方式,对于不同的文件
  327. if ".xlsx" in file_path:
  328. #采用openpyxl
  329. openpyxl_read(file_path,dbpath)
  330. else:
  331. #采用xlrd
  332. xlrd_read(file_path,dbpath)