Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: interaction IRMT <--> GeoViewer #720

Draft
wants to merge 43 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
db19a92
Enable setting profiles to connect to instances of the OQ-GeoViewer
ptormene Jan 15, 2020
5c9ed0a
Set session.auth with username and password
ptormene Jan 16, 2020
e75a247
Add a menu group and item to import a geoviwer project
ptormene Jan 16, 2020
3b1c6ac
Add geometry validation as first step to upload project to geoviewer
ptormene Jan 24, 2020
1973795
Add missing files to import layers from geoviewer
ptormene Jan 24, 2020
0488f16
Add missing files to upload project to geoviewer
ptormene Jan 24, 2020
fa8ca7f
Add consolidation workflow
ptormene Jan 24, 2020
a817873
Add layer with invalid feats to tree; fix feedback
ptormene Jan 27, 2020
c53d6d2
Add authentication and call to the upload api
ptormene Jan 27, 2020
6eb2207
Check geometries only for vector layers
ptormene Jan 28, 2020
727f751
While uploading to geoviewer, add lincense to the POST data
ptormene Jan 28, 2020
efdfa0b
Display an error message if the project does not have a valid crs
ptormene Jan 28, 2020
a46c40b
Display a warning if a basemap is not in a group named basemaps or si…
ptormene Jan 28, 2020
64c46d1
Revert "While uploading to geoviewer, add lincense to the POST data"
ptormene Jan 29, 2020
651acfd
Improve a comment
ptormene Jan 29, 2020
bce29ed
Add warnings if project owsServiceCapabilities or wmsExtent are disabled
ptormene Jan 29, 2020
64ef883
Assign parent to UploadGvProjDialog; add comment about retrieving lic…
ptormene Jan 29, 2020
2badb4e
Merge remote-tracking branch 'origin/master' into geoviewer
ptormene Aug 27, 2020
5648650
Add warnings if "Add geometry to feature response" or "Segmentize fea…
ptormene Aug 27, 2020
af8f747
Populate project kind cbx; check if proj name is already taken
ptormene Aug 28, 2020
c6182bd
Add logo to ui_upload_gv_proj.ui
ptormene Aug 28, 2020
3aace17
Add buttons to download projects from GeoViewer
ptormene Aug 28, 2020
f07d411
Fix setting project kind and set some timeouts to requests
ptormene Sep 4, 2020
dea87ed
Add checkbod to automatically create a map out of the uploaded GeoVie…
ptormene Sep 4, 2020
45a6f41
Merge remote-tracking branch 'origin/master' into geoviewer
ptormene Sep 24, 2020
74b017f
Merge branch 'master' into geoviewer
ptormene Jan 7, 2021
b8d6f6f
Fixed downloading procedure and minor things
ptormene Jan 11, 2021
eb08865
Fix indentation of code generating download buttons
ptormene Jan 11, 2021
a45f4ee
In list of GeoViewer maps, put Download button first, then name, then…
ptormene Jan 11, 2021
bcdcacb
Add message boxes to automatically set up missing properties before u…
ptormene Jan 11, 2021
184d855
Do not give warnings about geometries for csv-based layers (tables)
ptormene Jan 12, 2021
c4787e5
Merge branch 'master' into geoviewer
ptormene Feb 16, 2021
6971d2a
Merge branch 'master' into geoviewer
ptormene Apr 7, 2021
7ac9017
Retrieve only published maps from geoviewer and display a clear error…
ptormene Apr 13, 2021
265a4a7
Check validity of geometries only for vector layers
ptormene Apr 13, 2021
0eb436e
Save project before consolidating (otherwise it does not change on se…
ptormene Apr 15, 2021
ae2a1a1
Check auto_create_map by default
ptormene Apr 16, 2021
8445e5c
When loading GeoViewer project, add basemaps into a group
ptormene Apr 21, 2021
196d3fa
Merge branch 'master' into geoviewer
ptormene Aug 17, 2021
c749de4
Merge branch 'master' into geoviewer
ptormene Aug 26, 2021
615c6c9
Merge branch 'master' into geoviewer
ptormene Aug 26, 2021
44de6e4
Merge branch 'master' into geoviewer
ptormene Sep 6, 2021
465edb0
Merge branch 'master' into geoviewer
ptormene Sep 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed downloading procedure and minor things
  • Loading branch information
ptormene committed Jan 11, 2021
commit b8d6f6ff60c598abbb18ada9f536c10270598971
138 changes: 138 additions & 0 deletions svir/dialogs/import_gv_map_dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# -*- coding: utf-8 -*-
# /***************************************************************************
# Irmt
# A QGIS plugin
# OpenQuake Integrated Risk Modelling Toolkit
# -------------------
# begin : 2020-01-23
# copyright : (C) 2020 by GEM Foundation
# email : devops@openquake.org
# ***************************************************************************/
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.

import json
import traceback
import tempfile
import requests
import zipfile
import os
from requests import Session
from qgis.core import QgsProject
from qgis.PyQt.QtCore import QSettings
from qgis.PyQt.QtWidgets import (
QDialog, QMessageBox, QTableWidgetItem, QPushButton)
from svir.utilities.utils import (
get_ui_class, log_msg, geoviewer_login, get_credentials)
from svir.utilities.shared import DEFAULT_GEOVIEWER_PROFILES

BUTTON_WIDTH = 75

FORM_CLASS = get_ui_class('ui_import_gv_map.ui')


class ImportGvMapDialog(QDialog, FORM_CLASS):
"""
Dialog listing maps available on a connected OqGeoviewer, showing map
properties and a button to download the map as a QGIS project
"""
def __init__(self, message_bar):
self.message_bar = message_bar
QDialog.__init__(self)
# Set up the user interface from Designer.
self.setupUi(self)
self.session = Session()
self.authenticate()
map_list = self.get_map_list()
if not map_list:
self.reject()
return
self.show_map_list(map_list)

def authenticate(self):
self.hostname, username, password = get_credentials('geoviewer')
geoviewer_login(self.hostname, username, password, self.session)

def show_map_list(self, map_list):
if not map_list:
return
fields_to_display = map_list[0]['fields']
self.list_of_maps_tbl.setRowCount(len(map_list))
self.list_of_maps_tbl.setColumnCount(len(fields_to_display) + 1)
for row, map in enumerate(map_list):
for col, field in enumerate(fields_to_display):
item = QTableWidgetItem(str(map['fields'][field]))
self.list_of_maps_tbl.setItem(row, col, item)
self.list_of_maps_tbl.setHorizontalHeaderItem(
col, QTableWidgetItem(field))
# if True: # map['fields']['downloadable']:
button = QPushButton('Download')
self.list_of_maps_tbl.setCellWidget(
row, len(fields_to_display), button)
self.list_of_maps_tbl.setColumnWidth(col, BUTTON_WIDTH)
button.clicked.connect(
lambda checked=False,
map_name=map['fields']['name'],
map_slug=map['fields']['slug']:
self.on_download_btn_clicked(map_name, map_slug))

def download_url(self, url, save_path, chunk_size=128):
r = requests.get(url, stream=True)
if not r.ok:
msg = 'Unable to download the selected map: %s' % r.reason
log_msg(msg, level='C', message_bar=self.message_bar)
self.reject()
return False
with open(save_path, 'wb') as fd:
for chunk in r.iter_content(chunk_size=chunk_size):
fd.write(chunk)
return True

def on_download_btn_clicked(self, map_name, map_slug):
msg = 'Downloading map %s (%s)' % (map_name, map_slug)
log_msg(msg, level='S', message_bar=self.message_bar)
map_download_url = '%s/map/%s/download' % (self.hostname, map_slug)
zip_file = tempfile.NamedTemporaryFile(suffix='.zip')
zip_file.close()
if not self.download_url(map_download_url, zip_file.name):
return
dirpath = tempfile.mkdtemp()
with zipfile.ZipFile(zip_file.name, 'r') as zip_ref:
zip_ref.extractall(dirpath)
log_msg('The project was downloaded into the folder: %s' % dirpath,
level='S')
for filename in os.listdir(dirpath):
if filename.endswith('.qgs'):
qgsfilepath = os.path.join(dirpath, filename)
project = QgsProject.instance()
project.read(qgsfilepath)
break

def get_map_list(self):
map_list_url = self.hostname + '/api/map_list/'
try:
resp = self.session.get(map_list_url, timeout=10)
except Exception:
msg = "Unable to retrieve the list of maps.\n%s" % (
traceback.format_exc())
log_msg(msg, level='C', message_bar=self.messageBar())
self.reject()
return
if resp.status_code != 200: # 200 means successful:OK
msg = ('Unable to retrieve the list of maps: %s' % resp.text)
log_msg(msg, level='C', message_bar=self.messageBar())
self.reject()
return
map_list = json.loads(resp.text)
return map_list
95 changes: 0 additions & 95 deletions svir/dialogs/import_gv_proj_dialog.py

This file was deleted.

7 changes: 4 additions & 3 deletions svir/dialogs/upload_gv_proj_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import os
import re
import json
import traceback
import tempfile
from requests import Session
from qgis.server import QgsServerProjectUtils
Expand All @@ -34,15 +33,15 @@
QgsProcessingUtils, QgsField, QgsFields, QgsFeature, QgsGeometry,
QgsWkbTypes, QgsMemoryProviderUtils, QgsCoordinateReferenceSystem,
QgsTask, QgsMapLayerType)
from qgis.PyQt.QtCore import QSettings, QVariant, QDir, QFile
from qgis.PyQt.QtCore import QVariant, QDir, QFile
from qgis.PyQt.QtWidgets import (
QDialog, QDialogButtonBox)
from processing.gui.AlgorithmExecutor import execute
from svir.tasks.consolidate_task import ConsolidateTask
from svir.utilities.utils import (
get_ui_class, log_msg, get_credentials, tr, geoviewer_login)
from svir.utilities.shared import (
DEFAULT_GEOVIEWER_PROFILES, LICENSES, DEFAULT_LICENSE, PROJECT_KINDS,
LICENSES, DEFAULT_LICENSE, PROJECT_KINDS,
DEFAULT_PROJECT_KIND)


