Skip to content

Commit

Permalink
Merge pull request #171 from kartAI/tempbranch
Browse files Browse the repository at this point in the history
Tempbranch
  • Loading branch information
gsvartsund authored Feb 20, 2024
2 parents d61852d + 34c3d4c commit 766f9b7
Show file tree
Hide file tree
Showing 45 changed files with 82 additions and 64 deletions.
Binary file modified WMS/__pycache__/ortofoto.cpython-311.pyc
Binary file not shown.
Binary file modified WMS/__pycache__/sanderscript.cpython-311.pyc
Binary file not shown.
Binary file modified WMS/__pycache__/util.cpython-311.pyc
Binary file not shown.
Binary file removed WMS/email/train/images/tile_0_0.png
Binary file not shown.
Binary file removed WMS/email/train/images/tile_10_10.png
Binary file not shown.
Binary file removed WMS/email/train/images/tile_11_11.png
Binary file not shown.
Binary file removed WMS/email/train/images/tile_1_1.png
Binary file not shown.
Binary file removed WMS/email/train/images/tile_2_2.png
Binary file not shown.
Binary file removed WMS/email/train/images/tile_3_3.png
Binary file not shown.
Binary file removed WMS/email/train/images/tile_4_4.png
Binary file not shown.
Binary file removed WMS/email/train/images/tile_5_5.png
Binary file not shown.
Binary file removed WMS/email/train/images/tile_6_6.png
Binary file not shown.
Binary file removed WMS/email/train/images/tile_7_7.png
Binary file not shown.
Binary file removed WMS/email/train/images/tile_8_8.png
Binary file not shown.
Binary file removed WMS/email/train/images/tile_9_9.png
Binary file not shown.
Binary file removed WMS/email/train/masks/tile_0_0.png
Binary file not shown.
Binary file removed WMS/email/train/masks/tile_10_10.png
Binary file not shown.
Binary file removed WMS/email/train/masks/tile_11_11.png
Binary file not shown.
Binary file removed WMS/email/train/masks/tile_1_1.png
Binary file not shown.
Binary file removed WMS/email/train/masks/tile_2_2.png
Binary file not shown.
Binary file removed WMS/email/train/masks/tile_3_3.png
Binary file not shown.
Binary file removed WMS/email/train/masks/tile_4_4.png
Binary file not shown.
Binary file removed WMS/email/train/masks/tile_5_5.png
Binary file not shown.
Binary file removed WMS/email/train/masks/tile_6_6.png
Binary file not shown.
Binary file removed WMS/email/train/masks/tile_7_7.png
Binary file not shown.
Binary file removed WMS/email/train/masks/tile_8_8.png
Binary file not shown.
Binary file removed WMS/email/train/masks/tile_9_9.png
Binary file not shown.
Binary file removed WMS/email/val/images/tile_12_12.png
Binary file not shown.
Binary file removed WMS/email/val/images/tile_13_13.png
Diff not rendered.
Binary file removed WMS/email/val/images/tile_14_14.png
Diff not rendered.
Binary file removed WMS/email/val/images/tile_15_15.png
Diff not rendered.
Binary file removed WMS/email/val/masks/tile_12_12.png
Diff not rendered.
Binary file removed WMS/email/val/masks/tile_13_13.png
Diff not rendered.
Binary file removed WMS/email/val/masks/tile_14_14.png
Diff not rendered.
Binary file removed WMS/email/val/masks/tile_15_15.png
Diff not rendered.
2 changes: 1 addition & 1 deletion WMS/ortofoto.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def generate_orto_picture():

# Oppretter en get request til WMS serveren gjennom url og api nøkkel
response = requests.get(full_url, headers=headers) # Ensure the request is made to `full_url`
print("HELLO");

