-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6de01e0
commit 496617c
Showing
5 changed files
with
1,428 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.