diff --git a/get_nib.py b/get_nib.py index 15cceb0d63b23c3f16458e4a1ca59b1820de6ece..470cdc4aad57cbce9d9fc9c6ac0deae011cf43ef 100644 --- a/get_nib.py +++ b/get_nib.py @@ -64,6 +64,7 @@ from urllib import parse # to parse a string import requests # https://www.geeksforgeeks.org/response-json-python-requests/ https://realpython.com/python-requests/ import os # get the current working directory, access to file / path import datetime # get the current year +import json # https://docs.python.org/3/library/json.html # Initialize Qt resources from file resources.py from .resources import * @@ -660,19 +661,19 @@ class getnib: # https://norgeibilder.no/dok/webtjenester.pdf # 1 = Ortofoto 10, 2 = Ortofoto 20, 3 = Ortofoto 50, 4 = Ortofoto N50, 5 = Ortofoto Skog, 6 = Satellittbilde, 7 = Infrarødt, 8 = Rektifiserte flybilder, 9 = Ortofoto if of and not sat and not ir: - param ='{Filter:"ortofototype in (1,2,3,4,5,8,9)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',StopOnCover:false}' + param ='{Filter:"ortofototype in (1,2,3,4,5,8,9)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',ReturnMetadata:true,StopOnCover:false}' if of and sat and not ir: - param = '{Filter:"ortofototype in (1,2,3,4,5,6,8,9)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',StopOnCover:false}' + param = '{Filter:"ortofototype in (1,2,3,4,5,6,8,9)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',ReturnMetadata:true,StopOnCover:false}' if of and ir and not sat: - param = '{Filter:"ortofototype in (1,2,3,4,5,6,7,9)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',StopOnCover:false}' + param = '{Filter:"ortofototype in (1,2,3,4,5,6,7,9)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',ReturnMetadata:true,StopOnCover:false}' if of and sat and ir: - param = '{Filter:"ortofototype in (1,2,3,4,5,6,7,8,9)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',StopOnCover:false}' + param = '{Filter:"ortofototype in (1,2,3,4,5,6,7,8,9)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',ReturnMetadata:true,StopOnCover:false}' if sat and not of and not ir: - param = '{Filter:"ortofototype in (6)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',StopOnCover:false}' + param = '{Filter:"ortofototype in (6)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',ReturnMetadata:true,StopOnCover:false}' if sat and ir and not of: - param ='{Filter:"ortofototype in (6,7)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',StopOnCover:false}' + param ='{Filter:"ortofototype in (6,7)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',ReturnMetadata:true,StopOnCover:false}' if ir and not sat and not of: - param ='{Filter:"ortofototype in (7)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',StopOnCover:false}' + param ='{Filter:"ortofototype in (7)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',ReturnMetadata:true,StopOnCover:false}' if not of and not sat and not ir: self.iface.messageBar().pushMessage("Error", "project type must be set", level=Qgis.Critical, duration=3) return @@ -687,6 +688,7 @@ class getnib: # self.iface.messageBar().pushMessage("Info", "All OF: "+ str(nib_liste),level=Qgis.Info, duration=3) nib_liste_years = [] # Initialize + projectinfo_years = [] # Initialize # Ensure 4 digits in start and end-year and start year < end year. if self.dlg.radioButton_btwyears.isChecked(): @@ -703,7 +705,8 @@ class getnib: for n in (nib_liste): if year in n.split(): nib_liste_years.append(n) - nib_liste = nib_liste_years + projectinfo_years.append(n) + nib_liste = nib_liste_years # Get a modified list of projects for the chosen year(s) else: self.iface.messageBar().pushMessage("Warning", "year must have 4 digits",level=Qgis.Warning, duration=3) return @@ -752,7 +755,7 @@ class getnib: if self.dlg.checkBox_savelist.isChecked(): if self.dlg.radioButton_btwyears.isChecked(): for y in years: # Year-list is created above and contain all years from start to end year - # Delete files if existing + # Delete file if existing fn = str(cwd)+"\\OF_"+str(y)+".txt" if os.path.isfile(fn): os.remove(fn) @@ -772,3 +775,20 @@ class getnib: f.write(str(n)+'\n') # Write all OF-projects to the textfile # Show message self.iface.messageBar().pushMessage("Success", "List with Nib-projects saved to file", level=Qgis.Success, duration=3) + + prop = {} # Initialise dict + + if self.dlg.checkBox_savemeta.isChecked(): + projectinfo = js['ProjectMetadata'] # Get the metadata on json-format + fn = str(cwd)+"\\OF_metadata.json" # Set default file name + if os.path.isfile(fn): # Delete file if existing + os.remove(fn) + # Extracts properties-data for projects in nib_liste + for d in projectinfo: + if d["properties"]: + prop = d["properties"] # properties holds the projectname element + if prop['prosjektnavn'] in nib_liste: # Only write metadata for projects in nib_liste + with open(str(cwd)+'\\OF_metadata.json','a',encoding='utf-8') as f: + json.dump(prop,f,indent=1,ensure_ascii=False) # Save non-ASCII as-is + # Show message + self.iface.messageBar().pushMessage("Success", "Metadata for Nib-projects saved to file", level=Qgis.Success, duration=3)