Skip to content

Commit

Permalink
Support importing QGIS project
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Jul 2, 2024
1 parent 6de01e0 commit 496617c
Show file tree
Hide file tree
Showing 5 changed files with 1,428 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Test

on:
push:
branches:
- main
pull_request:
branches:
- main

defaults:
run:
shell: bash

jobs:
test:
name: ${{ matrix.os }} python-${{ matrix.python-version }}
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: [ '3.12' ]
defaults:
run:
shell: bash -l {0}

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup conda environment
uses: mamba-org/setup-micromamba@v1
with:
environment-file: environment-dev.yml
environment-name: jupytergis
create-args: python=${{ matrix.python-version }}

- name: Build and install jupytergis_core
run: python scripts/dev-install.py

- name: Run tests
run: pytest --color=yes -v python/jupytergis_core/tests
8 changes: 8 additions & 0 deletions environment-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: jupytergis
channels:
- conda-forge
dependencies:
- pip
- qgis
- pytest
- dirty-equals
102 changes: 102 additions & 0 deletions python/jupytergis_core/jupytergis_core/qgis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from __future__ import annotations

from pathlib import Path
from urllib.parse import unquote
from uuid import uuid4

from qgis.core import (
QgsApplication,
QgsLayerTreeGroup,
QgsLayerTreeLayer,
QgsRasterLayer,
QgsProject,
)


def qgis_to_jgis(
node: QgsLayerTreeGroup,
layer_tree: list | None = None,
layers: dict[str, dict[str, Any]] | None = None,
sources: dict[str, dict[str, Any]] | None = None,
) -> list[dict[str, Any]] | None:
do_return = False
if layer_tree is None:
do_return = True
layer_tree = []
layers = {}
sources = {}
children = node.children()
for child in children:
is_visible = child.isVisible()
if isinstance(child, QgsLayerTreeGroup):
_layer_tree = []
group = {
"layers": _layer_tree,
"name": child.name(),
}
layer_tree.append(group)
qgis_to_jgis(child, _layer_tree, layers, sources)
elif isinstance(child, QgsLayerTreeLayer):
layer = child.layer()
layer_name = layer.name()
layer_type = None
parameters = {}
if isinstance(layer, QgsRasterLayer):
layer_type = "RasterLayer"
source_params = layer.source().split("&")
url = ""
max_zoom = 0
min_zoom = 0
for param in source_params:
if param.startswith("url="):
url = unquote(param[4:])
elif param.startswith("zmax="):
max_zoom = int(param[5:])
elif param.startswith("zmin="):
min_zoom = int(param[5:])
parameters.update(
url=url,
maxZoom=max_zoom,
minZoom=min_zoom,
)
layer_id = layer.id()
layer_tree.append(layer_id)
source_id = str(uuid4())
layers[layer_id] = {
"name": layer_name,
"parameters": {
"source": source_id,
},
"type": layer_type,
"visible": is_visible,
}
sources[source_id] = {
"name": layer_name,
"type": layer_type,
"parameters": parameters,
}
if do_return:
return {
"layers": layers,
"sources": sources,
"layerTree": layer_tree
}


def import_project_from_qgis(path: str | Path):
if isinstance(path, Path):
path = str(path)

QgsApplication.setPrefixPath("", True)
qgs = QgsApplication([], False)
qgs.initQgis()

project = QgsProject.instance()
project.read(path)
layer_tree_root = project.layerTreeRoot()

jgis_layer_tree = qgis_to_jgis(layer_tree_root)

qgs.exitQgis()

return jgis_layer_tree
Loading

0 comments on commit 496617c

Please sign in to comment.