Skip to content

Instantly share code, notes, and snippets.

@deeplook
Created December 9, 2021 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deeplook/8452c3afa6cd692ed812364c189e8fd1 to your computer and use it in GitHub Desktop.
Save deeplook/8452c3afa6cd692ed812364c189e8fd1 to your computer and use it in GitHub Desktop.
Testing ipyleaflt map.fit_bounds(), setting zoom level too low
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "2aeb6e9f-8471-447e-b5b5-e9688ebafd2d",
"metadata": {},
"source": [
"# Check map zoom level after fit_bounds()"
]
},
{
"cell_type": "markdown",
"id": "852cf6e7-8330-4070-bf91-0a644cc5ec8a",
"metadata": {
"tags": []
},
"source": [
"## Load data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e65753b6-fee7-42db-8d5e-f9924abd5534",
"metadata": {},
"outputs": [],
"source": [
"import geojson\n",
"import requests"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a769297e-e74a-4dfc-926b-c6c0e079f765",
"metadata": {},
"outputs": [],
"source": [
"def bounds(feat):\n",
" bounds = geojson.utils.coords(feat)\n",
" lon, lat = next(bounds)\n",
" east, west, north, south = lon, lon, lat, lat\n",
" for lon, lat in bounds:\n",
" east = lon if lon > east else east\n",
" west = lon if lon < west else west\n",
" north = lat if lat > north else north\n",
" south = lat if lat < south else south\n",
" return [[south, west], [north, east]]\n",
"\n",
"countries = requests.get(\n",
" \"https://raw.githubusercontent.com/johan/world.geo.json\"\n",
" \"/master/countries.geo.json\"\n",
").json()"
]
},
{
"cell_type": "markdown",
"id": "7b117cb2-163f-4554-a8cf-8ae64de39126",
"metadata": {
"tags": []
},
"source": [
"## Simple, zoom level too low"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "65e5a908-9e2f-42bc-a26f-716c8c1593fc",
"metadata": {},
"outputs": [],
"source": [
"from ipyleaflet import basemaps, GeoJSON, Map\n",
"\n",
"m = Map(basemap=basemaps.CartoDB.Positron)\n",
"feat = countries[\"features\"][0]\n",
"m += GeoJSON(data=feat)\n",
"bnds = bounds(feat)\n",
"print(feat[\"properties\"][\"name\"])\n",
"m.fit_bounds(bnds)\n",
"m"
]
},
{
"cell_type": "markdown",
"id": "0cafe5a1-fa7b-438d-8240-2c03a932371b",
"metadata": {
"tags": []
},
"source": [
"## interact(), zoom level too low"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "02fd9c2a-48f5-459b-a051-d1a38aeb6209",
"metadata": {},
"outputs": [],
"source": [
"from ipyleaflet import basemaps, GeoJSON, Map\n",
"from ipywidgets import interact\n",
"\n",
"@interact\n",
"def run(index=(0, len(countries[\"features\"])-1)):\n",
" m = Map(basemap=basemaps.CartoDB.Positron)\n",
" feat = countries[\"features\"][index]\n",
" m += GeoJSON(data=feat)\n",
" bnds = bounds(feat)\n",
" print(feat[\"properties\"][\"name\"])\n",
" m.fit_bounds(bnds)\n",
" return m"
]
},
{
"cell_type": "markdown",
"id": "c8b58982-a97a-433c-8acf-de4d49539c9b",
"metadata": {
"tags": []
},
"source": [
"## Play widget, zoom level ok"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1cb6b49f-68b8-483b-a403-8de5039ecb55",
"metadata": {},
"outputs": [],
"source": [
"from ipyleaflet import basemaps, GeoJSON, Map, WidgetControl\n",
"from ipywidgets import HBox, Play, Text\n",
"\n",
"def changed(event):\n",
" global m, prev_gj\n",
" if event[\"name\"] != \"value\":\n",
" return\n",
" if prev_gj:\n",
" m -= prev_gj\n",
" i = event[\"owner\"].value\n",
" feat = countries[\"features\"][i]\n",
" cc_name = feat[\"properties\"][\"name\"]\n",
" tx.value = f\"{i}: {cc_name}\"\n",
" gj = GeoJSON(data=feat)\n",
" m += gj\n",
" prev_gj = gj\n",
" m.fit_bounds(bounds(feat))\n",
"\n",
"prev_gj = None\n",
"p = Play(min=0, max=len(countries[\"features\"])-1, interval=1000)\n",
"p.observe(changed)\n",
"tx = Text()\n",
"w = HBox([p, tx])\n",
"m = Map(zoom=1, basemap=basemaps.CartoDB.Positron)\n",
"m += WidgetControl(widget=w, position=\"bottomleft\", transparent_bg=True)\n",
"m"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment