1
|
##############################################################################################
|
2
|
#
|
3
|
# makeImagePairTable.r
|
4
|
#
|
5
|
# R script generates table of adjacent ASTER / SRTM / Mosaic Border / CDEM pixel values
|
6
|
# to be used to generate plots of 'delta elevation' vs 'pixel pair proximity' and 'elevation'
|
7
|
# suggested by Mark Schildhauer on May 3.
|
8
|
#
|
9
|
# Author: Rick Reeves, NCEAS
|
10
|
# May 4, 2011
|
11
|
##############################################################################################
|
12
|
#
|
13
|
makeImagePairTable <- function()
|
14
|
{
|
15
|
require(raster)
|
16
|
require(rgdal)
|
17
|
|
18
|
inputCgiarRaster <- raster("/data/project/organisms/rcr/AsterCgiarMerge/mergeCgiarAsterBdySRTM_BLEven.tif")
|
19
|
inputAsterRaster <- raster("/data/project/organisms/rcr/AsterCgiarMerge/mergeCgiarAsterBdyASTER_BLEven.tif")
|
20
|
inputMosaicRaster <- raster("/data/project/organisms/rcr/AsterCgiarMerge/mergeCgiarAsterBdyFinalBLEven.tif")
|
21
|
inputCDEMRaster <- raster("/data/project/organisms/rcr/ValidateBoundary/CDEMMosCgiarAsterBdy_BLEven.tif")
|
22
|
#
|
23
|
# Difference image for entire merged image takes a while to create,
|
24
|
# so we created it once, now read it back in.
|
25
|
#
|
26
|
rDeltaWhole <- raster("/data/project/organisms/rcr/ValidateBoundary/DeltaEntireImage.tif")
|
27
|
rDeltaWhole@data@values <-getValues(rDeltaWhole)
|
28
|
|
29
|
# Create extent objects used to extract raster subimges.
|
30
|
# The object will be centered along the 60 degree North latitude line,
|
31
|
# and have varying depths (number of rows).
|
32
|
# The Western Canada study area runs from -135 (west) to -100 (west) longitude,
|
33
|
# and 55.0 to 64.00 degrees (north) latitude.
|
34
|
# the ASTER and SRTM/CGIAR image components are merged at the 60 Deg N Latitude line.
|
35
|
|
36
|
#eTestAreaExtent <- extent(-135.2,-100.2, 59.997,60.001) # Creates a 5 row subimage
|
37
|
eTestAreaExtent <- extent(-135.0,-105.0, 59.995,60.005) # Creates a 12 row subimage
|
38
|
|
39
|
# Extract a sub image corresponding to the selected extent.
|
40
|
# Two different alternatives:
|
41
|
# The extract() function returns a vector of cell values,
|
42
|
# the crop() function returns a complete raster* object.
|
43
|
|
44
|
rEdgeRegionAster <- crop(inputAsterRaster,eTestAreaExtent)
|
45
|
rEdgeRegionCgiar <- crop(inputCgiarRaster,eTestAreaExtent)
|
46
|
rEdgeRegionMosaic <- crop(inputMosaicRaster,eTestAreaExtent)
|
47
|
rEdgeRegionCDEM <- crop(inputCDEMRaster,eTestAreaExtent)
|
48
|
|
49
|
# Important: In order for the image subtraction to work, the extents
|
50
|
# of the two images must be IDENTICAL. I used ArcMap GIS Raster Crop By Mask
|
51
|
# to create subimages with identical extents.
|
52
|
|
53
|
# Compute the difference image for the entire study area, and for the region along
|
54
|
# the boundary (narrow, maybe 10 pixels either side)
|
55
|
#extent(rEdgeRegionMosaic) = extent(rEdgeRegionCDEM) # raster package author suggests this to resolve slight extent differences
|
56
|
rEdgeRegionDelta <- rEdgeRegionMosaic - rEdgeRegionCDEM
|
57
|
|
58
|
# get a vector of random column index numbers, constrained by column dimension of image
|
59
|
# Loop three times, sampling pixel pairs from above, below, across the border
|
60
|
|
61
|
nColsToGet <- 2000
|
62
|
iDiffVecNorth <- vector(mode="integer",length=nColsToGet)
|
63
|
|
64
|
iDiffVecBorder <- vector(mode="integer",length=nColsToGet)
|
65
|
iDiffVecSouth <- vector(mode="integer",length=nColsToGet)
|
66
|
|
67
|
# Here is the output matrix, (nColSamples * 3) rows, four columns
|
68
|
colsToGet <-sample(1:inputMosaicRaster@ncols,nColsToGet)
|
69
|
|
70
|
mOutTable <- matrix(nrow=(nColsToGet * 3), ncol=5)
|
71
|
colnames(mOutTable) <- c("ColumnID","elevNorth","elevSouth","cdemNorth","cdemSouth")
|
72
|
# in this difference image, the border edge occurs at column 6
|
73
|
|
74
|
# NOTE Wed eve: A better start and end for the border: firstRow=5, lastRow = 8....
|
75
|
iFirstRow <- 5 #4 # two rows before the border
|
76
|
iLastRow <- 8 #7 # two rows after the border
|
77
|
iRowCtr = 1 # points to latest output table being written
|
78
|
message(sprintf("starting col sample loop"))
|
79
|
for (iNextCol in colsToGet)
|
80
|
{
|
81
|
#message(sprintf("in col sample loop getting col sample %d - hit key..",iNextCol))
|
82
|
#browser()
|
83
|
|
84
|
rColVecMosaic <- cellFromRowCol(rEdgeRegionMosaic,iFirstRow:(iLastRow),iNextCol:iNextCol)
|
85
|
rColVecCDEM <- cellFromRowCol(rEdgeRegionCDEM,iFirstRow:(iLastRow),iNextCol:iNextCol)
|
86
|
|
87
|
# Split the column vector into the pairs that Mark requested
|
88
|
# For each column sampled, the output table has three rows:
|
89
|
#
|
90
|
# Row 1: from (input) Aster layer: Two pixels above border
|
91
|
# Row 2: from (input) Mosaic layer: Two pixels straddling border rDeltaWhole@data@values[rColVec]
|
92
|
# Row 3: from (input) Cgiar layer: Two pixels below border
|
93
|
#
|
94
|
# each with four columns, arranged into two column pairs:
|
95
|
#
|
96
|
# North Pixel and South Pixel elevation, North Pixel and South Pixel CDEM (baseline)
|
97
|
#
|
98
|
mOutTable[iRowCtr,1] <- iNextCol # The (radomly sampled) col ID (surrogate for longitude)
|
99
|
mOutTable[iRowCtr,2:3] <- rEdgeRegionMosaic@data@values[rColVecMosaic[1:2]] # First column pair from extracted vector:
|
100
|
mOutTable[iRowCtr,4:5] <- rEdgeRegionCDEM@data@values[rColVecCDEM[1:2]] # entirely top (ASTER part of mosaic) image
|
101
|
iRowCtr = iRowCtr + 1
|
102
|
#
|
103
|
mOutTable[iRowCtr,1] <- iNextCol # The (radomly sampled) col ID (surrogate for longitude)
|
104
|
mOutTable[iRowCtr,2:3] <- rEdgeRegionMosaic@data@values[rColVecMosaic[2:3]] # Second column pair:
|
105
|
mOutTable[iRowCtr,4:5] <- rEdgeRegionCDEM@data@values[rColVecCDEM[2:3]] # straddles border region
|
106
|
iRowCtr = iRowCtr + 1
|
107
|
#
|
108
|
mOutTable[iRowCtr,1] <- iNextCol # The (radomly sampled) col ID (surrogate for longitude)
|
109
|
mOutTable[iRowCtr,2:3] <- rEdgeRegionMosaic@data@values[rColVecMosaic[3:4]] # third column pair:
|
110
|
mOutTable[iRowCtr,4:5] <- rEdgeRegionCDEM@data@values[rColVecCDEM[3:4]] # entirely bottom (SRTM part of mosaic) image
|
111
|
iRowCtr = iRowCtr + 1
|
112
|
}
|
113
|
|
114
|
# write the table out as CSV file
|
115
|
|
116
|
message("end of loop - hit key to write output table..")
|
117
|
browser()
|
118
|
write.csv(mOutTable,file="/data/project/organisms/rcr/tableForRick2000_5_8Even.csv",row.names=FALSE)
|
119
|
#
|
120
|
}
|