From fbe475f22287d829fa64ad374481ce14ba4693cf Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 24 Feb 2023 11:17:59 +0100 Subject: [PATCH] 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