Skip to content

Instantly share code, notes, and snippets.

@invisiblefunnel
Last active May 30, 2018 06:54
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 invisiblefunnel/1712fae98307eb21b88ac70aee5a8eae to your computer and use it in GitHub Desktop.
Save invisiblefunnel/1712fae98307eb21b88ac70aee5a8eae to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import shutil\n",
"import time\n",
"\n",
"import diskcache as dc\n",
"import requests\n",
"\n",
"DATA_URL_TEMPLATE = 'https://tiles.sharedstreets.io/osm/planet-180312/{z}-{x}-{y}.{layer}.6.pbf'\n",
"CACHE_PATH = '~/.sharedstreets'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def get_raw_tile(url, cache_path=CACHE_PATH):\n",
" with dc.Cache(cache_path) as cache:\n",
" key = url.encode('ascii')\n",
" tile = cache.get(key)\n",
" if not tile:\n",
" resp = requests.get(url)\n",
" resp.raise_for_status()\n",
" tile = resp.content\n",
" cache.set(key, tile)\n",
" return tile"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 655 3.792465925216675\n",
"0 656 0.3620469570159912\n",
"0 657 0.9518589973449707\n",
"0 658 0.8505878448486328\n",
"0 659 0.48528313636779785\n",
"1 655 0.009367942810058594\n",
"1 656 0.0073740482330322266\n",
"1 657 0.006818056106567383\n",
"1 658 0.006678104400634766\n",
"1 659 0.007056236267089844\n"
]
}
],
"source": [
"shutil.rmtree(CACHE_PATH)\n",
"\n",
"for i in range(2):\n",
" for x in range(655, 660):\n",
" url = DATA_URL_TEMPLATE.format(x=x, y=1583, z=12, layer='geometry')\n",
" start = time.time()\n",
" get_raw_tile(url)\n",
" print(i, x, time.time() - start)"
]
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment