Skip to content

Basemap selectors #985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/access-data.json
Original file line number Diff line number Diff line change
@@ -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"
}

}
74 changes: 74 additions & 0 deletions examples/basemaps_with_apikey.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
76 changes: 76 additions & 0 deletions examples/basemaps_without_apikey.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
74 changes: 74 additions & 0 deletions examples/gallery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import xyzservices.providers as basemaps
import 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
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


# Distribute the basemaps between 2 lists : the one with apikey and the one without
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


# Select one basemap from the list given on index
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