-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ryddet litt i koden og lagt til kommentarer
- Loading branch information
1 parent
9b93923
commit 40a0e45
Showing
11 changed files
with
82 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,73 @@ | ||
import os | ||
import requests | ||
from datetime import datetime | ||
import urllib.parse | ||
from dotenv import load_dotenv | ||
import json | ||
from WMS import util | ||
|
||
def generate_orto_picture(): | ||
# Finner path til .env filen som ligger i ngisopenapi mappen | ||
''' | ||
This function is used to generate a photo using Norkart's WMS with orthophoto capabilities. | ||
Returns: | ||
True (Bool) if the image was generated | ||
False (Bool) otherwise | ||
''' | ||
|
||
#Defines the URL for the WMS to be used | ||
wms_url = 'https://waapi.webatlas.no/wms-orto/' | ||
|
||
#Finds the path to the enviornment file in the NGISopenAPI directory and the path to the coordinates file | ||
current_script_directory = os.path.dirname(os.path.abspath(__file__)) | ||
env_file_path = os.path.join(current_script_directory, "..", "ngisopenapi", ".env") | ||
coordinates_file_path = os.path.join(current_script_directory, 'resources', 'coordinates.json') | ||
|
||
# Laster .env fra riktig path | ||
#Loads environment variables from the right file | ||
load_dotenv(env_file_path) | ||
|
||
# Henter API nøkkelen fra .env | ||
api_key = os.getenv('NK_WMS_API_KEY') | ||
|
||
# Definerer WMS url | ||
wms_url = 'https://waapi.webatlas.no/wms-orto/' | ||
|
||
# Leser inn koordinatene fra JSON-filen | ||
#Reads the coordinates from the coordinates-JSON file | ||
coordinates = util.read_file(coordinates_file_path)['Coordinates'] | ||
|
||
# Beregner bbox fra koordinatene gitt i applikasjonen | ||
#Calculates a bbox based on the coordinates | ||
bbox = util.create_bbox(coordinates) | ||
|
||
# Setter directory for lagring av bilde | ||
#Directory where the image will be saved | ||
images_directory = "rawphotos" | ||
|
||
# Lager hele pathen i samme mappe | ||
images_directory_path = os.path.join(current_script_directory, images_directory) | ||
|
||
# Angi hvilke layers, bbox og hva enn du er interessert i | ||
#Parameters for the WMS call | ||
params = { | ||
"api_key": api_key, | ||
"api_key": os.getenv('NK_WMS_API_KEY'), | ||
"request": "GetMap", | ||
"width" : "1600", | ||
"height": "1600", | ||
"layers": "ortofoto", | ||
"srs": "EPSG:25832", | ||
'format': 'image/png', # Fil format | ||
'bbox': bbox, # Oppdaterer bbox med de nye verdiene | ||
'format': 'image/png', | ||
'bbox': bbox, | ||
} | ||
|
||
# Build the full URL with the parameters and headers | ||
encoded_params = urllib.parse.urlencode(params, quote_via=urllib.parse.quote) | ||
|
||
# Build and print the full URL | ||
full_url = f"{wms_url}?{encoded_params}" | ||
|
||
# Headers som legger en browser request | ||
headers = { | ||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" | ||
} | ||
|
||
# Oppretter en get request til WMS serveren gjennom url og api nøkkel | ||
#Uses the request library to make a request to the url | ||
response = requests.get(full_url, headers=headers) # Ensure the request is made to `full_url` | ||
|
||
#Check if the response from the server was OK | ||
if response.status_code == 200: | ||
#Define the file name and path for the image | ||
file_name = f"orto.png" | ||
|
||
# Hele fil pathen | ||
image_path = os.path.join(images_directory_path, file_name) | ||
|
||
|
||
#Save the image | ||
with open(image_path, 'wb') as file: | ||
file.write(response.content) | ||
print(f"Bildet ble lagret i {image_path}.") | ||
return True | ||
else: | ||
#If something went wrong, print the status code and explanation | ||
print(f"Kunne ikke lagre ortofoto-bilde, statuskode: {response.status_code}") | ||
print(f"Error in creating ortophoto: {response.reason}" ) | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters