Project

General

Profile

« Previous | Next » 

Revision f6c86ce4

Added by Jim Regetz over 13 years ago

  • ID f6c86ce40a633a4ec15a80d5b06f89729929eded

added GRASS/R code to run/compare flow analysis on Oregon test area

View differences:

terrain/flow/flow-comparison-oregon.R
1
# Exploratory assessment of differences between ESRI's flow direction
2
# output and GRASS Terraflow's flow direction output, with focus here on
3
# the SFD (D8) Terraflow output, which is (I think) basically the same
4
# as what ESRI uses.
5
#
6
# The input DEM in all cases is what I gather to be the Oregon case
7
# study DEM, which is this 3x3 degree, 3" resolution, smoothed (somehow?
8
# by someone?) SRTM:
9
#   jupiter:~organisms/flow/experimental/srtmv41_smth
10
# 
11
# The ESRI flowdirection raster used here is what I gather was produced by 
12
# layers.aml, which applies the GRID flowdirection function to the
13
# smoothed DEM to produce this output:
14
#   jupiter:~organisms/flow/experimental/flowdir
15
#
16
# I converted both of the above rasters to GeoTIFFs to bring them over
17
# to my machine to work with.
18
#
19
# I then generated the two Terraflow output flow direction grids (SFD
20
# and MFD) using the commands contained in grass-terraflow.sh.
21
#
22
# Other resources:
23
# * ESRI ArcGIS 9.3 Flow Direction documentation
24
#   http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?topicname=flow%20direction
25
# * GRASS terraflow documentation
26
#   http://grass.osgeo.org/grass64/manuals/html64_user/r.terraflow.html
27
#
28
# Jim Regetz
29
# NCEAS
30
# Created 16-Jun-2011
31

  
32

  
33
# data directory (local to xander)
34
datadir <- "~/media/temp/dem/new/flow"
35

  
36
# load data
37

  
38
# ESRI flowdirection output (see notes above)
39
or.flowdir.esri <- raster(file.path(datadir, "flowdir_OR_esri.tif"))
40
# GRASS terraflow (MFD) output 
41
or.flowdir.grass.mfd <- raster(file.path(datadir,
42
    "flowdir_OR_grass.tif"))
43
# GRASS terraflow (SFD) output 
44
or.flowdir.grass.sfd <- raster(file.path(datadir,
45
    "flowdir_OR_grass_sfd.tif"))
46

  
47
# create raster indicating where ESRI and terraflow(SFD) differ
48
flow.disagreement <- or.flowdir.esri
49
flow.disagreement[] <- ifelse(values(or.flowdir.esri !=
50
    or.flowdir.grass.sfd), 1, NA)
51

  
52
# illustrated differences
53
plot(or.hs, col=grey.colors(100))
54
mp <- prod(dim(flow.disagreement))
55
plot(flow.disagreement, add=TRUE, col="blue", legend=FALSE, maxpixels=mp)
56

  
57
# now isolate differences in cases where terraflow is not zero (i.e.,
58
# places calculated as pits?
59
flow.disagreement[or.flowdir.grass.sfd==0] <- NA
60
plot(flow.disagreement, add=TRUE, col="red", legend=FALSE, maxpixels=mp)
61

  
62
# notice what happens at the edges...
63

  
64
# in northernmost row, esri routes most cells east or west (i.e, along
65
# border) plus some north, and a few other misc directions 
66
table(as.matrix(or.flowdir.esri)[1,])
67
##    1    2    4    8   16   32   64 
68
## 1574    4    1    3 1600    1  425 
69

  
70
# whereas terraflow SFD routes all cells north
71
table(as.matrix(or.flowdir.grass.sfd)[1,])
72
##   64 
73
## 3608 
74

  
75
# (analogous patterns for the other 3 raster edges)
76

  
77
# quantify proportion in concordance, excluding the edges
78
edges <- c(1, 3608)
79
mean(as.matrix(or.flowdir.esri)[-edges, -edges] ==
80
    as.matrix(or.flowdir.grass.sfd)[-edges, -edges])
81
## [1] 0.9086855
82

  
83
# ...and what proportion are either in concordance or have 0 values
84
# associated with terraflow SFD? (means pits?)
85
mean((as.matrix(or.flowdir.esri)[-edges, -edges] ==
86
  as.matrix(or.flowdir.grass.sfd)[-edges, -edges]) |
87
  as.matrix(or.flowdir.grass.sfd)[-edges, -edges]==0)
88
## [1] 0.9755789
89

  
terrain/flow/flow-comparison-oregon.sh
1
# GRASS terraflow execution for Oregon case study area, done for
2
# comparison to the pre-existing Arc GRID flowaccumulation results.
3
#
4
# Jim Regetz
5
# NCEAS
6
# Created 16-Jun-2011
7

  
8
export DEMDIR=~/media/temp/dem/new/flow
9
export OUTDIR=~/media/temp/dem/new/flow
10

  
11
#
12
# Oregon case study
13
#
14

  
15
# create new mapset for oregon case study
16
g.mapset -c mapset=oregon
17

  
18
# read in smoothed srtm
19
# ming did smoothing? note that data type is float
20
r.in.gdal input=$DEMDIR/srtmv41_smth.tif output=srtmv41_smth
21

  
22
# set region based on this srtm raster
23
g.region rast=srtmv41_smth
24

  
25
# compute flow
26
# [took ~3 minutes on xander (16-Jun-2011)]
27
r.terraflow elevation=srtmv41_smth filled=filled_smth \
28
  direction=direction_smth swatershed=swatershed_smth \
29
  accumulation=accumulation_smth tci=tci_smth
30

  
31
# compute flow again, but this time using SFD (D8)
32
# [took ~1.5 minutes on xander (16-Jun-2011)]
33
r.terraflow -s elevation=srtmv41_smth filled=filled_smth_sfd \
34
  direction=direction_smth_sfd swatershed=swatershed_smth_sfd \
35
  accumulation=accumulation_smth_sfd tci=tci_smth_sfd
36

  
37
# write geotiffs out to disk
38
r.out.gdal input=direction_smth output=$OUTDIR/flowdir_OR_grass.tif
39
r.out.gdal input=direction_smth_sfd output=$OUTDIR/flowdir_OR_grass_sfd.tif

Also available in: Unified diff