Skip to content

Instantly share code, notes, and snippets.

@minrk
Created August 13, 2025 23:15
Show Gist options
  • Save minrk/ac0514d25dbf966dc630775263c8957f to your computer and use it in GitHub Desktop.
Save minrk/ac0514d25dbf966dc630775263c8957f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "1008b547-f396-4960-8a80-118370f0fab1",
"metadata": {},
"source": [
"# JupyterGIS Python API"
]
},
{
"cell_type": "markdown",
"id": "28c64dd8-6b90-4408-bbeb-e5037add1fbc",
"metadata": {},
"source": [
"#### Simple usage"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "050e5936-f9e1-44ff-8bc9-a8556d7643ee",
"metadata": {},
"outputs": [],
"source": [
"from jupytergis import GISDocument"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "4379472b-f5fd-4ccc-befd-aca98c020e01",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hiking.jGIS'"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# make a copy so we aren't editing the original\n",
"import shutil\n",
"shutil.copyfile(\"france_hiking.jGIS\", \"hiking.jGIS\")"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "9821d466-6ae7-418f-aea7-9d67153da7d6",
"metadata": {},
"outputs": [],
"source": [
"doc = GISDocument(\"hiking.jGIS\")"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "f4a7000c-1bfc-4d41-8a0b-5c82c6e67716",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.ywidget-view+json": {
"model_id": "98706c1385c649df90babe4696734f28",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"<jupytergis_lab.notebook.gis_document.GISDocument object at 0x10c6533b0>"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"doc"
]
},
{
"cell_type": "markdown",
"id": "90d3ea9f-0eeb-449f-8365-46ef531c0413",
"metadata": {},
"source": [
"Creating a layer returns a layer id"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "5938ba70-36d7-4af3-b877-815464ef6d1a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'b7d8902c-4b93-403e-bbb9-67909e1bc3b8'"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"layer_id = doc.add_raster_layer(\n",
" url=\"https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}\",\n",
" name=\"Google Satellite\",\n",
" attribution=\"Google\",\n",
" opacity=0.6,\n",
")\n",
"layer_id"
]
},
{
"cell_type": "markdown",
"id": "c56442e8-6a25-4612-874b-cf5e5e9a52ab",
"metadata": {},
"source": [
"Wrapper class for updating layers "
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "0beafd06-6efa-416a-a368-6e6ad0666c75",
"metadata": {},
"outputs": [],
"source": [
"from dataclasses import dataclass\n",
"from jupytergis_lab.notebook.gis_document import IRasterLayer, JGISLayer\n",
"\n",
"@dataclass\n",
"class LayerWrapper:\n",
" layer_id: str\n",
" doc: GISDocument\n",
"\n",
" @property\n",
" def layer_model(self):\n",
" return self.doc.layers[self.layer_id]\n",
"\n",
" @property\n",
" def visible(self):\n",
" return self.doc.layers[self.layer_id]['visible']\n",
"\n",
" @visible.setter\n",
" def visible(self, value):\n",
" self.update_fields(visible=value)\n",
" \n",
" @property\n",
" def opacity(self):\n",
" return self.doc.layers[self.layer_id]['parameters']['opacity']\n",
"\n",
" @opacity.setter\n",
" def opacity(self, value):\n",
" self.update_parameters(opacity=value)\n",
" \n",
" def update_parameters(self, **params):\n",
" \"\"\"update parameters in a layer\"\"\"\n",
" existing_params = self.doc._layers[self.layer_id]['parameters']\n",
" new_params = {}\n",
" new_params.update(existing_params)\n",
" new_params.update(params)\n",
" self.update_fields(parameters=new_params)\n",
"\n",
" def update_fields(self, **fields):\n",
" \"\"\"set top-level fields\"\"\"\n",
" layer_copy = self.doc._layers[self.layer_id].copy()\n",
" layer_copy.update(fields)\n",
" # assigning `doc._layers` to a COPY is what actually updates the document and view\n",
" self.doc._layers[self.layer_id] = layer_copy\n",
"\n",
" def remove(self):\n",
" self.doc.remove_layer(self.layer_id)\n"
]
},
{
"cell_type": "markdown",
"id": "520922de-ed16-4dbf-a0f0-eef88d7602ed",
"metadata": {},
"source": [
"Create a layer wrapper given a layer id and doc object"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "2d760f1c-64cb-4399-9e0b-4bdcb7bad871",
"metadata": {},
"outputs": [],
"source": [
"layer_wrapper = LayerWrapper(layer_id, doc)"
]
},
{
"cell_type": "markdown",
"id": "b7394edf-4faa-414b-ad56-528667bcc97e",
"metadata": {},
"source": [
"now we have methods and properties to interact with the layer"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "c14c5ffd-6518-44c0-ab87-65988fff08cf",
"metadata": {},
"outputs": [],
"source": [
"layer_wrapper.opacity = 0.8"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "96731844-3be6-4a5e-a45d-b950e45c2e84",
"metadata": {},
"outputs": [],
"source": [
"layer_wrapper.visible = False"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "2b30eb68-8741-4a1e-aa48-bc7050e1e56a",
"metadata": {},
"outputs": [],
"source": [
"layer_wrapper.visible = True"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "f7c43b10-a8f9-4208-bd64-dab31fd34ced",
"metadata": {},
"outputs": [],
"source": [
"layer_wrapper.opacity = 0.6"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "8545df4d-d12b-4747-9f01-b019c5096042",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'filters': None,\n",
" 'type': 'RasterLayer',\n",
" 'name': 'Google Satellite',\n",
" 'parameters': {'opacity': 0.6,\n",
" 'source': '71d74f8b-27eb-416d-8098-921618b875b5'},\n",
" 'visible': True}"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"layer_wrapper.layer_model"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "69ac2a9a-d9a9-4170-b1f5-451b9fd5780e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'visible': True,\n",
" 'name': 'GeoJSON Layer',\n",
" 'parameters': {'symbologyState': None,\n",
" 'color': None,\n",
" 'opacity': 1.0,\n",
" 'source': '89aa9685-12e0-4103-a044-8512dbe1643d'},\n",
" 'filters': {'logicalOp': None,\n",
" 'appliedFilters': [{'value': None, 'feature': None, 'operator': None}]},\n",
" 'type': 'VectorLayer'}"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"geo_id = doc.add_geojson_layer(path=\"france_regions.geojson\")\n",
"geo = LayerWrapper(geo_id, doc)\n",
"geo.layer_model"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "6f8f0dc6-ba4a-4734-bb9c-6d1fa2fe3504",
"metadata": {},
"outputs": [],
"source": [
"geo.update_parameters(color=None)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "edc8083f-416f-4262-b12f-5636f4c89eeb",
"metadata": {},
"outputs": [],
"source": [
"geo.visible = False"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "3629be32-bf06-4f68-a734-9af3cc17a7e3",
"metadata": {},
"outputs": [],
"source": [
"image_layer_id = doc.add_image_layer(\n",
" url=\"https://maplibre.org/maplibre-gl-js/docs/assets/radar.gif\",\n",
" coordinates=[\n",
" [-80.425, 46.437],\n",
" [-71.516, 46.437],\n",
" [-71.516, 37.936],\n",
" [-80.425, 37.936],\n",
" ],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "d06a6468-2bfc-4b38-ab94-4edb7de6f48c",
"metadata": {},
"outputs": [],
"source": [
"layer_wrapper = LayerWrapper(image_layer_id, doc)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "5237b6c2-77fa-4cbc-be80-f1759b0655ec",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'visible': True,\n",
" 'name': 'Image Layer',\n",
" 'filters': None,\n",
" 'parameters': {'opacity': 1.0,\n",
" 'source': '79d88918-d752-4870-9683-8edf83daad92'},\n",
" 'type': 'ImageLayer'}"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"layer_wrapper.layer_model"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "50efcc85-a21e-43f1-afa3-845cf56d28f8",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.11"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment