控制网复测平面基准归算程序(包含控制网复测平面基准计算,平面控制网稳定性计算,水准测段高差稳定计算三个程序功能)
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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, beforename, excelname, listname1, listpastx1, listpasty1,
  26. listcgcs1, pjbc, fxzwc, points, zbs, zfxs, sf, pjbcs, pjfxs, listbex, listbey, listnewx,
  27. 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. QMessageBox.information(None, "提示",
  85. f"文件 '{excelname}' 已成功更新到本地数据库中,点击'OK'以关闭此对话框。")
  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. QMessageBox.information(None, "提示",
  115. f"文件 '{excelname}' 已成功添加到本地数据库中,点击'OK'以关闭此对话框。")
  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,
  220. pjbc, fxzwc, points, zbs, zfxs, sf, pjbcs, pjfxs, listbex, listbey, listnewx, listnewy)
  221. # 读取xlsx
  222. def openpyxl_read(excelpath, dbpath):
  223. global gszbx
  224. global gszby
  225. listname1 = []
  226. listpastx1 = []
  227. listpasty1 = []
  228. listcgcs1 = []
  229. # excel表名就是表名
  230. excelname = os.path.basename(excelpath)
  231. # 读取excel
  232. xlr = openpyxl.load_workbook(excelpath)
  233. tabler = xlr.worksheets[0]
  234. # 从第3行开始,3A为点号,3B为最新x,3C为最新y,3D为往期x
  235. # 3E为往期y
  236. # 先看点数
  237. rows = tabler.max_row
  238. row = 3
  239. points = rows - 2
  240. # 收集每一列的数据,方便后期计算
  241. listname = []
  242. listnewx = []
  243. listnewy = []
  244. listpastx = []
  245. listpasty = []
  246. listcgcs = []
  247. listbex = []
  248. listbey = []
  249. sumnewx = 0
  250. sumnewy = 0
  251. sumpastx = 0
  252. sumpasty = 0
  253. # 记录下最早的那几个开头
  254. # excelpath是输入表格,outpath是输出路径,pjbc是平均边长
  255. # fxzwc是方向中误差,zrbzwc是最弱边边长相对中误差,pjbcs是平均边数,pjfxs是平均方向数,sf是缩放值
  256. # 新增总边数zbs,总方向数zfxs
  257. # 先计算位移判断值
  258. pjbc = float(tabler['J1'].value)
  259. fxzwc = float(tabler['J2'].value)
  260. try:
  261. zbs = float(tabler['J3'].value)
  262. except:
  263. zbs = load_and_calculate_excel(excelpath, 'J3')
  264. try:
  265. zfxs = float(tabler['J4'].value)
  266. except:
  267. zfxs = load_and_calculate_excel(excelpath, 'J4')
  268. pjbcs = zbs / points
  269. pjfxs = zfxs / points
  270. sf = float(tabler['J5'].value)
  271. # 再拆细点
  272. wy1 = pjbc / 1000
  273. wy2 = wy1 + 1
  274. wypd1 = wy2 * wy2 / pjbcs
  275. # 好像206.265是常数系数
  276. wy3 = pjbc * fxzwc / 206.265
  277. wypd2 = wy3 * wy3 / pjfxs
  278. wypd3 = math.sqrt(wypd1 + wypd2)
  279. wypd = round((wypd3 * 2 * math.sqrt(2)), 9)
  280. wypd0 = wypd
  281. befname = tabler['G1'].value
  282. lastname = tabler['D1'].value
  283. finalname = tabler['B1'].value
  284. while row <= rows:
  285. try:
  286. pname = tabler.cell(row, 1).value
  287. pnewx = float(tabler.cell(row, 2).value)
  288. sumnewx = sumnewx + pnewx
  289. pnewy = float(tabler.cell(row, 3).value)
  290. sumnewy = sumnewy + pnewy
  291. ppastx = float(tabler.cell(row, 4).value)
  292. sumpastx = sumpastx + ppastx
  293. ppasty = float(tabler.cell(row, 5).value)
  294. sumpasty = sumpasty + ppasty
  295. pcgcs = float(tabler.cell(row, 6).value)
  296. pbex = float(tabler.cell(row, 7).value)
  297. pbey = float(tabler.cell(row, 8).value)
  298. listname.append(pname)
  299. listnewx.append(pnewx)
  300. listnewy.append(pnewy)
  301. listpastx.append(ppastx)
  302. listpasty.append(ppasty)
  303. listcgcs.append(pcgcs)
  304. listbex.append(pbex)
  305. listbey.append(pbey)
  306. row = row + 1
  307. except:
  308. break
  309. # 最后返回这里输出
  310. rr = 0
  311. while rr < len(listname):
  312. rr1 = listname[rr]
  313. rr2 = listpastx[rr]
  314. rr3 = listpasty[rr]
  315. rr4 = listcgcs[rr]
  316. listname1.append(rr1)
  317. listpastx1.append(rr2)
  318. listpasty1.append(rr3)
  319. listcgcs1.append(rr4)
  320. rr = rr + 1
  321. # 存入数据库
  322. insert_into_database(dbpath, lastname, finalname, befname, excelname, listname1, listpastx1, listpasty1, listcgcs1,
  323. pjbc, fxzwc, points, zbs, zfxs, sf, pjbcs, pjfxs, listbex, listbey, listnewx, listnewy)
  324. # 主函数 读取excel文件
  325. def main_function(file_path, dbpath):
  326. # 调用读取Excel文件的函数
  327. file_name = os.path.basename(file_path)
  328. # 两种加载方式,对于不同的文件
  329. if ".xlsx" in file_path:
  330. # 采用openpyxl
  331. openpyxl_read(file_path, dbpath)
  332. else:
  333. # 采用xlrd
  334. xlrd_read(file_path, dbpath)