diff --git a/WMS/Util.py b/WMS/Util.py index 8705c09..dd0a1c7 100644 --- a/WMS/Util.py +++ b/WMS/Util.py @@ -1,5 +1,7 @@ #Import Pillow library for working with images from PIL import Image +import os +import shutil def split_image(image_path, output_folder, tile_size): @@ -19,7 +21,7 @@ def split_image(image_path, output_folder, tile_size): print("done"); #Checks if the file is of the correct type, otherwise raises an error - if(image_path.split(".")[2] not in acceptedFileTypes): + if(image_path.split(".")[1] not in acceptedFileTypes): return "Filetype not supported" @@ -46,8 +48,65 @@ def split_image(image_path, output_folder, tile_size): tile = image.crop((left_top, left_bottom, right_top, right_bottom)) #Save the tile to the output folder - tile.save(f"{output_folder}/tile_{i}_{j}.jpeg") + tile.save(f"{output_folder}/tile_{i}_{i}.png") - #Return the amount of tiles created to be used for testing purposes + #Return the amount of tiles created return horizontal_tiles + vertical_tiles + +def split_files(image_path, output_folder, tiles, training_fraction, validation_fraction): + ''' + Splits a set of tiles and sorts them according to machine learning specifications + + Args: + image_path (str): The path to where the images are stored + output_folder (str): The path to the output folder + tiles (int): The amount of tiles that need to be handled + training_fraction (int): The amount of tiles that will be used for training + validation (int): The amount of tiles that will be used for validation + + ''' + + + + try: + #Generate output folder + os.mkdir("WMS/" + output_folder + "/") + + #Generate subfolders based on the standard + folders = ["train", "val", "/train/images", "/train/masks", "/val/images", "/val/masks"] + for folder in folders: + path = os.path.join("WMS/" + output_folder + "/" + folder) + os.mkdir(path) + except: + print("Something went wrong with generating the folders...") + + + #Calculate the amount of files for each fraction + training_files = int(training_fraction)/100 * int(tiles) + validation_files = int(validation_fraction)/100 * int(tiles) + + #Copy the files into the right places + for i in range(0, tiles - 1): + if(i > 0 and i < training_files): + try: + shutil.copy2(image_path + f"/orto/tile_{i}_{i}.png", "WMS/" + output_folder + "/train/images") + shutil.copy2(image_path + f"/fasit/tile_{i}_{i}.png", "WMS/" + output_folder + "/train/masks") + except: + print("Something went wrong with copying...") + else: + try: + shutil.copy2(image_path + f"/orto/tile_{i}_{i}.png", "WMS/" + output_folder + "/val/images") + shutil.copy2(image_path + f"/fasit/tile_{i}_{i}.png", "WMS/" + output_folder + "/val/masks") + except: + print("Something went wrong with copying...") + + #Delete the files, we dont need them anymore + for i in range(0, tiles): + try: + os.remove(image_path + f"/orto/tile_{i}_{i}.png") + os.remove(image_path + f"/fasit/tile_{i}_{i}.png") + except: + print("Couldn't delete") + + diff --git a/WMS/__pycache__/__init__.cpython-311.pyc b/WMS/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000..3190edb Binary files /dev/null and b/WMS/__pycache__/__init__.cpython-311.pyc differ diff --git a/WMS/email/train/images/tile_1_1.png b/WMS/email/train/images/tile_1_1.png new file mode 100644 index 0000000..51a6b3e Binary files /dev/null and b/WMS/email/train/images/tile_1_1.png differ diff --git a/WMS/email/train/images/tile_2_2.png b/WMS/email/train/images/tile_2_2.png new file mode 100644 index 0000000..894ba60 Binary files /dev/null and b/WMS/email/train/images/tile_2_2.png differ diff --git a/WMS/email/train/images/tile_3_3.png b/WMS/email/train/images/tile_3_3.png new file mode 100644 index 0000000..2eae7b0 Binary files /dev/null and b/WMS/email/train/images/tile_3_3.png differ diff --git a/WMS/email/train/images/tile_4_4.png b/WMS/email/train/images/tile_4_4.png new file mode 100644 index 0000000..2e9cef8 Binary files /dev/null and b/WMS/email/train/images/tile_4_4.png differ diff --git a/WMS/email/train/images/tile_5_5.png b/WMS/email/train/images/tile_5_5.png new file mode 100644 index 0000000..53c0855 Binary files /dev/null and b/WMS/email/train/images/tile_5_5.png differ diff --git a/WMS/email/train/images/tile_6_6.png b/WMS/email/train/images/tile_6_6.png new file mode 100644 index 0000000..72b5014 Binary files /dev/null and b/WMS/email/train/images/tile_6_6.png differ diff --git a/WMS/email/train/images/tile_7_7.png b/WMS/email/train/images/tile_7_7.png new file mode 100644 index 0000000..ad6338f Binary files /dev/null and b/WMS/email/train/images/tile_7_7.png differ diff --git a/WMS/email/train/images/tile_8_8.png b/WMS/email/train/images/tile_8_8.png new file mode 100644 index 0000000..9293b8d Binary files /dev/null and b/WMS/email/train/images/tile_8_8.png differ diff --git a/WMS/email/train/masks/tile_1_1.png b/WMS/email/train/masks/tile_1_1.png new file mode 100644 index 0000000..3f60232 Binary files /dev/null and b/WMS/email/train/masks/tile_1_1.png differ diff --git a/WMS/email/train/masks/tile_2_2.png b/WMS/email/train/masks/tile_2_2.png new file mode 100644 index 0000000..5f85b1d Binary files /dev/null and b/WMS/email/train/masks/tile_2_2.png differ diff --git a/WMS/email/train/masks/tile_3_3.png b/WMS/email/train/masks/tile_3_3.png new file mode 100644 index 0000000..db4731b Binary files /dev/null and b/WMS/email/train/masks/tile_3_3.png differ diff --git a/WMS/email/train/masks/tile_4_4.png b/WMS/email/train/masks/tile_4_4.png new file mode 100644 index 0000000..89a9435 Binary files /dev/null and b/WMS/email/train/masks/tile_4_4.png differ diff --git a/WMS/email/train/masks/tile_5_5.png b/WMS/email/train/masks/tile_5_5.png new file mode 100644 index 0000000..94caf44 Binary files /dev/null and b/WMS/email/train/masks/tile_5_5.png differ diff --git a/WMS/email/train/masks/tile_6_6.png b/WMS/email/train/masks/tile_6_6.png new file mode 100644 index 0000000..439ff78 Binary files /dev/null and b/WMS/email/train/masks/tile_6_6.png differ diff --git a/WMS/email/train/masks/tile_7_7.png b/WMS/email/train/masks/tile_7_7.png new file mode 100644 index 0000000..7657e70 Binary files /dev/null and b/WMS/email/train/masks/tile_7_7.png differ diff --git a/WMS/email/train/masks/tile_8_8.png b/WMS/email/train/masks/tile_8_8.png new file mode 100644 index 0000000..b99c2e3 Binary files /dev/null and b/WMS/email/train/masks/tile_8_8.png differ diff --git a/WMS/email/val/images/tile_0_0.png b/WMS/email/val/images/tile_0_0.png new file mode 100644 index 0000000..51a6b3e Binary files /dev/null and b/WMS/email/val/images/tile_0_0.png differ diff --git a/WMS/email/val/masks/tile_0_0.png b/WMS/email/val/masks/tile_0_0.png new file mode 100644 index 0000000..3f60232 Binary files /dev/null and b/WMS/email/val/masks/tile_0_0.png differ diff --git a/WMS/ortofoto_images/output_20240217013013.png b/WMS/ortofoto_images/output_20240217013013.png new file mode 100644 index 0000000..526d9cc Binary files /dev/null and b/WMS/ortofoto_images/output_20240217013013.png differ diff --git a/WMS/rawphotos/eksempel.png b/WMS/rawphotos/eksempel.png deleted file mode 100644 index ddc8e77..0000000 Binary files a/WMS/rawphotos/eksempel.png and /dev/null differ diff --git a/WMS/rawphotos/fasit.png b/WMS/rawphotos/fasit.png new file mode 100644 index 0000000..37e7981 Binary files /dev/null and b/WMS/rawphotos/fasit.png differ diff --git a/WMS/rawphotos/orto.png b/WMS/rawphotos/orto.png new file mode 100644 index 0000000..95bb9c7 Binary files /dev/null and b/WMS/rawphotos/orto.png differ diff --git a/WMS/readyforemail/tile_0_0.jpeg b/WMS/readyforemail/tile_0_0.jpeg deleted file mode 100644 index 52c1048..0000000 Binary files a/WMS/readyforemail/tile_0_0.jpeg and /dev/null differ diff --git a/WMS/readyforemail/tile_0_1.jpeg b/WMS/readyforemail/tile_0_1.jpeg deleted file mode 100644 index 238b02e..0000000 Binary files a/WMS/readyforemail/tile_0_1.jpeg and /dev/null differ diff --git a/WMS/readyforemail/tile_0_2.jpeg b/WMS/readyforemail/tile_0_2.jpeg deleted file mode 100644 index bd23abf..0000000 Binary files a/WMS/readyforemail/tile_0_2.jpeg and /dev/null differ diff --git a/WMS/readyforemail/tile_1_0.jpeg b/WMS/readyforemail/tile_1_0.jpeg deleted file mode 100644 index 8ed6583..0000000 Binary files a/WMS/readyforemail/tile_1_0.jpeg and /dev/null differ diff --git a/WMS/readyforemail/tile_1_1.jpeg b/WMS/readyforemail/tile_1_1.jpeg deleted file mode 100644 index 14ec2e1..0000000 Binary files a/WMS/readyforemail/tile_1_1.jpeg and /dev/null differ diff --git a/WMS/readyforemail/tile_1_2.jpeg b/WMS/readyforemail/tile_1_2.jpeg deleted file mode 100644 index 44dfad1..0000000 Binary files a/WMS/readyforemail/tile_1_2.jpeg and /dev/null differ diff --git a/WMS/readyforemail/tile_2_0.jpeg b/WMS/readyforemail/tile_2_0.jpeg deleted file mode 100644 index 29346bf..0000000 Binary files a/WMS/readyforemail/tile_2_0.jpeg and /dev/null differ diff --git a/WMS/readyforemail/tile_2_1.jpeg b/WMS/readyforemail/tile_2_1.jpeg deleted file mode 100644 index 15c10d8..0000000 Binary files a/WMS/readyforemail/tile_2_1.jpeg and /dev/null differ diff --git a/WMS/readyforemail/tile_2_2.jpeg b/WMS/readyforemail/tile_2_2.jpeg deleted file mode 100644 index 42a7bf7..0000000 Binary files a/WMS/readyforemail/tile_2_2.jpeg and /dev/null differ diff --git a/WMS/readyforemail/tile_3_0.jpeg b/WMS/readyforemail/tile_3_0.jpeg deleted file mode 100644 index 46ff02e..0000000 Binary files a/WMS/readyforemail/tile_3_0.jpeg and /dev/null differ diff --git a/WMS/readyforemail/tile_3_1.jpeg b/WMS/readyforemail/tile_3_1.jpeg deleted file mode 100644 index bc8d2bf..0000000 Binary files a/WMS/readyforemail/tile_3_1.jpeg and /dev/null differ diff --git a/WMS/readyforemail/tile_3_2.jpeg b/WMS/readyforemail/tile_3_2.jpeg deleted file mode 100644 index 855e19d..0000000 Binary files a/WMS/readyforemail/tile_3_2.jpeg and /dev/null differ diff --git a/WMS/resources/config.json b/WMS/resources/config.json index 5329e30..8b1d465 100644 --- a/WMS/resources/config.json +++ b/WMS/resources/config.json @@ -1 +1 @@ -{"Config": {"data_parameters": ["60", "40", "5"], "layers": ["Bygning", "Veg", "Bru"], "colors": ["#000000", "#ffff00", "#00ff00"]}} \ No newline at end of file +{"Config": {"data_parameters": ["90", "10", "10"], "layers": ["Bygning", "Veg", "Bru"], "colors": ["#000000", "#ffff00", "#00ff00"]}} \ No newline at end of file diff --git a/WMS/resources/coordinates.json b/WMS/resources/coordinates.json index 7e42e62..eff35b1 100644 --- a/WMS/resources/coordinates.json +++ b/WMS/resources/coordinates.json @@ -1 +1 @@ -{"Coordinates": [[588751.5420105711, 6642899.275265058], [588749.8344259012, 6642970.498298281], [589238.8910459754, 6642982.255880976], [589240.6080427505, 6642911.032997987], [588751.5420105711, 6642899.275265058]]} \ No newline at end of file +{"Coordinates": [[586077.6407936335, 6646104.917552568], [586025.9038943398, 6648327.088944961], [588710.10992247, 6648390.564976909], [588763.4615420719, 6646168.418973851], [586077.6407936335, 6646104.917552568]]} \ No newline at end of file diff --git a/__pycache__/deleteFolder.cpython-311.pyc b/__pycache__/deleteFolder.cpython-311.pyc index 24091c5..222bd6e 100644 Binary files a/__pycache__/deleteFolder.cpython-311.pyc and b/__pycache__/deleteFolder.cpython-311.pyc differ diff --git a/__pycache__/main.cpython-311.pyc b/__pycache__/main.cpython-311.pyc index 9c96478..4ef9196 100644 Binary files a/__pycache__/main.cpython-311.pyc and b/__pycache__/main.cpython-311.pyc differ diff --git a/frontend/pages/setConstraints.html b/frontend/pages/setConstraints.html index f76b1c8..b01efb5 100644 --- a/frontend/pages/setConstraints.html +++ b/frontend/pages/setConstraints.html @@ -92,9 +92,13 @@ diff --git a/frontend/scripts/script.js b/frontend/scripts/script.js index ff73f95..216a8dd 100644 --- a/frontend/scripts/script.js +++ b/frontend/scripts/script.js @@ -132,6 +132,17 @@ function confirmTraining() { .catch(error => console.error(error)); } +// Initiate a download process and redirect to next page +function generatePhotos() { + fetch('/generatePhotos', { + method: 'POST' + }) + .then(() => { + window.location.href = '/order.html'; + }) + .catch(error => console.error(error)); +} + // Display the loading modal function loadingModal() { $('.modal').modal('show'); diff --git a/main.py b/main.py index 9767c7d..1c190da 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ import smtplib import ssl import zipfile +from zipfile import ZipFile import base64 import sendgrid import asyncio @@ -23,6 +24,7 @@ from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from deleteFolder import delete_all_folders +from WMS import util # Class for the FastAPI. Will contain all our methods for updating values and starting scripts @@ -35,8 +37,6 @@ class ConfigInput(BaseModel): layers: list colors: list - - # Import and create instance of the FastAPI framework app = FastAPI() @@ -308,3 +308,96 @@ async def update_wms_config_file(configInput: ConfigInput): except Exception as e: raise HTTPException(status_code=500, detail=f"Failed to write config to json file: {str(e)}") return {"Message": "Config was updated successfully"} + + +@app.post("/generatePhotos") +async def generatePhotos(): + + #Read config from the file + file = open(CONFIG_FILE) + data = json.load(file) + config = data["Config"]; + + + #Her må de forskjellige WMSene plugges inn + #generate_wms_picture(coordinates[0], coordinates[1], coordinates[2], coordinates[3]) + #generate_wms_photo(coordinates, config) + + #Også må de riktige urlene plugges inn som image_path + + util.split_image("WMS/rawphotos/fasit.png", "WMS/tiles/fasit", 100) + tiles = util.split_image("WMS/rawphotos/orto.png", "WMS/tiles/orto", 100) + util.split_files("WMS/tiles", "email", tiles, config["data_parameters"][0], config["data_parameters"][1]) + +# Her begynner fil zipping og epost sending for WMS/Fasit + +# Finner path til .env filen som ligger i ngisopenapi mappen +current_script_directory = os.path.dirname(os.path.abspath(__file__)) +project_root = os.path.abspath(os.path.join(current_script_directory, '..', 'ngisopenapi')) +env_file_path = os.path.join(project_root, '.env') + +# Laster .env fra riktig path +load_dotenv(env_file_path) + + +def send_email_with_attachment(to_emails, subject, content, attachment_path): + """Define email sending through SendGrid""" + + if not os.path.exists(attachment_path): + raise FileNotFoundError(f"Attachment '{attachment_path}' not found.") + + message = Mail( + from_email='victbakk@gmail.com', # Sender epost api + to_emails=to_emails, # Til epost som blir lagt inn, tror den er definert som "email" i koden. + subject=subject, + html_content=content + ) + + with open(attachment_path, 'rb') as f: + data = f.read() + encoded_file = base64.b64encode(data).decode() + + attachedFile = Attachment( + FileContent(encoded_file), + FileName(os.path.basename(attachment_path)), + FileType('application/zip'), + Disposition('attachment') + ) + message.attachment = attachedFile + + try: + sg = SendGridAPIClient(os.getenv('SENDGRID_API_KEY')) # Henter API nøkkel + response = sg.send(message) + # Prints response below + print(f"Email sent. Status code: {response.status_code}") + except Exception as e: + print(f"An error occurred: {e}") + +def zip_files(directory_path: str = 'WMS/email/', zip_name: str = 'attachments.zip'): + """Zip all files in the specified directory and save them to a zip file.""" + with ZipFile(zip_name, 'w') as zipf: + for root, dirs, files in os.walk(directory_path): + for file in files: + file_path = os.path.join(root, file) + zipf.write(file_path, arcname=os.path.relpath(file_path, directory_path)) + +@app.post("/send-email/") +async def send_zipped_files_email(): + """Zip and send email to endpoint""" + zip_files() # Zipper alle filer i WMS/email/ + + send_email_with_attachment( + to_emails=["recipient@example.com"], + subject="Here are your zipped files", + content="Zip file holding the requested data.", + attachment_path="attachments.zip" + ) + + # Sletter zip etter sending + os.remove("attachments.zip") + + return {"message": "Email sent successfully with zipped files."} + +if __name__ == "__main__": + import uvicorn + uvicorn.run(app, host="127.0.0.1", port=8000) \ No newline at end of file diff --git a/test_main.py b/test_main.py index 6c913d8..d182f85 100644 --- a/test_main.py +++ b/test_main.py @@ -1,5 +1,6 @@ from fastapi.testclient import TestClient from main import app +from unittest.mock import patch #Import test client @@ -28,8 +29,4 @@ def test_update_wms_config_file(): #Check that the responses are okay assert response.status_code == 200 - assert response.json() == {"Message": "Config was updated successfully"} - - - - + assert response.json() == {"Message": "Config was updated successfully"} \ No newline at end of file