making_image_jobs_optional #6

Merged
Roman_Schenk merged 5 commits from making_image_jobs_optional into main 2023-03-01 11:01:53 +01:00
2 changed files with 156 additions and 152 deletions
Showing only changes of commit b620704a54 - Show all commits

View File

@ -16,7 +16,7 @@ from selenium.webdriver.common.action_chains import ActionChains
import json import json
import requests 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 = "," file_split_char = ","
OptionList = [] OptionList = []
exit = False exit = False
@ -49,45 +49,180 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText):
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
for i in range(address_count): 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] line = adress_list[i]
adress = line.split(file_split_char) 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)): 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)))):
search_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]]
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": address_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']
if(createImages.get() > 0): # you need the coordinates from other services, once you have them
search_bar = driver.find_element(By.ID, "searchTypeahead1") coordinates = (float(firstLocation['y']), float(firstLocation['x'])) # example coordinates
search_bar.send_keys(Keys.CONTROL + "a") except:
search_bar.send_keys(Keys.DELETE)
search_bar.send_keys(search_string)
driver.implicitly_wait(10)
time.sleep(0.7)
found = False found = False
suggestions = driver.find_elements(By.XPATH, "//div[@class='tt-suggestion tt-selectable']") else:
for suggestion in suggestions: # Get Coordinates from File
if(suggestion.text == search_string): coordinate_x = float(adress[columnIndexes[4]]) % 1000000
found = True coordinate_y = float(adress[columnIndexes[5]]) % 1000000
suggestion.click() coordinates = (coordinate_x, coordinate_y)
driver.implicitly_wait(10)
break
if(found): if(found):
time.sleep(0.7) # Get Building ID from coordinates
url = driver.current_url try:
suitability = driver.find_element(By.ID, "eignung").text coordinates_str = ','.join(map(str, coordinates))
pv_Production50 = driver.find_element(By.ID, "pv50").text.replace("'", "") tolerance = 1
pv_Production75 = driver.find_element(By.ID, "pv75").text.replace("'", "") # this is wrong, bbox should be computed using tolerance and pixel size, but there are no problems for the
pv_Production100 = driver.find_element(By.ID, "pv100").text.replace("'", "") # purpose of this exercise
value_electricity_production = driver.find_elements(By.XPATH, "//h2[@id='TitelSolarstrom']//strong")[2].text.replace("'", "").replace(" Franken", "") bbox = (coordinates[0] - tolerance, coordinates[1] - tolerance, coordinates[0] + tolerance,
roof_area = driver.find_element(By.ID, "areaOutput").text 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']
except:
found = False
time.sleep(2) if(found):
image_filename = suitability + " - " + search_string try:
# 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'])
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'])
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_folder_map = "screenshots/"
image_filename_map = image_filename + " map" + ".png" image_filename_map = image_filename + " map" + ".png"
image_folder_production = "screenshots/" image_folder_production = "screenshots/"
image_filename_production = image_filename + " production" + ".png" image_filename_production = image_filename + " production" + ".png"
image_folder_qrcode = "qrcodes/" image_folder_qrcode = "qrcodes/"
image_filename_qrcode = image_filename + " qrcode" + ".png" 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))
if(stopThread == True):
print("closing Thread")
return
outputtext = outputtext + "Reached end of list.\nPress Exit to close the application." + "\n"
mainText.config(text=outputtext)
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 # Create QR-Code
qr = qrcode.QRCode(version=1, box_size=10, border=5) qr = qrcode.QRCode(version=1, box_size=10, border=5)
@ -137,138 +272,8 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText):
Image.open(image_folder_production + image_filename_production).crop(area).save( Image.open(image_folder_production + image_filename_production).crop(area).save(
image_folder_production + image_filename_production) image_folder_production + image_filename_production)
except: except:
image_filename_production = "not-found" pass
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
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']
# 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
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()
if(stopThread == True):
print("closing Thread")
return
outputtext = outputtext + "Reached end of list.\nPress Exit to close the application." + "\n"
mainText.config(text=outputtext)
print("closing Thread")
return
def read_adresslist(filename_adresslist): def read_adresslist(filename_adresslist):
returnvalues = [] returnvalues = []
@ -323,7 +328,7 @@ def command():
thread_search_adresses.join() thread_search_adresses.join()
except: except:
pass pass
print("Thread is closed") print("Threads are closed")
root.quit() root.quit()
print("Application is closed") print("Application is closed")
driver.quit() driver.quit()
@ -388,7 +393,6 @@ def command_exit():
root = tkinter.Tk() root = tkinter.Tk()
root.protocol("WM_DELETE_WINDOW", command_exit) root.protocol("WM_DELETE_WINDOW", command_exit)
root.wm_title("Sonnendach") root.wm_title("Sonnendach")

View File

@ -22,6 +22,7 @@ tolerance=1
# purpose of this exercise # purpose of this exercise
bbox=(coordinates[0]-tolerance, coordinates[1]-tolerance, coordinates[0]+tolerance,coordinates[1]+tolerance) bbox=(coordinates[0]-tolerance, coordinates[1]-tolerance, coordinates[0]+tolerance,coordinates[1]+tolerance)
bbox_str=','.join(map(str,bbox)) bbox_str=','.join(map(str,bbox))
# retrieve one roof and the building_id associated
params={ params={
"geometryType":"esriGeometryPoint", "geometryType":"esriGeometryPoint",
"returnGeometry":"true", "returnGeometry":"true",
@ -33,12 +34,12 @@ params={
"order":"distance", "order":"distance",
"lang":"de", "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 = 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()) response_one_roof_decoded = json.loads(response_one_roof.content.decode())
results = (response_one_roof_decoded['results']) results = (response_one_roof_decoded['results'])
# for result in results: for result in results:
# print(result['attributes']['building_id']) print(result['attributes']['building_id'])
building_id=(response_one_roof_decoded)['results'][0]['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 # 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), 'searchText': str(building_id),
'returnGeometry': 'false' 'returnGeometry': 'false'
} }
response_all_roofs = requests.get('https://api3.geo.admin.ch/rest/services/api/MapServer/find', params=params) 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'] response_all_roofs_decoded = json.loads(response_all_roofs.content.decode())['results']
for roof in response_all_roofs_decoded: for roof in response_all_roofs_decoded: