{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Juypter notebooks have magic functions! one of them is the timeit. %timeit is great for single lines and %%timeit for multi lines" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%timeit data = [x * x for x in range(1000)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%timeit\n", "data = []\n", "for x in range(1000):\n", " data.append(x * x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That makes a pretty convincing case for using list comprehension! What is the fastest way to read a satellite image into a numpy array? Lets test using a Landsat 8 image (Band 4) downloaded from AWS\n", "https://landsat-pds.s3.amazonaws.com/c1/L8/139/045/LC08_L1TP_139045_20170304_20170316_01_T1/index.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "image_path = \"D:/Fastest_image/LC81390452014295LGN00_B4.TIF\" ### set the path to your image choice" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import all the Python libraries. I recommend using a virtual python environment for running the tests, but it is not essential" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "## method 1 - opencv\n", "import cv2\n", "import numpy as np\n", "# Import GDAL method 2\n", "from osgeo import gdal\n", "## import method 3 - skimage\n", "from skimage import io\n", "## import method 4 - rasterio\n", "##import rasterio\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%timeit\n", "### Open CV approach\n", "\n", "img = cv2.imread(image_path, -1) ### add your image here\n", "print img.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%timeit\n", "## GDAL approach\n", "\n", "raster_ds = gdal.Open(image_path, gdal.GA_ReadOnly)\n", "image_gdal = raster_ds.GetRasterBand(1).ReadAsArray()\n", "print image_gdal.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%timeit\n", "## skimage\n", "\n", "im = io.imread(image_path)\n", "print im.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%%timeit\n", "with rasterio.open(image_path) as r:\n", " arr = r.read() # read raster\n", " print(arr.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In my test (albeit just 1 single band Landsat 8 image) GDAL is the winner!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4 different ways to read satellite images into numpy arrays\n", "The case for using list comprehension to speed up code\n", "GDAL is the fastest!\n", "BUT use the most suited approach to your needs!\n", "use the timeit%% for multi line testing and timeit% for single line testing" ] }, { "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.10" } }, "nbformat": 4, "nbformat_minor": 2 }