{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# pip install colorcet" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import folium\n", "import matplotlib\n", "import numpy as np\n", "import colorcet\n", "from matplotlib.pyplot import imread\n", "from matplotlib.colors import Normalize\n", "from matplotlib.colors import ListedColormap\n", "from folium import raster_layers\n", "from folium import plugins\n", "from folium import branca" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = '/notebooks/resources/gpm/gpm_1d.20190531.tif'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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": null, "metadata": {}, "outputs": [], "source": [ "!gdalinfo '/notebooks/resources/gpm/gpm_1d.20190531.tif'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# !gdal_edit -colorinterp_1 alpha /notebooks/resources/gpm/gpm_1d.20190531.tif" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Choose colormap\n", "cmap = colorcet.cm.fire\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)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "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 = lambda x: (1, 0, 0, x)\n", " , colormap = colorcet.cm.fire\n", "# , colormap = branca.colormap.linear.PuBuGn_07.scale(0,700)\n", "# , colormap = my_cmap\n", ")\n", " )\n", "\n", "folium.Marker(\n", " ext[2]\n", " , popup = str(ext[2])\n", " , tooltip = str(ext[2])\n", ").add_to(m)\n", "\n", "folium.Marker(\n", " ext[0]\n", " , popup = str(ext[0])\n", " , tooltip = str(ext[0])\n", ").add_to(m)\n", "\n", "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vars(m)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(data)\n", "print(data.shape)\n", "# data = data.transpose()\n", "# print(data)\n", "# print(data.shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# First: read the geotiff image with GDAL.\n", "from osgeo import gdal, osr\n", "\n", "gdal.UseExceptions()\n", "\n", "ds = gdal.Open(image)\n", "data = ds.ReadAsArray()\n", "gt = ds.GetGeoTransform()\n", "proj = ds.GetProjection()\n", "\n", "inproj = osr.SpatialReference()\n", "inproj.ImportFromWkt(proj)\n", "\n", "print('\\n\\n## ds ##:\\n\\n' + str(ds))\n", "print('\\n\\n## data ##:\\n\\n' + str(data))\n", "print('\\n\\n## gt ##:\\n\\n' + str(gt))\n", "print('\\n\\n## proj ##:\\n\\n' + str(proj))\n", "print('\\n\\n## inproj ##:\\n\\n' + str(inproj))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "q = lambda x: (0, 0, 0, 0)\n", "print(q(0))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmap = colorcet.cm.bmw\n", "\n", "rgba = cmap(0.5)\n", "print(rgba) # (0.99807766255210428, 0.99923106502084169, 0.74602077638401709, 1.0\n", "print(rgba[:-1])\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pylab as pl\n", "from matplotlib.colors import ListedColormap\n", "\n", "# Random data\n", "data1 = np.random.random((4,4))\n", "\n", "# Choose colormap\n", "cmap = pl.cm.RdBu\n", "\n", "# Get the colormap colors\n", "my_cmap = cmap(np.arange(cmap.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)\n", "\n", "pl.figure()\n", "pl.subplot(121)\n", "pl.pcolormesh(data1, cmap=pl.cm.RdBu)\n", "pl.colorbar()\n", "\n", "pl.subplot(122)\n", "pl.pcolormesh(data1, cmap=my_cmap)\n", "pl.colorbar()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Choose colormap\n", "cmap = colorcet.cm.fire\n", "\n", "# Get the colormap colors\n", "my_cmap = cmap(np.arange(cmap.N))\n", "\n", "print(my_cmap)\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)\n", "\n", "pl.figure()\n", "pl.subplot(121)\n", "pl.pcolormesh(data1, cmap=colorcet.cm.fire)\n", "pl.colorbar()\n", "\n", "pl.subplot(122)\n", "pl.pcolormesh(data1, cmap=my_cmap)\n", "pl.colorbar()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "010000" ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import branca\n", "# branca.colormap.linear.Spectral_04\n", "colormap = branca.colormap.linear.Spectral_04.scale(0, 10000)\n", "colormap = colormap.to_step(index=[0, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000])\n", "colormap.caption = 'mm'\n", "colormap" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "010000" ], "text/plain": [ "" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import branca\n", "\n", "\n", "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", "colormap" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "0.01.0" ], "text/plain": [ "" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cm.StepColormap(['#64abb0'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ftp://jsimpson.pps.eosdis.nasa.gov/NRTPUB/imerg/gis/README.GIS.pdf" ] } ], "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 }