控制网复测平面基准归算程序(包含控制网复测平面基准计算,平面控制网稳定性计算,水准测段高差稳定计算三个程序功能)
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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