From 3ad0f6666da6301e22a2d0b3f88a6561685b14ed Mon Sep 17 00:00:00 2001 From: Qiusheng Wu Date: Sat, 2 Nov 2024 22:44:59 -0400 Subject: [PATCH] Add support for visualizing Fields of The World --- docs/maplibre/fields_of_the_world.ipynb | 140 ++++++++++++++++++++++++ docs/maplibre/overview.md | 6 + leafmap/maplibregl.py | 8 +- mkdocs.yml | 1 + 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 docs/maplibre/fields_of_the_world.ipynb diff --git a/docs/maplibre/fields_of_the_world.ipynb b/docs/maplibre/fields_of_the_world.ipynb new file mode 100644 index 0000000000..193bbdbfa8 --- /dev/null +++ b/docs/maplibre/fields_of_the_world.ipynb @@ -0,0 +1,140 @@ +{ + "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/fields_of_the_world.ipynb)\n", + "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/leafmap/blob/master/docs/maplibre/fields_of_the_world.ipynb)\n", + "[![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/opengeos/leafmap/HEAD)\n", + "\n", + "**Visualizing Agricultural Field Boundary Dataset - Field of the World**\n", + "\n", + "Fields of The World (FTW) is a comprehensive benchmark dataset designed to enhance the development of machine learning models for instance segmentation of agricultural field boundaries. It aggregates and harmonizes a number of open datasets into 1.6 million parcel boundaries and over 70,000 samples covering diverse agricultural landscapes across 4 continents and 24 countries.\n", + "\n", + "For more information about Field of the World, please visit: https://fieldsofthe.world\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": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import leafmap.maplibregl as leafmap" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "layer names: ['ftw-sources']\n", + "bounds: [-46.3979962, -34.3992236, 109.0434154, 62.8408051]\n" + ] + } + ], + "source": [ + "url = \"https://data.source.coop/kerner-lab/fields-of-the-world/ftw-sources.pmtiles\"\n", + "metadata = leafmap.pmtiles_metadata(url)\n", + "print(f\"layer names: {metadata['layer_names']}\")\n", + "print(f\"bounds: {metadata['bounds']}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "5406aaaa7ce947eab30b204bcc524268", + "version_major": 2, + "version_minor": 1 + }, + "text/plain": [ + "Map(height='600px', map_options={'bearing': 0, 'center': (0, 20), 'pitch': 0, 'style': 'https://basemaps.carto…" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "m = leafmap.Map()\n", + "url = \"https://data.source.coop/kerner-lab/fields-of-the-world/ftw-sources.pmtiles\"\n", + "style = {\n", + " \"layers\": [\n", + " {\n", + " \"id\": \"Field Polygon\",\n", + " \"source\": \"example_source\",\n", + " \"source-layer\": \"ftw-sources\",\n", + " \"type\": \"fill\",\n", + " \"paint\": {\n", + " \"fill-color\": \"#ffff00\",\n", + " \"fill-opacity\": 0.2,\n", + " },\n", + " },\n", + " {\n", + " \"id\": \"Field Outline\",\n", + " \"source\": \"example_source\",\n", + " \"source-layer\": \"ftw-sources\",\n", + " \"type\": \"line\",\n", + " \"paint\": {\"line-color\": \"#ff0000\", \"line-width\": 1, \"line-opacity\": 1},\n", + " },\n", + " ],\n", + "}\n", + "\n", + "m.add_basemap(\"Satellite\")\n", + "m.add_pmtiles(url, style=style, name=\"FTW\", zoom_to_layer=False)\n", + "m.add_layer_control()\n", + "m" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![image](https://github.com/user-attachments/assets/933264d9-378b-4943-9611-0e105b250f63)" + ] + } + ], + "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 +} diff --git a/docs/maplibre/overview.md b/docs/maplibre/overview.md index 1ea8ad7ac9..cdcf581c32 100644 --- a/docs/maplibre/overview.md +++ b/docs/maplibre/overview.md @@ -236,6 +236,12 @@ Use the draw control to draw features on the map. [![](https://i.imgur.com/w8UFssd.png)](https://leafmap.org/maplibre/draw_features) +## Visualize fields of the world + +Visualize Agricultural Field Boundary Dataset - Field of the World. + +[![](https://i.imgur.com/0A9yuyL.png)](https://leafmap.org/maplibre/fields_of_the_world) + ## Use a fallback image Use a coalesce expression to display another image when a requested image is not available. diff --git a/leafmap/maplibregl.py b/leafmap/maplibregl.py index 35d8a8c698..f1567e93c4 100644 --- a/leafmap/maplibregl.py +++ b/leafmap/maplibregl.py @@ -1855,7 +1855,13 @@ def add_pmtiles( layer = Layer(**params) self.add_layer(layer) self.set_visibility(params["id"], visible) - self.set_opacity(params["id"], opacity) + if "paint" in params: + for key in params["paint"]: + if "opacity" in key: + self.set_opacity(params["id"], params["paint"][key]) + break + else: + self.set_opacity(params["id"], opacity) if tooltip: self.add_tooltip(params["id"], properties, template) diff --git a/mkdocs.yml b/mkdocs.yml index 4c4267192b..27cdcb2ac9 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -182,6 +182,7 @@ nav: - maplibre/drag_a_marker.ipynb - maplibre/draw_features.ipynb - maplibre/fallback_image.ipynb + - maplibre/fields_of_the_world.ipynb - maplibre/fit_bounds.ipynb - maplibre/fill_pattern.ipynb - maplibre/fly_to.ipynb