diff --git a/get_nib.py b/get_nib.py index 7259e8d38504f09d278c3499122dc40673d76504..665fe07fea42f32131fbbe0dfb498dc935957e3d 100644 --- a/get_nib.py +++ b/get_nib.py @@ -215,11 +215,7 @@ class getnib: filename, _filter = QFileDialog.getOpenFileName( self.dlg, "Select a file ","", 'geo-files (*.shp *.geojson *.gpkg *.gml *.jpg *.tif);; All files (*.*)') self.dlg.lineEdit_file.setText(filename) - # self.dlg.lineEdit_file.setStyleSheet("QLineEdit" - # "{" - # "background-color : white" - # "}") - self.dlg.radioButton_file.setChecked(True) + self.dlg.radioButton_file.setChecked(True) # In case you choose a file directly without first chosing this option def show_help(self): """ Display application help to the user. """ @@ -235,7 +231,7 @@ class getnib: transformContext = QgsProject.instance().transformContext() xform = QgsCoordinateTransform(sourceCrs, destCrs, transformContext) # Forward transformation: src -> dest - # Computes length and height in LAE regardless input crs + # Computes length and height in LAEA regardless input crs ll = xform.transform(QgsPointXY(xmin,ymin)) # Bounding box's lower left corner ur = xform.transform(QgsPointXY(xmax,ymax)) # Bounding box's upper right corner # Get the maximum height and length of the bounding box (limit is set to 50 km) @@ -249,40 +245,46 @@ class getnib: return ok ## Bounding box ## + + # When you change your mind during input (started input of one bb-option and then chnages to another bb-option), + # e.g. have entered a length or selected a file, but change your mind and click Use current map canvas insted. + # Then length and the file should be wiped out. + def notUdbb(self): + self.dlg.lineEdit_lengthbb.clear() + self.dlg.lineEdit_heightbb.clear() + self.dlg.lineEdit_lengthbb.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + self.dlg.lineEdit_heightbb.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + + def notFile(self): + self.dlg.lineEdit_file.clear() + self.dlg.lineEdit_file.setStyleSheet("QLineEdit" + "{" + "background-color : white" + "}") + # 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" - "}") + self.notUdbb() # if you change option + self.notFile() # if you change option # 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.notFile() # if you change option self.dlg.lineEdit_lengthbb.setFocus() self.dlg.lineEdit_lengthbb.setStyleSheet("QLineEdit" "{" "background-color : yellow" "}") - # When length entered: length lineedit turns white + # When length entered: length turns white def lengthbbEdited(self): self.dlg.lineEdit_lengthbb.setStyleSheet("QLineEdit" "{" @@ -290,7 +292,7 @@ class getnib: "}") self.dlg.radioButton_udbb.setChecked(True) - # When length finished entered: height lineedit turns yellow and is focused + # When length finished entered: height turns yellow and is focused def lengthbbFinished(self): self.dlg.lineEdit_heightbb.setFocus() self.dlg.lineEdit_heightbb.setStyleSheet("QLineEdit" @@ -298,7 +300,7 @@ class getnib: "background-color : yellow" "}") - # When height edited: height lineedit turns white + # When height edited: height turns white def heightbbEdited(self): self.dlg.lineEdit_heightbb.setStyleSheet("QLineEdit" "{" @@ -308,27 +310,14 @@ class getnib: # 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" - "}") + self.notUdbb() # if you change option + self.notFile() # if you change option - # When selected lyr is changed: + # 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 + # When "file"-option: file turns yellow and is focused def fileClicked(self): if self.dlg.radioButton_file.isChecked(): if self.dlg.lineEdit_file.text() == '': @@ -336,16 +325,7 @@ class getnib: "{" "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" - "}") + self.notUdbb() # if you change option # When file selected: file lineedit turns white def fileSelected(self): @@ -378,18 +358,16 @@ class getnib: "background-color : yellow" "}") - # When start-year entered: start-year lineedit turns white + # When start-year entered: start-year 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 + # When start-year finished entered: end-year turns yellow and is focused def startFinished(self): self.dlg.lineEdit_endyear.setFocus() self.dlg.lineEdit_endyear.setStyleSheet("QLineEdit" @@ -397,7 +375,7 @@ class getnib: "background-color : yellow" "}") - # When end-year edited: end-year lineedit turns white + # When end-year edited: end-year turns white def endEdited(self): self.dlg.lineEdit_endyear.setStyleSheet("QLineEdit" "{" @@ -419,7 +397,12 @@ class getnib: 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_ucmcbb.toggled.connect(self.ucmcbbClicked) # When current map canvas bb is chosen + + 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_lyr.toggled.connect(self.lyrClicked) # When lyr bb is chosen self.dlg.comboBox_lyr.activated.connect(self.lyrActivated) # Whenever a lyr item is chosen @@ -427,11 +410,6 @@ class getnib: 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 @@ -445,7 +423,7 @@ class getnib: # 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 + self.dlg.radioButton_ucmcbb.setChecked(True) # Checked # Clear the contents of the lineEdit_lengthbb from previous runs self.dlg.lineEdit_lengthbb.clear() @@ -458,7 +436,7 @@ class getnib: # Clear the contents of the lineEdit_file (select file) from previous runs self.dlg.lineEdit_file.clear() - self.dlg.radioButton_file.setChecked(False) # Unchecked + self.dlg.radioButton_file.setChecked(False) # Unchecked # Fetch the currently loaded layers layers = QgsProject.instance().mapLayers() @@ -500,12 +478,17 @@ class getnib: 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 + epsg_list = [25832, 25833, 25835] + if not crs_proj_int in epsg_list: + self.iface.messageBar().pushMessage("Error", "Project must be in UTM 32, 33 or 35", level=Qgis.Critical, duration=3) + return # Return from (end) plugin + """ end additional code """ + # show the dialog self.dlg.show() # Run the dialog event loop @@ -522,7 +505,7 @@ class getnib: if self.dlg.checkBox_resetNib.isChecked(): # Empty the Nib-prosjekt group reset = True - # If checked, use current map canvas as bounding box (=extent of) + # 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() @@ -542,23 +525,15 @@ class getnib: 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 + # Debugging + # self.iface.messageBar().pushMessage("Info", "Center x, y: " + str(e.x()) +", "+ str(e.y())+", "+crs_proj_str, level=Qgis.Info, duration=5) # 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 + xmin = e.x() - int(l)*1000/2 + xmax = e.x() + int(l)*1000/2 + ymin = e.y() - int(h)*1000/2 + ymax = e.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 @@ -591,7 +566,7 @@ class getnib: return # Return from (end) plugin # Hverken layer eller fil er valgt else: - self.iface.messageBar().pushMessage("Error", "Ingen extent", level=Qgis.Critical, duration=5) + self.iface.messageBar().pushMessage("Error", "No extent", level=Qgis.Critical, duration=5) # Activate (select) the selected layer in the combobox or in the lineEdit iface.setActiveLayer(selectedLayer) @@ -634,7 +609,6 @@ 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;...) @@ -740,7 +714,7 @@ class getnib: 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) + with open(str(cwd)+'\\OF_'+str(year)+'.txt','a',encoding='utf-8') as f: # a = append to textfile f.write(str(n)+'\n') # write the OF-project for the specific year to the textfile else: # Delete file if existing