forked from awickert/GRASS-GIS-for-geomorphologists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGRASSshortcourse.tex
1484 lines (1143 loc) · 93.3 KB
/
GRASSshortcourse.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass{book}
\usepackage{textcomp}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{url}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{comment}
\usepackage[tikz]{bclogo}
\usepackage{afterpage}
\usepackage{hyperref}
\usepackage{hyperref}
\hypersetup{
colorlinks,
citecolor=black,
filecolor=black,
linkcolor=blue,
urlcolor=black
}
%\usepackage[top=1in, bottom=1in, left=1.3in, right=1.3in]{geometry}
\usepackage{caption}
%\usepackage{trivfloat}
\lstset{ %
columns=fullflexible, % Remove superfluous spaces - copy/pasteable
%%%%%%%%%%%%%%%
%language=bash, % the language of the code
basicstyle=\footnotesize\ttfamily, % the size and font that are used for the code
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
frame=single, % adds a frame around the code
tabsize=2, % sets default tabsize to 2 spaces
%captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
%title=\lstname, % show the filename of files included with \lstinputlisting;
% also try caption instead of title
%escapeinside={\%*}{*)}, % if you want to add a comment within your code
%morekeywords={*,...} % if you want to add more keywords to the set
upquote=true % Use straight quotes
% The following is to make this copy-pasteable (in addition to the upquote and columns, above)
literate={*}{{\char42}}1
{-}{{\char45}}1
}
% SET UP INVISIBLE (WHITE) CAPTIONS SO I CAN HAVE A TOC FOR BOXES
% NOT ALL THAT ELEGANT, BUT IT WORKS, WHICH IS #1
\usepackage{color}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionType[fileext=lob,placement=b,within=chapter]{boxx}[Box][Boxes]
\DeclareCaptionStyle{mystyle}[margin=0mm,justification=centering]{labelfont={scriptsize,white},font={scriptsize,white},labelfont={scriptsize,white},margin={0mm,0mm}}
%\captionsetup[boxx]{labelfont=white,textfont=white}
%\captionsetup[boxx]{labelfont={white,scriptsize},textfont={white,scriptsize},margin={0mm,0mm}}
\captionsetup[boxx]{style=mystyle}
\begin{comment}
\DeclareCaptionType[fileext=lob,placement=b,within=chapter]{boxx}[Box][List of Boxes]
\renewenvironment{box}
{
\captionsetup{type=boxx,position=b} %skip BELOW the caption
}
\end{comment}
\title{GRASS GIS for Geomorphologists:\\An Introductory Guide}
\author{Andrew Wickert}
\begin{document}
\maketitle
\tableofcontents
\listofboxxs
\section*{Notes}
\noindent Original draft finished 22 December 2011 \\
\noindent First finished draft completed on 16 April 2012 \\
\noindent These notes are written for GRASS 6.4.X. \\
\section*{License}
\includegraphics[width=5cm]{figures/license/CC-BY-SA_icon.pdf} \\
% vectorized icon from http://upload.wikimedia.org/wikipedia/commons/d/d0/CC-BY-SA_icon.svg
GRASS GIS for Geomorphologists: An Introductory Guide by Andrew D. Wickert is licensed under a \href{http://creativecommons.org/licenses/by-sa/3.0/}{\textcolor{purple}{Creative Commons Attribution-ShareAlike 3.0 Unported License}}. You may freely use it, share it, and change it, so long as the author gets some recognition. And if you want to change it, please let me know! I'd love to have help in maintaining and/or expanding this manual.
\chapter{Introduction}
\section{GIS}
Most of you are probably familiar with GIS, and some of you probably use it very often. But for those of you who don't, GIS stands for "Geographical Information System". It is a software / programming language that is designed to work with data that are displayed spatially---in x,y,z or lat/lon,z coordinates.
Geospatial data take two main forms. {\bf Raster} data are regularly-spaced Cartesian grids. {\bf Vector} data are specified by sets of ungridded x,y,z points that do not necessarily coincide with the position and spacing of raster grids. These can include points, lines, and areas. Raster and vector data types can be used in three dimensions as well, with three dimensional grids and the addition of volume-filling vector elements. The most common geomorphic raster is the digital elevation model (DEM); other common rasters are classified land-use maps and remotely sensed imagery: in general, they represent the intensity of some value across the whole region of interest. Common examples of vector data in geomorphology are river channels (lines), lakes (areas), and sample locations (points).
GIS packages contain a number of tools to work with raster and vector data, make calculations based on them, perform file input/output, reproject data from one coordinate system to another, manage databases of georeferenced information, and create human-readable maps to display the data that the GIS package stores and processes.
Some reasons that geomorphologists use GIS are to:
\begin{enumerate}
\item Build maps
\item Display topographic data
\item Make calculations of topographic features (e.g., hypsometry, curvatures of hillslopes)
\item Generate features involving water: lakes, rivers, drainage basins, shorelines
\item Calculate regional values, such as insolation and $^{10}$Be production rates
\item Calculate and/or show regions of landscape change (e.g., erosion, deposition, glacier retreat) over time
\end{enumerate}
\section{What is GRASS?}
GRASS is the most popular open-source GIS package available. GRASS stands for \emph{Geographic Resources Analysis Support System}. It was first developed in 1982 by the US Army Corps of engineers to be able to perform analyses for the National Environmental Policy Act, with an emphasis on environmental research and monitoring. It was released to the community in the early 90's, and has been a community-driven open source project ever since.
\begin{boxx}[!ht]
\begin{bclogo}[arrondi = 0.1, logo = \bcrosevents]{Why GRASS?}
I use GRASS because it is cross-platform (I am much more comfortable on UNIX-like systems than on Windows); it is very good at hydrologic analyses; it is very scriptable for easy batch processing, sharing of reproducible analyses, and geospatial integration of numerical models; it is open-source (so I can change components to fit my needs), and because I can share my work with anyone from around the world without being tied down to expensive software. \\
On a personal note, I find programming in Windows to be a utterly horrific, time-wasting, and demoralizing experience. GRASS lets me stay on a UNIX-like OS, and this alone is reason enough for me to use it.
\end{bclogo}
\caption{Why GRASS?}
\end{boxx}
Being open-source means that you can view and change all of the source code---mostly C, with some bash and Python---in which it is written. While I have occasionally tweaked the source code for specialized projects, I almost never have to: in my experience, GRASS is well-vetted and fully-functional.
For more information, go to \url{http://grass.osgeo.org/}.
\subsection{GRASS Interfaces}
GRASS is more of a computational / scripting GIS and less of a point-and-click GIS than ArcGIS. I find this to be an advantage, but for those of you who like point-and-click there are a couple of options. GRASS now has a new interface that is much improved from its old one, which is good for displaying data. For editing vector files, I have found Quantum GIS to be very nice: \url{http://www.qgis.org/}. It can work with GRASS data structures, and therefore be integrated with your GRASS GIS project.
\subsection{Topology}
A major feature of GRASS is that it is a topological GIS. That is, it is impossible to have small gaps or overlaps between vector areas (or ``polygons''). It also forces lines to meet and interact according to some fairly logical rules. This helps for consistency in geologic mapping and allows users to queery vector maps based on their neighbors.
\subsection{File structure}
GRASS creates its own file system. This is more rigid than the way that Arc handles files, but means that you will never lose your GIS files, and you can take your whole folder with you as a bundle. File operations are handled internally by GRASS. There are a large number of import/export options (GDAL, OGR, ASCII, etc.) that can be used to share your work with other GIS applications. I mention the file system again in Section \ref{s:CreateLocation}.
\begin{comment}
\section{Why GRASS?}
There are a lot of reasons to use GRASS. These came from the top of my head. Points 1, 6, and 8 are the three of these below that are the most important to me.
\begin{enumerate}
\item {\bf Cross-platform:} Mac, Linux, and Windows
\item {\bf Design for geomorphic and hydrologic analysis:} GRASS was originally designed by the US Army Corps of Engineers, hence its historic strength lies in its abilities for water resources and other natural world analyses. It requires no extra packages to be able to do these.
\item {\bf Fast hydrologic analysis algorithms:} GRASS implements the Terraflow project from Duke, and its own watershed analysis programs which have now been modified to run even more quickly
\item {\bf Lots of included tools for analyzing the natural world}: Works with remotely-sensed data, calculates solar radiation, can perform ecological analyses, powerful raster map calculator,
\item {\bf Ability to build rasters from LiDAR point clouds}
\item {\bf File I/O with all GDAL and OGR formats}: This means that basically any data set format, from any GIS package, can be imported or exported to/from GRASS. This includes things like ESRI shapefiles, KML files such as those used by Google Earth, and multiple raster data formats.
\item {\bf You'll be learning a skill that isn't tied to an extremely expensive software}
\item {\bf GRASS is heavily used overseas}: While ArcGIS is common in North America, GRASS is used quite a lot in the rest of the world, both because ESRI has a strong influence in the US geospatial industry and because GRASS is free.
\item {\bf Easy batch processing and script writing}: You can write scripts for batch processing and to be able to reuse old analyses in GRASS with little effort. Indeed, the software is geared towards an easy-to-use command line interface. This was the major draw for me: I need to reconstruct paleohydrologies over the last glacial cycle!
\item {\bf Active development and open-source}: You can talk to the GRASS user community about problems. And if a tool that you are trying to use doesn't work, you can change it, recompile, and presto! Now it does work.
\end{enumerate}
\end{comment}
\chapter{Downloading and Installing GRASS}
Installation varies from a no-brainer to a difficult task, depending on your operating system. I am going to be giving instructions for how to make the precompiled binaries work. Compiling your own version of GRASS is important if you want to change the source code and/or use the most updated version, but it takes more work. (Pre-compiled binaries are the normal kinds of programs you would install on a computer from a CD or the internet; compiling it yourself means that you download the code and use a compiler, which turns this code into a binary program that your computer can run.) We will be downloading and installing the stable release of GRASS, which right now is GRASS 6.4.1 or GRASS 6.4.2.
For all of these installation instructions, your computer must have access to the internet.
In addition to the version and platforms mentioned, other versions of GRASS can be downloaded from \url{http://grass.osgeo.org/download/software.php}.
\section{Ubuntu (should work for Debian too)}
Open a terminal (CTRL+ALT+t). Type:
%\lstset{language=bash}
\begin{lstlisting}
sudo apt-get install grass
\end{lstlisting}
Type your password, press "y" for yes, and wait as the computer installs the program and its dependencies.
\section{Mac OS X}
(Written after installing GRASS for Mac OS 10.6: Snow Leopard)
Go to \url{http://grass.osgeo.org/grass64/binary/macosx/} for instructions. We're going to be a bit newer than what they suggest, so let's go for Python 2.7.X and a new-ish version of wxpython. As the website says, these are used to run the graphical interface. Python is also important to be able to build reusable scripts that automate tasks within GRASS in a way that allows more flexibility than shell (e.g., Bash) scripts.
Apple computers come with pre-installed python, but this is often out-of-date because of the 1-2 year Apple release cycle. So go to:
\\ \\
\noindent \url{http://www.python.org/download/releases/}
\\ \\
\noindent and click on the highest Python 2.X you can find. Then scroll down to "Mac OS X 64-bit/32-bit x86-64/i386 Installer" (under "Download"). As of the time of writing, the highest 2.X version of Python is 2.7.2, and a direct link to that is:
\\ \\
\noindent \url{http://www.python.org/ftp/python/2.7.2/python-2.7.2-macosx10.6.dmg}
\\
Now, go to \url{http://www.kyngchaos.com/software/unixport/grass}. This is the bottom link on your current page, \url{http://grass.osgeo.org/grass64/binary/macosx/}. This gives you the instructions to install GRASS and the required "frameworks packages". These are listed in the order in which they should be installed. Click on one of the frameworks packages or go directly to: \url{http://www.kyngchaos.com/software/frameworks} to download the \url{.dmg} files and install them.
As of the time of writing, these are direct links in the order that they should be installed for Mac OS 10.6 "Snow Leopard":
\\ \\
GDAL: \\
\url{http://www.kyngchaos.com/files/software/frameworks/PROJ_Framework-4.7.0-2-snow.dmg} \\
Free Type: \\
\url{http://www.kyngchaos.com/files/software/frameworks/FreeType_Framework-2.4.6-1-snow.dmg} \\
cairo: \\
\url{http://www.kyngchaos.com/files/software/frameworks/cairo_Framework-1.10.2-3a-snow.dmg} \\
You may also download the R programming language for statistics, but I won't be using it here.
Once all of these frameworks packages have been installed, you are ready to download and install GRASS. The links are given on the GRASS mac page, \url{http://www.kyngchaos.com/software/unixport/grass}. The URL for the download for Mac OS 10.6 is: \\
\\
\url{http://www.kyngchaos.com/files/software/grass/GRASS-6.4.1-5-Snow.dmg}
\begin{boxx}[!ht]
\begin{bclogo}[arrondi = 0.1, logo = \bcrosevents]{Good things to have for MacOS}
When I run MacOS, I like to use \emph{iTerm} as my terminal application, and either \emph{gedit} or \emph{textWrangler} as a text editor. These all have syntax highlighting and a bunch of nice additions that make programming and scripting easier. [Full-time Mac users may have better suggestions, so if you do, please let me know so I can provide these here!]
\end{bclogo}
\caption{Good things to have for MacOS}
\end{boxx}
\section{Windows 7}
There are two ways you can install GRASS in Windows. The first, the "Native Install", is very easy and runs "out-of-the-box". This is sufficient for the exercises that we will work here. However, it is incompatible with scripting in Bash to create reusable sets of code for repeating certain processes and analyses. (I think that the standard Windows GRASS should work fine with Python scripting, but I haven't tested this.) For running Bash scripts, you need to install the UNIX oriented version of GRASS.
The instructions on the following pages assume that you are running an Unix-like system, so you may have to make some changes if you are running Windows... I'm not entirely sure what those might be though, other than needing to enter commands in the GRASS command prompt instead of in either this command prompt or the terminal...
\subsection{Native Install}
The main page for GRASS for Windows is \url{http://grass.osgeo.org/grass64/binary/mswindows/}. This links to a download page, which also has a good introduction to GRASS: \\
\\
\url{http://grass.osgeo.org/grass64/binary/mswindows/native/}
\\ \\
To download GRASS, click the link under "Installing GRASS" on that web page, or go directly to this URL:
\\ \\
\url{http://grass.osgeo.org/grass64/binary/mswindows/native/WinGRASS-6.4.2RC2-1-Setup.exe}
\\ \\
and install that package.
\subsection{Unix oriented}
{\bf NOTE: I HAVE NOT YET TRIED THIS}
\\ \\
To install GRASS GIS to work in a Unix-like environment on Windows, you will need to download and install the Cygwin package and then install GRASS. For the most part, you can follow the instructions on:
\\ \\
\url{http://grass.osgeo.org/grass64/binary/mswindows/cygwin/}
\\ \\
When you get to \emph{Part 2: GRASS GIS installation, step 4}, the install file is the \url{grass-6.4.0*} file that you can see at the top of the page and will need to download.
In the "To start GRASS" instructions, you will want to use the new wxpython GUI: instead of typing:
\begin{lstlisting}
grass64 -tcltk
\end{lstlisting}
\noindent Type:
\begin{lstlisting}
grass64 -wx
\end{lstlisting}
\chapter{Starting GRASS and Creating a Location}
\section{Starting GRASS}
There are two ways to start GRASS GIS. You can click on the provided icon (Mac, Windows, Linux), or (in Linux and---probably---Windows running the Unix-like cygwin) you can open a terminal window and type:
\begin{lstlisting}
grass64 -wx
\end{lstlisting}
There must be a way to start it in the command line on Mac, but I haven't been able to make a Mac know where its programs are (\url{which grass} and \url{which grass64} turn up blanks).
If you click on the icon, check if your operating system also opens a terminal window. GRASS has an internal terminal, but I prefer the to use the system default one. (You, of course, are welcome to use either.)
Don't worry if you aren't used to using the command prompt: all of the GRASS commands have a very consistent structure and good documentation, and we will discuss those in depth later.
\section{Database manager \label{s:database}}
Your default database manager for vector data will be DBF. We want to change that to SQLite. This will be important later, but we should do it before we start anything else. We will store our databases in our GIS directory. See \url{http://grass.fbk.eu/gdp/html_grass64/grass-sqlite.html}.
\begin{lstlisting}
# GISDBASE=<THE_FOLDER_IN_WHICH_YOU_PLACED_GRASS: next line has my guess of what it is
# These definitions should work on UNIX-like OS's; not so sure about Windows
GISDBASE=$HOME/grassdata
LOCATION=GordonGulch
MAPSET=PERMANENT
# Then set the database
db.connect driver=sqlite database='$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite.db'
db.connect -p
\end{lstlisting}
\section{Creating a Location: Gordon Gulch \label{s:CreateLocation}}
Once you start GRASS, a graphical window will pop up. If this is your first time using GRASS, it will ask you to define a directory to hold all of your GRASS GIS files. I typically use something like the default (a ``grass'' or ``grassdata'' folder in my home directory).
Once this folder is created, you will see a start-up screen (see Figure \ref{fig:startup_screen}). Click {\bf Location Wizard}. You should see a window like Figure \ref{fig:location_wizard_start}. Set the project location to ``GordonGulch'' and the location title to ``Gordon Gulch - Snow Off'' so the location directory on your computer matches that in this documentation. Click ``next'', and select the ``Read projection and datum terms from a georeferenced data file'' option (Figure \ref{fig:location_wizard_method}). Lots of other projection selection methods are available, but we want to match the projection of the sample data file, and it is easiest to just pull that out of its metadata. Click ``next'' and then browse to the folder where you have the Gordon Gulch data that come with this tutorial. I usually click on the ``hdr'' file (see Figure \ref{fig:location_wizard_import_georeferencing}, but I think that it is smart enough to figure it out no matter which particular file you choose. Click ``next''. You'll see a summary of your selections. Then click ``Finish''. A window will pop up asking if you want to set the default region (Figure \ref{fig:set_window_popup}). Click ``no'' (or click ``yes'' and then don't change anything): the region is the bounding box of the map and the resolution, but this has already been imported properly from the Gordon Gulch LiDAR DEM metadata. If you do click ``yes'', you will see a screen like Figure \ref{fig:setregion_startup}.
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/ubuntu/main_screen.png}
\caption{GRASS GIS start-up screen on Ubuntu. If you have just installed GRASS, you will have no project locations or mapsets, and your ``Create mapset'', ``Rename mapset'', and ``Start GRASS'' buttons will be grayed out.}
\label{fig:startup_screen}
\end{center}
\end{figure}
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/ubuntu/location_wizard_start.png}
\caption{Location wizard start screen.}
\label{fig:location_wizard_start}
\end{center}
\end{figure}
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/ubuntu/location_wizard_method.png}
\caption{We will be using the metadata with the Gordon Gulch LiDAR file to georeference our map, though you can do this in many ways.}
\label{fig:location_wizard_method}
\end{center}
\end{figure}
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/ubuntu/location_wizard_import_georeferencing.png}
\caption{We will be using the metadata with the Gordon Gulch LiDAR file to georeference our map, though you can do this in many ways.}
\label{fig:location_wizard_import_georeferencing}
\end{center}
\end{figure}
\begin{figure}[h]
\begin{center}
\includegraphics[width=.5\linewidth]{figures/ubuntu/set_window_popup.png}
\caption{You can click ``no'', since the Gordon Gulck LiDAR metadata have already properly set the computational region, which is the window that covers the portion of the map of interest at the provided resolution. (Click ``yes'' to see the parameters that can be set, but don't change them.)}
\label{fig:set_window_popup}
\end{center}
\end{figure}
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/ubuntu/setregion_startup.png}
\caption{If you click ``yes'' to edit the region (see Figure \ref{fig:set_window_popup}), you will get a screen like this.}
\label{fig:setregion_startup}
\end{center}
\end{figure}
Now that you're done, you'll be back at the starting window, but with your new location ``GordonGulch'' defined (Figure \ref{fig:startup_screen_ready}). Inside GordonGulch, there is a mapset called ``PERMANENT''. This is the mapset that you will be using.
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/ubuntu/startup_screen_ready.png}
\caption{Back at the start window, but all set to go.}
\label{fig:startup_screen_ready}
\end{center}
\end{figure}
In collaborative projects the PERMANENT mapset typically contains only communal files, and each of you would have your own mapset created by clicking the ``create mapset'' button. For example, if we were making a geologic map, a DEM, scanned USGS topo sheets, and remotely sensed data products might be in the PERMANENT mapset. I could read these files, but could not write to them or change anyone else's personal mapset. A few of us could then have our own ``contacts'' vector maps that we would work on independently, without risk of changing someone else's work. When we feel that we are done with our respective sections of the map, we could merge our maps into a final ``contacts'' vector layer, and add that to PERMANENT. This basically exists to avoid common file management issues in collaborative work. The reason that we will be each using PERMANENT as our only mapset for this tutorial should now be obvious: you will be the only user of your GIS location.
There is a reason and a big advantage to this initial set-up step. GRASS internally manages its file structure, and keeps all of its data inside subdirectories of this directory. So long as you don't add and delete files from this directory outside of GRASS (unless you really know what you are doing), this keeps everything very well organized: I have never had to mess with any of this. A big advantage of this internal organization is that your GRASS directories are portable: just copy/paste the entire GRASS directory or location folder, and it will be properly set up on another machine without any of those painful broken links to data files.
\section{Starting a GRASS GIS session}
Now that you've finished setting up your location, you're ready to start using GRASS GIS! We will each use the PERMANENT mapset, since we are the sole users of our GIS projects and want access to everything.
Click on ``Start GRASS'' in the start-up window (Figure \ref{fig:startup_screen_ready}). This window will close, and in its place you will have 2 new GRASS windows (Figure \ref{fig:GUI}). These are the display and the layer manager. If you have a command line window open as well, you should see something that looks like Figure \ref{fig:macCLI}
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/mac/GUI.png}
\caption{The graphical user interface (GUI) windows in Mac OS. The right-hand window is the layer manager that is the main graphical interface for GRASS. The left-hand window is the map display.}
\label{fig:GUI}
\end{center}
\end{figure}
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/mac/macCLI.png}
\caption{The command line interface (CLI) in Mac.}
\label{fig:macCLI}
\end{center}
\end{figure}
\chapter{Basic Raster Display and Region Operations}
\section{Importing GDAL Raster Data \label{s:importgdal}}
We'll start by importing the DEM with the command line interface. Begin by navigating to the directory where the DEM files are located. For me, this looks like:
\begin{lstlisting}
GRASS 6.4.1 (GordonGulch):~ > cd Documents/geology_docs/courses/GRASS/data/czo_1m_gg_snwOff/czo_1m_gg
GRASS 6.4.1 (GordonGulch):~/Documents/geology_docs/courses/GRASS/data/czo_1m_gg_snwOff/czo_1m_gg > ls
dblbnd.adf hdr.adf metadata.xml prj.adf sta.adf w001001.adf w001001x.adf
\end{lstlisting}
The \url{cd} command changes directory. The \url{ls} command lists the files in the directory. (On Windows this second command is \url{dir}.) The text after the \url{ls} command is the list of files in my DEM directory: if this looks like what you have, then you are in the right place.
The command that you will be using is \url{r.in.gdal}, which imports raster data from any of the common GDAL formats (a GIS standard). Start by typing:
\begin{lstlisting}
r.in.gdal help
\end{lstlisting}
This brings up a brief manual for that command that should look something like:
\begin{lstlisting}
Description:
Import GDAL supported raster file into a binary raster map layer.
Keywords:
raster, import
Usage:
r.in.gdal [-oeflk] [input=name] [output=name] [band=value]
[memory=value] [target=string] [title="phrase"] [location=string]
[--overwrite] [--verbose] [--quiet]
Flags:
-o Override projection (use location's projection)
-e Extend location extents based on new dataset
-f List supported formats and exit
-l Force Lat/Lon maps to fit into geographic coordinates (90N,S; 180E,W)
-k Keep band numbers instead of using band color names
--o Allow output files to overwrite existing files
--v Verbose module output
--q Quiet module output
Parameters:
input Raster file to be imported
output Name for output raster map
band Band to select (default is all bands)
memory Cache size (MiB)
target Name of location to read projection from for GCPs transformation
title Title for resultant raster map
location Name for new location to create
\end{lstlisting}
\begin{boxx}[!ht]
\begin{bclogo}[arrondi = 0.1, logo = \bcrosevents]{Help!}
Typing ``help'' after any command gives a summary of what it does and how it should be used. I have been using GRASS for a long time, and I still do this fairly frequently to make sure that I am using commands correctly, especially when I am working in an interactive session. GRASS has a lot of features, and it can be hard to remember all of the options for all of them!
\end{bclogo}
\caption{Help!}
\end{boxx}
Our import is not going to need to use most of these commands. What we will need to do is to type:
\begin{lstlisting}
r.in.gdal input=hdr.adf output=topo
\end{lstlisting}
It is possible to shorten the names of the parameters (e.g., ``input'' to ``in'') so long as there is no other parameter that starts with those letters. Once again, I use the \url{hdr.adf} file, though I think that most of them should work for this import. Actually using this command should look something like this:
\begin{lstlisting}
GRASS 6.4.1 (GordonGulch):~/Documents/geology_docs/courses/GRASS/data/czo_1m_gg_snwOff/czo_1m_gg > r.in.gdal input=hdr.adf output=topo
Projection of input dataset and current location appear to match
100%
r.in.gdal complete. Raster map <topo> created.
GRASS 6.4.1 (GordonGulch):~/Documents/geology_docs/courses/GRASS/data/czo_1m_gg_snwOff/czo_1m_gg >
\end{lstlisting}
This command starts with ``r'' because it operates on rasters. GRASS uses these ``X.'' beginnings of commands to help differentiate the types of data on which they operate.
\begin{boxx}[!ht]
\begin{bclogo}[arrondi = 0.1, logo = \bcrosevents]{UNIX/GRASS terminal syntax}
Spaces separate distinct things in the UNIX terminal, so you can't have spaces around your ``='' signs. In GRASS, these spaces separate inputs to the commands. Variables are evaluated with ``\$'', and strings are concatenated by doing nothing special. For example:
\begin{lstlisting}
tmp = "testing"
d.mon x0 # open display monitor
d.rast $tmp # display "testing" raster, if it exists
d.out.file -t format=png output=$tmp_file # outputs "testing_file.png" --> d.out.file adds the extension
\end{lstlisting}
\end{bclogo}
Options sent to GRASS GIS commands can be shortened, if unambiguous. For example: ``column'' to ``col'', ``input'' to ``in'', ``output'' to ``out'', ... \\
Flags with one hyphen can be combined (e.g., ``-g -n'' = ``-gn''), and I typically put these before the main commands. I typcally put flags with two hyphens after commands; the most commonly-used of these is the overwrite flag, ``--o''. If this flag is not set, files will not be overwritten.
\caption{UNIX/GRASS terminal syntax}
\end{boxx}
\section{Viewing and setting the region}
All of the commands that you run in GRASS are subject to a particular computational window, which is called the ``region''. You can use the \url{g.region} command to look at the extent of this region and modify it.
\begin{lstlisting}
GRASS 6.4.1 (GordonGulch):~ > g.region -p
projection: 1 (UTM)
zone: 13
datum: nad83
ellipsoid: grs80
north: 4431084.5
south: 4428353.5
west: 457305.5
east: 461795.5
nsres: 1
ewres: 1
rows: 2731
cols: 4490
cells: 12262190
\end{lstlisting}
This command gives information on the projection type, region boundaries, and resolution. The current (and default) region is the entire area defined by the Gordon Gulch raster that you used to define the coordinate system. If you change the region and want to bring it back to this full extent and original resolution, type:
\begin{lstlisting}
GRASS 6.4.1 (GordonGulch):~ > g.region rast=topo
\end{lstlisting}
This sets the region to the resolution and edges of the raster ``topo''.
Note that this command starts with a ``g''. These are the general utilities, used for copying data sets, moving them, setting projections, and other general-purpose commands.
\section{Displaying Raster Data}
Now that we know that we have set the computational region to the full extent of the DEM, we are ready to display the data.
\subsection{Simple raster display with the command line}
We will start by viewing the raster data with the command line tool. This isn't as nice/interactive as the GUI, but allows us to write short scripts to create and save map images automatically. This can be nice for creating consistent figures for use in presentations, papers, etc.
We will do some more complex work with both the command line and GUI map displays later on, but for now we will just initialize a window and display the raster DEM.
\begin{lstlisting}
d.mon start=x0 # Start X-windowing display monitor #0
r.colors map=topo color=elevation # Use the scalable elevation color scheme for the topo map
d.rast map=topo # Display the topo map
\end{lstlisting}
When you run these commands, you should have a window appear that looks like Figure \ref{fig:DEMfig}.
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/mac/DEMfig.png}
\caption{A colorized digital elevation model (DEM) of Gordon Gulch in the Boulder Creek Critical Zone Observatory.}
\label{fig:DEMfig}
\end{center}
\end{figure}
\subsection{Displaying raster (elevation and shaded relief) maps in the graphical interface}
We can do the same thing in the graphical interface. In the ``GRASS GIS Layer Manager'' window, click on the icon with the checkerboard grid and the plus sign in the top bar. This is the raster display button. A window will appear as soon as you click it (Figure \ref{fig:display_raster}). There should be only one map in the drop down list, our ``topo''. Select it and hit ``OK''. You should now see the LiDAR DEM displayed a second time. If it isn't showing up correctly, you might have to hit the ``zoom to computational region'' button on the GRASS GIS Map Display that goes along with the GUI.
\begin{figure}
\begin{center}
\includegraphics[width=.9\linewidth]{figures/mac/display_raster.png}
\caption{Displaying raster data in the GUI. Note that the command line command is printed in the lower-left corner. A good way to learn how to script in GRASS is to use the graphical interface and read how what you do graphically relates to what you would type at the command prompt.}
\label{fig:display_raster}
\end{center}
\end{figure}
Now, let's add a shaded relief map. Since we have not yet executed any commands other than the map display with the graphical interface, we will use that to construct the shaded relief map. On the menu bar, click {\bf Raster $\rightarrow$ Terrain analysis $\rightarrow$ Shaded relief}. The input elevation map should be ``topo''. Under the ``optional'' tab, set the output shaded relief map name to ``shaded''. Click ``Run''.
This is equivalent to running the following on the command line:
\begin{lstlisting}
> r.shaded.relief map=topo shadedmap=shaded
Calculating shading, please stand by.
100%
Color table for raster map <shaded> set to 'grey'
Shaded relief map created and named <shaded>.
\end{lstlisting}
We can show the shaded relief map using the GUI. Add the shaded relief map to the layer manager below the ``topo'' DEM. Then right-click on topo and select ``change opacity level''. This cannot be done using the command line in GRASS 6.4 (the command line graphics gets an overhaul in the upcoming GRASS 7.0). I like 30--40\% opacity on the DEM that overlays the shaded relief map.
Now you have a pretty map that combines a DEM with shaded relief to give a good sense of what the topography looks like (Figure \ref{fig:topo_relief_overlay}). If you feel really proud of your map, you can click the ``export image'' button on the toolbar and save it to a file.
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/ubuntu/topo_relief_overlay.png}
\caption{The gorgeous Gordon Gulch!}
\label{fig:topo_relief_overlay}
\end{center}
\end{figure}
\subsection{Combining rasters for display in the command line: elevation and shaded relief \label{s:drastCLI}}
It is also possible to combine raster maps on the command line into a nice-looking result. This allows you to script the generation of maps, which I find especially useful for time-series of data.
I use \url{r.blend} so I can export the resultant set of rasters later on in this tutorial. Another command, \url{d.shadedmap}, works well for draping maps over one another in the display monitor without creating any new rasters. While you can't export the raster directly from the display window, you can also use \url{d.save}, \url{d.out.file}, and other commands to save an image of the current display.
\begin{lstlisting}
# Blend two rasters into a nice result!
r.blend first=topo second=shaded output=colored_shaded_relief percent=40
# This creates a RGB triplet for the shaded relief map that you can display
d.mon x0
d.rgb r=colored_shaded_relief.r g=colored_shaded_relief.g b=colored_shaded_relief.b
# We can use commands to add a title and other nifty features too!
# d.title is used to display the map title and other info; this is
# in the map's metadata.
# d.text adds selected text
# There are lots of d.* commands to choose from!
\end{lstlisting}
\begin{boxx}[!ht]
\begin{bclogo}[arrondi = 0.1, logo = \bcrosevents]{Tips for the terminal}
If you aren't used to the command prompt, but have a nice terminal emulator, there are some nice tools to know about. The first is autocomplete: press ``tab'' once to complete the current command / file path / etc. you are typing, or at least get it to the point at which it becomes ambiguous as to what you wanted to type. Pressing ``tab'' twice gives you the list of all of the commands / file paths / etc. that start with what you have typed.
\end{bclogo}
\caption{Tips for the terminal}
\end{boxx}
\chapter{Topographic and hydrologic analyses}
\section{Slope, aspect, and resampling \label{s:slope.aspect.resample}}
Let's try another example using the graphical interface (GUI) instead of the command line. We will measure slope and aspect.
Go to the Raster menu. {\bf Raster $\rightarrow$ Terrain analysis $\rightarrow$ Slope and aspect}. Set the raster map to "topo", then click the "outputs" tab. We won't calculate $x$- and $y$- derivatives, as these are in an arbitrary orientation with respect to the hills and valleys. We instead will calculate the slope (steepest decent), aspect, profile (steepest descent orientation) curvature, and tangential (shallowest descent orientation) curvature. I call these "slope", "aspect", "pcurv", and "tcurv", respectively. (See Figure \ref{fig:slope_aspect}.) Look at the bottom. The command-line output is given here. This means that by entering values in the GUI, you can teach yourself how to use the command line -- which is very nice as you start to want to write pre-packaged analysis algorithms. With the GUI window for r.slope.aspect selected, press CTRL+c (or Command+c on mac -- actually, I can't get this to work on mac), and you copy this command-line string:
\begin{lstlisting}
r.slope.aspect elevation=topo slope=slope aspect=aspect pcurv=pcurv tcurv=tcurv
\end{lstlisting}
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/ubuntu/slope_aspect.png}
\caption{Names for slope and aspect outputs.}
\label{fig:slope_aspect}
\end{center}
\end{figure}
Click "run" and it prints out this command line string and runs the desired command.
Try displaying one of the curvature maps in the viewer. It looks like a bunch of random noise (Figure \ref{fig:tcurv1m}). This is because of the high (1 meter) resolution of the LiDAR data: tiny bumps on the surface are dominating the signal. Let's resample these data to 5 meter resolution. First, we have to change our computational region's resolution. Then we use the "r.resample" tool to coarsen our raster map.
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/ubuntu/tcurv1m.png}
\caption{At 1 meter resolution, local topographic variations completely dominate the curvature signal, removing any obvious signal of the larger-scale landscape.}
\label{fig:tcurv1m}
\end{center}
\end{figure}
\begin{lstlisting}
g.region -p nsres=5 ewres=5 # "-p" prints the computational window.
r.resample input=topo output=topo5m
\end{lstlisting}
Now let's run r.slope.aspect again:
\begin{lstlisting}
r.slope.aspect elevation=topo5m slope=slope5m aspect=aspect5m pcurv=pcurv5m tcurv=tcurv5m
\end{lstlisting}
That's better. The profile curvature follows the hillslope contours well, and the tangential curvature traces out river channels (Figure \ref{fig:tcurv5m}). Look at the slope maps as well, and how much more continuous and smooth the 5m slope map is than the 1m slope map.
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/ubuntu/tcurv5m.png}
\caption{At 5 meter resolution, the tangential curvature is still noisy, but channel networks have become visuallly discernable.}
\label{fig:tcurv5m}
\end{center}
\end{figure}
\section{Drainage Networks \label{s:drainage_networks}}
Now let's build some drainage networks. By now you have the general idea behind the syntax, so we'll get right to it. We are going to start by building watersheds with a multi-flow-direction algorithm.
\begin{boxx}[!ht]
\begin{bclogo}[arrondi = 0.1, logo = \bcrosevents]{GRASS GIS online (and offline) help}
To learn about the multi-flow-direction algorithm, or most of the other algorithms in GRASS, you can go to their help page. Look at the main GUI window (the one that shows the layers in display or the command line). On its toolbar, click on the rescue ring; this opens a browser window with the GRASS help. \\
The GRASS help index tells you how to use the commands and what research, theories, and/or publications the commands implement. This help often comes with examples and/or diagrams to better explain the situations. \\
In this particular case, we will navigate to the raster command index and look for \emph{r.watershed}, a watershed basin creation program. [NEW REF]
\end{bclogo}
\caption{GRASS GIS online (and offline) help}
\end{boxx}
[FIGURES]
\begin{lstlisting}
g.region -p nsres=1 ewres=1 # Back to 1-m resolution
# Use "r.watershed help" to find out what the various outputs are
# We are using multi-direction flow; you can use single-direction ("SFD")
# by removing the "-f" flag
# Threshold=62500 means that we need an accumulation area > 250x250 cells
# = 250x250 meters here
r.watershed -f elevation=topo accumulation=accum_mfd drainage=draindir_mfd basin=basins_mfd stream=streams_mfd threshold=62500
# Thin the channels raster so we can vectorize it
r.thin input=streams_mfd output=streams_mfd_thinned
# And to vector of stream channels
# Vector and raster datasets may have the same name without overwriting each other.
r.to.vect input=streams_mfd_thinned output=streams_mfd
# Now to vectorize drainage basins
r.to.vect input=basins_mfd output=basins_mfd feature=area
\end{lstlisting}
The ``thinning'' step ensures that there are no clumps of pixels, and that a single vector line therefore can be drawn cleanly through the raster during the \url{r.to.vect} step.
\begin{boxx}[!ht]
\begin{bclogo}[arrondi = 0.1, logo = \bcrosevents]{GRASS add-ons}
GRASS GIS has a number of add-ons. These are applications that have not been fully adopted and integrated into the GRASS suite of tools, but that can still be very useful. One of these, \url{r.stream}, is used for the kinds of watershed problems that we are tackling right now. We will be using the built-in GRASS tools in this tutorial, but to learn more about the GRASS add-ons, go to their wiki page: \url{http://grass.osgeo.org/wiki/GRASS_AddOns}.
\end{bclogo}
\caption{GRASS add-ons}
\end{boxx}
While the multiple flow direction algorithm is good for providing a more accurate representation of flow paths, single flow direction is needed for stream profiling. We are also going to relax the threshold drainage area down to 10,000 cells (10,000 m$^2$ = 100x100 meters). Let's do that now.
\begin{lstlisting}
r.watershed elevation=topo accumulation=accum drainage=draindir basin=basins stream=streams threshold=10000
r.thin input=streams output=streams_thinned
r.to.vect input=streams_thinned output=streams
r.to.vect input=basins output=basins feature=area
\end{lstlisting}
Now we have a set of single vector stream lines in the basin, each of which represents a segment of the full river channel.
Where I have been using the threshold, I have been doing so in terms of cells. But if your cell sizes vary, or you want to input real flow contributions from each cell, you can do that by setting the \url{flow} parameter in \url{r.watershed} to the name of a raster map.
Let's cap our calculatory achievments by displaying the SFD streams on top of the color shaded relief via the command line:
\begin{lstlisting}
d.mon x0
# d.vect -c map=basins_mfd type=boundary # These get confusing, but this is how to display area boundaries
d.rgb r=colored_shaded_relief.r g=colored_shaded_relief.g b=colored_shaded_relief.b
d.vect map=streams color=blue
\end{lstlisting}
\section{Gordon Gulch drainage basin from set pour point \label{s:GGbasin}}
We can use the GRASS \url{r.water.outlet} function to build basins from pour points. In the next exercise, we will do that, along with vectorizing the resultant drainage basin.
A pour point is the selected map location from which to calculate the upstream catchment area. This is given by the \url{easting} and \url{northing} values, which I have typed here to be the outlet of Gordon Gulch, thus making us find the largest basin on the map.
When we vectorize the drainage basin area, note that we have to clean the topology by removing small (1-pixel) areas that are created along with the large drainage basin. This sometimes happens when converting from raster to vector. I use \url{v.clean}, a topology cleaning tool, to remove these small areas. If there are small areas without centroids like these that you would actually want to keep, you can try to add centroids to them with the \url{v.centroids} tool.
\begin{lstlisting}
# Gordon Gulch basin. Output: 1=inside basin; 0=outside basin
r.water.outlet easting=461795 northing=4429173 drainage=draindir basin=GGbasin
# Set non-basin cells to NULL
r.null map=GGbasin setnull=0
# Convert raster to vector: Use the "-s" flag if you want to smooth the vector instead
# of having rectangular pixel-shaped edges
r.to.vect in=GGbasin out=GGbasin feature=area
# What? 4 areas created? But only 1 centroid?
# Need to clean topology after raster conversion. All of the areas without
# centroids are just one pixel, so we will use v.clean:
v.clean in=GGbasin out=tmp tool=rmarea thresh=1 --o
# And now we will copy over our GGbasin vector file and remove "tmp",
# all in one fell swoop, with "rename":
g.rename vect=tmp,GGbasin --o
\end{lstlisting}
Let's look at this in the GRASS graphical user interface. First, click on the ``add vector'' button (Figure \ref{fig:addVector}). Then follow the sequence of events in Figures \ref{fig:dispVector}, \ref{fig:hydroPlot}, and \ref{fig:hydroMap} to display the basin.
\begin{figure}[h]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/ubuntu/addVector.png}
\caption{Click on this button to open a GUI \protect\url{d.vect} dialog to display the vector map and select display preferences.}
\label{fig:addVector}
\end{center}
\end{figure}
\begin{figure}[!h]
\begin{center}
\includegraphics[width=.45\linewidth]{figures/ubuntu/selectVector.png}
\includegraphics[width=.45\linewidth]{figures/ubuntu/deselectCentroidArea.png}\\
\vspace{2mm}
\includegraphics[width=.45\linewidth]{figures/ubuntu/linewidth2.png}
\caption{Select the vector to display. Then go through the tabs to show only the boundary and make the line width be 2.}
\label{fig:dispVector}
\end{center}
\end{figure}
\begin{figure}[!h]
\begin{center}
\includegraphics[width=.7\linewidth]{figures/ubuntu/hydroPlot.png}
\caption{I've added our shaded relief map with topography draped over it and a blue vector map of the streams (calculated with the SFD algorithm) to my map along with the drainage basin.}
\label{fig:hydroPlot}
\end{center}
\end{figure}
\begin{figure}[!htb]
\begin{center}
\includegraphics[width=.9\linewidth]{figures/ubuntu/addMapElements.png}
\includegraphics[width=.9\linewidth]{figures/ubuntu/hydroMapScale.png}
\caption{In the display window of the GUI, I have added a North arrow and scale bar.}
\label{fig:hydroMap}
\end{center}
\end{figure}
This is all for now, but we go back to this drainage basin and these streams in Chapter \ref{s:vector}, in which we learn how to work with vector data and database tables.
\afterpage{\clearpage}
\clearpage
\section{Solar radiation}
Another important topographic analysis is solar radiation. GRASS GIS has a \url{r.sun} module that does just this, with all the bells and whistles (direct, diffuse, and reflected radiation, variable surface albedo, atmospheric conditions, etc.)
You will need to have run r.slope.aspect to get the slope and aspect in decimal degrees as inputs to the solar radiation file (Section \ref{s:slope.aspect.resample}).
The following is an example of r.sun usage. I haven't used it too much, so I don't have too much to say about it, but it looks pretty powerful. The help page (\url{http://grass.fbk.eu/gdp/html_grass64/r.sun.html} for GRASS 6.4.X) gives a pretty thorough description of it, as well as a nice bibliography of the literature (largely solar energy papers) from which the \url{r.sun} developers drew. The GRASS wiki page has more information: \url{http://grass.osgeo.org/wiki/R.sun}. I've thought about linking it in with measured temperature data: \url{r.sun} could help to obtain the upper boundary heat flux, and could be a way towards obtaining a realistic distributed thermal history for a region that has limited point measurements.
\begin{lstlisting}
# lat~40, but we don't need this: GRASS knows where we are
# This process may take several minutes, especially when you are running it for a whole day!
r.sun -s elevin=topo aspin=aspect slopein=slope day=28 beam_rad=beam_irradiation_day insol_time=insolation_time_day diff_rad=diffuse_irradiation_day refl_rad=reflected_irradiation_day
# Let's sum all of the three radiation types for the whole-day option, using the map calculatior:
r.mapcalc "total_irradiation_day28 = beam_irradiation_day + diffuse_irradiation_day + reflected_irradiation_day"
# Given time - segfaults as with 6.4.1: fixed in newer versions (6.4.2 might be release version now, so we could be safe on this)
# Segfault also caused by including latitude as either value or raster
r.sun -s elevin=topo aspin=aspect slopein=slope lat=40 day=28 time=8.0 beam_rad=beam_irradiance_0800 diff_rad=diffuse_irradiance_0800 refl_rad=reflected_irradiance_0800
\end{lstlisting}
As a quick review of plotting with the addition of a legend, and an early introduction to Bash scripting (Section \ref{s:BashScripting}) and file I/O (Chapter \ref{s:FIO}), I am going to run the following script to display and save images of all of the time steps. I am also using a number of plotting bells and whistles as an example of their usage. The output is given in Figure \ref{fig:rad}
\begin{lstlisting}
d.mon x1 # start display monitor
# start a for loop
for radrast in beam_irradiation_day diffuse_irradiation_day reflected_irradiation_day total_irradiation_day28
do
# "echo" is Bash's cute way of saying "print this to an output"; by default, stdout = terminal window
# "$" in Bash indicates that a variable should be evaluated; otherwise, it is treated as itself
# (e.g., radrast = radrast; $radrast = beam_irradiation_day)
echo $radrast
d.rast $radrast
d.title -ds map=$radrast
d.grid -gw size="0:01" color=white textcolor=black fontsize=16
d.legend map=$radrast color=black at=15,85,15,18
d.barscale bcolor=none at=0,93
# save the image here
d.out.file -t output=$radrast format=png --o
sleep 1 # Take a look at it!
d.erase
done
\end{lstlisting}
\begin{figure}[h]
\begin{center}
\includegraphics[width=.45\linewidth]{figures/d.out.file/beam_irradiation_day.png}
\includegraphics[width=.45\linewidth]{figures/d.out.file/diffuse_irradiation_day.png}
\vspace{3mm}
\includegraphics[width=.45\linewidth]{figures/d.out.file/reflected_irradiation_day.png}
\includegraphics[width=.45\linewidth]{figures/d.out.file/total_irradiation_day28.png}
\caption{Solar radiation maps for January 28th. Some of the features in the standard GRASS output, like the grid and legend text, are thin and don't show up well in this multi-color figure. This is a weakness of the GRASS automated displays.}
\label{fig:rad}
\end{center}
\end{figure}
\section{Cosmogenic dating}
One of the many things on my long to-do list is to create a GRASS GIS program to obtain cosmogenic production rates. While I havne't even started coding this, I'll say a little here about how I would do this in GRASS.
GRASS has a function called \url{r.horizon}. It gets the elevation of the horizon from around a point on a DEM. This, coupled the elevation of the sample and an atmospheric thickness function, should be able to give a production rate with a global-uniform assumption. The point also has an $(x,y)$ position. From this, any spatial variability in cosmogenic production rates should be calculatable.
\chapter{Vectors and databases \label{s:vector}}
\section{Vector topology}
In GRASS GIS, an ``area'' comprises a ``boundary''---a line that encloses it---and a ``centroid''---a point within the area that shows on which side of the line the area exists. Vector attributes for areas are attached to the centroids of these areas. Boundaries are shared between areas, and have their own set of attributes: they know about the areas on either side of them, their lengths, etc.
This is a ``topological'' paradigm for vector files that prevents the slivers and overlaps in vector files that are common in shapefiles. (I think that Arc/Info had topologically correct features, but ArcDesktop stopped doing that, possibly to make our lives more difficult...) Anyway, Figure \ref{fig:vector_paradigms} shows the differences between shapefiles and the topological vectors that GRASS uses. The figures are from the GRASS Wiki, \url{http://grass.osgeo.org/wiki/Digitizing_Area_Features}; this page has more information on vector topology.
\begin{figure}[h]
\begin{center}
\includegraphics[width=.45\linewidth]{figures/GRASSwiki/Shape.png}
\includegraphics[width=.45\linewidth]{figures/GRASSwiki/Grass_vectors.png}
\caption{GRASS maintains vector topology, with shared boundaries between areas and individual areas defined by centroids. Figures from GRASS Wiki, \protect\url{http://grass.osgeo.org/wiki/Digitizing_Area_Features}.}
\label{fig:vector_paradigms}
\end{center}
\end{figure}
Other aspects of GRASS vector topology are that points cannot overlap, and lines should not cross or overlap. The whole point of this is to make sure that there are not redundancies or ambiguities in the vecto data: careful preparation of vector data sets can be hugely important for data processing and interpretation!
\section{Managing databases and uploading vector attributes}
We will learn how to manage vector databases with the \url{GGbasin} vector created in Section \ref{s:GGbasin}. This vector has only 1 area and no shared boundaries, so it is the simplest possible starting point (aside from, well, a point).
My most-commonly used command is \url{v.db.select}. This command prints the database values to the screen (or a file), and optionally filters them according to a SQL queery. Try it out! When I do, this happens:
\begin{lstlisting}
GRASS 6.4.1 (GordonGulch):~/grasstmp > v.db.select GGbasin
cat|value|label
1|1|
\end{lstlisting}
The category, ``cat'', is a unique ID for each vector feature that starts at 1 and goes upward. ``Value'' is 1 from the conversion from raster. ``Label'' is empty, because there was no label attached to the drainage basin raster.
Let's add the area of the drainage basin. To do this, we need to add a column:
\begin{lstlisting}
v.db.addcol map=GGbasin columns="area double precision"
\end{lstlisting}
Check it out: a new column is here!
\begin{lstlisting}
GRASS 6.4.1 (GordonGulch):~/grasstmp > v.db.select GGbasin
cat|value|label|area
1|1||
\end{lstlisting}
Columns can be ``double precision'', ``int'', ``varchar'', or ``date''. Other types exist as well, depending on the database manager being used, but I have never had to use anything but the first three mentioned here.
To put a real value in this column, we must get the vector attribute (known but hidden) into the database table. This uses the command \url{v.to.db}. Makes sense, huh? Vector to database! I choose ``units=k'' for km$^2$.
\begin{lstlisting}
GRASS 6.4.1 (GordonGulch):~/grasstmp > v.to.db map=GGbasin opt=area columns=area units=k
Reading areas...
100%
Updating database...
100%
1 categories read from vector map (layer 1)
1 records selected from table (layer 1)
1 categories read from vector map exist in selection from table
1 records updated/inserted (layer 1)
GRASS 6.4.1 (GordonGulch):~/grasstmp > v.db.select GGbasincat|value|label|area
1|1||4.195303
\end{lstlisting}
There we go! This can be used to find neighbors, line slopes, perimeters, and lots more fun stuff. Something I've thought about is writing a slope-area calculation algorithm for a whole landscape... would be very do-able, but I just haven't done it yet!
\section{Advanced Vector and Database Processing}
I thought about subtitling this section \textbf{Streams (lines), Stream Segment Endpoints (points), and Drainage Basins (areas)}. It could also be subtitled \textbf{Manipulating Lines and Points, Using Databases, and File I/O}. So this may make it sound like a mixed bag, but it will make sense when it is all done. The point of it is that we can do a lot of nifty stuff with lines, what we've just learned about getting attribute values, and some basin piping of output to extract drainage basins and/or get some pretty interesting landscape characteristics.
\subsection{Obtaining and cleaning points in stream networks}
Back in Section \ref{s:drainage_networks}, we created a set of vector lines for streams. Each of the lines in this vector extends between the two nearest next tributary junctions, the start of the river system (determined by the drainage threshold we selected), and/or the edge of the map.
We can extract points from this vector using the \url{v.to.points} function. In this case, I am setting the \url{-n} flag to look just at the nodes (endpoints) of the lines. The \url{-v} command would get all of the vertices of the line, and \url{-i} would interpolate between these vertices.
\begin{lstlisting}
v.to.points -n input=streams output=streams_endpoints
# Multiple points at tributary junctions: let's clean the topology
v.clean in=streams_endpoints out=tmp tool=rmdupl
# The current attribute tables do not represent our cleaned vector, so let's drop them
v.db.droptable -f map=tmp layer=1
v.db.droptable -f map=tmp layer=2
# We also caused categories to merge by removing duplicate points; let's
# regenerate them in a nice, sequential list
v.category in=tmp out=tmp2 opt=del --o
v.category in=tmp2 out=tmp opt=add --o
# And now we'll regenerate a new table with our new categories
v.db.addtable map=tmp # Adds a table with just the default "cat" column
# Now that we're done, we will overwrite the input file
g.copy vect=tmp,streams_endpoints --o
\end{lstlisting}
I will now add the positions of those points with \url{v.to.db}, as well as their elevations via queerying the raster map with \url{v.what.rast}. (Getting their locations probably won't be necessary, but it's good practice; note also that you can use \url{v.to.db} to get the start and end points of lines directly, in a database table that is attached to those lines. Cool, huh?)
\begin{lstlisting}
v.db.addcol map=streams_endpoints columns="x double precision, y double precision, z double precision"
v.to.db map=streams_endpoints col=x,y units=meters option=coor
v.what.rast vect=streams_endpoints rast=topo col=z
# Run a quick v.db.select to check on your handiwork
v.db.select streams_endpoints
\end{lstlisting}
Some vector files (not this one) have z-coordinates attached as well, meaning that we could skip the \url{v.what.rast} step and just use \url{col=x,y,z}.
\subsection{Queerying categories}
Let's say we want to find only the point that we used to define the major basin of Gordon Gulch. This point should have the lowest of all of the values in our vector categories---and indeed should likely have the lowest elevation of all the points on the map.
Doing this requires making database queeries. Remember Section \ref{s:database}, way back up top? This is where that becomes important. We defined our database manager as SQLite, which is a useful subset of SQL (``Structured Queery Language''), pronounced ``sequel''. Honestly, I find SQL to be a total pain, but once you understand its (rigid) structure and that it is better than the normal set of database managers out there, you start to put up with it and use it.
SQL can be used to perform queeries. For example, to find the minimum elevation from the set of stream endpoints and assign it to variable ``zmin'', I type:
\begin{lstlisting}
zmin=`echo "SELECT MIN(z) from streams_endpoints" | db.select -c`
# Without the "-c" flag, we would need this to cut off the column name:
# zmin=${zmin:7:${#zmin}} #
\end{lstlisting}
Now that we have this value, we can find the column that is associated with this and save its $x$ and $y$ coordinates.
\begin{lstlisting}
# First, print it all
echo "SELECT * from streams_endpoints WHERE z=$zmin" | db.select
# Then save the latitude and longitude
GGx=`echo "SELECT x from streams_endpoints WHERE z=$zmin" | db.select -c`
GGy=`echo "SELECT y from streams_endpoints WHERE z=$zmin" | db.select -c`
\end{lstlisting}
The commands with pattern \url{db.*} are the purely-databse-oriented queeries.
We can't do this ``x = MIN(x)'' queery in a single step because a SQL command that returns a single value like MIN() can't be used to queery whole columns. See - sort of a pain! This is one way in which the GRASS Python interface becomes useful: vector data can be ingested as lists, which are waaaay more flexible and a lot less painful to use. But let's keep in 100\% GRASS + Bash to do the rest for now.
Now we can build our watershed from before using entirely vector outputs, without having to pick (by hand) these values.
\begin{lstlisting}
r.water.outlet easting=$GGx northing=$GGy drainage=draindir basin=GGbasinVARxy
\end{lstlisting}
... but if you go to display this raster, you'll find out that it is NOT our big basin! We are looking at a small enough area that the normal assumption that the biggest basin's outlet will have the lowest elevation won't always hold. Interesting!
\subsection{Extracting vector subsets}
Let's use our big basin vector to extract only those streams that are within it. We use \url{v.select} for this.
\begin{lstlisting}
v.select ainput=streams binput=GGbasin operator=overlap output=GGstreams
\end{lstlisting}
Other useful commands that are like this are \url{v.overlay}, which combines (overlays) two vector maps, and \url{v.extract}, which extracts a subset of a vector map based on database queeries.
The output of this command can be produced in the display window (Figure \ref{fig:GGdrainage}):
\begin{lstlisting}
d.mon x0
d.shadedmap reliefmap=shaded drapemap=topo brighten=15
d.vect map=GGstreams color=blue
d.vect map=GGbasin color=black type=boundary width=2
d.text -b size=8 text="Gordon Gulch Drainage" color=black font=romanc
d.barscale bcolor=none at=0,93
\end{lstlisting}