Advertisement
Guest User

Untitled

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