{ "cells": [ { "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", "\n", "fname = '/notebooks/resources/T22KHG_20190425T132241_TCI_smaller.tif'\n", "\n", "ds = gdal.Open(fname)\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": { "scrolled": false }, "outputs": [], "source": [ "# Second: convert the WKT projection information to a cartopy projection.\n", "import cartopy.crs as ccrs\n", "\n", "projcs = inproj.GetAuthorityCode('PROJCS')\n", "print('\\n\\n## projcs ##:\\n\\n' + str(projcs))\n", "\n", "projection = ccrs.epsg(projcs)\n", "print('\\n\\n## projection ##:\\n\\n' + str(projection))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Third (and last): the figure.\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "\n", "subplot_kw = dict(projection=projection)\n", "fig, ax = plt.subplots(figsize=(9, 9), subplot_kw=subplot_kw)\n", "\n", "extent = (gt[0], gt[0] + ds.RasterXSize * gt[1],\n", " gt[3] + ds.RasterYSize * gt[5], gt[3])\n", "\n", "img = ax.imshow(data[:3, :, :].transpose((1, 2, 0)), extent=extent, origin='upper')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import inspect\n", "\n", "for i in inspect.getmembers(ds):\n", " # Ignores anything starting with underscore \n", " # (that is, private and protected attributes)\n", " if not i[0].startswith('_'):\n", " # Ignores methods\n", " if not inspect.ismethod(i[1]):\n", " print(i)\n", "print('\\n')\n", "# dir(ds)" ] }, { "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", "\n", "# fname = 'T22KHG_20190425T132241_TCI_smaller.tif'\n", "\n", "# ds = gdal.Open(fname)\n", "# data = ds.ReadAsArray()\n", "# gt = ds.GetGeoTransform()\n", "# proj = ds.GetProjection()\n", "\n", "# inproj = osr.SpatialReference()\n", "# inproj.ImportFromWkt(proj)\n", "\n", "# print(inproj)\n", "\n", "# # Second: convert the WKT projection information to a cartopy projection.\n", "# import cartopy.crs as ccrs\n", "\n", "\n", "\n", "# projcs = inproj.GetAuthorityCode('PROJCS')\n", "# print(projcs)\n", "# projection = ccrs.epsg(projcs)\n", "# print(projection)\n", "\n", "# # Third (and last): the figure.\n", "# import matplotlib.pyplot as plt\n", "# %matplotlib inline\n", "\n", "# subplot_kw = dict(projection=projection)\n", "# fig, ax = plt.subplots(figsize=(9, 9), subplot_kw=subplot_kw)\n", "\n", "# extent = (gt[0], gt[0] + ds.RasterXSize * gt[1],\n", "# gt[3] + ds.RasterYSize * gt[5], gt[3])\n", "\n", "# img = ax.imshow(data[:3, :, :].transpose((1, 2, 0)), extent=extent, origin='upper')" ] } ], "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 }