diff --git a/get_nib.py b/get_nib.py index 55196f602df83ce2309acc5aae743439dee2cee1..7259e8d38504f09d278c3499122dc40673d76504 100644 --- a/get_nib.py +++ b/get_nib.py @@ -45,11 +45,15 @@ from qgis.PyQt.QtGui import QIcon, QDesktopServices from qgis.PyQt.QtWidgets import QAction, QFileDialog, QMessageBox #Import additional modules +from qgis.PyQt.QtGui import QRegExpValidator, QIntValidator +from qgis.PyQt.QtCore import QRegExp from qgis.core import QgsProject, Qgis, QgsMessageLog, QgsRasterLayer, QgsVectorLayer, QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsPointXY from qgis.gui import QgsMapCanvas from qgis.utils import iface, showPluginHelp -from urllib import request, parse # for å lese url, gå gjennom streng -import os +from urllib import parse # for å gå gjennom streng +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 # Initialize Qt resources from file resources.py from .resources import * @@ -210,7 +214,12 @@ class getnib: """ Får å velge fil som skal tjene som bounding box """ filename, _filter = QFileDialog.getOpenFileName( self.dlg, "Select a file ","", 'geo-files (*.shp *.geojson *.gpkg *.gml *.jpg *.tif);; All files (*.*)') - self.dlg.lineEdit.setText(filename) + self.dlg.lineEdit_file.setText(filename) + # self.dlg.lineEdit_file.setStyleSheet("QLineEdit" + # "{" + # "background-color : white" + # "}") + self.dlg.radioButton_file.setChecked(True) def show_help(self): """ Display application help to the user. """ @@ -232,12 +241,168 @@ class getnib: # Get the maximum height and length of the bounding box (limit is set to 50 km) xdist = (ur.x()-ll.x())/1000 # Get lenght in km (xmax-xmin) ydist = (ur.y()-ll.y())/1000 # Get height in km (ymax-ymin) - if xdist > 50 or ydist > 50: - self.iface.messageBar().pushMessage("Error", "Height or length of bounding box can't be > 50 km",level=Qgis.Critical, duration=3) + if xdist > 500 or ydist > 500: + self.iface.messageBar().pushMessage("Error", "Height or length of bounding box can't be > 500 km",level=Qgis.Critical, duration=3) ok = False else: ok = True # Bounding box is small enough return ok + + ## Bounding box ## + # When "Use current map canvas"-option: length, height and file turns white (if you change your mind) + def ucmcbbClicked(self): + if self.dlg.radioButton_ucmcbb.isChecked(): + self.dlg.lineEdit_lengthbb.clear() + self.dlg.lineEdit_heightbb.clear() + self.dlg.lineEdit_file.clear() + self.dlg.lineEdit_lengthbb.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + self.dlg.lineEdit_heightbb.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + self.dlg.lineEdit_file.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + + # When "User-defined"-option: length turns yellow and is focused + def udbbClicked(self): + if self.dlg.radioButton_udbb.isChecked(): + self.dlg.lineEdit_file.clear() + self.dlg.lineEdit_file.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + self.dlg.lineEdit_lengthbb.setFocus() + self.dlg.lineEdit_lengthbb.setStyleSheet("QLineEdit" + "{" + "background-color : yellow" + "}") + + # When length entered: length lineedit turns white + def lengthbbEdited(self): + self.dlg.lineEdit_lengthbb.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + self.dlg.radioButton_udbb.setChecked(True) + + # When length finished entered: height lineedit turns yellow and is focused + def lengthbbFinished(self): + self.dlg.lineEdit_heightbb.setFocus() + self.dlg.lineEdit_heightbb.setStyleSheet("QLineEdit" + "{" + "background-color : yellow" + "}") + + # When height edited: height lineedit turns white + def heightbbEdited(self): + self.dlg.lineEdit_heightbb.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + + # When "layer"-option: + def lyrClicked(self): + if self.dlg.radioButton_lyr.isChecked(): + self.dlg.lineEdit_lengthbb.clear() + self.dlg.lineEdit_heightbb.clear() + self.dlg.lineEdit_file.clear() + self.dlg.lineEdit_lengthbb.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + self.dlg.lineEdit_heightbb.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + self.dlg.lineEdit_file.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + + # When selected lyr is changed: + def lyrActivated(self): + self.dlg.radioButton_lyr.setChecked(True) + + # When "file"-option: file turns yellow and is focused along with the file select-button + def fileClicked(self): + if self.dlg.radioButton_file.isChecked(): + if self.dlg.lineEdit_file.text() == '': + self.dlg.lineEdit_file.setStyleSheet("QLineEdit" + "{" + "background-color : yellow" + "}") + self.dlg.lineEdit_lengthbb.clear() + self.dlg.lineEdit_lengthbb.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + self.dlg.lineEdit_heightbb.clear() + self.dlg.lineEdit_heightbb.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + + # When file selected: file lineedit turns white + def fileSelected(self): + self.dlg.lineEdit_file.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + + ## Years ## + # When "All years"-option: start-year turns white (if you change your mind) + def allyearsClicked(self): + if self.dlg.radioButton_allyears.isChecked(): + self.dlg.lineEdit_startyear.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + self.dlg.lineEdit_startyear.clear() + self.dlg.lineEdit_endyear.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + self.dlg.lineEdit_endyear.clear() + + # When "Between years"-option: start-year turns yellow and is focused + def btwyearsClicked(self): + if self.dlg.radioButton_btwyears.isChecked(): + self.dlg.lineEdit_startyear.setFocus() + self.dlg.lineEdit_startyear.setStyleSheet("QLineEdit" + "{" + "background-color : yellow" + "}") + + # When start-year entered: start-year lineedit turns white + def startEdited(self): + self.dlg.lineEdit_startyear.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + # Due to "All years"-option is set to default: + # Turn off "All years" and turn on "Between years" if user just start typing in a start year + self.dlg.radioButton_allyears.setChecked(False) # UnCheck + self.dlg.radioButton_btwyears.setChecked(True) # Check + + # When start-year finished entered: end-year lineedit turns yellow and is focused + def startFinished(self): + self.dlg.lineEdit_endyear.setFocus() + self.dlg.lineEdit_endyear.setStyleSheet("QLineEdit" + "{" + "background-color : yellow" + "}") + + # When end-year edited: end-year lineedit turns white + def endEdited(self): + self.dlg.lineEdit_endyear.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") """ end additional methods """ @@ -251,31 +416,91 @@ class getnib: self.dlg = getnibDialog() """ Additional code """ #connect the select_input_file method to the clicked signal of the push button widget - self.dlg.pushButton.clicked.connect(self.select_input_file) + self.dlg.pushButton_file.clicked.connect(self.select_input_file) self.dlg.toolButton_help.clicked.connect(self.show_help) + self.dlg.radioButton_ucmcbb.toggled.connect(self.ucmcbbClicked) # When Use current maps canvas is chosen + + self.dlg.radioButton_lyr.toggled.connect(self.lyrClicked) # When lyr bb is chosen + self.dlg.comboBox_lyr.activated.connect(self.lyrActivated) # Whenever a lyr item is chosen + + self.dlg.radioButton_file.toggled.connect(self.fileClicked) # When file bb is chosen + self.dlg.lineEdit_file.textChanged.connect(self.fileSelected) # Whenever file is selected + + self.dlg.radioButton_udbb.toggled.connect(self.udbbClicked) # When User-defined bb is chosen + self.dlg.lineEdit_lengthbb.textEdited.connect(self.lengthbbEdited) # Whenever length is edited + self.dlg.lineEdit_lengthbb.editingFinished.connect(self.lengthbbFinished) # When you press ‘Enter’ or length loses focus + self.dlg.lineEdit_heightbb.textEdited.connect(self.heightbbEdited) # Whenever height is edited + + self.dlg.radioButton_allyears.toggled.connect(self.allyearsClicked) # When All years is chosen + + self.dlg.radioButton_btwyears.toggled.connect(self.btwyearsClicked) # When Between years is chosen + self.dlg.lineEdit_startyear.textEdited.connect(self.startEdited) # Whenever the start year is edited + self.dlg.lineEdit_startyear.editingFinished.connect(self.startFinished) # When you press ‘Enter’ or start loses focus + self.dlg.lineEdit_endyear.textEdited.connect(self.endEdited) # Whenever end year is edited + + # As default: Remember user input from previous run + """ If this should not be the default, remove comment # from next line """ + # self.dlg.checkBox_resetNib.setChecked(False) # Unchecked + + # As default: Set map canvas checkbox to checked + """ If this should not be the default, comment # next line """ + self.dlg.radioButton_ucmcbb.setChecked(True) # Checked + + # Clear the contents of the lineEdit_lengthbb from previous runs + self.dlg.lineEdit_lengthbb.clear() + # Clear the contents of the lineEdit_heightbb from previous runs + self.dlg.lineEdit_heightbb.clear() + # Clear the contents of the lineEdit_start (year) from previous runs + self.dlg.lineEdit_startyear.clear() + # Clear the contents of the lineEdit_end (year) from previous runs + self.dlg.lineEdit_endyear.clear() + # Clear the contents of the lineEdit_file (select file) from previous runs + self.dlg.lineEdit_file.clear() + + self.dlg.radioButton_file.setChecked(False) # Unchecked + # Fetch the currently loaded layers layers = QgsProject.instance().mapLayers() # Clear the contents of the combobox from previous runs - self.dlg.comboBox.clear() + self.dlg.comboBox_lyr.clear() # Populate the combobox with names of all the loaded layers layer_list = [] unique_lyr = [] for layer in layers.values(): item = layer.name() - layer_list.append(item) # Includes duplicates - unique_lyr = list(set(layer_list)) # No duplicates - self.dlg.comboBox.addItems(unique_lyr) # Populate combobox - self.dlg.comboBox.model().sort(0) # Sort layer names (includes filepath) alfabetically - # Clear the contents of the lineEdit from previous runs - self.dlg.lineEdit.clear() - # As default: Remember user input from previous run - """ If this should not be the default, remove comment # from next line """ - # self.dlg.checkBoxNib.setChecked(False) # Unchecked - # As default: Set map canvas checkbox to checked + layer_list.append(item) # Includes duplicates + unique_lyr = list(set(layer_list)) # No duplicates + self.dlg.comboBox_lyr.addItems(unique_lyr) # Populate combobox + self.dlg.comboBox_lyr.model().sort(0) # Sort layer names (includes filepath) alfabetically + + # Length and height: allowing only 1-500 + l = self.dlg.lineEdit_lengthbb + l.setValidator(QRegExpValidator(QRegExp("[1-9]|[1-9][0-9]|[1-4][0-9][0-9]|500"))) + h = self.dlg.lineEdit_heightbb + h.setValidator(QRegExpValidator(QRegExp("[1-9]|[1-9][0-9]|[1-4][0-9][0-9]|500"))) + + # As default: Set all years radiobutton to checked """ If this should not be the default, comment # next line """ - self.dlg.checkBox.setChecked(True) # Checked - + self.dlg.radioButton_allyears.setChecked(True) # Checked + + # Start- and end-year: must be 1900-current year + currentDate = datetime.date.today() + cy = currentDate.year # get current year, e.g. 2022 (position 0,1,2,3) + # cy = 2042 # used for testing the regexp + cy = str(cy) # Convert number to string + d3 = cy[2] # Get the third digit (in position 2) + d4 = cy[3] # Get the fourth digit (in position 3) + # # Debugging + # self.iface.messageBar().pushMessage("Info", "To siste sifre i år er: "+d3+', '+d4, level=Qgis.Info, duration=3) + + # Verifying the entered years + start = self.dlg.lineEdit_startyear + start.setValidator(QRegExpValidator(QRegExp("19[0-9][0-9]|20[0-"+str(int(d3)-1)+"][0-9]|20["+d3+"][0-"+d4+"]"))) + end = self.dlg.lineEdit_endyear + end.setValidator(QRegExpValidator(QRegExp("19[0-9][0-9]|20[0-"+str(int(d3)-1)+"][0-9]|20["+d3+"][0-"+d4+"]"))) + + # Get projects epsg-code # e.g. crs_proj_str = iface.mapCanvas().mapSettings().destinationCrs().authid() # EPSG:25832 crs_proj_int = int(crs_proj_str[5:]) # 25832 @@ -293,10 +518,12 @@ class getnib: """" Additional code""" selectedLayer = '' # Initialise reset = False - if self.dlg.checkBoxNib.isChecked(): + + if self.dlg.checkBox_resetNib.isChecked(): # Empty the Nib-prosjekt group reset = True - # If checked, use the bounding box (= extent of current map canvas) - if self.dlg.checkBox.isChecked(): + + # If checked, use current map canvas as bounding box (=extent of) + if self.dlg.radioButton_ucmcbb.isChecked(): # Get the extent of current map canvas (coordinates in the project's crs) e = iface.mapCanvas().extent() xmin = e.xMinimum() @@ -304,37 +531,73 @@ class getnib: ymin = e.yMinimum() ymax = e.yMaximum() ok = self.check_bbsize(crs_proj_str, xmin, xmax, ymin, ymax) # Check if bb is small enough - else: # If not checked, use existing layer or open new from file - fname = self.dlg.lineEdit.text() # get the text (path and filename) - lname = os.path.splitext(os.path.basename(fname))[0] #get only the filename without the extension, will be used as layer name in the Layers panel in QGIS - # No file is chosen - if fname == "": # Use selected layer from combobox - lyr_name = self.dlg.comboBox.currentText() # Get the layer name + # If checked, use the length and height from map canvas sentre point + elif self.dlg.radioButton_udbb.isChecked(): + # Get the extent of current map canvas (coordinates in the project's crs) + l = self.dlg.lineEdit_lengthbb.text() + h = self.dlg.lineEdit_heightbb.text() + if l == "" or h == "": + self.iface.messageBar().pushMessage("Error", "Length and/or height are/is missing!", level=Qgis.Critical, duration=3) + return # Return from (end) plugin + else: + # Length and height (in km) requires projected coordinated. Ensures this by converting the coordinates to LAEA + e = iface.mapCanvas().extent().center() + sourceCrs = QgsCoordinateReferenceSystem(crs_proj_str) # Input project crs + destCrs = QgsCoordinateReferenceSystem('EPSG:3035') # ETRS89-extended / LAEA Europe + transformContext = QgsProject.instance().transformContext() + xform = QgsCoordinateTransform(sourceCrs, destCrs, transformContext) + # Forward transformation: src -> dest + # Computes corner coordinates in LAE regardless input crs + self.iface.messageBar().pushMessage("Info", "Center x, y: " + str(e.x()) +", "+ str(e.y())+", "+crs_proj_str, level=Qgis.Info, duration=5) + cc = xform.transform(QgsPointXY(e.x(),e.y())) + self.iface.messageBar().pushMessage("Info", "Center x, y: " + str(cc.x()) +", "+ str(cc.y()), level=Qgis.Info, duration=3) + xmin = cc.x() - int(l)*1000/2 + xmax = cc.x() + int(l)*1000/2 + ymin = cc.y() - int(h)*1000/2 + ymax = cc.y() + int(h)*1000/2 + """ Check length and height of bounding box. Limit is set to 500 km """ + ok = self.check_bbsize(destCrs, xmin, xmax, ymin, ymax) # Check if bb is small enough + crs_proj_int = 3035 # Use this as input CRS in the NiB WMS-call regardless if project's CRS + else: + if self.dlg.radioButton_lyr.isChecked(): # layer-option checked + lyr_name = self.dlg.comboBox_lyr.currentText() # Get the layer name if lyr_name != "": # If a layer is present/chosen + # self.iface.messageBar().pushMessage("Info", "Valgt layer: "+lyr_name, level=Qgis.Info, duration=3) selectedLayer = QgsProject.instance().mapLayersByName(lyr_name)[0] # Get this vector layer - # Get the extent of the file (coordinates in the selected layer's crs) + # Get the extent of the layer (coordinates in the selected layer's crs) e = selectedLayer.extent() else: # If no file or layer is selcted, throw an error mesage and end plugin - self.iface.messageBar().pushMessage("Error", "Nothing selected!", level=Qgis.Critical, duration=3) - return # Return from (end) plugin - else: # Use selected file - file = r""+fname+"" # Reads the file - fLayer = QgsVectorLayer(file, lname, "ogr") # If vector fie - if not fLayer.isValid(): # If not valid vector laayer - fLayer = QgsRasterLayer(file, lname, "gdal") # If raster file - if not fLayer.isValid(): # If not valid raster layer either, show error message and end plugin - self.iface.messageBar().pushMessage("Error", "Not a valid geo-file: "+ str(lname),level=Qgis.Critical, duration=3) - return # Return from (end) plugin - QgsProject.instance().addMapLayer(fLayer, True) # Add the layer and show it (True) - selectedLayer = QgsProject.instance().mapLayersByName(lname)[0] # Get this layer - # Get the extent of the active layer (coordinates in the selected file's crs) - e = selectedLayer.extent() - + self.iface.messageBar().pushMessage("Error", "No layer selected!", level=Qgis.Critical, duration=3) + return # Return from (end) plugin + + elif self.dlg.radioButton_file.isChecked(): # file-option checked + fname = self.dlg.lineEdit_file.text() # get the text (path and filename) + if fname != "": + lname = os.path.splitext(os.path.basename(fname))[0] #get only the filename without the extension, will be used as layer name in the Layers panel in QGIS + # self.iface.messageBar().pushMessage("Info", "Valgt fil: "+lname, level=Qgis.Info, duration=3) + file = r""+fname+"" # Reads the file + fLayer = QgsVectorLayer(file, lname, "ogr") # If vector file + if not fLayer.isValid(): # If not valid vector laayer + fLayer = QgsRasterLayer(file, lname, "gdal") # If raster file + if not fLayer.isValid(): # If not valid raster layer either, show error message and end plugin + self.iface.messageBar().pushMessage("Error", "Not a valid geo-file: "+ str(lname),level=Qgis.Critical, duration=3) + return # Return from (end) plugin + QgsProject.instance().addMapLayer(fLayer, True) # Add the layer and show it (True) + selectedLayer = QgsProject.instance().mapLayersByName(lname)[0] # Get this layer + # Get the extent of the active layer (coordinates in the selected file's crs) + e = selectedLayer.extent() + else: + self.iface.messageBar().pushMessage("Error", "No file selected!", level=Qgis.Critical, duration=3) + return # Return from (end) plugin + # Hverken layer eller fil er valgt + else: + self.iface.messageBar().pushMessage("Error", "Ingen extent", level=Qgis.Critical, duration=5) + # Activate (select) the selected layer in the combobox or in the lineEdit iface.setActiveLayer(selectedLayer) # Zoom to activated layer iface.zoomToActiveLayer() - + # Get selected layer's epsg:code crs_lyr = selectedLayer.crs() # example: crs_lyr_str = crs_lyr.authid() # EPSG:4258 @@ -345,11 +608,13 @@ class getnib: layers = [] # Empty layers to get a fresh start when rerunning the plugin return # Return from (end) plugin - # Get the extent of the active layer (coordinates in file or layer crs) + # Get the extent of the active layer (coordinates in file or layer crs) xmin = e.xMinimum() xmax = e.xMaximum() ymin = e.yMinimum() - ymax = e.yMaximum() + ymax = e.yMaximum() + # coords = "%f,%f;%f,%f;%f,%f;%f,%f" %(xmin,ymin,xmin,ymax,xmax,ymax,xmax,ymin) + # self.iface.messageBar().pushMessage("Info", "extent"+str(coords), level=Qgis.Info, duration=3) # If selected file or layer and project have different crs, the layer's # bounding box coordinates must be transformed into the project's crs if crs_lyr_int != crs_proj_int: @@ -369,6 +634,7 @@ class getnib: else: # Selected file or layer and project have the same crs # Check if bb is small enough - input crs and bb coordinates are in layer crs = project crs ok = self.check_bbsize(crs_lyr_str, xmin, xmax, ymin, ymax) + if ok: # If bb small enough, get the corner coordinates (to be used in url-request) # Set bounding box corner coordinates as geojson (x1,y1;x2,y2;...) @@ -394,12 +660,37 @@ class getnib: # (geojson-format x1,y1;x2,y2;x3,y3;...) # https://stackoverflow.com/questions/50337388/how-to-use-special-character-%c3%a6-%c3%b8-or-%c3%a5-in-a-urllib-request-urlopen-in-python-3 para=parse.quote('{Filter:"ortofototype in (1,2,3,4,8,9,12)",Coordinates:"'+coords+'",InputWKID:'+str(crs_proj_int)+',StopOnCover:false}') - inn = request.urlopen('https://tjenester.norgeibilder.no/rest/projectMetadata.ashx?request='+para).read() #list with of-projects - ut = inn.decode() # Convert from bytes to string - ut = ut.replace('"','') # Remove quotes, i.e. replace " with nothing - a = ut.split("[") # Split string in two at bracket [ - b = a[1].split("]") # Split 2nd. substring at each bracket [ gives 2 elements since there is only one occurence of [ (counting starts at 0) - nib_liste = b[0].split(",") + js = requests.get('https://tjenester.norgeibilder.no/rest/projectMetadata.ashx?request='+para).json() #liste med aktuelle of-prosjekt på json-format + print(js) # {Success:true,ErrorMessage:null,ProjectList:[Sør-Varanger 2019,Sør-Varanger veg 2016,Sør-Varanger 2013,Sør-Varanger 2011],ProjectMetadata:null} + nib_liste = js['ProjectList'] + print('nib_liste =',nib_liste) # nib_liste = ['Sør-Varanger 2019', 'Sør-Varanger veg 2016', 'Sør-Varanger 2013', 'Sør-Varanger 2011'] + # self.iface.messageBar().pushMessage("Info", "All OF: "+ str(nib_liste),level=Qgis.Info, duration=3) + + nib_liste_years = [] #Initialise + + # Ensured 4 digits in start and end-year. However the years might be wrong. start > end, < 4 digits. Checking this. + if self.dlg.radioButton_btwyears.isChecked(): + # Get the extent of current map canvas (coordinates in the project's crs) + start = self.dlg.lineEdit_startyear.text() + end = self.dlg.lineEdit_endyear.text() + if int(start) <= int(end): + if len(start) == 4 and len(end) == 4: + # Creates a new list with only the OF-proejcts from between the start- and end-year. + years = [str(y) for y in range(int(start),int(end)+1,1)] + # We have an original list with all nib-projects (nib_liste) + # We make a new list selecting projects from nib_liste from the wanted time period + for year in years: + for n in (nib_liste): + if year in n.split(): + nib_liste_years.append(n) + nib_liste = nib_liste_years + else: + self.iface.messageBar().pushMessage("Warning", "year must have 4 digits",level=Qgis.Warning, duration=3) + return + else: + self.iface.messageBar().pushMessage("Warning", "end-year must be >= start-year",level=Qgis.Warning, duration=3) + return + lyr = '' # Initiate for nibprosjwms in nib_liste: # Loads WMS (raster layer) for each of-project within the bounding box in question urlWithParams = 'contextualWMSLegend=0&crs=EPSG:'+str(crs_proj_int)+'&dpiMode=7&featureCount=10&format=image/png&layers='+nibprosjwms+'&styles&url=https://wms.geonorge.no/skwms1/wms.nib-prosjekter' @@ -414,7 +705,7 @@ class getnib: if str(layer_parent.name()) == 'Nib-prosjekt': # If layer exists in group Nib-prosjekt, it will not be added self.iface.messageBar().pushMessage("Info", "WMS layer exists: "+ str(nibprosjwms),level=Qgis.Info, duration=1) continue # Return the control to the beginning of the for loop - else: # The layer exists but not in the Nib-prsjekt group + else: # The layer exists but not in the Nib-prosjekt group QgsProject.instance().addMapLayer(rlayer, False) # Add the raster layer without showing it (False) mygroup = root.findGroup("Nib-prosjekt") # Get the group named "Nib-prosjekt" mygroup.addLayer(rlayer) # Add the layer to this group @@ -430,3 +721,34 @@ class getnib: self.iface.messageBar().pushMessage("Success", "WMS added in Nib-prosjekt", level=Qgis.Success, duration=3) else: # If not valid raster layer, an error message will appear self.iface.messageBar().pushMessage("Warning", "Can't load: "+ str(nibprosjwms),level=Qgis.Warning, duration=3) + + # Current working directory will be set as destination folder for project lists + cwd = os.getcwd() # Get current working directory + # self.iface.messageBar().pushMessage("Info", "katalogen er:" +str(cwd), level=Qgis.Info, duration=3) + + if self.dlg.checkBox_sort.isChecked(): + nib_liste = sorted(nib_liste) + + if self.dlg.checkBox_savelist.isChecked(): + if self.dlg.radioButton_btwyears.isChecked(): + for y in years: # years list is created above and contain all years from start to end year + # Delete files if existing + fn = str(cwd)+"\\OF_"+str(y)+".txt" + if os.path.isfile(fn): + os.remove(fn) + for n in nib_liste: # sort the nib_liste alphabetically + for year in years: + if year in n.split(): + if y == year: + with open(str(cwd)+'\\OF_'+str(year)+'.txt','a',encoding='utf-8') as f: # a = append to textfile (overwrite if file exists) + f.write(str(n)+'\n') # write the OF-project for the specific year to the textfile + else: + # Delete file if existing + fn = str(cwd)+"\\OF_all.txt" + if os.path.isfile(fn): + os.remove(fn) + with open(str(cwd)+'\\OF_all.txt','w',encoding='utf-8') as f: # w = write - will overwrite any existing content https://www.w3schools.com/python/python_file_write.asp + for n in nib_liste: + f.write(str(n)+'\n') # write the 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)