From 243564c1bce3d9347f0b1ef2ce23814c75dea735 Mon Sep 17 00:00:00 2001 From: Florence Haudin Date: Tue, 24 May 2022 13:10:07 +0200 Subject: [PATCH 1/2] Propose in examples 2 notebooks basemaps_with_apikey.ipynb and basemaps_without_apikey.ipynb and a script gallery.py to easily aiming at selecting basemaps among a list and display them. Each notebook deals with the case with and without apikey. --- examples/access-data.json | 34 ++++++++++++ examples/basemaps_with_apikey.ipynb | 74 +++++++++++++++++++++++++ examples/basemaps_without_apikey.ipynb | 76 ++++++++++++++++++++++++++ examples/gallery.py | 71 ++++++++++++++++++++++++ 4 files changed, 255 insertions(+) create mode 100644 examples/access-data.json create mode 100644 examples/basemaps_with_apikey.ipynb create mode 100644 examples/basemaps_without_apikey.ipynb create mode 100644 examples/gallery.py diff --git a/examples/access-data.json b/examples/access-data.json new file mode 100644 index 000000000..f56378575 --- /dev/null +++ b/examples/access-data.json @@ -0,0 +1,34 @@ +{"Thunderforest": { + "keyString":"apikey", + "name": "Thunderforest" + }, + "OpenWeatherMap": { + "keyString":"apiKey", + "name": "OpenWeatherMap" + }, + "MapTiler": { + "keyString": "key", + "name": "MapTiler" + }, + "MapBox": { + "keyString": "accessToken", + "name": "MapBox" + }, + "Jawg": { + "keyString": "accessToken", + "name": "Jawg" + }, + "TomTom": { + "keyString": "apikey", + "name": "TomTom" + }, + "HEREv3": { + "keyString": "apiKey", + "name": "HEREv3" + }, + "AzureMaps": { + "keyString": "subscriptionKey", + "name": "AzureMaps" + } + +} diff --git a/examples/basemaps_with_apikey.ipynb b/examples/basemaps_with_apikey.ipynb new file mode 100644 index 000000000..b3b685588 --- /dev/null +++ b/examples/basemaps_with_apikey.ipynb @@ -0,0 +1,74 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "22369c05-6bb4-4072-803b-b074e6a0c8d8", + "metadata": {}, + "outputs": [], + "source": [ + "from ipyleaflet import Map, LayersControl, WidgetControl\n", + "import gallery\n", + "import ipywidgets as widgets\n", + "import json\n", + "access_data = json.load(open(\"access-data.json\"))\n", + "\n", + "center = [38.128, 2.588]\n", + "zoom = 3\n", + "m = Map(center=center, zoom=zoom)\n", + "withoutApikeyList,withApikeyList = gallery.define_basemapsList()\n", + "index = 43\n", + "basemap, apiname = gallery.define_basemap_from_list(withApikeyList , index)\n", + "\n", + "keyString = access_data[apiname][\"keyString\"]\n", + "print(withApikeyList[index])\n", + "apikey_widget = widgets.Password(\n", + " value='',\n", + " placeholder='Type something',\n", + " description='apikey:',\n", + " )\n", + "display(apikey_widget) " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "69b355db-b90a-4535-8122-7f133e853683", + "metadata": {}, + "outputs": [], + "source": [ + "basemap[keyString] = apikey_widget.value\n", + "display(Map(basemap=basemap, center=center, zoom=zoom))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a3344d9f-4dfc-4a87-8246-f3cecfc324af", + "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.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/basemaps_without_apikey.ipynb b/examples/basemaps_without_apikey.ipynb new file mode 100644 index 000000000..0f7f1045c --- /dev/null +++ b/examples/basemaps_without_apikey.ipynb @@ -0,0 +1,76 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "7739aba6", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "ba862bc2bba44381859faaf4eb624d99", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Map(center=[38.128, 2.588], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title', 'zoom_…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "from ipyleaflet import Map, basemap_to_tiles, WidgetControl\n", + "import gallery\n", + "import ipywidgets as widgets\n", + "center = [38.128, 2.588]\n", + "zoom = 3\n", + "m = Map(center=center, zoom=zoom)\n", + "layersWithoutApikey = gallery.define_basemapsDict()\n", + "\n", + "\n", + "layer_selector = widgets.Dropdown(\n", + " options=layersWithoutApikey,\n", + " description='Layer :'\n", + ")\n", + "widgets.link((layer_selector,'value'),(m,'layers'))\n", + "\n", + "layer_selector_control = WidgetControl(widget=layer_selector, position='topright')\n", + "\n", + "m.add_control(layer_selector_control)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ba69e289-68ef-47e1-8afa-76ac0ee3bf49", + "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.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/gallery.py b/examples/gallery.py new file mode 100644 index 000000000..07888ac64 --- /dev/null +++ b/examples/gallery.py @@ -0,0 +1,71 @@ +import xyzservices.providers as basemaps +import json +access_data = json.load(open("access-data.json")) +from ipyleaflet import basemap_to_tiles + +## Using a dict of basemaps to use Dropdown widget for basemaps that don't require apikey +def define_basemapsDict(): + WithoutApikeyDict = {} + + for [key, val] in basemaps.items(): + if ('url' in val): + apiname = val.name + if (apiname in access_data): + pass + else: + WithoutApikeyDict[apiname] = (basemap_to_tiles(basemaps[apiname]),) + + else: + newdata = basemaps[key] + + + for newval in newdata.values(): + basemap_name = newval.name + apiname = basemap_name.split('.')[0] + subname = basemap_name.split('.')[1] + + if (apiname == 'HERE'): + pass + elif (apiname in access_data): + pass + else: + WithoutApikeyDict[basemap_name] = (basemap_to_tiles(basemaps[apiname][subname]),) + return WithoutApikeyDict + +def define_basemapsList(): + WithApikeyList = [] + WithoutApikeyList = [] + for [key, val] in basemaps.items(): + if ('url' in val): + apiname = val.name + if (apiname in access_data): + WithApikeyList.append(val.name) + else: + WithoutApikeyList.append(val.name) + else: + newdata = basemaps[key] + + for newval in newdata.values(): + basemap_name = newval.name + apiname = basemap_name.split('.')[0] + + if (apiname == 'HERE'): + pass + elif (apiname in access_data): + WithApikeyList.append(newval.name) + else: + WithoutApikeyList.append(newval.name) + return WithoutApikeyList, WithApikeyList + +def define_basemap_from_list(List, index) : + basemap_name = List[index] + + if ('.' in basemap_name): + apiname = basemap_name.split('.')[0] + subname = basemap_name.split('.')[1] + basemap = basemaps[apiname][subname] + else: + basemap = basemaps[apiname] + + return basemap , apiname + From c278b62f628a65c6f5171cfebae0f6708ab0373c Mon Sep 17 00:00:00 2001 From: Florence Haudin Date: Wed, 25 May 2022 16:53:01 +0200 Subject: [PATCH 2/2] Fix code formatting in gallery.py. --- examples/gallery.py | 49 ++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/examples/gallery.py b/examples/gallery.py index 07888ac64..9748f7297 100644 --- a/examples/gallery.py +++ b/examples/gallery.py @@ -1,9 +1,10 @@ import xyzservices.providers as basemaps import json -access_data = json.load(open("access-data.json")) from ipyleaflet import basemap_to_tiles +access_data = json.load(open("access-data.json")) -## Using a dict of basemaps to use Dropdown widget for basemaps that don't require apikey + +# Using a dict of basemaps to use Dropdown widget for basemaps that don't require apikey def define_basemapsDict(): WithoutApikeyDict = {} @@ -18,20 +19,21 @@ def define_basemapsDict(): else: newdata = basemaps[key] - for newval in newdata.values(): - basemap_name = newval.name - apiname = basemap_name.split('.')[0] - subname = basemap_name.split('.')[1] + basemap_name = newval.name + apiname = basemap_name.split('.')[0] + subname = basemap_name.split('.')[1] - if (apiname == 'HERE'): - pass - elif (apiname in access_data): - pass - else: - WithoutApikeyDict[basemap_name] = (basemap_to_tiles(basemaps[apiname][subname]),) + if (apiname == 'HERE'): + pass + elif (apiname in access_data): + pass + else: + WithoutApikeyDict[basemap_name] = (basemap_to_tiles(basemaps[apiname][subname]),) return WithoutApikeyDict + +# Distribute the basemaps between 2 lists : the one with apikey and the one without def define_basemapsList(): WithApikeyList = [] WithoutApikeyList = [] @@ -46,18 +48,20 @@ def define_basemapsList(): newdata = basemaps[key] for newval in newdata.values(): - basemap_name = newval.name - apiname = basemap_name.split('.')[0] + basemap_name = newval.name + apiname = basemap_name.split('.')[0] - if (apiname == 'HERE'): - pass - elif (apiname in access_data): - WithApikeyList.append(newval.name) - else: - WithoutApikeyList.append(newval.name) + if (apiname == 'HERE'): + pass + elif (apiname in access_data): + WithApikeyList.append(newval.name) + else: + WithoutApikeyList.append(newval.name) return WithoutApikeyList, WithApikeyList -def define_basemap_from_list(List, index) : + +# Select one basemap from the list given on index +def define_basemap_from_list(List, index): basemap_name = List[index] if ('.' in basemap_name): @@ -67,5 +71,4 @@ def define_basemap_from_list(List, index) : else: basemap = basemaps[apiname] - return basemap , apiname - + return basemap, apiname