{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data type in GTiff\n", "\n", "---\n", "\n", "Exercise presentation [Video 2:19:00 - 2:19:43](https://www.youtube.com/watch?v=kBRW2Z5jX8M) \n", "Exercise soultion [Video 16:15 - 41:40](https://youtu.be/Yy5UaCOvuYo?t=975)\n", "\n", "---\n", "\n", "In this exercise we will see the importance to use the correct data type in GTiff environment. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Build up a GTiff using AWK" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "echo ncols 10 > geodata_small/rast_ID.asc \n", "echo nrows 10 >> geodata_small/rast_ID.asc \n", "echo xllcorner 0 >> geodata_small/rast_ID.asc \n", "echo yllcorner 0 >> geodata_small/rast_ID.asc \n", "echo cellsize 1 >> geodata_small/rast_ID.asc\n", "\n", "awk ' BEGIN { \n", "for (row=1 ; row<=10 ; row++) { \n", " for (col=1 ; col<=10 ; col++) { \n", " printf (\"%i \" , 16777200+col+(row-1)*10 ) } ; printf (\"\\n\") }}' >> geodata_small/rast_ID.asc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The rast_ID.asc has the typical structure of Arc/Info ASCII Grid" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ncols 10\n", "nrows 10\n", "xllcorner 0\n", "yllcorner 0\n", "cellsize 1\n", "16777201 16777202 16777203 16777204 16777205 16777206 16777207 16777208 16777209 16777210 \n", "16777211 16777212 16777213 16777214 16777215 16777216 16777217 16777218 16777219 16777220 \n", "16777221 16777222 16777223 16777224 16777225 16777226 16777227 16777228 16777229 16777230 \n", "16777231 16777232 16777233 16777234 16777235 16777236 16777237 16777238 16777239 16777240 \n", "16777241 16777242 16777243 16777244 16777245 16777246 16777247 16777248 16777249 16777250 \n", "16777251 16777252 16777253 16777254 16777255 16777256 16777257 16777258 16777259 16777260 \n", "16777261 16777262 16777263 16777264 16777265 16777266 16777267 16777268 16777269 16777270 \n", "16777271 16777272 16777273 16777274 16777275 16777276 16777277 16777278 16777279 16777280 \n", "16777281 16777282 16777283 16777284 16777285 16777286 16777287 16777288 16777289 16777290 \n", "16777291 16777292 16777293 16777294 16777295 16777296 16777297 16777298 16777299 16777300 \n" ] } ], "source": [ "%%bash\n", "cat geodata_small/rast_ID.asc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can convert the rast_ID.asc Arc/Info ASCII Grid file to a GTiff" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Input file size is 10, 10\n", "0...10...20...30...40...50...60...70...80...90...100 - done.\n" ] } ], "source": [ "%%bash\n", "gdal_translate -ot UInt32 -co COMPRESS=DEFLATE -co ZLEVEL=9 geodata_small/rast_ID.asc geodata_small/rast_ID.tif" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's suppose that you want make a mathematical operation rast_ID.asc * 2222 using gdal_calc.py.\n", "How you would build-up the line?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "gdal_calc.py ????????????????????????????????????? --outfile=geodata_small/rast_ID_mult.tif --overwrite " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use the inverse procedure (GTiff to Arc/Info ASCII Grid) to see if your computation is correct." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Input file size is 10, 10\n", "0...10...20...30...40...50...60...70...80...90...100 - done.\n", "ncols 10\n", "nrows 10\n", "xllcorner 0.000000000000\n", "yllcorner 0.000000000000\n", "cellsize 1.000000000000\n", "NODATA_value 1.7976931348623157081e+308\n", " 37278940622.0 37278942844 37278945066 37278947288 37278949510 37278951732 37278953954 37278956176 37278958398 37278960620\n", " 37278962842 37278965064 37278967286 37278969508 37278971730 37278973952 37278976174 37278978396 37278980618 37278982840\n", " 37278985062 37278987284 37278989506 37278991728 37278993950 37278996172 37278998394 37279000616 37279002838 37279005060\n", " 37279007282 37279009504 37279011726 37279013948 37279016170 37279018392 37279020614 37279022836 37279025058 37279027280\n", " 37279029502 37279031724 37279033946 37279036168 37279038390 37279040612 37279042834 37279045056 37279047278 37279049500\n", " 37279051722 37279053944 37279056166 37279058388 37279060610 37279062832 37279065054 37279067276 37279069498 37279071720\n", " 37279073942 37279076164 37279078386 37279080608 37279082830 37279085052 37279087274 37279089496 37279091718 37279093940\n", " 37279096162 37279098384 37279100606 37279102828 37279105050 37279107272 37279109494 37279111716 37279113938 37279116160\n", " 37279118382 37279120604 37279122826 37279125048 37279127270 37279129492 37279131714 37279133936 37279136158 37279138380\n", " 37279140602 37279142824 37279145046 37279147268 37279149490 37279151712 37279153934 37279156156 37279158378 37279160600\n" ] } ], "source": [ "%%bash\n", "gdal_translate -of AAIGrid geodata_small/rast_ID_mult.tif geodata_small/rast_ID_mult.asc\n", "cat geodata_small/rast_ID_mult.asc" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 4 }