Expand Down Expand Up @@ -185,6 +184,8 @@ def check_geometries(self):
" 'Basemaps'" % layer.name())
log_msg(msg, level='W', message_bar=self.message_bar)
continue
# FIXME: we could add a check to avoid warnings for layers that
# have no geometries (e.g. those used as data sources for joins)
parameters['INPUT_LAYER'] = layer.id()
ok, results = execute(
alg, parameters, context=context, feedback=feedback)
Expand Down
22 changes: 12 additions & 10 deletions svir/irmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
from svir.dialogs.viewer_dock import ViewerDock
from svir.utilities.import_sv_data import get_loggedin_downloader
from svir.dialogs.download_layer_dialog import DownloadLayerDialog
from svir.dialogs.import_gv_proj_dialog import ImportGvProjDialog
from svir.dialogs.import_gv_map_dialog import ImportGvMapDialog
from svir.dialogs.upload_gv_proj_dialog import UploadGvProjDialog
from svir.dialogs.projects_manager_dialog import ProjectsManagerDialog
from svir.dialogs.select_sv_variables_dialog import SelectSvVariablesDialog
Expand Down Expand Up @@ -204,11 +204,13 @@ def initGui(self):
self.menu_action = menu_bar.insertMenu(
self.iface.firstRightStandardMenu().menuAction(), self.menu)

# Action to list OQ GeoViewer projects
self.add_menu_item("import_geoviewer_project",
# NOTE: we could either list maps and use the existing api to download
# maps or add a GeoViewer api to download projects
# Action to list OQ GeoViewer maps
self.add_menu_item("import_geoviewer_map",
":/plugins/irmt/load_layer.svg",
u"Import project from the OpenQuake &GeoViewer",
self.import_geoviewer_project,
u"Import map from the OpenQuake &GeoViewer",
self.import_geoviewer_map,
enable=True,
add_to_layer_actions=False,
submenu='OQ GeoViewer')
Expand Down Expand Up @@ -818,17 +820,17 @@ def _data_download_successful(
layer.setFieldAlias(
field_idx, 'Country name')

def import_geoviewer_project(self):
def import_geoviewer_map(self):
"""
FIXME
Open dialog to import maps from a connected OQ-GeoViewer
"""
self.download_gv_proj_dlg = ImportGvProjDialog(
self.download_gv_map_dlg = ImportGvMapDialog(
self.iface.messageBar())
self.download_gv_proj_dlg.exec_()
self.download_gv_map_dlg.exec_()

def upload_project_to_geoviewer(self):
"""
FIXME
Open dialog to upload the current project to a connected OQ-GeoViewer
"""
self.upload_gv_proj_dlg = UploadGvProjDialog(
self.iface.messageBar(), parent=self.iface.mainWindow())
Expand Down
12 changes: 6 additions & 6 deletions svir/ui/ui_import_gv_proj.ui → svir/ui/ui_import_gv_map.ui
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DownloadGeoviewerProjectDialog</class>
<widget class="QDialog" name="DownloadGeoviewerProjectDialog">
<class>DownloadGeoviewerMapDialog</class>
<widget class="QDialog" name="DownloadGeoviewerMapDialog">
<property name="geometry">
<rect>
<x>0</x>
Expand All @@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>Download OQ-GeoViewer projects</string>
<string>Download OQ-GeoViewer map</string>
</property>
<property name="windowIcon">
<iconset>
Expand All @@ -31,7 +31,7 @@
<number>0</number>
</property>
<item>
<widget class="QLabel" name="list_of_projects_lbl">
<widget class="QLabel" name="list_of_maps_lbl">
<property name="font">
<font>
<weight>75</weight>
Expand All @@ -40,14 +40,14 @@
</font>
</property>
<property name="text">
<string>OpenQuake GeoViewer Projects</string>
<string>OpenQuake GeoViewer Maps</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTableWidget" name="list_of_projects_tbl">
<widget class="QTableWidget" name="list_of_maps_tbl">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
Expand Down