Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.18 KB | None | 0 0
  1. import requests
  2. from tkinter.ttk import *
  3. from tkinter import *
  4. from PIL import Image
  5. from PIL import ImageTk
  6. import webbrowser
  7. from tkinter.filedialog import askopenfilenames
  8. import json
  9. global finestra
  10. from firebase import firebase
  11. import datetime
  12.  
  13.  
  14. def getFeature (data,zonadec):
  15.  
  16. CAP='CAP-'+ cap.get('1.0', 'end-1c')
  17. coord = data["geometries"][0]["coordinates"][0]
  18. feature={ }
  19. feature["type"] = "Feature"
  20.  
  21. properties = {}
  22. properties["ID"]= zonadec
  23. properties["CAP"]=CAP
  24.  
  25. feature["properties"] = properties
  26.  
  27. geometry = {}
  28. geometry["type"] = "Polygon"
  29. geometry["coordinates"] = coord
  30.  
  31. feature["geometry"] = geometry
  32.  
  33.  
  34.  
  35. return feature
  36.  
  37. def saveAll():
  38. global finalName
  39. finalName=nom.get('1.0', 'end-1c') + '.json'
  40. script4.destroy()
  41. i=0
  42. numFile=len(filename)
  43. features=[]
  44. while(i<numFile):
  45. with open (filename[i]) as file:
  46. data = json.load(file)
  47. features.append(getFeature(data,i+1))
  48. i=i+1
  49.  
  50. featureCollection={}
  51. featureCollection["type"] = "FeatureCollection"
  52.  
  53. featureCollection["features"] = features
  54.  
  55. featureJson=json.dumps(featureCollection)
  56.  
  57. f = open(finalName, 'w')
  58. f.write(featureJson)
  59. f.close()
  60.  
  61.  
  62. def Converti():
  63. global nom, filename,script4
  64. filename=[]
  65. Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
  66. filename = askopenfilenames() # show an "Open" dialog box and return the path to the selected file
  67. print(filename)
  68. script4=Toplevel()
  69. script4.configure(background="limegreen")
  70. script4.geometry('300x350')
  71. script4.title("SAVE ALL")
  72. Label( script4, text='Salva con nome',
  73. font='Helvetica 12',
  74. foreground="white",
  75. width=200,
  76. background="darkgreen").pack()
  77. nom=Text(script4,width=50, height=2 )
  78. nom.pack()
  79. Label(script4, text="", width=500, height=2, background="limegreen").pack()
  80. Button(script4, text="SALVA", command=saveAll,
  81. font="Gill 10 bold",
  82. background="white",
  83. foreground="lightseagreen",
  84. width=30,
  85. height=2,
  86. highlightbackground="forestgreen",
  87. highlightcolor="forestgreen",
  88. relief=GROOVE).pack()
  89.  
  90.  
  91.  
  92. script4.mainloop()
  93.  
  94. def salvaSuFirebase():
  95.  
  96. print(finalName)
  97. with open(finalName) as f:
  98. data = json.load(f)
  99. i=0
  100. zonadec=[]
  101. length=len(filename)
  102. Cap='CAP-'+cap.get('1.0','end-1c')
  103. while(i<length):
  104. zonaFeature=data['features'][i] #max 8 --per la zona7 inserisce 6
  105. zonadec= zonaFeature['properties']['ID']
  106. try:
  107. db=accessdb_fb_auth('https://prova-d091a.firebaseio.com/','bRVdTkQAasnWqCHZatCpT7jeT7jZv1n5B81xjEKB',"provaprova@gmail.com",True)
  108. db.put('Zones/Areas/'+Cap,'/ZONA-'+str(zonadec).zfill(2),zonaFeature)
  109. print("Inserimento nel database avvenuto con successo")
  110. except:
  111. print("Errore nell'inserimento")
  112. i=i+1
  113.  
  114. def Step3():
  115. global cap,script3
  116. script3=Toplevel()
  117. script3.configure(background="limegreen")
  118. script3.geometry('300x350')
  119. script3.title("CONVERTI")
  120. Label(script3, text='Inserisci CAP',
  121. font='Helvetica 12',
  122. foreground="white",
  123. width=200,
  124. background="darkgreen").pack()
  125. cap=Text(script3,width=50, height=2 )
  126. cap.pack()
  127. Label(script3, text="", width=500, height=2, background="limegreen").pack()
  128. Button(script3, text="CONVERTI IN FEATURECOLLECTION", command=Converti,
  129. font="Gill 10 bold",
  130. background="white",
  131. foreground="lightseagreen",
  132. width=30,
  133. height=2,
  134. highlightbackground="forestgreen",
  135. highlightcolor="forestgreen",
  136. relief=GROOVE).pack()
  137.  
  138. Button(script3, text="SALVA SU FIREBASE", command=salvaSuFirebase,
  139. font="Gill 10 bold",
  140. background="white",
  141. foreground="lightseagreen",
  142. width=30,
  143. height=2,
  144. highlightbackground="forestgreen",
  145. highlightcolor="forestgreen",
  146. relief=GROOVE).pack()
  147.  
  148.  
  149.  
  150. script3.mainloop()
  151.  
  152. def richiesta():
  153. nome=name.get('1.0', 'end-1c')+ '.json'
  154. r=requests.get('http://polygons.openstreetmap.fr/get_geojson.py?id='+id+'&params=0')
  155. file=open(nome, 'w')
  156. file.write(r.text)
  157. file.close()
  158. script2.destroy()
  159.  
  160. def salvaNome():
  161.  
  162. global name, script2
  163. script2=Toplevel()
  164. script2.configure(background="limegreen")
  165. script2.geometry('300x350')
  166. script2.title("SALVA CON NOME")
  167. Label( script2, text='Salva con nome ',
  168. font='Helvetica 12',
  169. foreground="white",
  170. width=200,
  171. background="darkgreen").pack()
  172. name=Text(script2,width=50, height=2 )
  173. name.pack()
  174. Label(script2, text="", width=500, height=2, background="limegreen").pack()
  175. Button(script2, text="SALVA", command=richiesta,
  176. font="Gill 10 bold",
  177. background="white",
  178. foreground="lightseagreen",
  179. width=30,
  180. height=2,
  181. highlightbackground="forestgreen",
  182. highlightcolor="forestgreen",
  183. relief=GROOVE).pack()
  184.  
  185.  
  186.  
  187.  
  188.  
  189. script2.mainloop()
  190.  
  191. def webBrowser():
  192. url='https://nominatim.openstreetmap.org/'
  193. webbrowser.open(url, new=2)
  194.  
  195. def step2():
  196. global id
  197. id=text.get('1.0', 'end-1c')
  198. salvaNome()
  199.  
  200. def openStreetMap():
  201. global script, text
  202. webBrowser()
  203. script=Tk()
  204. script.configure(background="limegreen")
  205. script.geometry('500x700')
  206. script.title("INSERISCI ID")
  207. immagine_tk = ImageTk.PhotoImage(Image.open('Sense Square1.png'))
  208. Label(script, image=immagine_tk, background="white").pack()
  209. Label( script, text='Inserisci ID della Città o della circoscrizione desiderata',
  210. font='Helvetica 12',
  211. foreground="white",
  212. width=500,
  213. background="darkgreen").pack()
  214. Label(script, text="", width=500, height=2, background="limegreen").pack()
  215. text=Text(script, width=50, height=2)
  216. text.pack()
  217. Label(script, text="", width=500, height=2, background="limegreen").pack()
  218. Button(script, text="SCARICA", command= step2,
  219. font="Gill 10 bold",
  220. background="white",
  221. foreground="lightseagreen",
  222. width=30,
  223. height=2,
  224. highlightbackground="forestgreen",
  225. highlightcolor="forestgreen",
  226. relief=GROOVE).pack()
  227. Label(script, text="", width=500, height=1, background="limegreen").pack()
  228. Button(script, text="PROCEDI CON LA CONVERSIONE", command= Step3,
  229. font="Gill 10 bold",
  230. background="white",
  231. foreground="lightseagreen",
  232. width=30,
  233. height=2,
  234. highlightbackground="forestgreen",
  235. highlightcolor="forestgreen",
  236. relief=GROOVE).pack()
  237.  
  238. script.mainloop()
  239.  
  240. def accessdb_fb_auth(dsn,SECRET_KEY, email, admin=False):
  241.  
  242. auth=firebase.FirebaseAuthentication(SECRET_KEY, email, admin, admin) #funzione che serve a creare l'autenticazione
  243. db= firebase.FirebaseApplication(dsn,auth) #accedo al database con l'url "dsn" e l'autorizzazione e istanzio l'oggetto db
  244.  
  245. time=datetime.datetime.now() #acquisisco la data per salvare il login d'accesso al DB
  246. time_log=time.strftime('%d/%m/%Y %H:%M,%S')
  247. timeforpath=time.strftime('%d%m%Y%H%M%S')
  248.  
  249. nameemail=email.split('@')[0]
  250. db.put("/log/log_"+nameemail, "/datetime"+timeforpath+"/time", time_log) #registro l'orario e la data del login
  251. db.put("/log/log_"+nameemail, "/datetime"+timeforpath+"/email", email) #registro l'email nel login
  252. return db #ritorna l'oggetto tramite il quale accediamo al DataBase
  253.  
  254. #Funzione che effettua l'assegnazione al particolare utente
  255. #unico parametro di passaggio, id dell'account ADMIN
  256.  
  257. def caricaPrivatoSuDatabase():
  258. uid=idUserPriv.get('1.0','end-1c')
  259. code='CAP-'+ idCap.get('1.0','end-1c')
  260. zonesArray=[]
  261.  
  262.  
  263. db=accessdb_fb_auth('https://prova-d091a.firebaseio.com/','bRVdTkQAasnWqCHZatCpT7jeT7jZv1n5B81xjEKB',"provaprova@gmail.com",True)
  264. areas=db.get("/Zones/Areas/"+code+"/",None)
  265. for y in areas:
  266. zonesArray.append({"cap":code,"zone":y})
  267.  
  268. db.put("Users/"+uid,"/Areas/",zonesArray)
  269. db.put("Users/"+uid,"Type",'B')
  270.  
  271.  
  272.  
  273. def utentePrivato():
  274. global idUserPriv, idCap
  275. script5=Tk() #creazione dello script
  276. script5.configure(background="limegreen")
  277. script5.geometry('500x600')
  278. script5.title("Utente Privato")
  279.  
  280. Label(script5, text="ID UTENTE", width=500, height=1, background="darkgreen",font="Helvetica 12 bold",foreground="white").pack()
  281. idUserPriv=Text(script5, width=500, height=1, font='Helvetica 14') #dimensioni
  282. idUserPriv.pack()
  283. Label(script5, text="", width=500, height=1, background="limegreen").pack()
  284. Label(script5, text="CAP", width=500, height=1, background="darkgreen",font="Helvetica 12 bold",foreground="white").pack()
  285.  
  286. idCap=Text(script5, width=500, height=1, font='Helvetica 14') #dimensioni
  287. idCap.pack()
  288. Label(script5, text="", width=500, height=1, background="limegreen").pack()
  289. Button(script5, text="Inserisci utente privato", command=caricaPrivatoSuDatabase,
  290. font="Gill 10 bold",
  291. background="white",
  292. foreground="lightseagreen",
  293. width=30,
  294. height=2,
  295. highlightbackground="forestgreen",
  296. highlightcolor="forestgreen",
  297. relief=GROOVE).pack()
  298.  
  299. def caricaPubblicoSuDatabase():
  300. uid=idUserPub.get('1.0','end-1c')
  301. db=accessdb_fb_auth('https://prova-d091a.firebaseio.com/','bRVdTkQAasnWqCHZatCpT7jeT7jZv1n5B81xjEKB',"provaprova@gmail.com",True)
  302. db.put("Users/"+uid,"Type",'A')
  303.  
  304. def utentePubblico():
  305. global idUserPub
  306. script6=Tk() #creazione dello script
  307. script6.configure(background="limegreen")
  308. script6.geometry('500x600')
  309. script6.title("Utente Pubblico")
  310.  
  311. Label(script6, text="ID UTENTE", width=500, height=1, background="darkgreen",font="Helvetica 12 bold",foreground="white").pack()
  312. idUserPub=Text(script6, width=500, height=1, font='Helvetica 14') #dimensioni
  313. idUserPub.pack()
  314. Label(script6, text="", width=500, height=1, background="limegreen").pack()
  315. Button(script6, text="Inserisci utente pubblico", command=caricaPubblicoSuDatabase,
  316. font="Gill 10 bold",
  317. background="white",
  318. foreground="lightseagreen",
  319. width=30,
  320. height=2,
  321. highlightbackground="forestgreen",
  322. highlightcolor="forestgreen",
  323. relief=GROOVE).pack()
  324.  
  325. def caricaAdminSuDatabase():
  326. uid=idUserAdmin.get('1.0','end-1c')
  327. db=accessdb_fb_auth('https://prova-d091a.firebaseio.com/','bRVdTkQAasnWqCHZatCpT7jeT7jZv1n5B81xjEKB',"provaprova@gmail.com",True)
  328. areas=db.get("/Zones/Areas",None)
  329. zonesArray=[]
  330. for x in areas:
  331. for y in areas[x]:
  332. zonesArray.append({"cap":x,"zone":y})
  333.  
  334. db.put("Users/"+uid,"/Areas",zonesArray)
  335. db.put("Users/"+uid,"Type","C")
  336.  
  337. def utenteAdmin():
  338. global idUserAdmin
  339. script7=Tk() #creazione dello script
  340. script7.configure(background="limegreen")
  341. script7.geometry('500x600')
  342. script7.title("Utente Admin")
  343.  
  344. Label(script7, text="ID UTENTE", width=500, height=1, background="darkgreen",font="Helvetica 12 bold",foreground="white").pack()
  345. idUserAdmin=Text(script7, width=500, height=1, font='Helvetica 14') #dimensioni
  346. idUserAdmin.pack()
  347. Label(script7, text="", width=500, height=1, background="limegreen").pack()
  348. Button(script7, text="Inserisci utente Admin", command=caricaAdminSuDatabase,
  349. font="Gill 10 bold",
  350. background="white",
  351. foreground="lightseagreen",
  352. width=30,
  353. height=2,
  354. highlightbackground="forestgreen",
  355. highlightcolor="forestgreen",
  356. relief=GROOVE).pack()
  357.  
  358. def insertUtent():
  359. script8=Tk()
  360. script8.configure(background="limegreen")
  361. script8.geometry('500x600')
  362. script8.title("Utente Privato")
  363. Label(script8, text="SCEGLI TIPOLOGIA DI UTENTE", width=500, height=1, background="darkgreen",font="Helvetica 12 bold",foreground="white").pack()
  364. Label(script8, text="", width=500, height=1, background="limegreen").pack()
  365. Button(script8, text="A: UTENTE PUBBLICO", command=utentePubblico,
  366. font="Gill 10 bold",
  367. background="white",
  368. foreground="lightseagreen",
  369. width=30,
  370. height=2,
  371. highlightbackground="forestgreen",
  372. highlightcolor="forestgreen",
  373. relief=GROOVE).pack()
  374. Label(script8, text="", width=500, height=1, background="limegreen").pack()
  375. Button(script8, text="B: UTENTE PRIVATO", command=utentePrivato,
  376. font="Gill 10 bold",
  377. background="white",
  378. foreground="lightseagreen",
  379. width=30,
  380. height=2,
  381. highlightbackground="forestgreen",
  382. highlightcolor="forestgreen",
  383. relief=GROOVE).pack()
  384. Label(script8, text="", width=500, height=1, background="limegreen").pack()
  385. Button(script8, text="C: UTENTE ADMIN", command=utenteAdmin,
  386. font="Gill 10 bold",
  387. background="white",
  388. foreground="lightseagreen",
  389. width=30,
  390. height=2,
  391. highlightbackground="forestgreen",
  392. highlightcolor="forestgreen",
  393. relief=GROOVE).pack()
  394. def step1():
  395. finestra.destroy()
  396. openStreetMap()
  397.  
  398. finestra= Tk()
  399. finestra.configure(background="limegreen")
  400. finestra.geometry('500x600')
  401. finestra.title("CERCA CITTA'")
  402. immagine_tk = ImageTk.PhotoImage(Image.open('Sense Square1.png'))
  403. Label(finestra, image=immagine_tk, background="white").pack()
  404. Label(finestra, text="", width=500, height=2, background="limegreen").pack()
  405. Button(finestra, text="CERCA CITTA'", command=step1,
  406. font="Gill 10 bold",
  407. background="white",
  408. foreground="lightseagreen",
  409. width=30,
  410. height=2,
  411. highlightbackground="forestgreen",
  412. highlightcolor="forestgreen",
  413. relief=GROOVE).pack()
  414.  
  415. #Label di separazione tra i due button
  416. Label(finestra, text="", width=500, height=1, background="limegreen").pack()
  417.  
  418. Button(finestra, text="INSERISCI UTENTE", command=insertUtent,
  419. font="Gill 10 bold",
  420. background="white",
  421. foreground="lightseagreen",
  422. width=30,
  423. height=2,
  424. highlightbackground="forestgreen",
  425. highlightcolor="forestgreen",
  426. relief=GROOVE).pack()
  427.  
  428.  
  429.  
  430. finestra.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement