Skip to content

Instantly share code, notes, and snippets.

@prl900
Created April 6, 2018 13:12
Show Gist options
  • Save prl900/b89103197fe3785e65fe2dc6361ae6bb to your computer and use it in GitHub Desktop.
Save prl900/b89103197fe3785e65fe2dc6361ae6bb to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1490.6950122649223, 'wide')\n",
"(1179.3851185609158, 'high')\n",
"(1280, 1536, 3)\n",
"(0, 'of', 6, '...')\n",
"(1, 'of', 6, '...')\n",
"(2, 'of', 6, '...')\n",
"(3, 'of', 6, '...')\n",
"(4, 'of', 6, '...')\n",
"(5, 'of', 6, '...')\n",
"((256, 256, 3), dtype('uint8'))\n",
"((1280, 1536, 3), dtype('uint8'))\n"
]
}
],
"source": [
"%matplotlib inline\n",
"\n",
"from matplotlib.pyplot import imshow, imsave\n",
"import requests\n",
"from PIL import Image\n",
"from StringIO import StringIO\n",
"import numpy as np\n",
"import math\n",
"\n",
"url = 'http://gsky.nci.org.au/ows?time=2017-09-04T00%3A00%3A00.000Z&srs=EPSG%3A3857&transparent=true&format=image%2Fpng&exceptions=application%2Fvnd.ogc.se_xml&styles=&tiled=true&feature_count=101&service=WMS&version=1.1.1&request=GetMap&layers=LS8%3ANBART%3AFALSE&width=256&height=256&bbox={}%2C{}%2C{}%2C{}'\n",
"\n",
"# EPSG 3857 BBox (Use http://epsg.io)\n",
"x = [14122668, 14578448]\n",
"y = [-1583412, -1944009]\n",
"\n",
"pix_size = 305.75 # metres\n",
"tile_size = 256 # pixels\n",
"\n",
"# Print image size in pixels\n",
"print((x[1]-x[0])/pix_size, \"wide\")\n",
"print((y[0]-y[1])/pix_size, \"high\")\n",
"\n",
"x_tiles = int(math.ceil(float((x[1]-x[0])/pix_size)/tile_size))\n",
"y_tiles = int(math.ceil(float((y[0]-y[1])/pix_size)/tile_size))\n",
"\n",
"canvas = np.zeros((y_tiles*tile_size, x_tiles*tile_size, 3), dtype='uint8')\n",
"print(canvas.shape)\n",
"\n",
"for i in range(x_tiles):\n",
" print(i, \"of\", x_tiles, \"...\")\n",
" for j in range(y_tiles):\n",
" x0 = x[0] + i*tile_size*pix_size\n",
" x1 = x[0] + (i+1)*tile_size*pix_size\n",
" y0 = y[0] - j*tile_size*pix_size\n",
" y1 = y[0] - (j+1)*tile_size*pix_size\n",
" \n",
" r = requests.get(url.format(x0,y1,x1,y0))\n",
" \n",
" im = Image.open(StringIO(r.content))\n",
" arr = np.asarray(im)\n",
" \n",
" canvas[j*tile_size:(j+1)*tile_size, i*tile_size:(i+1)*tile_size, :] = arr[:, :, :3]\n",
"\n",
"print(arr.shape, arr.dtype)\n",
"print(canvas.shape, canvas.dtype)\n",
"\n",
"imsave(\"poster.png\", canvas)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment