changed search to use Geo Admin API and calculate sum of all roofs.

added functionality to add addresses to dolibarr
This commit is contained in:
schrom01 2023-03-01 10:42:11 +01:00
parent fbe475f222
commit db2bda5bc0
1 changed files with 95 additions and 13 deletions

View File

@ -15,9 +15,13 @@ from dateutil.relativedelta import relativedelta
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.action_chains import ActionChains
import json import json
import requests 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"] 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 = "," file_split_char = ","
minRoofSize = 2
minSuitability = 3
electricityPrice = 0.1
OptionList = [] OptionList = []
exit = False exit = False
stopThread = False stopThread = False
@ -29,15 +33,27 @@ driver = webdriver.Chrome(service=service)
driver.minimize_window() driver.minimize_window()
outputtext = "Welcome to application Sonnendach\n" outputtext = "Welcome to application Sonnendach\n"
columnIndexes = [] 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): def search_adresses(adress_list, filename_adresslist, driver, mainText):
print("function to search Addresses called")
global file_split_char global file_split_char
global stopThread global stopThread
global columnIndexes global columnIndexes
global outputtext global outputtext
global checkBoxCreateImages 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. #Create Direcotrys to save screenshots and qrcodes if they don't exist yet.
image_folder_map = "screenshots_map/" image_folder_map = "screenshots_map/"
@ -53,17 +69,21 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText):
startTime = datetime.datetime.now() startTime = datetime.datetime.now()
address_count = len(adress_list) address_count = len(adress_list)
#Search for each adress in the list and create screenshots and qrcodes #Search for each adress in the list and create screenshots and qrcodes
print("Starting Address Loop")
for i in range(address_count): for i in range(address_count):
url = "not-found" url = "not-found"
suitability = "not-found" suitability = "not-found"
pv_Production100 = "not-found" sumEnergyRoofs = "not-found"
value_electricity_production = "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 found = True
line = adress_list[i] line = adress_list[i]
adress = line.split(file_split_char) 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)))): 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]] 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))): if(not ((len(adress[columnIndexes[4]]) > 0) and (len(adress[columnIndexes[5]]) > 0))):
# Get Coordinates for Address # Get Coordinates for Address
try: try:
@ -133,15 +153,25 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText):
params=params) params=params)
response_all_roofs_decoded = json.loads(response_all_roofs.content.decode())['results'] response_all_roofs_decoded = json.loads(response_all_roofs.content.decode())['results']
if(len(response_all_roofs_decoded) > 0): 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: for roof in response_all_roofs_decoded:
if(float(roof['attributes']['flaeche']) > float(best_roof['attributes']['flaeche'])): energy = float(roof['attributes']['gstrahlung'])*0.2*0.8
best_roof = roof if(energy > bestRoofEnergy):
url = "https://www.uvek-gis.admin.ch/BFE/sonnendach/index.html?featureId=" + str(best_roof['featureId']) + "&lang=de" bestRoofEnergy = energy
suitability = str(best_roof['attributes']['klasse']) bestRoof = roof
pv_Production100 = str(float(best_roof['attributes']['gstrahlung'])*0.2*0.8) if(float(roof['attributes']['flaeche']) > minRoofSize and int(roof['attributes']['klasse']) >= minSuitability):
value_electricity_production = str(best_roof['attributes']['finanzertrag']) sumEnergyRoofs = sumEnergyRoofs + energy
roof_area = str(best_roof['attributes']['flaeche']) 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: else:
found = False found = False
except: except:
@ -157,12 +187,20 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText):
else: else:
image_filename = "not-created" image_filename = "not-created"
print("saving Address: " + address_string)
save_information_to_addresslist(adress, adress_list, columnIndexes, coordinates, file_split_char, i, 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) value_electricity_production)
save_addresslist_in_file(adress_list, filename_adresslist) 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("saved Address: " + address_string)
print("Address " + str(i) + " of " + str(address_count)) print("Address " + str(i) + " of " + str(address_count))
timediff = relativedelta(datetime.datetime.now(), startTime) 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 adress[columnIndexes[11]] = roof_area
new_line_string = "" new_line_string = ""
for j in adress: 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 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.write(new_adress_list)
adress_file.close() 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): def create_images(address_string, driver, image_filename, image_folder_map, image_folder_production, image_folder_qrcode, url):
driver.get(url) driver.get(url)