if response.status_code == 200:
# Genererer et filnavn basert på dato og tid bildet ble hentet på
#timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
Expand Down
Binary file removed WMS/ortofoto_images/output_20240217013013.png
Diff not rendered.
Binary file removed WMS/rawphotos/fasit.png
Diff not rendered.
Binary file removed WMS/rawphotos/orto.png
Diff not rendered.
2 changes: 1 addition & 1 deletion WMS/resources/config.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"Config": {"data_parameters": ["70", "30", "10"], "layers": ["Bygning", "Veg", "Bru"], "colors": ["#000000", "#ffff00", "#00ff00"]}}
{"Config": {"data_parameters": ["90", "10", "100"], "layers": ["Bygning", "Veg", "Bru"], "colors": ["#000000", "#ffff00", "#00ff00"]}}
2 changes: 1 addition & 1 deletion WMS/resources/coordinates.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"Coordinates": [[586077.6407936335, 6646104.917552568], [586025.9038943398, 6648327.088944961], [588710.10992247, 6648390.564976909], [588763.4615420719, 6646168.418973851], [586077.6407936335, 6646104.917552568]]}
{"Coordinates": [[593258.009753139, 6644808.377803862], [593255.0498549859, 6644925.788442824], [593641.2676597863, 6644935.545142756], [593644.2398199342, 6644818.134709595], [593258.009753139, 6644808.377803862]]}
67 changes: 43 additions & 24 deletions WMS/Util.py → WMS/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from PIL import Image
import os
import shutil
import json
from fastapi import HTTPException


