Skip to content
Snippets Groups Projects
Commit 5e834676 authored by A Nilsen's avatar A Nilsen
Browse files

Lagt til nye muligheter for å angi bb og OF mellom år

parent 92131590
Branches
Tags v1.1.16
No related merge requests found
...@@ -45,11 +45,15 @@ from qgis.PyQt.QtGui import QIcon, QDesktopServices ...@@ -45,11 +45,15 @@ from qgis.PyQt.QtGui import QIcon, QDesktopServices
from qgis.PyQt.QtWidgets import QAction, QFileDialog, QMessageBox from qgis.PyQt.QtWidgets import QAction, QFileDialog, QMessageBox
#Import additional modules #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.core import QgsProject, Qgis, QgsMessageLog, QgsRasterLayer, QgsVectorLayer, QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsPointXY
from qgis.gui import QgsMapCanvas from qgis.gui import QgsMapCanvas
from qgis.utils import iface, showPluginHelp from qgis.utils import iface, showPluginHelp
from urllib import request, parse # for å lese url, gå gjennom streng from urllib import parse # for å gå gjennom streng
import os 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 # Initialize Qt resources from file resources.py
from .resources import * from .resources import *
...@@ -210,7 +214,12 @@ class getnib: ...@@ -210,7 +214,12 @@ class getnib:
""" Får å velge fil som skal tjene som bounding box """ """ Får å velge fil som skal tjene som bounding box """
filename, _filter = QFileDialog.getOpenFileName( filename, _filter = QFileDialog.getOpenFileName(
self.dlg, "Select a file ","", 'geo-files (*.shp *.geojson *.gpkg *.gml *.jpg *.tif);; All files (*.*)') 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): def show_help(self):
""" Display application help to the user. """ """ Display application help to the user. """
...@@ -232,12 +241,168 @@ class getnib: ...@@ -232,12 +241,168 @@ class getnib:
# Get the maximum height and length of the bounding box (limit is set to 50 km) # 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) xdist = (ur.x()-ll.x())/1000 # Get lenght in km (xmax-xmin)
ydist = (ur.y()-ll.y())/1000 # Get height in km (ymax-ymin) ydist = (ur.y()-ll.y())/1000 # Get height in km (ymax-ymin)
if xdist > 50 or ydist > 50: if xdist > 500 or ydist > 500:
self.iface.messageBar().pushMessage("Error", "Height or length of bounding box can't be > 50 km",level=Qgis.Critical, duration=3) self.iface.messageBar().pushMessage("Error", "Height or length of bounding box can't be > 500 km",level=Qgis.Critical, duration=3)
ok = False ok = False
else: else:
ok = True # Bounding box is small enough ok = True # Bounding box is small enough
return ok 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 """ """ end additional methods """
...@@ -251,31 +416,91 @@ class getnib: ...@@ -251,31 +416,91 @@ class getnib:
self.dlg = getnibDialog() self.dlg = getnibDialog()
""" Additional code """ """ Additional code """
#connect the select_input_file method to the clicked signal of the push button widget #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.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 # Fetch the currently loaded layers
layers = QgsProject.instance().mapLayers() layers = QgsProject.instance().mapLayers()
# Clear the contents of the combobox from previous runs # 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 # Populate the combobox with names of all the loaded layers
layer_list = [] layer_list = []
unique_lyr = [] unique_lyr = []
for layer in layers.values(): for layer in layers.values():
item = layer.name() item = layer.name()
layer_list.append(item) # Includes duplicates layer_list.append(item) # Includes duplicates
unique_lyr = list(set(layer_list)) # No duplicates unique_lyr = list(set(layer_list)) # No duplicates
self.dlg.comboBox.addItems(unique_lyr) # Populate combobox self.dlg.comboBox_lyr.addItems(unique_lyr) # Populate combobox
self.dlg.comboBox.model().sort(0) # Sort layer names (includes filepath) alfabetically self.dlg.comboBox_lyr.model().sort(0) # Sort layer names (includes filepath) alfabetically
# Clear the contents of the lineEdit from previous runs
self.dlg.lineEdit.clear() # Length and height: allowing only 1-500
# As default: Remember user input from previous run l = self.dlg.lineEdit_lengthbb
""" If this should not be the default, remove comment # from next line """ l.setValidator(QRegExpValidator(QRegExp("[1-9]|[1-9][0-9]|[1-4][0-9][0-9]|500")))
# self.dlg.checkBoxNib.setChecked(False) # Unchecked h = self.dlg.lineEdit_heightbb
# As default: Set map canvas checkbox to checked 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 """ """ 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. # Get projects epsg-code # e.g.
crs_proj_str = iface.mapCanvas().mapSettings().destinationCrs().authid() # EPSG:25832 crs_proj_str = iface.mapCanvas().mapSettings().destinationCrs().authid() # EPSG:25832
crs_proj_int = int(crs_proj_str[5:]) # 25832 crs_proj_int = int(crs_proj_str[5:]) # 25832
...@@ -293,10 +518,12 @@ class getnib: ...@@ -293,10 +518,12 @@ class getnib:
"""" Additional code""" """" Additional code"""
selectedLayer = '' # Initialise selectedLayer = '' # Initialise
reset = False reset = False
if self.dlg.checkBoxNib.isChecked():
if self.dlg.checkBox_resetNib.isChecked(): # Empty the Nib-prosjekt group
reset = True 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) # Get the extent of current map canvas (coordinates in the project's crs)
e = iface.mapCanvas().extent() e = iface.mapCanvas().extent()
xmin = e.xMinimum() xmin = e.xMinimum()
...@@ -304,37 +531,73 @@ class getnib: ...@@ -304,37 +531,73 @@ class getnib:
ymin = e.yMinimum() ymin = e.yMinimum()
ymax = e.yMaximum() ymax = e.yMaximum()
ok = self.check_bbsize(crs_proj_str, xmin, xmax, ymin, ymax) # Check if bb is small enough 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 # If checked, use the length and height from map canvas sentre point
fname = self.dlg.lineEdit.text() # get the text (path and filename) elif self.dlg.radioButton_udbb.isChecked():
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 # Get the extent of current map canvas (coordinates in the project's crs)
# No file is chosen l = self.dlg.lineEdit_lengthbb.text()
if fname == "": # Use selected layer from combobox h = self.dlg.lineEdit_heightbb.text()
lyr_name = self.dlg.comboBox.currentText() # Get the layer name 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 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 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() e = selectedLayer.extent()
else: # If no file or layer is selcted, throw an error mesage and end plugin 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) self.iface.messageBar().pushMessage("Error", "No layer selected!", level=Qgis.Critical, duration=3)
return # Return from (end) plugin return # Return from (end) plugin
else: # Use selected file
file = r""+fname+"" # Reads the file elif self.dlg.radioButton_file.isChecked(): # file-option checked
fLayer = QgsVectorLayer(file, lname, "ogr") # If vector fie fname = self.dlg.lineEdit_file.text() # get the text (path and filename)
if not fLayer.isValid(): # If not valid vector laayer if fname != "":
fLayer = QgsRasterLayer(file, lname, "gdal") # If raster file 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
if not fLayer.isValid(): # If not valid raster layer either, show error message and end plugin # self.iface.messageBar().pushMessage("Info", "Valgt fil: "+lname, level=Qgis.Info, duration=3)
self.iface.messageBar().pushMessage("Error", "Not a valid geo-file: "+ str(lname),level=Qgis.Critical, duration=3) file = r""+fname+"" # Reads the file
return # Return from (end) plugin fLayer = QgsVectorLayer(file, lname, "ogr") # If vector file
QgsProject.instance().addMapLayer(fLayer, True) # Add the layer and show it (True) if not fLayer.isValid(): # If not valid vector laayer
selectedLayer = QgsProject.instance().mapLayersByName(lname)[0] # Get this layer fLayer = QgsRasterLayer(file, lname, "gdal") # If raster file
# Get the extent of the active layer (coordinates in the selected file's crs) if not fLayer.isValid(): # If not valid raster layer either, show error message and end plugin
e = selectedLayer.extent() 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 # Activate (select) the selected layer in the combobox or in the lineEdit
iface.setActiveLayer(selectedLayer) iface.setActiveLayer(selectedLayer)
# Zoom to activated layer # Zoom to activated layer
iface.zoomToActiveLayer() iface.zoomToActiveLayer()
# Get selected layer's epsg:code # Get selected layer's epsg:code
crs_lyr = selectedLayer.crs() # example: crs_lyr = selectedLayer.crs() # example:
crs_lyr_str = crs_lyr.authid() # EPSG:4258 crs_lyr_str = crs_lyr.authid() # EPSG:4258
...@@ -345,11 +608,13 @@ class getnib: ...@@ -345,11 +608,13 @@ class getnib:
layers = [] # Empty layers to get a fresh start when rerunning the plugin layers = [] # Empty layers to get a fresh start when rerunning the plugin
return # Return from (end) 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() xmin = e.xMinimum()
xmax = e.xMaximum() xmax = e.xMaximum()
ymin = e.yMinimum() 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 # If selected file or layer and project have different crs, the layer's
# bounding box coordinates must be transformed into the project's crs # bounding box coordinates must be transformed into the project's crs
if crs_lyr_int != crs_proj_int: if crs_lyr_int != crs_proj_int:
...@@ -369,6 +634,7 @@ class getnib: ...@@ -369,6 +634,7 @@ class getnib:
else: # Selected file or layer and project have the same crs 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 # 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) 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) 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;...) # Set bounding box corner coordinates as geojson (x1,y1;x2,y2;...)
...@@ -394,12 +660,37 @@ class getnib: ...@@ -394,12 +660,37 @@ class getnib:
# (geojson-format x1,y1;x2,y2;x3,y3;...) # (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 # 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}') 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 js = requests.get('https://tjenester.norgeibilder.no/rest/projectMetadata.ashx?request='+para).json() #liste med aktuelle of-prosjekt på json-format
ut = inn.decode() # Convert from bytes to string 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}
ut = ut.replace('"','') # Remove quotes, i.e. replace " with nothing nib_liste = js['ProjectList']
a = ut.split("[") # Split string in two at bracket [ print('nib_liste =',nib_liste) # nib_liste = ['Sør-Varanger 2019', 'Sør-Varanger veg 2016', 'Sør-Varanger 2013', 'Sør-Varanger 2011']
b = a[1].split("]") # Split 2nd. substring at each bracket [ gives 2 elements since there is only one occurence of [ (counting starts at 0) # self.iface.messageBar().pushMessage("Info", "All OF: "+ str(nib_liste),level=Qgis.Info, duration=3)
nib_liste = b[0].split(",")
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 lyr = '' # Initiate
for nibprosjwms in nib_liste: # Loads WMS (raster layer) for each of-project within the bounding box in question 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' 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: ...@@ -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 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) 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 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) 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 = root.findGroup("Nib-prosjekt") # Get the group named "Nib-prosjekt"
mygroup.addLayer(rlayer) # Add the layer to this group mygroup.addLayer(rlayer) # Add the layer to this group
...@@ -430,3 +721,34 @@ class getnib: ...@@ -430,3 +721,34 @@ class getnib:
self.iface.messageBar().pushMessage("Success", "WMS added in Nib-prosjekt", level=Qgis.Success, duration=3) 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 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) 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment