open_pkfieur_PC.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/usr/bin/python
  2. # -*- coding: utf8 -*-
  3. from __future__ import unicode_literals
  4. try: # Compatibilité Python 3
  5. from Tkinter import *
  6. except:
  7. from tkinter import ttk
  8. from tkinter import messagebox as tkMessageBox
  9. from tkinter import Tk, Label, Entry, Button, END
  10. else:
  11. import ttk, tkFileDialog, tkMessageBox
  12. import socket
  13. import threading
  14. import time
  15. import re
  16. import webbrowser
  17. from utils import *
  18. #Emplacement de la base de données
  19. table_name = 'data/geo_sncf.db'
  20. var = True
  21. rgps = False
  22. class App(Tk):
  23. def __init__(self, parent):
  24. self.parent = parent
  25. self.initUI()
  26. self.config={}
  27. def initUI(self):
  28. self.parent.title("open_pkfieur")
  29. self.n = ttk.Notebook(self.parent)
  30. self.f1 = ttk.Frame(self.n)
  31. self.f2 = ttk.Frame(self.n)
  32. self.f3 = ttk.Frame(self.n)
  33. self.n.add(self.f1, text='Géo2pk')
  34. self.n.add(self.f2, text='Pk2Géo')
  35. self.n.add(self.f3, text='Localisateur Gps')
  36. self.n.grid(column=0, row=0, columnspan=1, sticky="nsew")
  37. self.f1_populate()
  38. self.f2_populate()
  39. self.f3_populate()
  40. def f1_populate(self):
  41. self.Frame10 = ttk.LabelFrame(self.f1, text='Coordonnées Géographiques')
  42. self.Frame10.grid(column=0, row=0, columnspan=3, rowspan=2, sticky="ew")
  43. self.Frame11 = ttk.LabelFrame(self.f1, text='Ligne/Pk')
  44. self.Frame11.grid(column=0, row=2, columnspan=3, rowspan=41, sticky="ew")
  45. self.Frame14 = ttk.Frame(self.f1, borderwidth=0, relief='groove')
  46. self.Frame14.grid(column=4, row=0, rowspan=6, sticky="EW")
  47. label = Label(self.Frame10, text="Lat").grid(row=0, column=0)
  48. label = Label(self.Frame10, text="Lon").grid(row=1, column=0)
  49. self.lat = Entry(self.Frame10)
  50. self.lat.grid(row=0, column=1)
  51. self.lon = Entry(self.Frame10)
  52. self.lon.grid(row=1, column=1)
  53. label = Label(self.Frame11, text="Ligne").grid(row=0, column=0)
  54. label = Label(self.Frame11, text="Pk").grid(row=1, column=0)
  55. label = Label(self.Frame11, text="Précision").grid(row=2, column=0)
  56. self.button_tri = Button(self.Frame14, text="Rechercher", command=self.get_coord)
  57. self.button_tri.grid(row=0,column=0)
  58. def f2_populate(self):
  59. self.Frame20 = ttk.LabelFrame(self.f2, text='Ligne/Pk')
  60. self.Frame20.grid(column=0, row=0, columnspan=2, rowspan=2, sticky="ew")
  61. self.Frame21 = ttk.LabelFrame(self.f2, text='Coordonnées Géographiques')
  62. self.Frame21.grid(column=0, row=3, columnspan=2, rowspan=3, sticky="ew")
  63. self.Frame23 = ttk.Frame(self.f2, borderwidth=0, relief='groove')
  64. self.Frame23.grid(column=3, row=0, rowspan=3, sticky="EW")
  65. self.Frame24 = ttk.Frame(self.f2, borderwidth=0, relief='groove')
  66. self.Frame24.grid(column=3, row=3, rowspan=3, sticky="EW")
  67. label = Label(self.Frame20, text="Ligne").grid(row=0, column=0)
  68. label = Label(self.Frame20, text="Pk").grid(row=1, column=0)
  69. self.ligne = Entry(self.Frame20)
  70. self.ligne.grid(row=0, column=1)
  71. self.pk = Entry(self.Frame20)
  72. self.pk.grid(row=1, column=1)
  73. label = Label(self.Frame21, text="Lat").grid(row=0, column=0)
  74. label = Label(self.Frame21, text="Lon").grid(row=1, column=0)
  75. label = Label(self.Frame21, text="Précision").grid(row=2, column=0)
  76. self.button_tri = Button(self.Frame23, text="Rechercher", command=self.get_pk)
  77. self.button_tri.grid(row=0,column=0)
  78. def f3_populate(self):
  79. self.Frame30 = ttk.LabelFrame(self.f3, text='@ NMEA')
  80. self.Frame30.grid(column=0, row=0, columnspan=2, rowspan=2, sticky="ew")
  81. self.Frame31 = ttk.LabelFrame(self.f3, text='Ligne/Pk')
  82. self.Frame31.grid(column=0, row=2, columnspan=2, rowspan=3, sticky="ew")
  83. self.Frame33 = ttk.Frame(self.f3, borderwidth=0, relief='groove')
  84. self.Frame33.grid(column=3, row=0, rowspan=3, sticky="EW")
  85. self.Frame34 = ttk.Frame(self.f3, borderwidth=0, relief='groove')
  86. self.Frame34.grid(column=3, row=3, rowspan=3, sticky="EW")
  87. label = Label(self.Frame30, text="IP").grid(row=0, column=0)
  88. label = Label(self.Frame30, text="Port").grid(row=1, column=0)
  89. self.adresse = Entry(self.Frame30)
  90. self.adresse.grid(row=0, column=1)
  91. self.adresse.insert(END, '192.168.43.1') # Adresse par défaut du Point d'accès mobile Android
  92. self.port = Entry(self.Frame30)
  93. self.port.grid(row=1, column=1)
  94. self.port.insert(END, '50000') # Port par défaut de Share GPS
  95. label = Label(self.Frame31, text="Ligne").grid(row=0, column=0)
  96. label = Label(self.Frame31, text="Pk").grid(row=1, column=0)
  97. label = Label(self.Frame31, text="Précision").grid(row=2, column=0)
  98. self.button_tri = Button(self.Frame33, text="Démarrer", command=self.get_nmea)
  99. self.button_tri.grid(row=0,column=0)
  100. def get_coord(self):
  101. lat = self.lat.get()
  102. lon = self.lon.get()
  103. lat = lat.replace(',','.')
  104. lon = lon.replace(',','.')
  105. regex = True
  106. rLatLon = re.compile(r"^[-]{0,1}\d{1,}[.]{1}\d{1,}$")
  107. if not rLatLon.search(lat):regex=False
  108. if not rLatLon.search(lon):regex=False
  109. if not regex :
  110. tkMessageBox.showinfo("Erreur Saisie", "Erreur de saisie lat/lon.")
  111. return False
  112. inp = lat+','+lon
  113. res, ecart = pkfieur.coord2pk(inp, table_name)
  114. if len(res) == 0:
  115. tkMessageBox.showinfo("Lat/lon introuvable", "Aucune correspondance trouvée.")
  116. return False
  117. try:
  118. for widget in self.Frame11.winfo_children():
  119. widget.destroy()
  120. except:
  121. pass
  122. label = Label(self.Frame11, text="Ligne").grid(row=0, column=0)
  123. label = Label(self.Frame11, text="Pk").grid(row=1, column=0)
  124. label = Label(self.Frame11, text="Précision").grid(row=2, column=0)
  125. label_nb = Label(self.Frame11, text=res['ligne']).grid(row=0, column=1)
  126. label_nb = Label(self.Frame11, text=res['pk']).grid(row=1, column=1)
  127. label_nb = Label(self.Frame11, text=(str(ecart)+' m')).grid(row=2, column=1)
  128. def get_pk(self):
  129. ligne = self.ligne.get()
  130. pk = self.pk.get()
  131. pk = pk.replace('+','.').replace(',','.')
  132. regex = True
  133. rLigne = re.compile(r"^\d{1,6}$")
  134. rPk = re.compile(r"^\d{1,3}[.]{0,1}\d{0,3}$")
  135. if not rLigne.match(ligne):regex=False
  136. if not rPk.match(pk):regex=False
  137. if not regex :
  138. tkMessageBox.showinfo("Erreur Saisie", "Erreur de saisie ligne/pk.")
  139. return False
  140. res, ecart = pkfieur.pk2coord(ligne, pk, table_name)
  141. if len(res) == 0:
  142. tkMessageBox.showinfo("Erreur Ligne", "Aucune ligne trouvée.")
  143. return False
  144. try:
  145. for widget in self.Frame21.winfo_children():
  146. widget.destroy()
  147. except:
  148. pass
  149. label = Label(self.Frame21, text="Lat").grid(row=0, column=0)
  150. label = Label(self.Frame21, text="Lon").grid(row=1, column=0)
  151. label = Label(self.Frame21, text="Précision").grid(row=2, column=0)
  152. label_nb = Label(self.Frame21, text=res['lat']).grid(row=0, column=1)
  153. label_nb = Label(self.Frame21, text=res['lon']).grid(row=1, column=1)
  154. label_nb = Label(self.Frame21, text=(str(ecart)+' m')).grid(row=2, column=1)
  155. button = Button(self.Frame24, text='web', command=lambda:self.web(res))
  156. button.grid(row=0,column=0)
  157. def web(self,res):
  158. url = 'http://www.openstreetmap.org/?mlat={0}&mlon={1}&zoom=15'.format(res['lat'],res['lon'])
  159. webbrowser.open(url)
  160. def stop(self):
  161. global var
  162. var = False
  163. def get_nmea(self):
  164. global var
  165. var = True
  166. adresse = self.adresse.get()
  167. port = self.port.get()
  168. def affiche():
  169. global rgps, var
  170. while var:
  171. if rgps:
  172. res, ecart = pkfieur.coord2pk(rgps, table_name)
  173. else:
  174. res = {'ligne':'no fix', 'pk':'no fix'}
  175. ecart = 9999
  176. try:
  177. for widget in self.Frame31.winfo_children():
  178. widget.destroy()
  179. except:
  180. pass
  181. label = Label(self.Frame31, text="Ligne").grid(row=0, column=0)
  182. label = Label(self.Frame31, text="Pk").grid(row=1, column=0)
  183. label = Label(self.Frame31, text="Précision").grid(row=2, column=0)
  184. label_nb = Label(self.Frame31, text=res['ligne']).grid(row=0, column=1)
  185. label_nb = Label(self.Frame31, text=res['pk']).grid(row=1, column=1)
  186. label_nb = Label(self.Frame31, text=(str(ecart)+' m')).grid(row=2, column=1)
  187. button = Button(self.Frame34, text='stop', command=self.stop)
  188. button.grid(row=0,column=0)
  189. time.sleep(1)
  190. def nmeaGps():
  191. global rgps, var
  192. host = adresse
  193. p = int(port)
  194. PACKET_SIZE=1152
  195. sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  196. try:
  197. sock.connect((host,p))
  198. except :
  199. var = False
  200. tkMessageBox.showinfo("Echec connection", "Vérifier adresse IP et périphérique GPS")
  201. else:
  202. while var: #continuously read and handle data
  203. data = sock.recv(PACKET_SIZE)
  204. try:
  205. for line in data.split(b"\r\n"):
  206. if line.startswith(b'$GPGGA'):
  207. lat, NS, lon, EW, fix = line.strip().split(b',')[2:7]
  208. if fix != '0':
  209. Decilat = int(float(lat)/100) + (float(lat)-int(float(lat)/100)*100)/60
  210. Decilon = int(float(lon)/100) + (float(lon)-int(float(lon)/100)*100)/60
  211. if (NS=='S'):
  212. Decilat = (-1)*Decilat
  213. if (EW=='W'):
  214. Decilon = (-1)*Decilon
  215. rgps = str(Decilat) + "," + str(Decilon)
  216. else: rgps = False
  217. except:
  218. pass
  219. sock.close()
  220. gps = threading.Thread(target=nmeaGps)
  221. aff = threading.Thread(target=affiche)
  222. gps.start()
  223. aff.start()
  224. if __name__ == '__main__':
  225. root = Tk()
  226. app = App(root)
  227. root.mainloop()