{"id":1626,"date":"2018-06-26T14:05:51","date_gmt":"2018-06-26T12:05:51","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/reseda\/?page_id=1626"},"modified":"2018-09-25T11:09:59","modified_gmt":"2018-09-25T09:09:59","slug":"preprocess-landsat-exercise","status":"publish","type":"page","link":"https:\/\/blogs.fu-berlin.de\/reseda\/preprocess-landsat-exercise\/","title":{"rendered":"Preprocess L8 Exercise"},"content":{"rendered":"<p>1) Commenting on scripts is extremely useful, especially if you want to look at it later and understand what&#8217;s going on. Test your understanding of the script for pre-processing a single L8 scene shown in the previous section. Copy the following code into the script window of R-Studio. For each line, try to give a brief explanation of what is being done in the next line of code! (Explanation of the first line is given)<\/p>\n<pre class=\"theme:amityreseda\">\r\n# load the raster library in the current session\r\n# (needed for rasterstack and writeRaster)\r\nlibrary(raster)\r\n\r\n#\r\nproduct &lt;- &quot;\/media\/sf_exchange\/landsatdata\/LC081930232017060201T1-SC20180613160412.tar.gz&quot;\r\n\r\n#\r\nproductname &lt;- substr(product, 1, nchar(product) - 7) \r\n\r\n#\r\nuntar(product, exdir = productname)\r\n\r\n#\r\nproductfiles &lt;- list.files(productname, full.names = TRUE)\r\n\r\n#\r\nbands &lt;- c(grep(&#039;band1&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band2&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band3&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band4&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band5&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band6&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band7&#039;, productfiles, value=TRUE)\r\n           ) \r\n\r\n#\r\nrasterstack &lt;- stack( bands )\r\n\r\n#\r\nwriteRaster(rasterstack, \r\n            paste0(productname, &quot;.tif&quot;), \r\n            format = &quot;GTiff&quot;,\r\n            overwrite = TRUE\r\n            )\r\n\r\n#\r\nunlink(productname, recursive=TRUE)\r\n\r\n#\r\nmakeOVR &lt;- paste0(&quot;gdaladdo -ro &quot;, productname, &quot;.tif 2 4 8 16&quot;)\r\nsystem( makeOVR )\r\n<\/pre>\n<p>Done? Compare it with the solution:<\/p>\n<pre class=\"theme:amityreseda minimize:true title='SHOW SOLUTION'\">\r\n# load the raster library in the current session\r\n# (needed for rasterstack and writeRaster)\r\nlibrary(raster)\r\n\r\n# define file path of a single L8 dataset \r\nproduct &lt;- &quot;\/media\/sf_exchange\/landsatdata\/LC081930232017060201T1-SC20180613160412.tar.gz&quot;\r\n\r\n# get the export folder for decompression by deleting suffix (.tar.gz) \r\nproductname &lt;- substr(product, 1, nchar(product) - 7) \r\n\r\n# decompress and save to export folder \r\nuntar(product, exdir = productname)\r\n\r\n# create a vector of all names of files in decompressed folder\r\nproductfiles &lt;- list.files(productname, full.names = TRUE)\r\n\r\n# search for individual bands inside file vector\r\nbands &lt;- c(grep(&#039;band1&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band2&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band3&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band4&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band5&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band6&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band7&#039;, productfiles, value=TRUE)\r\n           ) \r\n\r\n# create a rasterstack of the band selection\r\nrasterstack &lt;- stack( bands )\r\n\r\n# write the rasterstack to hard drive\r\nwriteRaster(rasterstack, \r\n            paste0(productname, &quot;.tif&quot;), \r\n            format = &quot;GTiff&quot;,\r\n            overwrite = TRUE\r\n            )\r\n\r\n# delete the uncompressed folder in order to save disc space\r\nunlink(productname, recursive=TRUE)\r\n\r\n# create pyramid layers for faster visualization\r\nmakeOVR &lt;- paste0(&quot;gdaladdo -ro &quot;, productname, &quot;.tif 2 4 8 16&quot;)\r\nsystem( makeOVR )\r\n<\/pre>\n<p>2) Download any Landsat-8 Level-1 dataset using the USGS EarthExplorer. Modify the code given in the previous task so that you create and store a datastack of all the spectral channels available in this Level-1 dataset!<\/p>\n<pre class=\"theme:amityreseda minimize:true title='SHOW SOLUTION'\">\r\nlibrary(raster)\r\n \r\nproduct &lt;- &quot;\/media\/sf_exchange\/landsatdata\/LC081930232017060201T1-SC20180613160412.tar.gz&quot;\r\n \r\nproductname &lt;- substr(product, 1, nchar(product) - 7) \r\n \r\nuntar(product, exdir = productname)\r\n \r\nproductfiles &lt;- list.files(productname, full.names = TRUE)\r\n \r\nbands &lt;- c(grep(&#039;band1&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band2&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band3&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band4&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band5&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band6&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band7&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band8&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band9&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band10&#039;, productfiles, value=TRUE),\r\n           grep(&#039;band11&#039;, productfiles, value=TRUE)\r\n           ) \r\n \r\nrasterstack &lt;- stack( bands )\r\n \r\nwriteRaster(rasterstack, \r\n            paste0(productname, &quot;.tif&quot;), \r\n            format = &quot;GTiff&quot;,\r\n            overwrite = TRUE\r\n            )\r\n \r\nunlink(productname, recursive=TRUE)\r\n \r\nmakeOVR &lt;- paste0(&quot;gdaladdo -ro &quot;, productname, &quot;.tif 2 4 8 16&quot;)\r\nsystem( makeOVR )\r\n<\/pre>\n<p><\/br><\/br><\/p>\n<hr style=\"height:4px;background-color:#6b9e1f\">\n<a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/sentinel-2-preprocessing\/\"><br \/>\n<button style=\"width:100%;text-align:right;padding: 10 0;background-color:white;margin:-55px 0 0 0\"><\/p>\n<div style=\"font-family: 'Noto Sans',sans-serif;line-height: 1.2\">\n<span style=\"font-size: 12px;color:#bfbfbf\"><strong><em>NEXT<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 30px;color:#6b9e1f\"><strong><em>Sentinel 2 Preprocessing<\/em><\/strong><\/span>\n<\/div>\n<p><\/button><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1) Commenting on scripts is extremely useful, especially if you want to look at it later and understand what&#8217;s going on. Test your understanding of the script for pre-processing a single L8 scene shown in the previous section. Copy the following code into the script window of R-Studio. For each line, try to give a &hellip; <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/preprocess-landsat-exercise\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Preprocess L8 Exercise&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3237,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1626","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/users\/3237"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/comments?post=1626"}],"version-history":[{"count":11,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1626\/revisions"}],"predecessor-version":[{"id":2502,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1626\/revisions\/2502"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/media?parent=1626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}