From ebb72cb845203f3a3a18f5dec167a5e5a9161c21 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Sat, 5 Nov 2022 14:26:59 +0100 Subject: [PATCH 1/5] roof area image optional --- Sonnendach.py | 111 +++++++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 50 deletions(-) diff --git a/Sonnendach.py b/Sonnendach.py index 86b1659..66956dd 100644 --- a/Sonnendach.py +++ b/Sonnendach.py @@ -12,7 +12,8 @@ from tkinter import filedialog import time from selenium.webdriver.common.action_chains import ActionChains -DropDownLabels = ["Street: ", "Number: ", "Postal code: ", "City: ", "Sonnendach URL: ", "Suitability", "Image Filename Map: ", "Image Filename Production: ", "Image Filename qrcode: ", "PV Production 50", "PV Production 75", "PV Production 100", "Value Electricity production"] +create_images = True +DropDownLabels = ["Street: ", "Number: ", "Postal code: ", "City: ", "Sonnendach URL: ", "Suitability", "Image Filename Map: ", "Image Filename Production: ", "Image Filename qrcode: ", "PV Production 50", "PV Production 75", "PV Production 100", "Value Electricity production", "Roof area"] file_split_char = "," OptionList = [] exit = False @@ -41,8 +42,9 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): if (not os.path.exists("qrcodes")): os.makedirs("qrcodes", exist_ok=False) + address_count = len(adress_list) #Search for each adress in the list and create screenshots and qrcodes - for i in range(len(adress_list)): + for i in range(address_count): line = adress_list[i] adress = line.split(file_split_char) if((line != adress_list[0]) & (adress[columnIndexes[4]] == "") & (len(adress[columnIndexes[0]]) > 0) & (len(adress[columnIndexes[1]]) > 0) & (len(adress[columnIndexes[2]]) > 0) & (len(adress[columnIndexes[3]]) > 0)): @@ -71,67 +73,75 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): pv_Production75 = driver.find_element(By.ID, "pv75").text.replace("'", "") pv_Production100 = driver.find_element(By.ID, "pv100").text.replace("'", "") value_electricity_production = driver.find_elements(By.XPATH, "//h2[@id='TitelSolarstrom']//strong")[2].text.replace("'", "").replace(" Franken", "") + roof_area = driver.find_element(By.ID, "areaOutput") + if(create_images): - image_filename = suitability + " - " + search_string - image_folder_map = "screenshots/" - image_filename_map = image_filename + " map" + ".png" - image_folder_production = "screenshots/" - image_filename_production = image_filename + " production" + ".png" - image_folder_qrcode = "qrcodes/" - image_filename_qrcode = image_filename + " qrcode" + ".png" + image_filename = suitability + " - " + search_string + image_folder_map = "screenshots/" + image_filename_map = image_filename + " map" + ".png" + image_folder_production = "screenshots/" + image_filename_production = image_filename + " production" + ".png" + image_folder_qrcode = "qrcodes/" + image_filename_qrcode = image_filename + " qrcode" + ".png" - # Create QR-Code - qr = qrcode.QRCode(version=1, box_size=10, border=5) - qr.add_data(url) - qr.make(fit=True) - qr.make_image(fill='black', back_color='white').save(image_folder_qrcode + image_filename_qrcode) + # Create QR-Code + qr = qrcode.QRCode(version=1, box_size=10, border=5) + qr.add_data(url) + qr.make(fit=True) + qr.make_image(fill='black', back_color='white').save(image_folder_qrcode + image_filename_qrcode) - # Create Screenshot of Map - actions = ActionChains(driver) - try: - driver.execute_script("""var l = document.getElementsByClassName("ol-zoom ol-unselectable ol-control")[0]; - l.parentNode.removeChild(l);""") - except: - pass + # Create Screenshot of Map + actions = ActionChains(driver) + try: + driver.execute_script("""var l = document.getElementsByClassName("ol-zoom ol-unselectable ol-control")[0]; + l.parentNode.removeChild(l);""") + except: + pass - mapElement = driver.find_element(By.XPATH, - "//div[@id='map']//div[@class='ol-viewport']") - location = mapElement.location - size = mapElement.size - x = location["x"] - y = location["y"] - w = x + size["width"] - h = size["height"] - area = (x, 0, w, h) - driver.execute_script("window.scrollTo(0, " + str(y) + ")") - time.sleep(1) - driver.save_screenshot(image_folder_map + image_filename_map) - time.sleep(0.2) - Image.open(image_folder_map + image_filename_map).crop(area).save( - image_folder_map + image_filename_map) - - # Create Screenshot of Chart - try: - chartElement = driver.find_elements(By.XPATH, "//div[@id='chart']//*")[0] - location = chartElement.location - size = chartElement.size + mapElement = driver.find_element(By.XPATH, + "//div[@id='map']//div[@class='ol-viewport']") + location = mapElement.location + size = mapElement.size x = location["x"] y = location["y"] w = x + size["width"] h = size["height"] area = (x, 0, w, h) driver.execute_script("window.scrollTo(0, " + str(y) + ")") - time.sleep(1) - driver.save_screenshot(image_folder_production + image_filename_production) + driver.save_screenshot(image_folder_map + image_filename_map) time.sleep(0.2) - Image.open(image_folder_production + image_filename_production).crop(area).save( - image_folder_production + image_filename_production) - except: - image_filename_production = "not-found" + Image.open(image_folder_map + image_filename_map).crop(area).save( + image_folder_map + image_filename_map) - print(image_filename_map + " was saved.") + # Create Screenshot of Chart + try: + chartElement = driver.find_elements(By.XPATH, "//div[@id='chart']//*")[0] + location = chartElement.location + size = chartElement.size + x = location["x"] + y = location["y"] + w = x + size["width"] + h = size["height"] + area = (x, 0, w, h) + driver.execute_script("window.scrollTo(0, " + str(y) + ")") + + time.sleep(1) + driver.save_screenshot(image_folder_production + image_filename_production) + time.sleep(0.2) + Image.open(image_folder_production + image_filename_production).crop(area).save( + image_folder_production + image_filename_production) + except: + image_filename_production = "not-found" + + else: + image_filename_map = "not created" + image_filename_production = "not created" + image_filename_qrcode = "not created" + + print("saved Address: " + search_string) + print("Address " + str(i) + " of " + str(address_count)) else: url = "not-found" suitability = "not-found" @@ -142,6 +152,7 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): pv_Production75 = "not-found" pv_Production100 = "not-found" value_electricity_production = "not-found" + roof_area = "not-found" print("not found: " + search_string) @@ -157,6 +168,7 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): adress[columnIndexes[10]] = pv_Production75 adress[columnIndexes[11]] = pv_Production100 adress[columnIndexes[12]] = value_electricity_production + adress[columnIndexes[13]] = roof_area adress_file = open(filename_adresslist, "w", encoding="utf-8") new_line_string = "" for j in adress: @@ -178,7 +190,6 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): print("closing Thread") return - def read_adresslist(filename_adresslist): returnvalues = [] try: -- 2.40.1 From af460021b05e50eb8434167170a6921f5d27fa62 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 7 Nov 2022 09:58:57 +0100 Subject: [PATCH 2/5] added usage of api --- Sonnendach.py | 173 +++++++++++++++++++++++++++++++++++++------------- api-Test.py | 57 +++++++++++++++++ 2 files changed, 185 insertions(+), 45 deletions(-) create mode 100644 api-Test.py diff --git a/Sonnendach.py b/Sonnendach.py index 66956dd..3d1f908 100644 --- a/Sonnendach.py +++ b/Sonnendach.py @@ -10,9 +10,12 @@ import os import qrcode from tkinter import filedialog import time +import datetime +from dateutil.relativedelta import relativedelta from selenium.webdriver.common.action_chains import ActionChains +import json +import requests -create_images = True DropDownLabels = ["Street: ", "Number: ", "Postal code: ", "City: ", "Sonnendach URL: ", "Suitability", "Image Filename Map: ", "Image Filename Production: ", "Image Filename qrcode: ", "PV Production 50", "PV Production 75", "PV Production 100", "Value Electricity production", "Roof area"] file_split_char = "," OptionList = [] @@ -34,7 +37,7 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): global stopThread global columnIndexes global outputtext - + global checkBoxCreateImages #Create Direcotrys to save screenshots and qrcodes if they don't exist yet. if(not os.path.exists("screenshots")): @@ -42,41 +45,42 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): if (not os.path.exists("qrcodes")): os.makedirs("qrcodes", exist_ok=False) + startTime = datetime.datetime.now() address_count = len(adress_list) #Search for each adress in the list and create screenshots and qrcodes for i in range(address_count): line = adress_list[i] adress = line.split(file_split_char) if((line != adress_list[0]) & (adress[columnIndexes[4]] == "") & (len(adress[columnIndexes[0]]) > 0) & (len(adress[columnIndexes[1]]) > 0) & (len(adress[columnIndexes[2]]) > 0) & (len(adress[columnIndexes[3]]) > 0)): - print(adress) search_string = adress[columnIndexes[0]] + " " + adress[columnIndexes[1]] + " " + adress[columnIndexes[2]] + " " + adress[columnIndexes[3]] - search_bar = driver.find_element(By.ID, "searchTypeahead1") - search_bar.send_keys(Keys.CONTROL + "a") - search_bar.send_keys(Keys.DELETE) - search_bar.send_keys(search_string) - driver.implicitly_wait(10) - found = False - suggestions = driver.find_elements(By.XPATH, "//div[@class='tt-suggestion tt-selectable']") - for suggestion in suggestions: - if(suggestion.text == search_string): - found = True - suggestion.click() - driver.implicitly_wait(10) - break + if(createImages.get() > 0): + search_bar = driver.find_element(By.ID, "searchTypeahead1") + search_bar.send_keys(Keys.CONTROL + "a") + search_bar.send_keys(Keys.DELETE) + search_bar.send_keys(search_string) + driver.implicitly_wait(10) + time.sleep(0.7) + found = False + suggestions = driver.find_elements(By.XPATH, "//div[@class='tt-suggestion tt-selectable']") + for suggestion in suggestions: + if(suggestion.text == search_string): + found = True + suggestion.click() + driver.implicitly_wait(10) + break - if(found): - time.sleep(2) - url = driver.current_url - suitability = driver.find_element(By.ID, "eignung").text - pv_Production50 = driver.find_element(By.ID, "pv50").text.replace("'", "") - pv_Production75 = driver.find_element(By.ID, "pv75").text.replace("'", "") - pv_Production100 = driver.find_element(By.ID, "pv100").text.replace("'", "") - value_electricity_production = driver.find_elements(By.XPATH, "//h2[@id='TitelSolarstrom']//strong")[2].text.replace("'", "").replace(" Franken", "") - roof_area = driver.find_element(By.ID, "areaOutput") - - if(create_images): + if(found): + time.sleep(0.7) + url = driver.current_url + suitability = driver.find_element(By.ID, "eignung").text + pv_Production50 = driver.find_element(By.ID, "pv50").text.replace("'", "") + pv_Production75 = driver.find_element(By.ID, "pv75").text.replace("'", "") + pv_Production100 = driver.find_element(By.ID, "pv100").text.replace("'", "") + value_electricity_production = driver.find_elements(By.XPATH, "//h2[@id='TitelSolarstrom']//strong")[2].text.replace("'", "").replace(" Franken", "") + roof_area = driver.find_element(By.ID, "areaOutput").text + time.sleep(2) image_filename = suitability + " - " + search_string image_folder_map = "screenshots/" image_filename_map = image_filename + " map" + ".png" @@ -135,27 +139,103 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): except: image_filename_production = "not-found" + print("saved Address: " + search_string) else: - image_filename_map = "not created" - image_filename_production = "not created" - image_filename_qrcode = "not created" + url = "not-found" + suitability = "not-found" + image_filename_map = "not-found" + image_filename_production = "not-found" + image_filename_qrcode = "not-found" + pv_Production50 = "not-found" + pv_Production75 = "not-found" + pv_Production100 = "not-found" + value_electricity_production = "not-found" + roof_area = "not-found" - print("saved Address: " + search_string) - print("Address " + str(i) + " of " + str(address_count)) - else: - url = "not-found" - suitability = "not-found" - image_filename_map = "not-found" - image_filename_production = "not-found" - image_filename_qrcode = "not-found" - pv_Production50 = "not-found" - pv_Production75 = "not-found" - pv_Production100 = "not-found" - value_electricity_production = "not-found" - roof_area = "not-found" + print("not found: " + search_string) + else: # If no images must be created + try: + # Address to coordinates + params = { + "type": "locations", + "searchText": search_string, + "lang": "de"} + locationsResponse = requests.get('https://api3.geo.admin.ch/rest/services/api/SearchServer', + params=params) + locations = json.loads(locationsResponse.content.decode()) + firstLocation = locations['results'][0]['attrs'] - print("not found: " + search_string) + # you need the coordinates from other services, once you have them + coordinates = (firstLocation['y'], firstLocation['x']) # example coordinates + coordinates_str = ','.join(map(str, coordinates)) + tolerance = 1 + + # this is wrong, bbox should be computed using tolerance and pixel size, but there are no problems for the + # purpose of this exercise + bbox = (coordinates[0] - tolerance, coordinates[1] - tolerance, coordinates[0] + tolerance, + coordinates[1] + tolerance) + bbox_str = ','.join(map(str, bbox)) + params = { + "geometryType": "esriGeometryPoint", + "returnGeometry": "true", + "layers": "all:ch.bfe.solarenergie-eignung-daecher", + "geometry": coordinates_str, + "mapExtent": bbox_str, + "imageDisplay": "1391,1070,96", + "tolerance": tolerance, + "order": "distance", + "lang": "de", + } + # retrieve one roof and the building_id associated + response_one_roof = requests.get('https://api3.geo.admin.ch/rest/services/api/MapServer/identify', + params=params) + response_one_roof_decoded = json.loads(response_one_roof.content.decode()) + results = (response_one_roof_decoded['results']) + # for result in results: + # print(result) + building_id = (response_one_roof_decoded)['results'][0]['attributes']['building_id'] + + # use the building_id of previous request to get all roofs associated with this building_id + params = { + 'layer': 'ch.bfe.solarenergie-eignung-daecher', + 'searchField': 'building_id', + 'contains': 'false', + 'searchText': str(building_id), + 'returnGeometry': 'false' + } + response_all_roofs = requests.get('https://api3.geo.admin.ch/rest/services/api/MapServer/find', + params=params) + response_all_roofs_decoded = json.loads(response_all_roofs.content.decode())['results'] + best_roof = response_all_roofs_decoded[0] # TODO: Find best roof + for roof in response_all_roofs_decoded: + if(float(roof['attributes']['flaeche']) > float(best_roof['attributes']['flaeche'])): + best_roof = roof + url = "https://www.uvek-gis.admin.ch/BFE/sonnendach/index.html?featureId=" + str(best_roof['featureId']) + "&lang=de" + suitability = str(best_roof['attributes']['klasse']) + image_filename_map = "not-created" + image_filename_production = "not-created" + image_filename_qrcode = "not-created" + pv_Production100 = str(float(best_roof['attributes']['gstrahlung'])*0.2*0.8) + pv_Production75 = str(float(pv_Production100) * 0.75) + pv_Production50 = str(float(pv_Production100) * 0.5) + value_electricity_production = str(best_roof['attributes']['finanzertrag']) + roof_area = str(best_roof['attributes']['flaeche']) + print("saved Address: " + search_string) + except: + url = "not-found" + suitability = "not-found" + image_filename_map = "not-found" + image_filename_production = "not-found" + image_filename_qrcode = "not-found" + pv_Production50 = "not-found" + pv_Production75 = "not-found" + pv_Production100 = "not-found" + value_electricity_production = "not-found" + roof_area = "not-found" + print("Address " + str(i) + " of " + str(address_count)) + timediff = relativedelta(datetime.datetime.now(), startTime) + print("Process is running since %d years %d months %d days %d hours %d minutes %d seconds" % (timediff.years, timediff.months, timediff.days, timediff.hours, timediff.minutes, timediff.seconds)) #Write back into file @@ -254,7 +334,7 @@ def command(): outputtext = outputtext + "Searching File " + filename_adresslist + "\n" mainText.config(text=outputtext) adress_list_result = read_adresslist(filename_adresslist) - if(adress_list_result): + if(adress_list_result[0]): adresslist = adress_list_result[1] outputtext = outputtext + "File " + filename_adresslist + " found." + "\n" mainText.config(text=outputtext) @@ -323,6 +403,9 @@ for i in range(len(DropDownLabels)): frameButtons = tkinter.Frame(root) frameButtons.grid(row=3, column=1) +createImages = tkinter.IntVar() +checkBoxCreateImages = tkinter.Checkbutton(frameButtons, text="create images", variable=createImages) +checkBoxCreateImages.grid() button1 = tkinter.Button(frameButtons, text="Select Adresslist file", command=command, width=20, height=2, bg="#FCCA03") button1.grid(row=3, column=1, padx=10, pady=3) button2 = tkinter.Button(frameButtons, text="EXIT", command=command_exit, width=20, height=2, bg="#FCCA03") diff --git a/api-Test.py b/api-Test.py new file mode 100644 index 0000000..05d19d2 --- /dev/null +++ b/api-Test.py @@ -0,0 +1,57 @@ +#take a roof knowing the coordinates and use it to retrieve all the others "on the same building" +import json +import requests + +search_string = "Toggenburgstrasse 31 8245 Feuerthalen" + +#Address to coordinates +params={ +"type":"locations", +"searchText":search_string, +"lang":"de"} +locationsResponse = requests.get('https://api3.geo.admin.ch/rest/services/api/SearchServer', params=params) +locations = json.loads(locationsResponse.content.decode()) +firstLocation = locations['results'][0]['attrs'] + +# you need the coordinates from other services, once you have them +coordinates=(firstLocation['y'],firstLocation['x']) # example coordinates +coordinates_str=','.join(map(str,coordinates)) +tolerance=1 + +# this is wrong, bbox should be computed using tolerance and pixel size, but there are no problems for the +# purpose of this exercise +bbox=(coordinates[0]-tolerance, coordinates[1]-tolerance, coordinates[0]+tolerance,coordinates[1]+tolerance) +bbox_str=','.join(map(str,bbox)) +params={ +"geometryType":"esriGeometryPoint", +"returnGeometry":"true", +"layers":"all:ch.bfe.solarenergie-eignung-daecher", +"geometry":coordinates_str, +"mapExtent":bbox_str, +"imageDisplay":"1391,1070,96", +"tolerance":tolerance, +"order":"distance", +"lang":"de", +} +# retrieve one roof and the building_id associated +response_one_roof = requests.get('https://api3.geo.admin.ch/rest/services/api/MapServer/identify', params=params) +response_one_roof_decoded = json.loads(response_one_roof.content.decode()) +results = (response_one_roof_decoded['results']) +# for result in results: +# print(result['attributes']['building_id']) +building_id=(response_one_roof_decoded)['results'][0]['attributes']['building_id'] + +# use the building_id of previous request to get all roofs associated with this building_id +params = { +'layer': 'ch.bfe.solarenergie-eignung-daecher', +'searchField': 'building_id', +'contains': 'false', +'searchText': str(building_id), +'returnGeometry': 'false' +} + +response_all_roofs = requests.get('https://api3.geo.admin.ch/rest/services/api/MapServer/find', params=params) +response_all_roofs_decoded = json.loads(response_all_roofs.content.decode())['results'] +for roof in response_all_roofs_decoded: + # print(roof['attributes'].keys()) + print("URL:", "https://www.uvek-gis.admin.ch/BFE/sonnendach/index.html?featureId=" + str(roof['featureId']) + "&lang=de", "Fläche:", roof['attributes']['flaeche'], "Eignung(1-5):", roof['attributes']['klasse'], "PV100:", float(roof['attributes']['gstrahlung']*0.2*0.8)) \ No newline at end of file -- 2.40.1 From b620704a54121c17242b3c46aa4d21890bf2fab5 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 7 Nov 2022 20:31:00 +0100 Subject: [PATCH 3/5] implemented alway use api to get Data. Open Sonnendach pages by url --- Sonnendach.py | 300 +++++++++++++++++++++++++------------------------- api-Test.py | 8 +- 2 files changed, 156 insertions(+), 152 deletions(-) diff --git a/Sonnendach.py b/Sonnendach.py index 3d1f908..3f102dd 100644 --- a/Sonnendach.py +++ b/Sonnendach.py @@ -16,7 +16,7 @@ from selenium.webdriver.common.action_chains import ActionChains import json import requests -DropDownLabels = ["Street: ", "Number: ", "Postal code: ", "City: ", "Sonnendach URL: ", "Suitability", "Image Filename Map: ", "Image Filename Production: ", "Image Filename qrcode: ", "PV Production 50", "PV Production 75", "PV Production 100", "Value Electricity production", "Roof area"] +DropDownLabels = ["Street: ", "Number: ", "Postal code: ", "City: ", "coordinate X", "coordinate Y", "Sonnendach URL: ", "Suitability", "Image Filename Map: ", "Image Filename Production: ", "Image Filename qrcode: ", "PV Production 50", "PV Production 75", "PV Production 100", "Value Electricity production", "Roof area"] file_split_char = "," OptionList = [] exit = False @@ -49,117 +49,25 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): address_count = len(adress_list) #Search for each adress in the list and create screenshots and qrcodes for i in range(address_count): + url = "not-found" + suitability = "not-found" + pv_Production50 = "not-found" + pv_Production75 = "not-found" + pv_Production100 = "not-found" + value_electricity_production = "not-found" + roof_area = "not-found" + found = True line = adress_list[i] adress = line.split(file_split_char) - if((line != adress_list[0]) & (adress[columnIndexes[4]] == "") & (len(adress[columnIndexes[0]]) > 0) & (len(adress[columnIndexes[1]]) > 0) & (len(adress[columnIndexes[2]]) > 0) & (len(adress[columnIndexes[3]]) > 0)): - search_string = adress[columnIndexes[0]] + " " + adress[columnIndexes[1]] + " " + adress[columnIndexes[2]] + " " + adress[columnIndexes[3]] - - if(createImages.get() > 0): - search_bar = driver.find_element(By.ID, "searchTypeahead1") - search_bar.send_keys(Keys.CONTROL + "a") - search_bar.send_keys(Keys.DELETE) - search_bar.send_keys(search_string) - driver.implicitly_wait(10) - time.sleep(0.7) - found = False - suggestions = driver.find_elements(By.XPATH, "//div[@class='tt-suggestion tt-selectable']") - for suggestion in suggestions: - if(suggestion.text == search_string): - found = True - suggestion.click() - driver.implicitly_wait(10) - break - - if(found): - time.sleep(0.7) - url = driver.current_url - suitability = driver.find_element(By.ID, "eignung").text - pv_Production50 = driver.find_element(By.ID, "pv50").text.replace("'", "") - pv_Production75 = driver.find_element(By.ID, "pv75").text.replace("'", "") - pv_Production100 = driver.find_element(By.ID, "pv100").text.replace("'", "") - value_electricity_production = driver.find_elements(By.XPATH, "//h2[@id='TitelSolarstrom']//strong")[2].text.replace("'", "").replace(" Franken", "") - roof_area = driver.find_element(By.ID, "areaOutput").text - - time.sleep(2) - image_filename = suitability + " - " + search_string - image_folder_map = "screenshots/" - image_filename_map = image_filename + " map" + ".png" - image_folder_production = "screenshots/" - image_filename_production = image_filename + " production" + ".png" - image_folder_qrcode = "qrcodes/" - image_filename_qrcode = image_filename + " qrcode" + ".png" - - # Create QR-Code - qr = qrcode.QRCode(version=1, box_size=10, border=5) - qr.add_data(url) - qr.make(fit=True) - qr.make_image(fill='black', back_color='white').save(image_folder_qrcode + image_filename_qrcode) - - # Create Screenshot of Map - actions = ActionChains(driver) - try: - driver.execute_script("""var l = document.getElementsByClassName("ol-zoom ol-unselectable ol-control")[0]; - l.parentNode.removeChild(l);""") - except: - pass - - mapElement = driver.find_element(By.XPATH, - "//div[@id='map']//div[@class='ol-viewport']") - location = mapElement.location - size = mapElement.size - x = location["x"] - y = location["y"] - w = x + size["width"] - h = size["height"] - area = (x, 0, w, h) - driver.execute_script("window.scrollTo(0, " + str(y) + ")") - time.sleep(1) - driver.save_screenshot(image_folder_map + image_filename_map) - time.sleep(0.2) - Image.open(image_folder_map + image_filename_map).crop(area).save( - image_folder_map + image_filename_map) - - # Create Screenshot of Chart - try: - chartElement = driver.find_elements(By.XPATH, "//div[@id='chart']//*")[0] - location = chartElement.location - size = chartElement.size - x = location["x"] - y = location["y"] - w = x + size["width"] - h = size["height"] - area = (x, 0, w, h) - driver.execute_script("window.scrollTo(0, " + str(y) + ")") - - time.sleep(1) - driver.save_screenshot(image_folder_production + image_filename_production) - time.sleep(0.2) - Image.open(image_folder_production + image_filename_production).crop(area).save( - image_folder_production + image_filename_production) - except: - image_filename_production = "not-found" - - print("saved Address: " + search_string) - else: - url = "not-found" - suitability = "not-found" - image_filename_map = "not-found" - image_filename_production = "not-found" - image_filename_qrcode = "not-found" - pv_Production50 = "not-found" - pv_Production75 = "not-found" - pv_Production100 = "not-found" - value_electricity_production = "not-found" - roof_area = "not-found" - - print("not found: " + search_string) - - else: # If no images must be created + if((line != adress_list[0]) and (adress[columnIndexes[6]] == "") and (((len(adress[columnIndexes[0]]) > 0) and (len(adress[columnIndexes[1]]) > 0) and (len(adress[columnIndexes[2]]) > 0) and (len(adress[columnIndexes[3]]) > 0)) or ((len(adress[columnIndexes[4]]) > 0) and (len(adress[columnIndexes[5]]) > 0)))): + address_string = adress[columnIndexes[0]] + " " + adress[columnIndexes[1]] + " " + adress[columnIndexes[2]] + " " + adress[columnIndexes[3]] + if(not ((len(adress[columnIndexes[4]]) > 0) and (len(adress[columnIndexes[5]]) > 0))): + # Get Coordinates for Address try: # Address to coordinates params = { "type": "locations", - "searchText": search_string, + "searchText": address_string, "lang": "de"} locationsResponse = requests.get('https://api3.geo.admin.ch/rest/services/api/SearchServer', params=params) @@ -167,10 +75,20 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): firstLocation = locations['results'][0]['attrs'] # you need the coordinates from other services, once you have them - coordinates = (firstLocation['y'], firstLocation['x']) # example coordinates + coordinates = (float(firstLocation['y']), float(firstLocation['x'])) # example coordinates + except: + found = False + else: + # Get Coordinates from File + coordinate_x = float(adress[columnIndexes[4]]) % 1000000 + coordinate_y = float(adress[columnIndexes[5]]) % 1000000 + coordinates = (coordinate_x, coordinate_y) + + if(found): + # Get Building ID from coordinates + try: coordinates_str = ','.join(map(str, coordinates)) tolerance = 1 - # this is wrong, bbox should be computed using tolerance and pixel size, but there are no problems for the # purpose of this exercise bbox = (coordinates[0] - tolerance, coordinates[1] - tolerance, coordinates[0] + tolerance, @@ -195,7 +113,11 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): # for result in results: # print(result) building_id = (response_one_roof_decoded)['results'][0]['attributes']['building_id'] + except: + found = False + if(found): + try: # use the building_id of previous request to get all roofs associated with this building_id params = { 'layer': 'ch.bfe.solarenergie-eignung-daecher', @@ -213,52 +135,45 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): best_roof = roof url = "https://www.uvek-gis.admin.ch/BFE/sonnendach/index.html?featureId=" + str(best_roof['featureId']) + "&lang=de" suitability = str(best_roof['attributes']['klasse']) - image_filename_map = "not-created" - image_filename_production = "not-created" - image_filename_qrcode = "not-created" pv_Production100 = str(float(best_roof['attributes']['gstrahlung'])*0.2*0.8) pv_Production75 = str(float(pv_Production100) * 0.75) pv_Production50 = str(float(pv_Production100) * 0.5) value_electricity_production = str(best_roof['attributes']['finanzertrag']) roof_area = str(best_roof['attributes']['flaeche']) - print("saved Address: " + search_string) except: - url = "not-found" - suitability = "not-found" - image_filename_map = "not-found" - image_filename_production = "not-found" - image_filename_qrcode = "not-found" - pv_Production50 = "not-found" - pv_Production75 = "not-found" - pv_Production100 = "not-found" - value_electricity_production = "not-found" - roof_area = "not-found" + found = False + + roof_is_suitable = True # TODO decide if roof is suitable + + if(found and createImages.get() > 0 and roof_is_suitable): + image_filename = suitability + " - " + address_string + image_folder_map = "screenshots/" + image_filename_map = image_filename + " map" + ".png" + image_folder_production = "screenshots/" + image_filename_production = image_filename + " production" + ".png" + image_folder_qrcode = "qrcodes/" + image_filename_qrcode = image_filename + " qrcode" + ".png" + create_images(address_string, driver, image_filename_map, image_filename_production, + image_filename_qrcode, image_folder_map, image_folder_production, image_folder_qrcode, + url) + + else: + image_filename_map = "not-created" + image_filename_production = "not-created" + image_filename_qrcode = "not-created" + + save_information_to_addresslist(adress, adress_list, columnIndexes, coordinates, file_split_char, i, + image_filename_map, image_filename_production, image_filename_qrcode, + pv_Production100, pv_Production50, pv_Production75, roof_area, suitability, + url, value_electricity_production) + + save_addresslist_in_file(adress_list, filename_adresslist) + + print("saved Address: " + address_string) print("Address " + str(i) + " of " + str(address_count)) timediff = relativedelta(datetime.datetime.now(), startTime) - print("Process is running since %d years %d months %d days %d hours %d minutes %d seconds" % (timediff.years, timediff.months, timediff.days, timediff.hours, timediff.minutes, timediff.seconds)) - - - #Write back into file - adress[columnIndexes[4]] = url - adress[columnIndexes[5]] = suitability - adress[columnIndexes[6]] = image_filename_map - adress[columnIndexes[7]] = image_filename_production - adress[columnIndexes[8]] = image_filename_qrcode - adress[columnIndexes[9]] = pv_Production50 - adress[columnIndexes[10]] = pv_Production75 - adress[columnIndexes[11]] = pv_Production100 - adress[columnIndexes[12]] = value_electricity_production - adress[columnIndexes[13]] = roof_area - adress_file = open(filename_adresslist, "w", encoding="utf-8") - new_line_string = "" - for j in adress: - new_line_string = new_line_string + j + file_split_char - adress_list[i] = new_line_string - new_adress_list = "" - for j in adress_list: - new_adress_list = new_adress_list + (j) + "\n" - adress_file.write(new_adress_list) - adress_file.close() + print("Process is running since %d years %d months %d days %d hours %d minutes %d seconds" % ( + timediff.years, timediff.months, timediff.days, timediff.hours, timediff.minutes, timediff.seconds)) if(stopThread == True): @@ -270,6 +185,96 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): print("closing Thread") return + +def save_information_to_addresslist(adress, adress_list, columnIndexes, coordinates, file_split_char, list_index, + image_filename_map, image_filename_production, image_filename_qrcode, + pv_Production100, pv_Production50, pv_Production75, roof_area, suitability, url, + value_electricity_production): + # Save Information in Addresslist + adress[columnIndexes[4]] = str(coordinates[0]) + adress[columnIndexes[5]] = str(coordinates[1]) + adress[columnIndexes[6]] = url + adress[columnIndexes[7]] = suitability + adress[columnIndexes[8]] = image_filename_map + adress[columnIndexes[9]] = image_filename_production + adress[columnIndexes[10]] = image_filename_qrcode + adress[columnIndexes[11]] = pv_Production50 + adress[columnIndexes[12]] = pv_Production75 + adress[columnIndexes[13]] = pv_Production100 + adress[columnIndexes[14]] = value_electricity_production + adress[columnIndexes[15]] = roof_area + new_line_string = "" + for j in adress: + new_line_string = new_line_string + j + file_split_char + adress_list[list_index] = new_line_string + + +def save_addresslist_in_file(adress_list, filename_adresslist): + new_adress_list = "" + for j in adress_list: + new_adress_list = new_adress_list + (j) + "\n" + # save File + adress_file = open(filename_adresslist, "w", encoding="utf-8") + adress_file.write(new_adress_list) + adress_file.close() + + +def create_images(address_string, driver, image_filename_map, image_filename_production, image_filename_qrcode, + image_folder_map, image_folder_production, image_folder_qrcode, url): + driver.get(url) + time.sleep(2) + + # Create QR-Code + qr = qrcode.QRCode(version=1, box_size=10, border=5) + qr.add_data(url) + qr.make(fit=True) + qr.make_image(fill='black', back_color='white').save(image_folder_qrcode + image_filename_qrcode) + + # Create Screenshot of Map + actions = ActionChains(driver) + try: + driver.execute_script("""var l = document.getElementsByClassName("ol-zoom ol-unselectable ol-control")[0]; + l.parentNode.removeChild(l);""") + except: + pass + + mapElement = driver.find_element(By.XPATH, + "//div[@id='map']//div[@class='ol-viewport']") + location = mapElement.location + size = mapElement.size + x = location["x"] + y = location["y"] + w = x + size["width"] + h = size["height"] + area = (x, 0, w, h) + driver.execute_script("window.scrollTo(0, " + str(y) + ")") + time.sleep(1) + driver.save_screenshot(image_folder_map + image_filename_map) + time.sleep(0.2) + Image.open(image_folder_map + image_filename_map).crop(area).save( + image_folder_map + image_filename_map) + + # Create Screenshot of Chart + try: + chartElement = driver.find_elements(By.XPATH, "//div[@id='chart']//*")[0] + location = chartElement.location + size = chartElement.size + x = location["x"] + y = location["y"] + w = x + size["width"] + h = size["height"] + area = (x, 0, w, h) + driver.execute_script("window.scrollTo(0, " + str(y) + ")") + + time.sleep(1) + driver.save_screenshot(image_folder_production + image_filename_production) + time.sleep(0.2) + Image.open(image_folder_production + image_filename_production).crop(area).save( + image_folder_production + image_filename_production) + except: + pass + + def read_adresslist(filename_adresslist): returnvalues = [] try: @@ -323,7 +328,7 @@ def command(): thread_search_adresses.join() except: pass - print("Thread is closed") + print("Threads are closed") root.quit() print("Application is closed") driver.quit() @@ -388,7 +393,6 @@ def command_exit(): - root = tkinter.Tk() root.protocol("WM_DELETE_WINDOW", command_exit) root.wm_title("Sonnendach") diff --git a/api-Test.py b/api-Test.py index 05d19d2..9f5a02c 100644 --- a/api-Test.py +++ b/api-Test.py @@ -22,6 +22,7 @@ tolerance=1 # purpose of this exercise bbox=(coordinates[0]-tolerance, coordinates[1]-tolerance, coordinates[0]+tolerance,coordinates[1]+tolerance) bbox_str=','.join(map(str,bbox)) +# retrieve one roof and the building_id associated params={ "geometryType":"esriGeometryPoint", "returnGeometry":"true", @@ -33,12 +34,12 @@ params={ "order":"distance", "lang":"de", } -# retrieve one roof and the building_id associated +print(coordinates_str) response_one_roof = requests.get('https://api3.geo.admin.ch/rest/services/api/MapServer/identify', params=params) response_one_roof_decoded = json.loads(response_one_roof.content.decode()) results = (response_one_roof_decoded['results']) -# for result in results: -# print(result['attributes']['building_id']) +for result in results: + print(result['attributes']['building_id']) building_id=(response_one_roof_decoded)['results'][0]['attributes']['building_id'] # use the building_id of previous request to get all roofs associated with this building_id @@ -49,7 +50,6 @@ params = { 'searchText': str(building_id), 'returnGeometry': 'false' } - response_all_roofs = requests.get('https://api3.geo.admin.ch/rest/services/api/MapServer/find', params=params) response_all_roofs_decoded = json.loads(response_all_roofs.content.decode())['results'] for roof in response_all_roofs_decoded: -- 2.40.1 From fbe475f22287d829fa64ad374481ce14ba4693cf Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 24 Feb 2023 11:17:59 +0100 Subject: [PATCH 4/5] changed variabel structure --- Sonnendach.py | 92 ++++++++++++++++++++++----------------------------- 1 file changed, 40 insertions(+), 52 deletions(-) diff --git a/Sonnendach.py b/Sonnendach.py index 3f102dd..be61a34 100644 --- a/Sonnendach.py +++ b/Sonnendach.py @@ -16,7 +16,7 @@ from selenium.webdriver.common.action_chains import ActionChains import json import requests -DropDownLabels = ["Street: ", "Number: ", "Postal code: ", "City: ", "coordinate X", "coordinate Y", "Sonnendach URL: ", "Suitability", "Image Filename Map: ", "Image Filename Production: ", "Image Filename qrcode: ", "PV Production 50", "PV Production 75", "PV Production 100", "Value Electricity production", "Roof area"] +DropDownLabels = ["Street: ", "Number: ", "Postal code: ", "City: ", "coordinate X", "coordinate Y", "Sonnendach URL: ", "Suitability:", "Image Filename: ", "PV Production 100", "Value Electricity production", "Roof area"] file_split_char = "," OptionList = [] exit = False @@ -40,10 +40,15 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): global checkBoxCreateImages #Create Direcotrys to save screenshots and qrcodes if they don't exist yet. - if(not os.path.exists("screenshots")): - os.makedirs("screenshots", exist_ok=False) - if (not os.path.exists("qrcodes")): - os.makedirs("qrcodes", exist_ok=False) + image_folder_map = "screenshots_map/" + image_folder_production = "screenshots_production/" + image_folder_qrcode = "qrcodes/" + if(not os.path.exists(image_folder_map[:-1])): + os.makedirs(image_folder_map[:-1], exist_ok=False) + if(not os.path.exists(image_folder_production[:-1])): + os.makedirs(image_folder_production[:-1], exist_ok=False) + if (not os.path.exists(image_folder_qrcode[:-1])): + os.makedirs(image_folder_qrcode[:-1], exist_ok=False) startTime = datetime.datetime.now() address_count = len(adress_list) @@ -51,8 +56,6 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): for i in range(address_count): url = "not-found" suitability = "not-found" - pv_Production50 = "not-found" - pv_Production75 = "not-found" pv_Production100 = "not-found" value_electricity_production = "not-found" roof_area = "not-found" @@ -129,43 +132,34 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): response_all_roofs = requests.get('https://api3.geo.admin.ch/rest/services/api/MapServer/find', params=params) response_all_roofs_decoded = json.loads(response_all_roofs.content.decode())['results'] - best_roof = response_all_roofs_decoded[0] # TODO: Find best roof - for roof in response_all_roofs_decoded: - if(float(roof['attributes']['flaeche']) > float(best_roof['attributes']['flaeche'])): - best_roof = roof - url = "https://www.uvek-gis.admin.ch/BFE/sonnendach/index.html?featureId=" + str(best_roof['featureId']) + "&lang=de" - suitability = str(best_roof['attributes']['klasse']) - pv_Production100 = str(float(best_roof['attributes']['gstrahlung'])*0.2*0.8) - pv_Production75 = str(float(pv_Production100) * 0.75) - pv_Production50 = str(float(pv_Production100) * 0.5) - value_electricity_production = str(best_roof['attributes']['finanzertrag']) - roof_area = str(best_roof['attributes']['flaeche']) + if(len(response_all_roofs_decoded) > 0): + best_roof = response_all_roofs_decoded[0] # TODO: Find best roof + for roof in response_all_roofs_decoded: + if(float(roof['attributes']['flaeche']) > float(best_roof['attributes']['flaeche'])): + best_roof = roof + url = "https://www.uvek-gis.admin.ch/BFE/sonnendach/index.html?featureId=" + str(best_roof['featureId']) + "&lang=de" + suitability = str(best_roof['attributes']['klasse']) + pv_Production100 = str(float(best_roof['attributes']['gstrahlung'])*0.2*0.8) + value_electricity_production = str(best_roof['attributes']['finanzertrag']) + roof_area = str(best_roof['attributes']['flaeche']) + else: + found = False except: found = False roof_is_suitable = True # TODO decide if roof is suitable if(found and createImages.get() > 0 and roof_is_suitable): - image_filename = suitability + " - " + address_string - image_folder_map = "screenshots/" - image_filename_map = image_filename + " map" + ".png" - image_folder_production = "screenshots/" - image_filename_production = image_filename + " production" + ".png" - image_folder_qrcode = "qrcodes/" - image_filename_qrcode = image_filename + " qrcode" + ".png" - create_images(address_string, driver, image_filename_map, image_filename_production, - image_filename_qrcode, image_folder_map, image_folder_production, image_folder_qrcode, + image_filename = suitability + " - " + address_string + ".png" + create_images(address_string, driver, image_filename, image_folder_map, image_folder_production, image_folder_qrcode, url) else: - image_filename_map = "not-created" - image_filename_production = "not-created" - image_filename_qrcode = "not-created" + image_filename = "not-created" save_information_to_addresslist(adress, adress_list, columnIndexes, coordinates, file_split_char, i, - image_filename_map, image_filename_production, image_filename_qrcode, - pv_Production100, pv_Production50, pv_Production75, roof_area, suitability, - url, value_electricity_production) + image_filename, pv_Production100, roof_area, suitability, url, + value_electricity_production) save_addresslist_in_file(adress_list, filename_adresslist) @@ -187,22 +181,17 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): def save_information_to_addresslist(adress, adress_list, columnIndexes, coordinates, file_split_char, list_index, - image_filename_map, image_filename_production, image_filename_qrcode, - pv_Production100, pv_Production50, pv_Production75, roof_area, suitability, url, + image_filename, pv_Production100, roof_area, suitability, url, value_electricity_production): # Save Information in Addresslist adress[columnIndexes[4]] = str(coordinates[0]) adress[columnIndexes[5]] = str(coordinates[1]) adress[columnIndexes[6]] = url adress[columnIndexes[7]] = suitability - adress[columnIndexes[8]] = image_filename_map - adress[columnIndexes[9]] = image_filename_production - adress[columnIndexes[10]] = image_filename_qrcode - adress[columnIndexes[11]] = pv_Production50 - adress[columnIndexes[12]] = pv_Production75 - adress[columnIndexes[13]] = pv_Production100 - adress[columnIndexes[14]] = value_electricity_production - adress[columnIndexes[15]] = roof_area + adress[columnIndexes[8]] = image_filename + adress[columnIndexes[9]] = pv_Production100 + adress[columnIndexes[10]] = value_electricity_production + adress[columnIndexes[11]] = roof_area new_line_string = "" for j in adress: new_line_string = new_line_string + j + file_split_char @@ -219,8 +208,7 @@ def save_addresslist_in_file(adress_list, filename_adresslist): adress_file.close() -def create_images(address_string, driver, image_filename_map, image_filename_production, image_filename_qrcode, - image_folder_map, image_folder_production, image_folder_qrcode, url): +def create_images(address_string, driver, image_filename, image_folder_map, image_folder_production, image_folder_qrcode, url): driver.get(url) time.sleep(2) @@ -228,7 +216,7 @@ def create_images(address_string, driver, image_filename_map, image_filename_pro qr = qrcode.QRCode(version=1, box_size=10, border=5) qr.add_data(url) qr.make(fit=True) - qr.make_image(fill='black', back_color='white').save(image_folder_qrcode + image_filename_qrcode) + qr.make_image(fill='black', back_color='white').save(image_folder_qrcode + image_filename) # Create Screenshot of Map actions = ActionChains(driver) @@ -249,10 +237,10 @@ def create_images(address_string, driver, image_filename_map, image_filename_pro area = (x, 0, w, h) driver.execute_script("window.scrollTo(0, " + str(y) + ")") time.sleep(1) - driver.save_screenshot(image_folder_map + image_filename_map) + driver.save_screenshot(image_folder_map + image_filename) time.sleep(0.2) - Image.open(image_folder_map + image_filename_map).crop(area).save( - image_folder_map + image_filename_map) + Image.open(image_folder_map + image_filename).crop(area).save( + image_folder_map + image_filename) # Create Screenshot of Chart try: @@ -267,10 +255,10 @@ def create_images(address_string, driver, image_filename_map, image_filename_pro driver.execute_script("window.scrollTo(0, " + str(y) + ")") time.sleep(1) - driver.save_screenshot(image_folder_production + image_filename_production) + driver.save_screenshot(image_folder_production + image_filename) time.sleep(0.2) - Image.open(image_folder_production + image_filename_production).crop(area).save( - image_folder_production + image_filename_production) + Image.open(image_folder_production + image_filename).crop(area).save( + image_folder_production + image_filename) except: pass -- 2.40.1 From db2bda5bc0941764909a224ec8808aa5d3e1ccfd Mon Sep 17 00:00:00 2001 From: schrom01 Date: Wed, 1 Mar 2023 10:42:11 +0100 Subject: [PATCH 5/5] changed search to use Geo Admin API and calculate sum of all roofs. added functionality to add addresses to dolibarr --- Sonnendach.py | 108 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 13 deletions(-) diff --git a/Sonnendach.py b/Sonnendach.py index be61a34..cc47926 100644 --- a/Sonnendach.py +++ b/Sonnendach.py @@ -15,9 +15,13 @@ from dateutil.relativedelta import relativedelta from selenium.webdriver.common.action_chains import ActionChains import json import requests +import statistics DropDownLabels = ["Street: ", "Number: ", "Postal code: ", "City: ", "coordinate X", "coordinate Y", "Sonnendach URL: ", "Suitability:", "Image Filename: ", "PV Production 100", "Value Electricity production", "Roof area"] file_split_char = "," +minRoofSize = 2 +minSuitability = 3 +electricityPrice = 0.1 OptionList = [] exit = False stopThread = False @@ -29,15 +33,27 @@ driver = webdriver.Chrome(service=service) driver.minimize_window() outputtext = "Welcome to application Sonnendach\n" columnIndexes = [] - +storeToDolibarr = True +dolibarrURL = "" +dolibarrApiKey = "" +dolibarrCountryID = "6" # 6 = Schweiz +dolibarrCountryCode = "CH" +dolibarrStateID = "403" # 403 = Kanton Zürich def search_adresses(adress_list, filename_adresslist, driver, mainText): + print("function to search Addresses called") global file_split_char global stopThread global columnIndexes global outputtext global checkBoxCreateImages + global minRoofSize + global minSuitability + global electricityPrice + global storeToDolibarr + global dolibarrURL + global dolibarrApiKey #Create Direcotrys to save screenshots and qrcodes if they don't exist yet. image_folder_map = "screenshots_map/" @@ -53,17 +69,21 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): startTime = datetime.datetime.now() address_count = len(adress_list) #Search for each adress in the list and create screenshots and qrcodes + print("Starting Address Loop") for i in range(address_count): url = "not-found" suitability = "not-found" - pv_Production100 = "not-found" + sumEnergyRoofs = "not-found" value_electricity_production = "not-found" - roof_area = "not-found" + sumRoofArea = "not-found" + featureId = "not-found" + response_all_roofs_decoded = "not-found" found = True line = adress_list[i] adress = line.split(file_split_char) if((line != adress_list[0]) and (adress[columnIndexes[6]] == "") and (((len(adress[columnIndexes[0]]) > 0) and (len(adress[columnIndexes[1]]) > 0) and (len(adress[columnIndexes[2]]) > 0) and (len(adress[columnIndexes[3]]) > 0)) or ((len(adress[columnIndexes[4]]) > 0) and (len(adress[columnIndexes[5]]) > 0)))): address_string = adress[columnIndexes[0]] + " " + adress[columnIndexes[1]] + " " + adress[columnIndexes[2]] + " " + adress[columnIndexes[3]] + print("searching for Address: " + address_string) if(not ((len(adress[columnIndexes[4]]) > 0) and (len(adress[columnIndexes[5]]) > 0))): # Get Coordinates for Address try: @@ -133,15 +153,25 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): params=params) response_all_roofs_decoded = json.loads(response_all_roofs.content.decode())['results'] if(len(response_all_roofs_decoded) > 0): - best_roof = response_all_roofs_decoded[0] # TODO: Find best roof + sumEnergyRoofs = 0 + sumRoofArea = 0 + suitabilities = [] + bestRoofEnergy = 0 + bestRoof = response_all_roofs_decoded[0] for roof in response_all_roofs_decoded: - if(float(roof['attributes']['flaeche']) > float(best_roof['attributes']['flaeche'])): - best_roof = roof - url = "https://www.uvek-gis.admin.ch/BFE/sonnendach/index.html?featureId=" + str(best_roof['featureId']) + "&lang=de" - suitability = str(best_roof['attributes']['klasse']) - pv_Production100 = str(float(best_roof['attributes']['gstrahlung'])*0.2*0.8) - value_electricity_production = str(best_roof['attributes']['finanzertrag']) - roof_area = str(best_roof['attributes']['flaeche']) + energy = float(roof['attributes']['gstrahlung'])*0.2*0.8 + if(energy > bestRoofEnergy): + bestRoofEnergy = energy + bestRoof = roof + if(float(roof['attributes']['flaeche']) > minRoofSize and int(roof['attributes']['klasse']) >= minSuitability): + sumEnergyRoofs = sumEnergyRoofs + energy + sumRoofArea = sumRoofArea + float(roof['attributes']['flaeche']) + suitabilities.append(int(roof['attributes']['klasse']) * float(roof['attributes']['flaeche'])) + + featureId = str(bestRoof['featureId']) + url = "https://www.uvek-gis.admin.ch/BFE/sonnendach/index.html?featureId=" + featureId + "&lang=de" + suitability = statistics.mean(suitabilities) / sumRoofArea + value_electricity_production = sumEnergyRoofs * electricityPrice else: found = False except: @@ -157,12 +187,20 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText): else: image_filename = "not-created" + print("saving Address: " + address_string) save_information_to_addresslist(adress, adress_list, columnIndexes, coordinates, file_split_char, i, - image_filename, pv_Production100, roof_area, suitability, url, + image_filename, sumEnergyRoofs, sumRoofArea, suitability, url, value_electricity_production) save_addresslist_in_file(adress_list, filename_adresslist) + + if(found): + street = adress[columnIndexes[0]] + " " + adress[columnIndexes[1]] + plz = adress[columnIndexes[2]] + city = adress[columnIndexes[3]] + create_Partner_in_dolibarr(street, plz, city, coordinates, sumEnergyRoofs, sumRoofArea, suitability, featureId, url, response_all_roofs_decoded) + print("saved Address: " + address_string) print("Address " + str(i) + " of " + str(address_count)) timediff = relativedelta(datetime.datetime.now(), startTime) @@ -194,7 +232,7 @@ def save_information_to_addresslist(adress, adress_list, columnIndexes, coordina adress[columnIndexes[11]] = roof_area new_line_string = "" for j in adress: - new_line_string = new_line_string + j + file_split_char + new_line_string = new_line_string + str(j) + file_split_char adress_list[list_index] = new_line_string @@ -207,6 +245,50 @@ def save_addresslist_in_file(adress_list, filename_adresslist): adress_file.write(new_adress_list) adress_file.close() +def create_Partner_in_dolibarr(street, plz, city, coordinates, sumEnergyRoofs, sumRoofArea, suitability, featureId, url, roofs): + global electricityPrice + global dolibarrStateID + global dolibarrCountryID + global dolibarrCountryCode + global dolibarrURL + global dolibarrApiKey + params = { + "name": "unbekannt", + "address": street, + "zip": plz, + "town": city, + "state_id": dolibarrStateID, + "client": "2", + "code_client": "FID-" + featureId, + "note_public": "", + "status_prospect_label": "Jamais contacté", + "array_options": { + "options_energy": str(sumEnergyRoofs), + "options_energy_value": str(float(sumEnergyRoofs) * electricityPrice), + "options_roof_size": str(sumRoofArea), + "options_avrg_suitability": str(suitability), + "options_geomx": str(coordinates[0]), + "options_geomy": str(coordinates[1]), + "options_roof_info": create_roof_info(roofs), + "options_sonnendach_url": url + }, + "country_id": dolibarrCountryID, + "country_code": dolibarrCountryCode + } + headers = { + "Content-Type": "application/json", + "Accept": "application/json", + "DOLAPIKEY": dolibarrApiKey + } + response = json.loads(requests.post(dolibarrURL, json=params, headers=headers).content.decode()) + # print(params) + # print(response) + +def create_roof_info(roofs): + info_text = "" + for i in range(len(roofs)): + info_text = info_text + str(i + 1) + ". Fläche\n" + "Potenzielle Energie: " + str(roofs[i]['attributes']['gstrahlung']*0.2*0.8) + "\nFläche: " + str(roofs[i]['attributes']['flaeche']) + "\nEignung: " + str(roofs[i]['attributes']['klasse']) + " (" + str(roofs[i]['attributes']['klasse_text']).split("##")[0] + ")\n\n" + return info_text def create_images(address_string, driver, image_filename, image_folder_map, image_folder_production, image_folder_qrcode, url): driver.get(url) -- 2.40.1