From d1a355bb9042b87d283e5f65214685924c2bf2df Mon Sep 17 00:00:00 2001 From: victbakk Date: Thu, 15 Feb 2024 12:29:42 +0100 Subject: [PATCH 1/2] WMS to orthophoto, missing accesses --- WMS/Orotfoto.py | 49 ++++++++++++++++++++++- __pycache__/deleteFolder.cpython-311.pyc | Bin 0 -> 2696 bytes __pycache__/main.cpython-311.pyc | Bin 13831 -> 13821 bytes requirements.txt | 3 +- 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 __pycache__/deleteFolder.cpython-311.pyc diff --git a/WMS/Orotfoto.py b/WMS/Orotfoto.py index c81d4d6..ee64d8d 100644 --- a/WMS/Orotfoto.py +++ b/WMS/Orotfoto.py @@ -1 +1,48 @@ -#Dette scriptet har med ortofoto å gjøre +import os +from dotenv import load_dotenv +import requests +from datetime import datetime + +# Laster inn environment fra .env filen +load_dotenv() + +# Laster API nøkkel +api_key = os.getenv("NK_WMS_API_KEY") + +# Definerer WMS url +wms_url = 'https://waapi.webatlas.no/wms-orto/' + +# Directory for lagrede bilder +images_directory = "ortofoto_images" + +# Hele pathen til directory for bilder +images_directory_path = os.path.join(os.path.dirname(__file__), images_directory) + +# Angi hvilke layers, bbox og hva enn du er interessert i +params = { + 'service': 'WMS', + 'request': 'GetMap', + 'layers': 'bygning, veg, bru', + 'bbox': '86862.34650433670322,6466039.970492540859,87579.68362640209671,6466748.95569468569', + 'width': '800', + 'height': '600', + 'srs': 'EPSG:25832', + 'format': 'image/png', # Fil format + 'apikey': api_key # Henter API nøkkel +} + +# Oppretter en get request til WMS serveren gjennom url og api nøkkel +response = requests.get(wms_url, params=params) + +if response.status_code == 200: + timestamp = datetime.now().strftime("%Y%m%d%H%M%S") # Legger til tid og dato filen ble opprettet på for å gjøre den unik + file_name = f"output_{timestamp}.png" + + # Hele pathen til bildefilen + image_path = os.path.join(images_directory_path, file_name) + + with open(image_path, 'wb') as file: + file.write(response.content) + print(f"Bildet ble lagret i {image_path}.") # Om det går feilfritt så blir denne kjørt +else: + print(f"Kunne ikke lagre bilde, statuskode: {response.status_code}") # Error, 403 er access denied, annet så må man søke seg opp. diff --git a/__pycache__/deleteFolder.cpython-311.pyc b/__pycache__/deleteFolder.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..2b655bb76bd5a26f57a6d60689dad23db25fdd21 GIT binary patch literal 2696 zcmbVNT}UHW6uy&8lF6S@v8`^_It`^7R%6-jvKn2**1C#RX|tsumLX=YH5wDSGd8;t z*R%*NBCM1aWxLDzWGzzYrez z_sn;G=DTn>fMERfzaUF&Gdh0(m*+OW%7 zv+BD;!&5D>=GeOjCmg`PGMuX^pTh!YX;t5`Pjri{=-C4g4s0)`{4c!ITeCCp!aIF6 zJA)zzyo7LA^e?!mk0jtiIJ5_|1KVW=i2=~66PXl>2mheYCo=Z?!-~EG<@Mwdz&4Tr ztub%w?Hcy2=hnT1ocxgv86s(6^a{5CF6KVR>Y(}1$p13iV>XJ!1o6qJy z=~_%@=CXO*Rm3xyTo&i$Y0>a*#PBV#bXyNs9tai7Bm_n&dD09(86Elj>%}kbe12zZ zLG{HnU+fsMp1vclZZix|xR}Pp6fSneg_XL*ZWI76mFw2HZpFHuQep4@H~?HKm(;kV zVqGV23evp`X(A7e>0!%nK}^eOJ$%!Ajmok%ZOh3T{lF3dW8s^#n%T&`GvYjlbCaEAXFgYs=e^iN>zzqN;R zT1izj?Ng9^ZoHh)TRpEqJ+s}HAnQbz=yo>FRpuPeo@axtvElk3HflZ^t8SUb*&Jfc zrwQ^tV#yFHovof~>6t8c>~%Xi1k2Uu`f8=L@y59cQU}KqdJ3th&_JP)LKB6T0OA~> zbxSC5x=+lKe0mY zi5lc#RMNlE?2_qR&a4=T#6aT`Reb^g#Sm%OEZ$w)SbIS37ay+OTl?Xb+Sa4B^{A0v zEz(;aJ&Ly8zxJ^IUjHFiztyobxi9?fW-bo=g8&q^`KhtLv{5p}O4lIB>xURn04$H% z3Ol2_b3aBNx2lmzEiws)&y=C!EW<5F89G+ym0>(mp_G0LjJ{!zOIqpjRSInsUZcQM zaI$AK3R=lD5Q1P(MOvty-BC`t{>6g0oWld;br8^ADE$U->}D9|5c$i-?+}H{#_u@b zVfv1d4UH&Nw|VXE;Ktz2Wngvi-r#Nw~`c1JC?*n=0I>OAM%G8lBgcUt)i?_V2mb<|_Y$T6 literal 0 HcmV?d00001 diff --git a/__pycache__/main.cpython-311.pyc b/__pycache__/main.cpython-311.pyc index 70ab2b10909145ee1e878e6e42d55dc436a3c681..8018a97023e5acf4ef7895b6590b28a9a3f2a62b 100644 GIT binary patch delta 137 zcmV;40CxX}Z2fBurwt7X00000@5{ht{jm)`2M;zwI$TwAWpZ;|c4=dDVzXoivIGG+ zvlk4#0|8pI)eqhQ0cW$c5w-yVZnJ9>Pyzvele`t)0fdvA7SI8Svuqc*0RfP+B^k~F r0il!h8~g#Cv+*2<0s*VDc^_yC0kpH?Dy0Yk4wHy8lK~8~95m|zZ9Ou1 delta 147 zcmV;E0BryLYlmzNrwt7X00000<8`lP`LPW?2N^&^I$TwAWpZ;|L1SZYb#8QAL}hbp zbZ>C8S_iTO0XVY_47~#ZS+mOz-T?t-v#Jrc0Re2YVG~dS0ezFQ72W}YlaUtC0f)0< z7q|fdjj5}_ BHBtZo diff --git a/requirements.txt b/requirements.txt index 64a2f7f..374b2ca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,4 +14,5 @@ pyproj shapely scipy python-multipart -httpx \ No newline at end of file +httpx +python-env \ No newline at end of file From 28dd861055fdeeb0ee9e55a28a671f646415f544 Mon Sep 17 00:00:00 2001 From: victbakk Date: Thu, 15 Feb 2024 12:50:55 +0100 Subject: [PATCH 2/2] fikset litt forskjellig, la til funksjon hvor den oppretter egen mappe om den ikke finnes --- WMS/Orotfoto.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/WMS/Orotfoto.py b/WMS/Orotfoto.py index ee64d8d..7d4bd72 100644 --- a/WMS/Orotfoto.py +++ b/WMS/Orotfoto.py @@ -12,12 +12,15 @@ # Definerer WMS url wms_url = 'https://waapi.webatlas.no/wms-orto/' -# Directory for lagrede bilder +# Setter directory for lagring av bilde images_directory = "ortofoto_images" -# Hele pathen til directory for bilder +# Lager hele pathen i samme mappe images_directory_path = os.path.join(os.path.dirname(__file__), images_directory) +# Sjekker om filen eksisterer +os.makedirs(images_directory_path, exist_ok=True) + # Angi hvilke layers, bbox og hva enn du er interessert i params = { 'service': 'WMS', @@ -35,14 +38,15 @@ response = requests.get(wms_url, params=params) if response.status_code == 200: - timestamp = datetime.now().strftime("%Y%m%d%H%M%S") # Legger til tid og dato filen ble opprettet på for å gjøre den unik + # Genererer et filnavn basert på dato og tid bildet ble hentet på + timestamp = datetime.now().strftime("%Y%m%d%H%M%S") file_name = f"output_{timestamp}.png" - # Hele pathen til bildefilen + # Hele fil pathen image_path = os.path.join(images_directory_path, file_name) with open(image_path, 'wb') as file: file.write(response.content) - print(f"Bildet ble lagret i {image_path}.") # Om det går feilfritt så blir denne kjørt + print(f"Bildet ble lagret i {image_path}.") else: - print(f"Kunne ikke lagre bilde, statuskode: {response.status_code}") # Error, 403 er access denied, annet så må man søke seg opp. + print(f"Kunne ikke lagre bilde, statuskode: {response.status_code}")