Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
48 changes: 38 additions & 10 deletions python/jupytergis_lab/jupytergis_lab/notebook/gis_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class GISDocument(CommWidget):
:param path: the path to the file that you would like to open. If not provided, a new empty document will be created.
"""

path: Optional[Path]

def __init__(
self,
path: Optional[str | Path] = None,
Expand All @@ -57,10 +59,12 @@ def __init__(
pitch: Optional[float] = None,
projection: Optional[str] = None,
):
if isinstance(path, Path):
path = str(path)
if isinstance(path, str):
path = Path(path)

comm_metadata = GISDocument._path_to_comm(path)
self.path = path

comm_metadata = GISDocument._path_to_comm(str(self.path) if self.path else None)

ydoc = Doc()

Expand Down Expand Up @@ -105,19 +109,33 @@ def layer_tree(self) -> List[str | Dict]:
"""
return self._layerTree.to_py()

def export_to_qgis(self, path: str | Path) -> bool:
def save_as(self, path: str | Path) -> None:
"""Save the document at a new path."""
if isinstance(path, str):
path = Path(path)

if path.name.lower().endswith(".qgz"):
_export_to_qgis(path)
self.path = path
return

if not path.name.lower().endswith(".jgis"):
path = Path(str(path) + ".jGIS")

path.write_text(json.dumps(self.to_py()))
self.path = path

def _export_to_qgis(self, path: str | Path) -> bool:
# Lazy import, jupytergis_qgis of qgis may not be installed
from jupytergis_qgis.qgis_loader import export_project_to_qgis

if isinstance(path, Path):
path = str(path)

virtual_file = {
"layers": self._layers.to_py(),
"sources": self._sources.to_py(),
"layerTree": reversed_tree(self._layerTree.to_py()),
"options": self._options.to_py(),
}
virtual_file = self.to_py()
virtual_file["layerTree"] = reversed_tree(virtual_file["layerTree"])
del virtual_file["metadata"]

return export_project_to_qgis(path, virtual_file)

def add_raster_layer(
Expand Down Expand Up @@ -759,6 +777,16 @@ def _path_to_comm(cls, filePath: Optional[str]) -> Dict:
path=path, format=format, contentType=contentType, create_ydoc=path is None
)

def to_py(self) -> dict:
"""Get the document structure as a Python dictionary."""
return {
"layers": self._layers.to_py(),
"sources": self._sources.to_py(),
"layerTree": self._layerTree.to_py(),
"options": self._options.to_py(),
"metadata": self._metadata.to_py(),
}


class JGISLayer(BaseModel):
class Config:
Expand Down
11 changes: 11 additions & 0 deletions python/jupytergis_lab/jupytergis_lab/notebook/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ def test_add_and_remove_layer_and_source(self):
def test_remove_nonexistent_layer_raises(self):
with pytest.raises(KeyError):
self.doc.remove_layer("foo")


def test_save_as(tmp_path):
os.chdir(tmp_path)

doc = GISDocument()
assert not list(tmp_path.iterdir())

fn = "test.jgis"
doc.save_as(fn)
assert (tmp_path / fn).is_file()
Loading