Skip to content

Commit

Permalink
Add more MapLibre examples (#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Jun 20, 2024
1 parent 350a66e commit 70f8e86
Show file tree
Hide file tree
Showing 6 changed files with 590 additions and 0 deletions.
129 changes: 129 additions & 0 deletions docs/maplibre/add_image.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![image](https://jupyterlite.rtfd.io/en/latest/_static/badge.svg)](https://demo.leafmap.org/lab/index.html?path=maplibre/add_image.ipynb)\n",
"[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/leafmap/blob/master/docs/maplibre/add_image.ipynb)\n",
"[![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/opengeos/leafmap/HEAD)\n",
"\n",
"**Add an icon to the map**\n",
"\n",
"This source code of this example is adapted from the MapLibre GL JS example - [Add an icon to the map](https://maplibre.org/maplibre-gl-js/docs/examples/add-image).\n",
"\n",
"Uncomment the following line to install [leafmap](https://leafmap.org) if needed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %pip install \"leafmap[maplibre]\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import leafmap.maplibregl as leafmap"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To run this notebook, you will need an [API key](https://docs.maptiler.com/cloud/api/authentication-key/) from [MapTiler](https://www.maptiler.com/cloud/). Once you have the API key, you can set it as an environment variable in your notebook or script as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# os.environ[\"MAPTILER_KEY\"] = \"YOUR_API_KEY\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"MAPTILER_KEY = os.environ.get(\"MAPTILER_KEY\")\n",
"style = f\"https://api.maptiler.com/maps/streets/style.json?key={MAPTILER_KEY}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = leafmap.Map(center=[-1.80921, 0.349419], zoom=3, style=style)\n",
"image = \"https://upload.wikimedia.org/wikipedia/commons/7/7c/201408_cat.png\"\n",
"source = {\n",
" \"type\": \"geojson\",\n",
" \"data\": {\n",
" \"type\": \"FeatureCollection\",\n",
" \"features\": [\n",
" {\"type\": \"Feature\", \"geometry\": {\"type\": \"Point\", \"coordinates\": [0, 0]}}\n",
" ],\n",
" },\n",
"}\n",
"\n",
"layer = {\n",
" \"id\": \"points\",\n",
" \"type\": \"symbol\",\n",
" \"source\": \"point\",\n",
" \"layout\": {\n",
" \"icon-image\": \"cat\",\n",
" \"icon-size\": 0.25,\n",
" \"text-field\": \"I love kitty!\",\n",
" \"text-font\": [\"Open Sans Regular\"],\n",
" \"text-offset\": [0, 3],\n",
" \"text-anchor\": \"top\",\n",
" },\n",
"}\n",
"m.add_image(\"cat\", image)\n",
"m.add_source(\"point\", source)\n",
"m.add_layer(layer)\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](https://i.imgur.com/Nq1uV9d.png)"
]
}
],
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
146 changes: 146 additions & 0 deletions docs/maplibre/add_image_generated.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![image](https://jupyterlite.rtfd.io/en/latest/_static/badge.svg)](https://demo.leafmap.org/lab/index.html?path=maplibre/add_image_generated.ipynb)\n",
"[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/leafmap/blob/master/docs/maplibre/add_image_generated.ipynb)\n",
"[![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/opengeos/leafmap/HEAD)\n",
"\n",
"**Add a generated icon to the map**\n",
"\n",
"This source code of this example is adapted from the MapLibre GL JS example - [Add a generated icon to the map](https://maplibre.org/maplibre-gl-js/docs/examples/add-image-generated).\n",
"\n",
"Uncomment the following line to install [leafmap](https://leafmap.org) if needed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %pip install \"leafmap[maplibre]\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import numpy as np\n",
"import leafmap.maplibregl as leafmap"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To run this notebook, you will need an [API key](https://docs.maptiler.com/cloud/api/authentication-key/) from [MapTiler](https://www.maptiler.com/cloud/). Once you have the API key, you can set it as an environment variable in your notebook or script as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# os.environ[\"MAPTILER_KEY\"] = \"YOUR_API_KEY\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"MAPTILER_KEY = os.environ.get(\"MAPTILER_KEY\")\n",
"style = f\"https://api.maptiler.com/maps/streets/style.json?key={MAPTILER_KEY}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Generate the icon data\n",
"width = 64 # The image will be 64 pixels square\n",
"height = 64\n",
"bytes_per_pixel = 4 # Each pixel is represented by 4 bytes: red, green, blue, and alpha\n",
"data = np.zeros((width, width, bytes_per_pixel), dtype=np.uint8)\n",
"\n",
"for x in range(width):\n",
" for y in range(width):\n",
" data[y, x, 0] = int((y / width) * 255) # red\n",
" data[y, x, 1] = int((x / width) * 255) # green\n",
" data[y, x, 2] = 128 # blue\n",
" data[y, x, 3] = 255 # alpha\n",
"\n",
"# Flatten the data array\n",
"flat_data = data.flatten()\n",
"\n",
"# Create the image dictionary\n",
"image_dict = {\n",
" \"width\": width,\n",
" \"height\": height,\n",
" \"data\": flat_data.tolist(),\n",
"}\n",
"\n",
"m = leafmap.Map(center=[0, 0], zoom=1, style=style)\n",
"m.add_image(\"gradient\", image_dict)\n",
"source = {\n",
" \"type\": \"geojson\",\n",
" \"data\": {\n",
" \"type\": \"FeatureCollection\",\n",
" \"features\": [\n",
" {\"type\": \"Feature\", \"geometry\": {\"type\": \"Point\", \"coordinates\": [0, 0]}}\n",
" ],\n",
" },\n",
"}\n",
"\n",
"layer = {\n",
" \"id\": \"points\",\n",
" \"type\": \"symbol\",\n",
" \"source\": \"point\",\n",
" \"layout\": {\"icon-image\": \"gradient\"},\n",
"}\n",
"\n",
"m.add_source(\"point\", source)\n",
"m.add_layer(layer)\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![](https://i.imgur.com/qWWlnAm.png)"
]
}
],
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit 70f8e86

Please sign in to comment.