From db2bda5bc0941764909a224ec8808aa5d3e1ccfd Mon Sep 17 00:00:00 2001 From: schrom01 Date: Wed, 1 Mar 2023 10:42:11 +0100 Subject: [PATCH] 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)