控制网复测平面基准归算程序(包含控制网复测平面基准计算,平面控制网稳定性计算,水准测段高差稳定计算三个程序功能)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WD.py 12KB

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