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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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,excelname,listname1,listpastx1,listpasty1,listcgcs1,listnewx1,listnewy1,pjbc,fxzwc,zrbzwc,points,zbs,zfxs,sf,pjbcs,pjfxs):
  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_tn = to_utf8(excelname)
  36. try:
  37. # 查询是否已存在相同的记录
  38. cursor.execute(
  39. "SELECT * FROM GS_Input_Param WHERE TableName = ?",
  40. (utf_tn,)
  41. )
  42. existing_record = cursor.fetchone()
  43. if existing_record:
  44. # 弹窗询问用户是否更新数据
  45. if not ask_for_update(excelname):
  46. print("用户选择不更新数据")
  47. return
  48. # 更新现有记录GS_Input_Param
  49. cursor.execute(
  50. """
  51. UPDATE GS_Input_Param
  52. SET Last_ResultName=?,New_ResultName=?,Avg_SL=?,Ms_Dir=?,Ms_WSL=?,Pt_Count=?,SL_Count=?,Dir_Count=?,Scale_Value=?,Avg_MSL=?,Avg_Dir=?
  53. WHERE TableName = ?
  54. """,
  55. (utf_pan,utf_nn,pjbc,fxzwc,zrbzwc,points,zbs,zfxs,sf,pjbcs,pjfxs,utf_tn,)
  56. )
  57. # 更新现有记录GS_Input_Point
  58. #删了已有的数据,重新插入
  59. cursor.execute(
  60. """
  61. DELETE FROM GS_Input_Point WHERE TableName = ?
  62. """,
  63. (utf_tn,)
  64. )
  65. rr = 0
  66. while rr < len(listname1):
  67. rr1 = listname1[rr]
  68. rr2 = listpastx1[rr]
  69. rr3 = listpasty1[rr]
  70. rr4 = listcgcs1[rr]
  71. rr5 = listnewx1[rr]
  72. rr6 = listnewy1[rr]
  73. utf_rr1 = to_utf8(rr1)
  74. cursor.execute(
  75. """
  76. INSERT INTO GS_Input_Point(TableName,Last_ResultName,New_ResultName,Last_X,Last_Y,New_X,New_Y,PointName,cgcs) VALUES (?,?,?,?,?,?,?,?,?)
  77. """,
  78. (utf_tn,utf_pan,utf_nn,rr2,rr3,rr5,rr6,utf_rr1,rr4,)
  79. )
  80. rr = rr + 1
  81. messagebox.showinfo("提示",
  82. f"文件 '{excelname}' 已成功更新到本地数据库中,点击确认以关闭此对话框。对话框关闭后,请点击“计算”以重新计算数据")
  83. else:
  84. # 插入新记录GS_Input_Param
  85. cursor.execute(
  86. """
  87. 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)
  88. VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
  89. """,
  90. (utf_tn,utf_pan,utf_nn,pjbc,fxzwc,zrbzwc,points,zbs,zfxs,sf,pjbcs,pjfxs,)
  91. )
  92. # 插入新记录GS_Input_Point
  93. rr = 0
  94. while rr < len(listname1):
  95. rr1 = listname1[rr]
  96. rr2 = listpastx1[rr]
  97. rr3 = listpasty1[rr]
  98. rr4 = listcgcs1[rr]
  99. rr5 = listnewx1[rr]
  100. rr6 = listnewy1[rr]
  101. utf_rr1 = to_utf8(rr1)
  102. cursor.execute(
  103. """
  104. INSERT INTO GS_Input_Point(TableName,Last_ResultName,New_ResultName,Last_X,Last_Y,New_X,New_Y,PointName,cgcs) VALUES (?,?,?,?,?,?,?,?,?)
  105. """,
  106. (utf_tn,utf_pan,utf_nn,rr2,rr3,rr5,rr6,utf_rr1,rr4,)
  107. )
  108. rr = rr + 1
  109. messagebox.showinfo("提示",
  110. f"文件 '{excelname}' 已成功添加到本地数据库中,点击确认以关闭此对话框。对话框关闭后,请点击“计算”以计算数据")
  111. db.commit()
  112. except sqlite3.Error as e:
  113. print(f"插入或更新文件名时发生错误: {e}")
  114. except Exception as e:
  115. print(f"处理文件时发生错误: {e}")
  116. finally:
  117. db.close()
  118. #读取xls
  119. def xlrd_read(excelpath,dbpath):
  120. global gszbx
  121. global gszby
  122. listname1 = []
  123. listnewx1 = []
  124. listnewy1 = []
  125. listpastx1 = []
  126. listpasty1 = []
  127. listcgcs1 = []
  128. #excel表名就是表名
  129. excelname = os.path.basename(excelpath)
  130. # 读取excel
  131. xlr = xlrd.open_workbook(excelpath)
  132. tabler = xlr.sheets()[0]
  133. # 从第3行开始,3A为点号,3B为最新x,3C为最新y,3D为往期x
  134. # 3E为往期y
  135. # 先看点数
  136. rows = tabler.nrows
  137. row = 2
  138. points = rows - 2
  139. # 收集每一列的数据,方便后期计算
  140. listname = []
  141. listnewx = []
  142. listnewy = []
  143. listpastx = []
  144. listpasty = []
  145. listcgcs = []
  146. sumnewx = 0
  147. sumnewy = 0
  148. sumpastx = 0
  149. sumpasty = 0
  150. # 记录下最早的那几个开头
  151. # excelpath是输入表格,outpath是输出路径,pjbc是平均边长
  152. # fxzwc是方向中误差,zrbzwc是最弱边边长相对中误差,pjbcs是平均边数,pjfxs是平均方向数,sf是缩放值
  153. # 新增总边数zbs,总方向数zfxs
  154. # 先计算位移判断值
  155. pjbc = float(tabler.cell(0, 6).value)
  156. fxzwc = float(tabler.cell(1, 6).value)
  157. str1 = str(tabler.cell(2, 6).value)
  158. #判断下是哪种情况,转化为数值
  159. if '.' in str1:
  160. #直接float使用
  161. zrbzwc = float(str1)
  162. else:
  163. #转化为分数
  164. # 10的6次方
  165. # ten_to_the_six = 10e6
  166. fzstr = float(str1.split('/',-1)[0])
  167. fmstr = float(str1.split('/',-1)[1])
  168. zrbzwc = float(fzstr/fmstr)
  169. zbs = float(tabler.cell(3, 6).value)
  170. zfxs = float(tabler.cell(4, 6).value)
  171. pjbcs = zbs / points
  172. pjfxs = zfxs / points
  173. sf = float(tabler.cell(5,6).value)
  174. # 再拆细点
  175. wy1 = pjbc / 1000
  176. wy2 = wy1 * zrbzwc * 1e6
  177. wypd1 = wy2 * wy2 / pjbcs
  178. # 206.265是常数系数
  179. wy3 = pjbc * fxzwc / 206.265
  180. wypd2 = wy3 * wy3 / pjfxs
  181. wypd3 = math.sqrt(wypd1 + wypd2)
  182. wypd = round(wypd3 * 3, 4)
  183. wypd0 = wypd
  184. pastname = tabler.cell(0, 3).value
  185. newname = tabler.cell(0, 1).value
  186. #如果第二次测量或者首次测量为空,则判断为新建
  187. while row < rows:
  188. pname = tabler.cell(row, 0).value
  189. pcgcs = 1
  190. try:
  191. pnewx = round(float(tabler.cell(row, 1).value),4)
  192. except:
  193. pnewx = 0
  194. pcgcs = -2
  195. try:
  196. pnewy = round(float(tabler.cell(row, 2).value),4)
  197. except:
  198. pnewy = 0
  199. pcgcs = -2
  200. try:
  201. ppastx = round(float(tabler.cell(row, 3).value),4)
  202. except:
  203. ppastx = 0
  204. pcgcs = -2
  205. try:
  206. ppasty = round(float(tabler.cell(row, 4).value),4)
  207. except:
  208. ppasty = 0
  209. pcgcs = -2
  210. sumpastx = sumpastx + ppastx
  211. sumnewy = sumnewy + pnewy
  212. sumnewx = sumnewx + pnewx
  213. sumpasty = sumpasty + ppasty
  214. listname1.append(pname)
  215. listnewx1.append(pnewx)
  216. listnewy1.append(pnewy)
  217. listpastx1.append(ppastx)
  218. listpasty1.append(ppasty)
  219. listcgcs1.append(pcgcs)
  220. if pcgcs == -2:
  221. points = points - 1
  222. row = row + 1
  223. #插入数据库
  224. insert_into_database(dbpath,pastname,newname,excelname,listname1,listpastx1,listpasty1,listcgcs1,listnewx1,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,listnewy1,pjbc,fxzwc,zrbzwc,points,zbs,zfxs,sf,pjbcs,pjfxs)
  357. #新建点不参与运算
  358. rr=0
  359. while rr < len(listname1):
  360. rr1 = listname1[rr]
  361. rr2 = listpastx1[rr]
  362. rr3 = listpasty1[rr]
  363. rr4 = listcgcs1[rr]
  364. rr5 = listnewx1[rr]
  365. rr6 = listnewy1[rr]
  366. if rr4 == -2:
  367. rr = rr + 1
  368. else:
  369. listname.append(rr1)
  370. listpastx.append(rr2)
  371. listpasty.append(rr3)
  372. listcgcs.append(rr4)
  373. listnewx.append(rr5)
  374. listnewy.append(rr6)
  375. rr = rr + 1
  376. # 主函数 读取excel文件
  377. def main_function(file_path,dbpath):
  378. # 调用读取Excel文件的函数
  379. file_name = os.path.basename(file_path)
  380. #两种加载方式,对于不同的文件
  381. if ".xlsx" in file_path:
  382. #采用openpyxl
  383. openpyxl_read(file_path,dbpath)
  384. else:
  385. #采用xlrd
  386. xlrd_read(file_path,dbpath)