Skip to content

Commit

Permalink
Create CustomTileServer.ipynb
Browse files Browse the repository at this point in the history
  • Loading branch information
deeplook authored Jun 11, 2019
1 parent 7b37efd commit 4d304df
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions examples/CustomTileServer.ipynb
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
}

0 comments on commit 4d304df

Please sign in to comment.