{"id":18,"date":"2020-10-03T09:33:29","date_gmt":"2020-10-03T09:33:29","guid":{"rendered":"https:\/\/ml-gis-service.com\/?p=18"},"modified":"2020-10-03T09:34:09","modified_gmt":"2020-10-03T09:34:09","slug":"toolbox-visualize-raster-band-in-python","status":"publish","type":"post","link":"https:\/\/ml-gis-service.com\/index.php\/2020\/10\/03\/toolbox-visualize-raster-band-in-python\/","title":{"rendered":"Toolbox: Visualize raster band in Python"},"content":{"rendered":"\n<p>Toolbox notes are short and precise. There is no introduction. Just code and few comments about it. Article is designed to make it easy to reproduce the code and understand how it works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Script to read and show all rasters in the given directory<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Imported libraries: os, NumPy, Rasterio, matplotlib.<\/li><li>Two functions:&nbsp;<code>read_images()<\/code>&nbsp;and&nbsp;<code>show_band()<\/code>.<\/li><li><code>read_images()<\/code>&nbsp;takes two arguments:&nbsp;<code>folder_name<\/code>&nbsp;and&nbsp;<code>file_end<\/code>. Parameter&nbsp;<code>folder_name<\/code>&nbsp;is a&nbsp;<strong>string<\/strong>&nbsp;with a path to the directory which contains tiff rasters. Parameter&nbsp;<code>file_end<\/code>&nbsp;is used to differentiate useful data from the other files. This function is not covered in the note.<\/li><li><code>show_band()&nbsp;<\/code>takes four parameters. One is necessary and three are optional. Starting from the first\u2026&nbsp;<code>band<\/code>&nbsp;is a&nbsp;<strong>numpy array<\/strong>&nbsp;read by the rasterio from the tiff file. It is processed inside the script to change all&nbsp;<code>nan_val<\/code>&nbsp;to numpy nan\u2019s which are invisible in the matplotlib figure. (<code>nan_val<\/code>&nbsp;is the second parameter and its default value is zero). We shouldn\u2019t perform this operation on the \u201cliving thing\u201d to avoid problems with the data integrity and analysis output, so it is limited to the function scope.<\/li><li>Parameter&nbsp;<code>color_map<\/code>&nbsp;is taken from the&nbsp;<strong>Matplotlib\u2019s colormaps<\/strong>&nbsp;(<a href=\"https:\/\/matplotlib.org\/3.1.0\/tutorials\/colors\/colormaps.html\">https:\/\/matplotlib.org\/3.1.0\/tutorials\/colors\/colormaps.html<\/a>). Default value is&nbsp;<em>Set1<\/em>&nbsp;due to the fact that image in this example is a classification output and has only few numbers.<\/li><li>The last parameter is&nbsp;<code>title<\/code>. It is a string which describes image content. If none is passed then image title doesn\u2019t appear.<\/li><li>The main function works as follow: variable&nbsp;<code>images<\/code>&nbsp;is a&nbsp;<strong>list<\/strong>&nbsp;with the paths to each&nbsp;<em>\u2018.tif\u2019<\/em>&nbsp;file stored in the folder where script is invoked. Then each element from the list is read by rasterio. Pixels are passed into the&nbsp;<code>show_band()<\/code>&nbsp;function. Title is set to be a full path to the presented image. Parameter&nbsp;<code>color_map<\/code>&nbsp;is changed to<em>&nbsp;\u2018Accent\u2019<\/em>&nbsp;because it gives better visual results than the default&nbsp;<em>\u2018Set1\u2019<\/em>&nbsp;<strong>colormap<\/strong>.<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nimport numpy as np\nimport rasterio as rio\nimport matplotlib.pyplot as plt\n\n\ndef read_images(folder_name, file_end):\n    file_list = os.listdir(folder_name)\n    channel_list = []\n    for f in file_list:\n        if f.endswith(file_end):\n            channel_list.append(folder_name + f)\n    channel_list.sort()\n    return channel_list\n\ndef show_band(band, nan_val=0, color_map='Set1', title=None):\n    band = band.astype(np.float)\n    band[band == nan_val] = np.nan\n    plt.figure(figsize=(11, 11))\n    image_layer = plt.imshow(band)\n    image_layer.set_cmap(color_map)\n    plt.colorbar()\n    if title is not None:\n        plt.title(title)\n    plt.axis('off')\n    plt.show()\n\n\nif __name__ == '__main__':\n    folder_address = '.\/'\n    file_ending = '.tif'\n    images = read_images(folder_address, file_ending)\n    for img in images:\n        with rio.open(img) as src:\n            image = src.read(1)\n            show_band(image, nan_val=0, color_map='Accent', title=img)<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"478\" height=\"500\" src=\"https:\/\/ml-gis-service.com\/wp-content\/uploads\/2020\/10\/toolbox_ras_vis_small.png\" alt=\"\" class=\"wp-image-19\" srcset=\"https:\/\/ml-gis-service.com\/wp-content\/uploads\/2020\/10\/toolbox_ras_vis_small.png 478w, https:\/\/ml-gis-service.com\/wp-content\/uploads\/2020\/10\/toolbox_ras_vis_small-287x300.png 287w\" sizes=\"auto, (max-width: 478px) 100vw, 478px\" \/><figcaption>Raster visualization with rasterio and matplotlib<\/figcaption><\/figure><\/div>\n\n\n\n<p><strong>Github:<\/strong> <a href=\"https:\/\/github.com\/szymon-datalions\/geoprocessing\/blob\/master\/rasters\/check_rasters.py\">https:\/\/github.com\/szymon-datalions\/geoprocessing\/blob\/master\/rasters\/check_rasters.py<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to visualize raster file in Python<\/p>\n","protected":false},"author":1,"featured_media":22,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3,4,17],"tags":[12,13,15,11,16,10,7,9,6,14],"class_list":["post-18","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-engineering","category-python","category-raster","category-scripts","tag-how-to-show-raster-in-python","tag-jupyter-notebook","tag-landsat","tag-matplotlib","tag-modis","tag-numpy","tag-python","tag-raster-visualization","tag-rasterio","tag-sentinel"],"_links":{"self":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":2,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":23,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions\/23"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/media\/22"}],"wp:attachment":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}