forked from jupyter-widgets/ipyleaflet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Custom Tile Server\n", | ||
"\n", | ||
"This example implements a minimal map tile server that serves map tiles from two different basemaps in a chess board pattern." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from multiprocessing import Process\n", | ||
"\n", | ||
"from flask import Flask, Response\n", | ||
"from requests import get as fetch\n", | ||
"from ipyleaflet import Map, basemaps" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Build and Run a Tile Server" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"app = Flask(__name__)\n", | ||
"\n", | ||
"@app.route(\"/scramble/<int:z>/<int:x>/<int:y>\")\n", | ||
"def scramble(z, x, y):\n", | ||
" \"Serve scrambled tiles from two basemaps.\"\n", | ||
"\n", | ||
" urls = [basemaps.Esri[name]['url'] for name in ['WorldStreetMap', 'WorldTopoMap']]\n", | ||
" url = urls[(x + y) % 2].format(x=x, y=y, z=z)\n", | ||
" img = fetch(url).content\n", | ||
" return Response(img, mimetype='image/png')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"p = Process(target=app.run)\n", | ||
"p.start()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Show a \"Scrambled Tiles\" Map" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"url = 'http://localhost:5000/scramble/{z}/{x}/{y}'\n", | ||
"bm = {'url': url, 'attribution': 'Scrambled Tiles'}\n", | ||
"Map(center=(48.86, 2.34), level=10, basemap=bm)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Stop the Tile Server" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"p.kill()" | ||
] | ||
} | ||
], | ||
"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.7.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |