Skip to content

Commit

Permalink
Some cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Apr 24, 2024
1 parent cc0f567 commit 54e0dfb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 60 deletions.
20 changes: 10 additions & 10 deletions qsa-api/qsa_api/mapproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import yaml
import shutil
from pathlib import Path
from flask import current_app

from .utils import config, qgisserver_base_url


class QSAMapProxy:
def __init__(self, name: str) -> None:
def __init__(self, name: str, schema: str = "") -> None:
self.name = name
self.schema = "public"
if schema:
self.schema = schema

def create(self) -> None:
parent = Path(__file__).resolve().parent
Expand Down Expand Up @@ -37,16 +41,16 @@ def add_layer(self, name: str, bbox: list, srs: int) -> None:
self.cfg["caches"] = {}
self.cfg["sources"] = {}

l = {"name": name, "title": name, "sources": [f"{name}_cache"]}
self.cfg["layers"].append(l)
lyr = {"name": name, "title": name, "sources": [f"{name}_cache"]}
self.cfg["layers"].append(lyr)

c = {"grids": ["webmercator"], "sources": [f"{name}_wms"]}
self.cfg["caches"][f"{name}_cache"] = c

s = {
"type": "wms",
"req": {
"url": self._qgisserver_url,
"url": qgisserver_base_url(self.name, self.schema),
"layers": name,
"transparent": True,
},
Expand Down Expand Up @@ -78,11 +82,7 @@ def remove_layer(self, name: str) -> None:

@staticmethod
def _mapproxy_projects_dir() -> Path:
return Path(current_app.config["CONFIG"].mapproxy_projects)

@property
def _qgisserver_url(self) -> str:
return f"{current_app.config['CONFIG'].qgisserver_url}/{self.name}"
return Path(config().mapproxy_projects_dir)

@property
def _mapproxy_project(self) -> Path:
Expand Down
52 changes: 13 additions & 39 deletions qsa-api/qsa_api/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import shutil
import sqlite3
from enum import Enum
from pathlib import Path
from flask import current_app

from qgis.PyQt.QtCore import Qt
from qgis.core import (
Expand All @@ -29,28 +27,14 @@
from qgis.PyQt.QtXml import QDomDocument, QDomNode

from .mapproxy import QSAMapProxy
from .utils import StorageBackend, config


RENDERER_TAG_NAME = "renderer-v2" # constant from core/symbology/renderer.h


class StorageBackend(Enum):
INVALID = 0
FILESYSTEM = 1
POSTGRESQL = 2

def from_str(storage: str) -> "StorageBackend":
if storage.lower() == StorageBackend.FILESYSTEM.name.lower():
return StorageBackend.FILESYSTEM
elif storage.lower() == StorageBackend.FILESYSTEM.name.lower():
return StorageBackend.POSTGRESQL
return StorageBackend.INVALID


class QSAProject:
def __init__(self, name: str, schema: str = "public") -> None:
print("Init QSAProject with:")
print(schema)
self.name: str = name
self.schema: str = "public"
if schema:
Expand Down Expand Up @@ -78,14 +62,14 @@ def sqlite_db(self) -> Path:
def projects(schema: str = "") -> list:
p = []

if QSAProject._storage_backend() == StorageBackend.FILESYSTEM:
if StorageBackend.type() == StorageBackend.FILESYSTEM:
for i in QSAProject._qgis_projects_dir().glob("**/*.qgs"):
name = i.parent.name.replace(
QSAProject._qgis_project_dir_prefix(), ""
)
p.append(QSAProject(name))
else:
service = QSAProject._config().qgisserver_projects_psql_service
service = config().qgisserver_projects_psql_service
uri = f"postgresql:?service={service}&schema={schema}"

storage = (
Expand Down Expand Up @@ -128,9 +112,9 @@ def metadata(self) -> dict:
p.metadata().creationDateTime().toString(Qt.ISODate)
)
m["crs"] = p.crs().authid()
m["storage"] = QSAProject._storage_backend().name.lower()
m["storage"] = StorageBackend.type().name.lower()

if QSAProject._storage_backend() == StorageBackend.POSTGRESQL:
if StorageBackend.type() == StorageBackend.POSTGRESQL:
m["schema"] = self.schema
return m

Expand Down Expand Up @@ -273,10 +257,10 @@ def remove_layer(self, name: str) -> None:
return rc

def exists(self) -> bool:
if QSAProject._storage_backend() == StorageBackend.FILESYSTEM:
if StorageBackend.type() == StorageBackend.FILESYSTEM:
return self._qgis_project_dir.exists()
else:
service = QSAProject._config().qgisserver_projects_psql_service
service = config().qgisserver_projects_psql_service
uri = f"postgresql:?service={service}&schema={self.schema}"

storage = (
Expand Down Expand Up @@ -316,7 +300,7 @@ def create(self, author: str) -> bool:
def remove(self) -> None:
shutil.rmtree(self._qgis_project_dir)

if QSAProject._storage_backend() == StorageBackend.POSTGRESQL:
if StorageBackend.type() == StorageBackend.POSTGRESQL:
storage = (
QgsApplication.instance()
.projectStorageRegistry()
Expand Down Expand Up @@ -462,7 +446,7 @@ def remove_style(self, name: str) -> bool:

@staticmethod
def _qgis_projects_dir() -> Path:
return Path(QSAProject._config().qgisserver_projects_dir)
return Path(config().qgisserver_projects_dir)

@staticmethod
def _layer_type(layer_type: str) -> Qgis.LayerType | None:
Expand All @@ -472,13 +456,9 @@ def _layer_type(layer_type: str) -> Qgis.LayerType | None:
return Qgis.LayerType.Raster
return None

@staticmethod
def _config():
return current_app.config["CONFIG"]

@property
def _mapproxy_enabled(self) -> bool:
return bool(self._config().mapproxy_projects_dir)
return bool(config().mapproxy_projects_dir)

@property
def _qgis_project_dir(self) -> Path:
Expand All @@ -490,20 +470,14 @@ def _qgis_project_dir(self) -> Path:
@staticmethod
def _qgis_project_dir_prefix(schema: str = "") -> str:
prefix = ""
if QSAProject._storage_backend() == StorageBackend.POSTGRESQL:
if StorageBackend.type() == StorageBackend.POSTGRESQL:
prefix = f"{schema}_"
return prefix

@property
def _qgis_project_uri(self) -> str:
if QSAProject._storage_backend() == StorageBackend.POSTGRESQL:
service = QSAProject._config().qgisserver_projects_psql_service
if StorageBackend.type() == StorageBackend.POSTGRESQL:
service = config().qgisserver_projects_psql_service
return f"postgresql:?service={service}&schema={self.schema}&project={self.name}"
else:
return (self._qgis_project_dir / f"{self.name}.qgs").as_posix()

@staticmethod
def _storage_backend() -> StorageBackend:
if QSAProject._config().qgisserver_projects_psql_service:
return StorageBackend.POSTGRESQL
return StorageBackend.FILESYSTEM
29 changes: 29 additions & 0 deletions qsa-api/qsa_api/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# coding: utf8

from enum import Enum
from flask import current_app


def config():
return current_app.config["CONFIG"]


class StorageBackend(Enum):
FILESYSTEM = 0
POSTGRESQL = 1

@staticmethod
def type() -> "StorageBackend":
if config().qgisserver_projects_psql_service:
return StorageBackend.POSTGRESQL
return StorageBackend.FILESYSTEM


def qgisserver_base_url(project: str, psql_schema: str) -> str:
url = f"{config().qgisserver_url}"
if StorageBackend.type() == StorageBackend.FILESYSTEM:
url = f"{url}/{project}?"
elif StorageBackend.type() == StorageBackend.POSTGRESQL:
service = config().qgisserver_projects_psql_service
url = f"{url}?MAP=postgresql:?service={service}%26schema={psql_schema}%26project={project}&"
return url
13 changes: 3 additions & 10 deletions qsa-api/qsa_api/wms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# coding: utf8

from flask import current_app

from .project import QSAProject, StorageBackend
from .project import QSAProject
from .utils import qgisserver_base_url


class WMS:
Expand All @@ -18,10 +17,4 @@ def getmap_url(project, psql_schema, layer):

@staticmethod
def getmap(project, psql_schema, layer):
base_url = f"{current_app.config['CONFIG'].qgisserver_url}"
if QSAProject._storage_backend() == StorageBackend.FILESYSTEM:
base_url = f"{base_url}/{project}?"
elif QSAProject._storage_backend() == StorageBackend.POSTGRESQL:
service = QSAProject._config().qgisserver_projects_psql_service
base_url = f"{base_url}?MAP=postgresql:?service={service}%26schema={psql_schema}%26project={project}&"
return f"{base_url}{WMS.getmap_url(project, psql_schema, layer)}"
return f"{qgisserver_base_url()}{WMS.getmap_url(project, psql_schema, layer)}"
1 change: 0 additions & 1 deletion qsa-api/tests/test_api_storage_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ def test_default_style(self):
data["author"] = "pblottiere"
data["storage"] = "filesystem"
p = self.app.post("/api/projects/", data)
print(p.get_json())
self.assertEqual(p.status_code, 201)

# default styles
Expand Down

0 comments on commit 54e0dfb

Please sign in to comment.