{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# pip install colorcet" ] }, { "cell_type": "code", "execution_count": 9, "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": 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": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Driver: GTiff/GeoTIFF\r\n", "Files: /notebooks/resources/gpm/gpm_1d.20190531.tif\r\n", "Size is 1500, 700\r\n", "Coordinate System is:\r\n", "GEOGCS[\"WGS 84\",\r\n", " DATUM[\"WGS_1984\",\r\n", " SPHEROID[\"WGS 84\",6378137,298.2572326660159,\r\n", " AUTHORITY[\"EPSG\",\"7030\"]],\r\n", " AUTHORITY[\"EPSG\",\"6326\"]],\r\n", " PRIMEM[\"Greenwich\",0],\r\n", " UNIT[\"degree\",0.0174532925199433],\r\n", " AUTHORITY[\"EPSG\",\"4326\"]]\r\n", "Origin = (-180.000000000000000,10.000000000000000)\r\n", "Pixel Size = (0.100000000000000,-0.100000000000000)\r\n", "Metadata:\r\n", " AREA_OR_POINT=Area\r\n", " TIFFTAG_DATETIME=2019:06:01 13:35:13\r\n", " TIFFTAG_DOCUMENTNAME=/NRTPUB/imerg/gis/2019/05/3B-HHR-L.MS.MRG.3IMERG.20190531-S233000-E235959.1410.V06B.1day.tif\r\n", " TIFFTAG_IMAGEDESCRIPTION=DOI=10.5067/GPM/IMERG/3B-HH-L/06 DOIauthority=http://dx.doi.org/ DOIshortName=3IMERGHH_LATE Unit=0.1(mm) ScaleFactor=10\r\n", " TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)\r\n", " TIFFTAG_SOFTWARE=IDL 8.7.2, Harris Geospatial Solutions, Inc.\r\n", " TIFFTAG_XRESOLUTION=100\r\n", " TIFFTAG_YRESOLUTION=100\r\n", "Image Structure Metadata:\r\n", " COMPRESSION=LZW\r\n", " INTERLEAVE=BAND\r\n", "Corner Coordinates:\r\n", "Upper Left (-180.0000000, 10.0000000) (180d 0' 0.00\"W, 10d 0' 0.00\"N)\r\n", "Lower Left (-180.0000000, -60.0000000) (180d 0' 0.00\"W, 60d 0' 0.00\"S)\r\n", "Upper Right ( -30.0000000, 10.0000000) ( 30d 0' 0.00\"W, 10d 0' 0.00\"N)\r\n", "Lower Right ( -30.0000000, -60.0000000) ( 30d 0' 0.00\"W, 60d 0' 0.00\"S)\r\n", "Center (-105.0000000, -25.0000000) (105d 0' 0.00\"W, 25d 0' 0.00\"S)\r\n", "Band 1 Block=1500x2 Type=UInt16, ColorInterp=Gray\r\n" ] } ], "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": 30, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
| Set3_03 | |
| Pastel2_08 | |
| RdGy_06 | |
| BuGn_08 | |
| BuGn_09 | |
| Oranges_03 | |
| PuRd_03 | |
| RdBu_11 | |
| Blues_06 | |
| Pastel2_05 | |
| BrBG_08 | |
| Pastel1_06 | |
| PRGn_06 | |
| Set3_10 | |
| PuBuGn_07 | |
| PuOr_11 | |
| Reds_09 | |
| PuBuGn_09 | |
| PuBuGn_08 | |
| PuRd_04 | |
| GnBu_06 | |
| RdYlBu_07 | |
| PRGn_09 | |
| Paired_12 | |
| Spectral_06 | |
| BrBG_07 | |
| Accent_03 | |
| Spectral_09 | |
| Accent_04 | |
| Reds_06 | |
| YlOrBr_03 | |
| PuOr_07 | |
| Greys_07 | |
| YlGn_05 | |
| YlGnBu_09 | |
| RdGy_10 | |
| YlOrBr_06 | |
| RdYlGn_11 | |
| RdYlGn_09 | |
| PuRd_09 | |
| YlGnBu_06 | |
| Accent_06 | |
| BrBG_05 | |
| Dark2_04 | |
| Oranges_07 | |
| PiYG_08 | |
| RdYlBu_05 | |
| Oranges_06 | |
| Purples_03 | |
| YlGnBu_04 | |
| BuPu_07 | |
| Dark2_03 | |
| Purples_06 | |
| RdPu_07 | |
| PRGn_04 | |
| YlOrBr_04 | |
| BrBG_03 | |
| YlGn_03 | |
| Spectral_04 | |
| Accent_05 | |
| GnBu_03 | |
| YlOrBr_08 | |
| PuBuGn_05 | |
| RdGy_05 | |
| PuBuGn_04 | |
| PuBuGn_06 | |
| RdPu_03 | |
| OrRd_04 | |
| Set3_05 | |
| YlOrRd_09 | |
| Greens_07 | |
| PuBuGn_03 | |
| PuOr_08 | |
| Paired_04 | |
| Set3_08 | |
| Reds_07 | |
| BrBG_04 | |
| Set2_05 | |
| PuBu_08 | |
| PiYG_03 | |
| PuBu_07 | |
| Set2_07 | |
| Pastel1_08 | |
| BuPu_05 | |
| Oranges_08 | |
| PuOr_05 | |
| BuGn_03 | |
| Greens_09 | |
| YlOrRd_07 | |
| Purples_09 | |
| Greys_08 | |
| Pastel1_05 | |
| OrRd_05 | |
| Oranges_04 | |
| viridis | |
| Pastel1_03 | |
| YlOrRd_06 | |
| RdYlBu_04 | |
| Dark2_05 | |
| Set3_12 | |
| RdPu_08 | |
| Greens_06 | |
| PuRd_08 | |
| PRGn_05 | |
| YlGn_06 | |
| Blues_07 | |
| Set3_09 | |
| PuBu_05 | |
| PuBu_06 | |
| Pastel2_04 | |
| YlOrBr_09 | |
| OrRd_06 | |
| RdYlGn_05 | |
| PuRd_07 | |
| Pastel1_07 | |
| Purples_07 | |
| OrRd_07 | |
| Pastel2_03 | |
| YlOrBr_05 | |
| BrBG_06 | |
| Set1_08 | |
| Set3_07 | |
| YlGnBu_08 | |
| GnBu_05 | |
| Dark2_07 | |
| Pastel1_09 | |
| Set1_09 | |
| Greys_06 | |
| PuOr_03 | |
| RdGy_09 | |
| PuBu_03 | |
| Blues_09 | |
| Accent_08 | |
| YlGn_04 | |
| RdGy_07 | |
| Blues_03 | |
| YlOrRd_08 | |
| Greens_08 | |
| Paired_06 | |
| PiYG_04 | |
| Greys_04 | |
| Oranges_05 | |
| Set2_04 | |
| PuBu_04 | |
| Paired_03 | |
| Spectral_07 | |
| RdYlGn_07 | |
| RdGy_08 | |
| Paired_05 | |
| BrBG_11 | |
| YlGn_08 | |
| Set2_08 | |
| PRGn_03 | |
| Greens_05 | |
| Pastel2_06 | |
| Paired_10 | |
| PuOr_04 | |
| BuPu_06 | |
| Greys_09 | |
| Blues_04 | |
| Paired_09 | |
| PRGn_11 | |
| YlGnBu_05 | |
| RdBu_08 | |
| PiYG_06 | |
| BrBG_10 | |
| Dark2_08 | |
| RdPu_04 | |
| Spectral_08 | |
| YlOrRd_05 | |
| Paired_11 | |
| YlGnBu_03 | |
| YlOrBr_07 | |
| Dark2_06 | |
| RdYlGn_10 | |
| OrRd_03 | |
| Set1_05 | |
| Reds_03 | |
| Pastel1_04 | |
| Accent_07 | |
| Purples_05 | |
| Spectral_11 | |
| YlGn_09 | |
| BuPu_03 | |
| PiYG_07 | |
| PRGn_10 | |
| Set2_06 | |
| Set1_03 | |
| RdGy_04 | |
| Set1_07 | |
| BuGn_04 | |
| RdBu_04 | |
| RdYlGn_04 | |
| PuRd_05 | |
| BrBG_09 | |
| Set3_11 | |
| Blues_08 | |
| GnBu_08 | |
| RdYlGn_06 | |
| PuOr_09 | |
| GnBu_04 | |
| OrRd_09 | |
| RdYlBu_08 | |
| PuBu_09 | |
| RdPu_09 | |
| BuPu_08 | |
| Blues_05 | |
| Purples_04 | |
| RdBu_06 | |
| RdYlBu_10 | |
| BuPu_04 | |
| PuRd_06 | |
| PRGn_07 | |
| RdBu_03 | |
| BuPu_09 | |
| Set3_06 | |
| RdBu_10 | |
| YlGn_07 | |
| YlOrRd_03 | |
| BuGn_07 | |
| RdBu_09 | |
| RdBu_05 | |
| Pastel2_07 | |
| RdYlGn_03 | |
| PiYG_05 | |
| OrRd_08 | |
| Paired_08 | |
| Spectral_03 | |
| PuOr_10 | |
| YlGnBu_07 | |
| RdYlBu_11 | |
| BuGn_06 | |
| Oranges_09 | |
| Spectral_10 | |
| RdBu_07 | |
| Paired_07 | |
| RdYlBu_03 | |
| RdPu_06 | |
| RdYlGn_08 | |
| PRGn_08 | |
| GnBu_09 | |
| RdGy_11 | |
| Greens_03 | |
| Greens_04 | |
| Purples_08 | |
| Set1_04 | |
| Set2_03 | |
| RdGy_03 | |
| Reds_08 | |
| YlOrRd_04 | |
| Reds_05 | |
| Set3_04 | |
| BuGn_05 | |
| RdYlBu_09 | |
| Set1_06 | |
| PuOr_06 | |
| Greys_03 | |
| Greys_05 | |
| RdPu_05 | |
| PiYG_11 | |
| RdYlBu_06 | |
| GnBu_07 | |
| Reds_04 | |
| PiYG_09 | |
| Spectral_05 | |
| PiYG_10 |