1
|
#####################################################################################
|
2
|
# FixDEMMosaicBlendedRows: Implements Image Blending treatment to DEM Mosaic image
|
3
|
# Collection of functions that attempt to mitigate ASTER / SRTM the boundary edge artifact
|
4
|
#
|
5
|
# Inputs:
|
6
|
# 1) DEM Mosaic image containing 'boundary edge' artifact
|
7
|
# 2) 'Aster-Only' subimage corresponding to the NORTHernmost part (adjacent to the 60 degree Lat
|
8
|
# boundary) of the SRTM component of the mosaic.
|
9
|
# 3) 'Aster-Only' subimage corresponding to the SOUTHernmost part (adjacent to the 60 degree Lat
|
10
|
# boundary) of the ASTER component of the mosaic.
|
11
|
# 4) numBlendRows: number of number of rows south of 60 degrees N Lat to modify ('blend') by
|
12
|
# generating (lnear) combination of SRTM and ASTER image pixels.
|
13
|
#
|
14
|
# Output:
|
15
|
# 1) A copy of the input DEM mosaic, with 'blended image' boundary edge treatment.
|
16
|
#
|
17
|
# To run:
|
18
|
#
|
19
|
# 1) start R
|
20
|
# 2) > source("FixDEMMosaicBlendedRows.r")
|
21
|
# 3) > FixDEMMosaicBlendedRows()
|
22
|
#
|
23
|
# Note: the input images used by this program located on /jupiter: /data/project/organisms/rcr/BoundaryFixTest
|
24
|
#
|
25
|
# Author: Rick Reeves, NCEAS
|
26
|
# June 12, 2011
|
27
|
#
|
28
|
##############################################################################################
|
29
|
FixDEMMosaicBlendedRows <- function()
|
30
|
{
|
31
|
require(raster)
|
32
|
require(rgdal)
|
33
|
|
34
|
# Key parameter: number of rows south of 60 degrees N Lat to modify ('blend') by
|
35
|
# generating (lnear) combination of SRTM and ASTER image pixels.
|
36
|
|
37
|
numBlendRows <- 100
|
38
|
|
39
|
# Read the mosaic image and component sub-images:
|
40
|
# - Aster/SRTM mosaic image (with boundary edge to be fixed)
|
41
|
# - 'Aster-only' subimage adjacent to Southern edge of 60 degree bounls -s dary,
|
42
|
# sampled into a grid with same extent and spatial resolution as image mosaic
|
43
|
# - 'Aster-only' subimage adjacent to Northern edge of 60 degree boundary,
|
44
|
# sampled into a grid with same extent and spatial resolution as image mosaic
|
45
|
# inAsterNorthFG <- raster("./aster60DegNorthRasterFG.tif")
|
46
|
# inAsterSouthFG <- raster("./aster59DegSouthRasterFG.tif")
|
47
|
# inBdyAsterBoth <- raster("./boundaryTestCase115DegBothAsterMos.img")
|
48
|
|
49
|
inMosaic <- raster("./AsterSrtm3ArcSecTestImg.tif")
|
50
|
inAsterSouth <- raster("./boundaryTest115_59DegreeAster.img")
|
51
|
inAsterSouthFG <- raster("./aster59DegSouthRasterFG.tif")
|
52
|
inAsterNorth <- raster("./boundaryTest115_60DegreeAster.img")
|
53
|
|
54
|
# First, get the extent of the 'below 60' sub image in the (big) input raster
|
55
|
|
56
|
northAsterEx <- extent(inAsterNorth)
|
57
|
southAsterEx <- extent(inAsterSouth)
|
58
|
|
59
|
# Create copy of input raster that we will use to create a 'fixed boundary' iamge
|
60
|
# Even SIMPLER, according to raster documentation
|
61
|
|
62
|
outMosaic <- raster(inMosaic)
|
63
|
|
64
|
sFixedRasterFile <- "TestOutRasterBdyBlendFixAft2.tif"
|
65
|
|
66
|
# create the output raster by copying the input file with 'block copy'
|
67
|
|
68
|
message("Copy input mosaic: Create output (repaired) mosaic")
|
69
|
#browser()
|
70
|
bs <- blockSize(inMosaic)
|
71
|
outMosaic <- writeStart(outMosaic,sFixedRasterFile, datatype="INT2S", format="GTiff",overwrite=TRUE)
|
72
|
for (iCtr in 1 : bs$n)
|
73
|
{
|
74
|
mosaicVals <- getValues(inMosaic,row=bs$row[iCtr],nrows=bs$nrows[iCtr])
|
75
|
writeValues(outMosaic,mosaicVals,bs$row[iCtr])
|
76
|
message(sprintf(".....processed input mosaic block %d",iCtr))
|
77
|
}
|
78
|
outMosaic <- writeStop(outMosaic)
|
79
|
#
|
80
|
message("Input mosaic copied to output raster: now, process boundary")
|
81
|
browser()
|
82
|
|
83
|
# now, we SHOULD be able to modify outMosaic with new 'column values'.
|
84
|
|
85
|
southCellsOfInterest <- cellsFromExtent(outMosaic,southAsterEx)
|
86
|
northCellsOfInterest <- cellsFromExtent(outMosaic,northAsterEx)
|
87
|
|
88
|
firstNorthSubImgRow <- rowFromCell(outMosaic,northCellsOfInterest[1])
|
89
|
lastNorthSubImgRow <- rowFromCell(outMosaic,northCellsOfInterest[(length(northCellsOfInterest) - 1)])
|
90
|
|
91
|
firstSouthSubImgRow <- rowFromCell(outMosaic,southCellsOfInterest[1])
|
92
|
lastSouthSubImgRow <- rowFromCell(outMosaic,southCellsOfInterest[(length(southCellsOfInterest) - 1)])
|
93
|
|
94
|
# note: last north image and first south image rows are the same.
|
95
|
|
96
|
northBorderEdgeRow <- lastNorthSubImgRow
|
97
|
southBorderEdgeRow <- firstSouthSubImgRow + 1
|
98
|
|
99
|
# The border 'edge' row is one row below the top of the south sub image
|
100
|
|
101
|
northBrdrRowVals <- getValues(outMosaic,northBorderEdgeRow,1)
|
102
|
southBrdrRowVals <- getValues(outMosaic,southBorderEdgeRow,1)
|
103
|
|
104
|
# Process the mosaic rows:
|
105
|
# Blend the first 'numBlendRows' south of the 60 degree line (first rows of the SRTM component)
|
106
|
# with the underlying and corresponding ASTER image rows.
|
107
|
# For first attempt, use a linear combination; for second attempt, use an exponential function.
|
108
|
#
|
109
|
nImageCols <- ncol(inMosaic)
|
110
|
sRows <- seq(southBorderEdgeRow,((southBorderEdgeRow + numBlendRows) - 1))
|
111
|
#
|
112
|
rowVecValuesBlend <- vector(mode="integer",length=nImageCols)
|
113
|
iRowCtr <- 1
|
114
|
for (curMosRow in sRows)
|
115
|
{
|
116
|
message(sprintf("transforming cur row: %d",curMosRow))
|
117
|
#:browser()
|
118
|
|
119
|
# even simpler here, as we implement a 'linear distance decay' function,
|
120
|
# and let each new column be a linear combination of the Aster and CGIAR image layers
|
121
|
|
122
|
deltaInc <- iRowCtr / as.numeric(numBlendRows)
|
123
|
colVecCells <- cellFromRowCol(outMosaic,curMosRow,1:nImageCols)
|
124
|
|
125
|
# get the current row from the mosaic(SRTM data) and raster images
|
126
|
# We get the ASTER values from a version of the 'South' ASTER image
|
127
|
# with same extent as the image mosaic.
|
128
|
|
129
|
rowVecValuesMosaic <- getValuesBlock(outMosaic,row=curMosRow,nrows=1,col=1,ncols=nImageCols)
|
130
|
rowVecValuesAster <- getValuesBlock(inAsterSouthFG,row=curMosRow,nrows=1,col=1,ncols=nImageCols)
|
131
|
rowVecValuesBlend <- (rowVecValuesMosaic * deltaInc) + (rowVecValuesAster * (1.0 - deltaInc))
|
132
|
|
133
|
# create the merged row as a linear combination of the Mosaic and Aster image.
|
134
|
# note: we need to deal with NA values, which are possible.
|
135
|
|
136
|
# write the blended row to the output mosaic
|
137
|
|
138
|
outMosaic[colVecCells] <- as.integer(round(rowVecValuesBlend))
|
139
|
iRowCtr <- iRowCtr + 1
|
140
|
}
|
141
|
#message(sprintf("cur col: %d about to write to outMosaic",curMosCol))
|
142
|
#browser()
|
143
|
|
144
|
# write the modified outMosaic values to the image file that we created.
|
145
|
|
146
|
message("Done with raster blending : hit key to update raster disk file..")
|
147
|
browser()
|
148
|
writeRaster(outMosaic, sFixedRasterFile, datatype="INT2S", format="GTiff",overwrite=TRUE)
|
149
|
#browser()
|
150
|
}
|