|
| 1 | +from dataclasses import dataclass |
| 2 | +from pathlib import Path |
| 3 | +from typing import Any, Literal, Optional |
| 4 | +import re |
| 5 | + |
| 6 | +from jupytergis_lab import GISDocument |
| 7 | + |
| 8 | + |
| 9 | +@dataclass |
| 10 | +class Basemap: |
| 11 | + name: str |
| 12 | + url: str |
| 13 | + |
| 14 | + |
| 15 | +BasemapChoice = Literal["light", "dark", "topo"] |
| 16 | +_basemaps: dict[BasemapChoice, list[Basemap]] = { |
| 17 | + "light": [ |
| 18 | + Basemap( |
| 19 | + name="ArcGIS dark basemap", |
| 20 | + url="https://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}.pbf", |
| 21 | + ), |
| 22 | + Basemap( |
| 23 | + name="ArcGIS dark basemap reference", |
| 24 | + url="https://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Reference/MapServer/tile/{z}/{y}/{x}.pbf", |
| 25 | + ), |
| 26 | + ], |
| 27 | + "dark": [ |
| 28 | + Basemap( |
| 29 | + name="ArcGIS light basemap", |
| 30 | + url="https://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Dark_Gray_Base/MapServer/tile/{z}/{y}/{x}.pbf", |
| 31 | + ), |
| 32 | + Basemap( |
| 33 | + name="ArcGIS light basemap reference", |
| 34 | + url="https://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Dark_Gray_Reference/MapServer/tile/{z}/{y}/{x}.pbf", |
| 35 | + ), |
| 36 | + ], |
| 37 | + "topo": [ |
| 38 | + Basemap( |
| 39 | + name="USGS topographic basemap", |
| 40 | + url="https://basemap.nationalmap.gov/arcgis/rest/services/USGSTopo/MapServer/tile/{z}/{y}/{x}", |
| 41 | + ), |
| 42 | + ], |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +def explore( |
| 47 | + data: str | Path | Any, |
| 48 | + *, |
| 49 | + layer_name: Optional[str] = "Exploration layer", |
| 50 | + basemap: BasemapChoice = "topo", |
| 51 | +) -> GISDocument: |
| 52 | + """Run a JupyterGIS data interaction interface alongside a Notebook. |
| 53 | +
|
| 54 | + :param data: A GeoDataFrame or path to a GeoJSON file. |
| 55 | +
|
| 56 | + :raises FileNotFoundError: Received a file path that doesn't exist. |
| 57 | + :raises NotImplementedError: Received an input value that isn't supported yet. |
| 58 | + :raises TypeError: Received an object type that isn't supported. |
| 59 | + :raises ValueError: Received an input value that isn't supported. |
| 60 | + """ |
| 61 | + doc = GISDocument() |
| 62 | + |
| 63 | + for basemap_obj in _basemaps[basemap]: |
| 64 | + doc.add_raster_layer(basemap_obj.url, name=basemap_obj.name) |
| 65 | + |
| 66 | + _add_layer(doc=doc, data=data, name=layer_name) |
| 67 | + |
| 68 | + # TODO: Zoom to layer. Currently not exposed in Python API. |
| 69 | + |
| 70 | + doc.sidecar(title="JupyterGIS explorer") |
| 71 | + |
| 72 | + # TODO: should we return `doc`? It enables the exploration environment more usable, |
| 73 | + # but by default, `explore(...)` would display a widget in the notebook _and_ open a |
| 74 | + # sidecar for the same widget. The user would need to append a semicolon to disable |
| 75 | + # that behavior. We can't disable that behavior from within this function to the |
| 76 | + # best of my knowlwedge. |
| 77 | + |
| 78 | + |
| 79 | +def _add_layer( |
| 80 | + *, |
| 81 | + doc: GISDocument, |
| 82 | + data: Any, |
| 83 | + name: str, |
| 84 | +) -> str: |
| 85 | + """Add a layer to the document, autodetecting its type. |
| 86 | +
|
| 87 | + This method currently supports only GeoDataFrames and GeoJSON files. |
| 88 | +
|
| 89 | + :param doc: A GISDocument to add the layer to. |
| 90 | + :param data: A data object. Valid data objects include geopandas GeoDataFrames and paths to GeoJSON files. |
| 91 | + :param name: The name that will be used for the layer. |
| 92 | +
|
| 93 | + :return: A layer ID string. |
| 94 | +
|
| 95 | + :raises FileNotFoundError: Received a file path that doesn't exist. |
| 96 | + :raises NotImplementedError: Received an input value that isn't supported yet. |
| 97 | + :raises TypeError: Received an object type that isn't supported. |
| 98 | + :raises ValueError: Received an input value that isn't supported. |
| 99 | + """ |
| 100 | + if isinstance(data, str): |
| 101 | + if re.match(r"^(http|https)://", data) is not None: |
| 102 | + raise NotImplementedError("URLs not yet supported.") |
| 103 | + else: |
| 104 | + data = Path(data) |
| 105 | + |
| 106 | + if isinstance(data, Path): |
| 107 | + if not data.exists(): |
| 108 | + raise FileNotFoundError(f"File not found: {data}") |
| 109 | + |
| 110 | + ext = data.suffix.lower() |
| 111 | + |
| 112 | + if ext in [".geojson", ".json"]: |
| 113 | + return doc.add_geojson_layer(path=data, name=name) |
| 114 | + elif ext in [".tif", ".tiff"]: |
| 115 | + raise NotImplementedError("GeoTIFFs not yet supported.") |
| 116 | + else: |
| 117 | + raise ValueError(f"Unsupported file type: {data}") |
| 118 | + |
| 119 | + try: |
| 120 | + from geopandas import GeoDataFrame |
| 121 | + |
| 122 | + if isinstance(data, GeoDataFrame): |
| 123 | + return doc.add_geojson_layer(data=data.to_geo_dict(), name=name) |
| 124 | + except ImportError: |
| 125 | + pass |
| 126 | + |
| 127 | + raise TypeError(f"Unsupported input type: {type(data)}") |
0 commit comments