def split_image(image_path, output_folder, tile_size):
Expand Down Expand Up @@ -67,44 +69,61 @@ def split_files(image_path, output_folder, tiles, training_fraction, validation_
'''

try:
#Generate output folder
os.mkdir(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(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):
if(i < training_files):
os.path.join(image_path, "orto", f"tile_{i}_{i}.png")
os.path.join(output_folder, "train", "images")
os.path.join(output_folder, "train", "mask")
try:
shutil.copy2(image_path + f"/orto/tile_{i}_{i}.png", output_folder + "/train/images")
shutil.copy2(image_path + f"/fasit/tile_{i}_{i}.png", output_folder + "/train/masks")
shutil.copy2(os.path.join(image_path, "orto", f"tile_{i}_{i}.png"), os.path.join(output_folder, "train", "images"))
shutil.copy2( os.path.join(image_path, "fasit", f"tile_{i}_{i}.png"), os.path.join(output_folder, "train", "masks"))
except:
print("Something went wrong with copying...")
else:
try:
shutil.copy2(image_path + f"/orto/tile_{i}_{i}.png", output_folder + "/val/images")
shutil.copy2(image_path + f"/fasit/tile_{i}_{i}.png", output_folder + "/val/masks")
shutil.copy2(os.path.join(image_path, "orto", f"tile_{i}_{i}.png"), os.path.join(output_folder, "val", "images"))
shutil.copy2( os.path.join(image_path, "fasit", f"tile_{i}_{i}.png"), os.path.join(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")

def setup_WMS_folders():
folders_to_make = [os.path.join("WMS", "email", "train", "images"),
os.path.join("WMS", "email", "train", "masks"),
os.path.join("WMS", "email", "val", "images"),
os.path.join("WMS", "email", "val", "masks"),
os.path.join("WMS", "rawphotos"),
os.path.join("WMS", "tiles", "fasit"),
os.path.join("WMS", "tiles", "orto")]
for folder in folders_to_make:
os.makedirs(folder, exist_ok=True)


def teardown_WMS_folders():

try:
folders_to_delete = [os.path.join("WMS", "email"),
os.path.join("WMS", "rawphotos"),
os.path.join("WMS", "tiles")]
for folder in folders_to_delete:
shutil.rmtree(folder)
except:
print("Folders are already deleted!")


def read_file(file_path):
file = open(file_path)
return json.load(file)

def write_file(file_path, data):
try:
with open(file_path, "w") as file:
json.dump(data, file)
return 1
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to write config to json file: {str(e)}")
Binary file modified __pycache__/main.cpython-311.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions frontend/scripts/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async function getFiles() {

// Calls the getFiles function when the page loads
window.onload = function () {
getFiles();
//getFilaes();
};

// Updates the file list summary based on the files selected by the user
Expand All @@ -39,7 +39,7 @@ function sendEmail() {
document.getElementById("filesPreview").innerHTML = "<p>Loading...</p>";

// Sends a POST request to the server to send the email with the selected files attached
fetch("/send_zip_file", {
fetch("/sendEmail", {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down
69 changes: 34 additions & 35 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

class Input(BaseModel):
input: list


class ConfigInput(BaseModel):
data_parameters: list
Expand Down Expand Up @@ -70,6 +71,7 @@ async def update_training(input: list):

with open(CONFIG_FILE, "r") as file:
data = json.load(file)


# Ensure that the "ProjectArguments" key exists in the JSON object
if "ProjectArguments" not in data:
Expand Down Expand Up @@ -290,12 +292,8 @@ async def send_zip_file(request: Request):
@app.post("/updateWMSCoordinateFile")
async def update_wms_coordinate_file(input: Input):
data = {"Coordinates": input.input}
try:
with open(COORDINATE_FILE, "w") as file:
json.dump(data, file)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to write coordinates to json file: {str(e)}")
return {"Message": "Coordinates were updated successfully"}
if(util.write_file(COORDINATE_FILE, data)):
return {"Message": "Coordinates were updated successfully"}

#Route for updating the coordinate file in the WMS/Resources folder
@app.post("/updateWMSConfigFile")
Expand All @@ -305,48 +303,46 @@ async def update_wms_config_file(configInput: ConfigInput):
"layers": configInput.layers,
"colors": configInput.colors
}}
try:
with open(CONFIG_FILE, "w") as file:
json.dump(data, file)
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"}
if(util.write_file(CONFIG_FILE, data)):
return {"Message": "Coordinates were updated successfully"}


@app.post("/generatePhotos")
async def generatePhotos():
#Slett de gamle mappene hvis de fortsatt er der.
util.teardown_WMS_folders()

#Read config from the file
file = open(CONFIG_FILE)
data = json.load(file)
config = data["Config"];


config = util.read_file(CONFIG_FILE)["Config"];

#Genererer alle mappene for WMS
util.setup_WMS_folders()
#Genererer bilder fra de forskjellige WMSene
fasit_path = sanderscript.generate_wms_picture()
orto_path = ortofoto.generate_orto_picture()
sanderscript.generate_wms_picture()
ortofoto.generate_orto_picture()

#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])
util.split_image(os.path.join("WMS", "rawphotos", "fasit.png"), os.path.join("WMS", "tiles", "fasit"), 100)
tiles = util.split_image(os.path.join("WMS", "rawphotos", "orto.png"), os.path.join("WMS", "tiles", "orto"), 100)
util.split_files(os.path.join("WMS", "tiles"), os.path.join("WMS/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')
env_file_path = os.path.join("ngisopenapi", ".env")

# Laster .env fra riktig path
load_dotenv(env_file_path)

print(os.getenv("SENDGRID_API_KEY"))

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='[email protected]', # Sender epost api
to_emails=to_emails, # Til epost som blir lagt inn, tror den er definert som "email" i koden.
Expand All @@ -367,38 +363,41 @@ def send_email_with_attachment(to_emails, subject, content, attachment_path):
message.attachment = attachedFile

try:
sg = SendGridAPIClient(os.getenv('SENDGRID_API_KEY')) # Henter API nøkkel
sg = SendGridAPIClient(api_key=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'):
def zip_files(directory_path: str = os.path.join("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():
@app.post("/sendEmail")
async def send_zipped_files_email(request : Request):

# Extract email from request
email = {}
if request.body:
email = await request.json()
"""Zip and send email to endpoint"""
zip_files() # Zipper alle filer i WMS/email/

send_email_with_attachment(
to_emails=["[email protected]"],
to_emails=email["email"],
subject="Here are your zipped files",
content="<strong>Zip file holding the requested data.</strong>",
attachment_path="attachments.zip"
)

# Sletter zip etter sending
os.remove("attachments.zip")
#Sletter alle de midlertidige mappene for WMS
util.teardown_WMS_folders()

return {"message": "Email sent successfully with zipped files."}

if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)

0 comments on commit 766f9b7

Please sign in to comment.