Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from tkinter.ttk import *
- from tkinter import *
- from PIL import Image
- from PIL import ImageTk
- import webbrowser
- from tkinter.filedialog import askopenfilenames
- import json
- global finestra
- #TERZA SCHERMATA
- def getFeature (data,zonadec):
- CAP='CAP-'+ cap.get('1.0', 'end-1c')
- coord = data["geometries"][0]["coordinates"][0]
- feature={ }
- feature["type"] = "Feature"
- properties = {}
- properties["ID"]= zonadec
- properties["CAP"]=CAP
- feature["properties"] = properties
- geometry = {}
- geometry["type"] = "Polygon"
- geometry["coordinates"] = coord
- feature["geometry"] = geometry
- return feature
- def saveAll():
- global finalName
- finalName=nom.get('1.0', 'end-1c') + '.json'
- script4.destroy()
- i=0
- numFile=len(filename)
- features=[]
- while(i<numFile):
- with open (filename[i]) as file:
- data = json.load(file)
- features.append(getFeature(data,i+1))
- i=i+1
- print(features)
- featureCollection={}
- featureCollection["type"] = "FeatureCollection"
- featureCollection["features"] = features
- featureJson=json.dumps(featureCollection)
- f = open(finalName, 'w')
- f.write(featureJson)
- f.close()
- def Converti():
- global nom, filename,script4
- filename=[]
- Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
- filename = askopenfilenames() # show an "Open" dialog box and return the path to the selected file
- print(filename)
- script4=Toplevel()
- script4.configure(background="limegreen")
- script4.geometry('300x350')
- script4.title("SAVE ALL")
- Label( script4, text='Salva con nome',
- font='Helvetica 12',
- foreground="white",
- width=200,
- background="darkgreen").pack()
- nom=Text(script4,width=50, height=2 )
- nom.pack()
- Label(script4, text="", width=500, height=2, background="limegreen").pack()
- Button(script4, text="SALVA", command=saveAll,
- font="Gill 10 bold",
- background="white",
- foreground="lightseagreen",
- width=30,
- height=2,
- highlightbackground="forestgreen",
- highlightcolor="forestgreen",
- relief=GROOVE).pack()
- script4.mainloop()
- def salvaSuFirebase():
- print(finalName)
- with open(finalName) as f:
- data = json.load(f)
- #pprint(data['features'][0]['geometry']['coordinates'])
- #-----------------------INIZIALIZZAZIONE MANUALE DEI DATI-------------------------
- #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
- i=0
- zonadec=[]
- length=len(filename)
- print(length)
- Cap='CAP-'+cap.get('1.0','end-1c')
- while(i<length):
- zonaFeature=data['features'][i] #max 8 --per la zona7 inserisce 6
- zonadec= zonaFeature['properties']['ID']
- try:
- #Viene invocata la funzione di sopra per l'accesso: accessdb_fb_auth(dsn,SECRET_KEY, email, admin=False)
- db=accessdb_fb_auth('https://prova-d091a.firebaseio.com/','bRVdTkQAasnWqCHZatCpT7jeT7jZv1n5B81xjEKB',"provaprova@gmail.com",True)
- #Inserimento
- db.put('Zones/Areas/'+Cap,'/ZONA-'+str(zonadec).zfill(2),zonaFeature)
- #Con '/ZONA-'+str(zonadec).zfill(2),zonaFeature si costruisce la chiave:
- #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
- #Nel database infatti troverò zone, aree, CAP, ID della singola zona e poi tutto l'oggetto che è una feature
- print("Inserimento nel database avvenuto con successo")
- except:
- print("Errore nell'inserimento")
- i=i+1
- def Step3():
- global cap,script3
- script3=Toplevel()
- script3.configure(background="limegreen")
- script3.geometry('300x350')
- script3.title("CONVERTI")
- Label(script3, text='Inserisci CAP',
- font='Helvetica 12',
- foreground="white",
- width=200,
- background="darkgreen").pack()
- cap=Text(script3,width=50, height=2 )
- cap.pack()
- Label(script3, text="", width=500, height=2, background="limegreen").pack()
- Button(script3, text="CONVERTI IN FEATURECOLLECTION", command=Converti,
- font="Gill 10 bold",
- background="white",
- foreground="lightseagreen",
- width=30,
- height=2,
- highlightbackground="forestgreen",
- highlightcolor="forestgreen",
- relief=GROOVE).pack()
- Button(script3, text="Salva su Firebase", command=salvaSuFirebase,
- font="Gill 10 bold",
- background="white",
- foreground="lightseagreen",
- width=30,
- height=2,
- highlightbackground="forestgreen",
- highlightcolor="forestgreen",
- relief=GROOVE).pack()
- script3.mainloop()
- def richiesta():
- nome=name.get('1.0', 'end-1c')+ '.json'
- r=requests.get('http://polygons.openstreetmap.fr/get_geojson.py?id='+id+'¶ms=0')
- file=open(nome, 'w')
- file.write(r.text)
- file.close()
- script2.destroy()
- def salvaNome():
- global name, script2
- script2=Toplevel()
- script2.configure(background="limegreen")
- script2.geometry('300x350')
- script2.title("SALVA CON NOME")
- Label( script2, text='Salva con nome ',
- font='Helvetica 12',
- foreground="white",
- width=200,
- background="darkgreen").pack()
- name=Text(script2,width=50, height=2 )
- name.pack()
- Label(script2, text="", width=500, height=2, background="limegreen").pack()
- Button(script2, text="SALVA", command=richiesta,
- font="Gill 10 bold",
- background="white",
- foreground="lightseagreen",
- width=30,
- height=2,
- highlightbackground="forestgreen",
- highlightcolor="forestgreen",
- relief=GROOVE).pack()
- script2.mainloop()
- def webBrowser():
- url='https://nominatim.openstreetmap.org/'
- webbrowser.open(url, new=2)
- def step2():
- global id
- id=text.get('1.0', 'end-1c')
- salvaNome()
- def openStreetMap():
- global script, text
- webBrowser()
- script=Tk()
- script.configure(background="limegreen")
- script.geometry('500x700')
- script.title("INSERISCI ID")
- immagine_tk = ImageTk.PhotoImage(Image.open('Sense Square2.png'))
- Label(script, image=immagine_tk, background="white").pack()
- Label( script, text='Inserisci ID della Città o della circoscrizione desiderata',
- font='Helvetica 12',
- foreground="white",
- width=500,
- background="darkgreen").pack()
- Label(script, text="", width=500, height=2, background="limegreen").pack()
- text=Text(script, width=50, height=2)
- text.pack()
- Label(script, text="", width=500, height=2, background="limegreen").pack()
- Button(script, text="SCARICA", command= step2,
- font="Gill 10 bold",
- background="white",
- foreground="lightseagreen",
- width=30,
- height=2,
- highlightbackground="forestgreen",
- highlightcolor="forestgreen",
- relief=GROOVE).pack()
- Label(script, text="", width=500, height=1, background="limegreen").pack()
- Button(script, text="PROCEDI CON LA CONVERSIONE", command= Step3,
- font="Gill 10 bold",
- background="white",
- foreground="lightseagreen",
- width=30,
- height=2,
- highlightbackground="forestgreen",
- highlightcolor="forestgreen",
- relief=GROOVE).pack()
- script.mainloop()
- def accessdb_fb_auth(dsn,SECRET_KEY, email, admin=False):
- from firebase import firebase
- import datetime
- auth=firebase.FirebaseAuthentication(SECRET_KEY, email, admin, admin) #funzione che serve a creare l'autenticazione
- db= firebase.FirebaseApplication(dsn,auth) #accedo al database con l'url "dsn" e l'autorizzazione e istanzio l'oggetto db
- time=datetime.datetime.now() #acquisisco la data per salvare il login d'accesso al DB
- time_log=time.strftime('%d/%m/%Y %H:%M,%S')
- timeforpath=time.strftime('%d%m%Y%H%M%S')
- nameemail=email.split('@')[0]
- db.put("/log/log_"+nameemail, "/datetime"+timeforpath+"/time", time_log) #registro l'orario e la data del login
- db.put("/log/log_"+nameemail, "/datetime"+timeforpath+"/email", email) #registro l'email nel login
- return db #ritorna l'oggetto tramite il quale accediamo al DataBase
- #Funzione che effettua l'assegnazione al particolare utente
- #unico parametro di passaggio, id dell'account ADMIN
- def caricaSuDatabase():
- uid=idUser.get('1.0','end-1c')
- code='CAP-'+ idCap.get('1.0','end-1c')
- type=tipo.get('1.0','end-1c')
- db=accessdb_fb_auth('https://prova-d091a.firebaseio.com/','bRVdTkQAasnWqCHZatCpT7jeT7jZv1n5B81xjEKB',"provaprova@gmail.com",True)
- areas=db.get("/Zones/Areas/"+code+"/",None)
- print(areas)
- db.put("Users/"+uid,"/Areas/"+code,areas)
- db.put("Users/"+uid,"Type",type)
- def inserut():
- global idUser, tipo, idCap
- script5=Tk() #creazione dello script
- script5.configure(background="limegreen")
- script5.geometry('400x300')
- script5.title("Utente")
- #Creazione della label "ID Utente"
- Label(script5, text='ID Utente', font='Helvetica 12',
- foreground="white",
- width=15,
- height=1,
- background="black").pack()
- #Label nella quale inserire l'ID da noi scelto
- idUser=Text(script5, width=35, height=1, font='Helvetica 14') #dimensioni
- idUser.pack()
- Label(script5, text="", width=50, height=1, background="limegreen").pack()
- #Creazione della label "Tipo Utente"
- Label(script5, text='Tipo Utente', font='Helvetica 12',
- foreground="white",
- width=15,
- height=1,
- background="black").pack()
- Label(script5, text="", width=50, height=1, background="limegreen").pack()
- #Label nella quale inserire il tipo
- tipo=Text(script5, width=35, height=1, font='Helvetica 14') #dimensioni
- tipo.pack()
- Label(script5, text="", width=50, height=1, background="limegreen").pack()
- #Creazione della label "CAP"
- Label(script5, text='CAP', font='Helvetica 12',
- foreground="white",
- width=15,
- height=1,
- background="black").pack()
- idCap=Text(script5, width=35, height=1, font='Helvetica 14') #dimensioni
- idCap.pack()
- Label(script5, text="", width=50, height=1, background="limegreen").pack()
- Button(script5, text="Inserisci utente", command=caricaSuDatabase,
- font="Gill 10 bold",
- background="white",
- foreground="lightseagreen",
- width=30,
- height=2,
- highlightbackground="forestgreen",
- highlightcolor="forestgreen",
- relief=GROOVE).pack()
- def step1():
- finestra.destroy()
- openStreetMap()
- finestra= Tk()
- finestra.configure(background="limegreen")
- finestra.geometry('500x600')
- finestra.title("CERCA CITTA'")
- immagine_tk = ImageTk.PhotoImage(Image.open('Sense Square2.png'))
- Label(finestra, image=immagine_tk, background="white").pack()
- Label(finestra, text="", width=500, height=2, background="limegreen").pack()
- Button(finestra, text="CERCA CITTA'", command=step1,
- font="Gill 10 bold",
- background="white",
- foreground="lightseagreen",
- width=30,
- height=2,
- highlightbackground="forestgreen",
- highlightcolor="forestgreen",
- relief=GROOVE).pack()
- #Label di separazione tra i due button
- Label(finestra, text="", width=30, height=1, background="limegreen").pack()
- Button(finestra, text="Inserisci utente", command=inserut,
- font="Gill 10 bold",
- background="white",
- foreground="lightseagreen",
- width=30,
- height=2,
- highlightbackground="forestgreen",
- highlightcolor="forestgreen",
- relief=GROOVE).pack()
- finestra.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement