1
|
##############################################################################################
|
2
|
#
|
3
|
# R Script performs edge filtering on Raster files
|
4
|
#
|
5
|
#
|
6
|
##############################################################################################
|
7
|
filterDiffRasterImage <- function(sInCDEMRasterName,sInAstCgiarMosName)
|
8
|
{
|
9
|
require(raster)
|
10
|
require(rgdal)
|
11
|
|
12
|
# Read a raster image file into Raster object
|
13
|
|
14
|
#setwd("/data/project/organisms/rcr/ValidateBoundary")
|
15
|
#setwd("t:/organisms/rcr/ValidateBoundary")
|
16
|
|
17
|
#sInCDEMName <- "CanadaDemMosTuesdayNewWGS84.tif"
|
18
|
#sInAstCgiarMosName <- "mergeCgiarAsterBdyFinalBLMonday.tif"
|
19
|
|
20
|
inputCDEMRaster <- raster(sInCDEMRasterName)
|
21
|
inputAsterCgiarRaster <- raster(sInAstCgiarMosName)
|
22
|
inputAsterCgiarRasterRes <- raster("./mergeCgiarAsterTuesdayCDEMGrid2.tif")
|
23
|
|
24
|
# Create an extent object with the coordinates of the 'edge zone'
|
25
|
# along the 60 degree North latitude line
|
26
|
# The Western Canada study area runs from -135 (west) to -100 (west) longitude,
|
27
|
# and 55.0 to 64.00 degrees (north) latitude.
|
28
|
|
29
|
eTestAreaExtent <- extent(-135.2,-100.2, 59.995,60.005) # this results in a 12 row subimage
|
30
|
eTestAreaExtent <- extent(-135.2,-100.2, 59.997,60.001) # Creates a 5 row subimage CENTERED on 60N Latitude
|
31
|
|
32
|
# extract a sub area corresponding to the extent.
|
33
|
# The extract() function returns a vector of cell values,
|
34
|
# the crop() function returns a raster* object.
|
35
|
|
36
|
vEdgeRegionCDEM <- extract(inputCDEMRaster,eTestAreaExtent)
|
37
|
rEdgeRegionCDEM <- crop(inputCDEMRaster,eTestAreaExtent)
|
38
|
vEdgeRegionAsterCgiar <- extract(inputAsterCgiarRaster,eTestAreaExtent)
|
39
|
rEdgeRegionAsterCgiar <- crop(inputAsterCgiarRaster,eTestAreaExtent)
|
40
|
|
41
|
vEdgeRegionAsterCgiarRes <- extract(inputAsterCgiarRasterRes,eTestAreaExtent)
|
42
|
rEdgeRegionAsterCgiarRes <- crop(inputAsterCgiarRasterRes,eTestAreaExtent)
|
43
|
|
44
|
# for raster subtraction to work, the raster object extracts must work.
|
45
|
# for some reason, the extent for these two were off by .1 degree.
|
46
|
# here is a workaround - It resulted in an 'aligned' image pair, for
|
47
|
# which the difference image values matched the actual differences
|
48
|
# between ASTER and CDEM values.
|
49
|
extent(rEdgeRegionAsterCgiar) <- extent(rEdgeRegionCDEM) # only do if extents dont match
|
50
|
rDelta <- rEdgeRegionAsterCgiar - rEdgeRegionCDEM
|
51
|
rDeltaRes <- rEdgeRegionAsterCgiarRes - rEdgeRegionCDEM
|
52
|
rBigDeltaRes <- inputAsterCgiarRasterRes - inputCDEMRaster
|
53
|
|
54
|
# compare the statistics between these two difference images
|
55
|
|
56
|
# Construct the filter kernal: a 3 x 3 integer matrix
|
57
|
|
58
|
# Execute the filter
|
59
|
|
60
|
# Compute image stats?
|
61
|
|
62
|
# display the file?
|
63
|
|
64
|
# Write the output file to disk
|
65
|
|
66
|
writeRaster(rEdgeRegionCDEM,filename="EdgeRegionCDEM5RowsS.tif",
|
67
|
format="GTiff",datatype="INT2S",overwrite=TRUE)
|
68
|
|
69
|
writeRaster(rEdgeRegionAsterCgiar,filename="EdgeRegionAsterCgiar5RowsS.tif",
|
70
|
format="GTiff",datatype="INT2S",overwrite=TRUE)
|
71
|
|
72
|
writeRaster(rEdgeRegionAsterCgiarRes,filename="EdgeRegionAsterCgiar5RowsResamp.tif",
|
73
|
format="GTiff",datatype="INT2S",overwrite=TRUE)
|
74
|
|
75
|
writeRaster(rDelta,filename="EdgeRegionDelta5RowsS.tif",
|
76
|
format="GTiff",datatype="INT2S",overwrite=TRUE)
|
77
|
writeRaster(rDeltaRes,filename="EdgeRegionDelta5RowsResS.tif",
|
78
|
format="GTiff",datatype="INT2S",overwrite=TRUE)
|
79
|
|
80
|
#writeRaster(rBigDelta,filename="CgiarCDEMRegionDelta.tif",
|
81
|
# format="GTiff",datatype="INT2S",overwrite=TRUE)
|
82
|
#writeRaster(rBigDeltaRes,filename="CgiarCDEMRegionDeltaRes.tif",
|
83
|
# format="GTiff",datatype="INT2S",overwrite=TRUE)
|
84
|
}
|