You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
12 KiB
253 lines
12 KiB
{ |
|
"cells": [ |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 1, |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"import folium\n", |
|
"import matplotlib\n", |
|
"import numpy as np\n", |
|
"import branca\n", |
|
"from matplotlib.pyplot import imread\n", |
|
"from matplotlib.colors import Normalize\n", |
|
"from matplotlib.colors import ListedColormap\n", |
|
"from folium import raster_layers" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 2, |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"image = '/notebooks/resources/gpm/gpm_1d.20190531.tif'" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 3, |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"10.0 -180.0\n", |
|
"-60.0 -180.0\n", |
|
"-60.0 -30.0\n", |
|
"10.0 -30.0\n", |
|
"ext = [[10.0, -180.0], [-60.0, -180.0], [-60.0, -30.0], [10.0, -30.0]]\n", |
|
"geo_ext = [[10.0, -180.0], [-60.0, -180.0], [-60.0, -30.0], [10.0, -30.0]]\n" |
|
] |
|
} |
|
], |
|
"source": [ |
|
"from osgeo import gdal,ogr,osr\n", |
|
"\n", |
|
"def GetExtent(gt,cols,rows):\n", |
|
" ''' Return list of corner coordinates from a geotransform\n", |
|
"\n", |
|
" @type gt: C{tuple/list}\n", |
|
" @param gt: geotransform\n", |
|
" @type cols: C{int}\n", |
|
" @param cols: number of columns in the dataset\n", |
|
" @type rows: C{int}\n", |
|
" @param rows: number of rows in the dataset\n", |
|
" @rtype: C{[float,...,float]}\n", |
|
" @return: coordinates of each corner\n", |
|
" '''\n", |
|
" ext=[]\n", |
|
" xarr=[0,cols]\n", |
|
" yarr=[0,rows]\n", |
|
"\n", |
|
" for px in xarr:\n", |
|
" for py in yarr:\n", |
|
" x=gt[0]+(px*gt[1])+(py*gt[2])\n", |
|
" y=gt[3]+(px*gt[4])+(py*gt[5])\n", |
|
" ext.append([y,x])\n", |
|
" print (y,x)\n", |
|
" yarr.reverse()\n", |
|
" return ext\n", |
|
"\n", |
|
"def ReprojectCoords(coords,src_srs,tgt_srs):\n", |
|
" ''' Reproject a list of x,y coordinates.\n", |
|
"\n", |
|
" @type geom: C{tuple/list}\n", |
|
" @param geom: List of [[x,y],...[x,y]] coordinates\n", |
|
" @type src_srs: C{osr.SpatialReference}\n", |
|
" @param src_srs: OSR SpatialReference object\n", |
|
" @type tgt_srs: C{osr.SpatialReference}\n", |
|
" @param tgt_srs: OSR SpatialReference object\n", |
|
" @rtype: C{tuple/list}\n", |
|
" @return: List of transformed [[x,y],...[x,y]] coordinates\n", |
|
" '''\n", |
|
" trans_coords=[]\n", |
|
" transform = osr.CoordinateTransformation( src_srs, tgt_srs)\n", |
|
" for x,y in coords:\n", |
|
" x,y,z = transform.TransformPoint(x,y)\n", |
|
" trans_coords.append([x,y])\n", |
|
" return trans_coords\n", |
|
"\n", |
|
"raster=image\n", |
|
"ds=gdal.Open(raster)\n", |
|
"\n", |
|
"gt=ds.GetGeoTransform()\n", |
|
"cols = ds.RasterXSize\n", |
|
"rows = ds.RasterYSize\n", |
|
"\n", |
|
"ext=GetExtent(gt,cols,rows)\n", |
|
"print(\"ext = \" + str(ext))\n", |
|
"\n", |
|
"src_srs=osr.SpatialReference()\n", |
|
"src_srs.ImportFromWkt(ds.GetProjection())\n", |
|
"# tgt_srs=osr.SpatialReference()\n", |
|
"# tgt_srs.ImportFromEPSG(3857)\n", |
|
"tgt_srs = src_srs.CloneGeogCS()\n", |
|
"\n", |
|
"geo_ext=ReprojectCoords(ext,src_srs,tgt_srs)\n", |
|
"print(\"geo_ext = \" + str(geo_ext))" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 4, |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"# # Choose colormap\n", |
|
"# cmap = matplotlib.cm.cool\n", |
|
"\n", |
|
"# # Get the colormap colors\n", |
|
"# my_cmap = cmap(np.arange(cmap.N))\n", |
|
"\n", |
|
"\n", |
|
"# # Set alpha\n", |
|
"# my_cmap[:,-1] = np.linspace(0, 1, cmap.N)\n", |
|
"\n", |
|
"# # Create new colormap\n", |
|
"# my_cmap = ListedColormap(my_cmap)" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 5, |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"# # Choose colormap\n", |
|
"# cmap = matplotlib.cm.cool\n", |
|
"\n", |
|
"# min_x = 0\n", |
|
"# max_x = 256\n", |
|
"\n", |
|
"# def my_cmap(x):\n", |
|
"# x = np.clip((x - min_x)/(max_x - min_x), 0, 1)\n", |
|
"# c = cmap(x)\n", |
|
"# # multiplying alpha by 10 to make a sharper transition to the colors in the lower range\n", |
|
"# return c[:3] + (np.clip(x*10, 0, 1),) " |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 10, |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"colormap = branca.colormap.StepColormap(\n", |
|
" ['#64abb0','#9dd3a7', '#c7e9ad', '#edf8b9', '#ffedaa', '#fec980', '#f99e59', '#e85b3a', '#d7191c'],\n", |
|
" vmin=0, vmax=10000,\n", |
|
" index=[0, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000],\n", |
|
" caption='step'\n", |
|
")\n", |
|
"\n", |
|
"my_cmap = colormap\n", |
|
"\n", |
|
"# # Get the colormap colors\n", |
|
"# my_cmap = colormap(np.arange(colormap.N))\n", |
|
"\n", |
|
"\n", |
|
"# # Set alpha\n", |
|
"# my_cmap[:,-1] = np.linspace(0, 1, colormap.N)\n", |
|
"\n", |
|
"# # Create new colormap\n", |
|
"# my_cmap = ListedColormap(my_cmap)" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 11, |
|
"metadata": { |
|
"scrolled": false |
|
}, |
|
"outputs": [ |
|
{ |
|
"ename": "IndexError", |
|
"evalue": "tuple index out of range", |
|
"output_type": "error", |
|
"traceback": [ |
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
|
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", |
|
"\u001b[0;32m<ipython-input-11-1cdfaf3b95d4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m,\u001b[0m \u001b[0mbounds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mext\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mext\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;34m,\u001b[0m \u001b[0mmercator_project\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0;34m,\u001b[0m \u001b[0mcolormap\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmy_cmap\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m )\n\u001b[1;32m 19\u001b[0m )\n", |
|
"\u001b[0;32m/usr/local/lib/python3.5/dist-packages/folium/raster_layers.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, image, bounds, origin, colormap, mercator_project, pixelated, name, overlay, control, show, **kwargs)\u001b[0m\n\u001b[1;32m 274\u001b[0m origin=origin)\n\u001b[1;32m 275\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 276\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0murl\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mimage_to_url\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0morigin\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0morigin\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolormap\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcolormap\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 277\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 278\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbounds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdumps\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbounds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
|
"\u001b[0;32m/usr/local/lib/python3.5/dist-packages/folium/utilities.py\u001b[0m in \u001b[0;36mimage_to_url\u001b[0;34m(image, colormap, origin)\u001b[0m\n\u001b[1;32m 109\u001b[0m \u001b[0murl\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'data:image/{};base64,{}'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfileformat\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb64encoded\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 110\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0;34m'ndarray'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mimage\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__class__\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__name__\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 111\u001b[0;31m \u001b[0mimg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwrite_png\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0morigin\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0morigin\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcolormap\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcolormap\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 112\u001b[0m \u001b[0mb64encoded\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbase64\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mb64encode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdecode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'utf-8'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 113\u001b[0m \u001b[0murl\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'data:image/png;base64,{}'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mb64encoded\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
|
"\u001b[0;32m/usr/local/lib/python3.5/dist-packages/folium/utilities.py\u001b[0m in \u001b[0;36mwrite_png\u001b[0;34m(data, origin, colormap)\u001b[0m\n\u001b[1;32m 172\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mnblayers\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 173\u001b[0m \u001b[0marr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcolormap\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0marr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mravel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 174\u001b[0;31m \u001b[0mnblayers\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 175\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mnblayers\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 176\u001b[0m raise ValueError('colormap must provide colors of r'\n", |
|
"\u001b[0;31mIndexError\u001b[0m: tuple index out of range" |
|
] |
|
} |
|
], |
|
"source": [ |
|
"m = folium.Map(\n", |
|
" location = [-22, -114]\n", |
|
" , zoom_start = 2\n", |
|
" , control_scale = True \n", |
|
" , tiles = 'Stamen Terrain'\n", |
|
")\n", |
|
"\n", |
|
"data = matplotlib.pyplot.imread(image)\n", |
|
"\n", |
|
"# Image bounds on the map in the form\n", |
|
"# [[lat_min, lon_min], [lat_max, lon_max]]\n", |
|
"m.add_child(raster_layers.ImageOverlay(\n", |
|
" data\n", |
|
" , opacity = 0.7\n", |
|
" , bounds = [ext[2], ext[0]]\n", |
|
" , mercator_project = True\n", |
|
" , colormap = my_cmap\n", |
|
")\n", |
|
" )\n", |
|
"\n", |
|
"m" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": null, |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [] |
|
} |
|
], |
|
"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.5.2" |
|
} |
|
}, |
|
"nbformat": 4, |
|
"nbformat_minor": 2 |
|
}
|
|
|