在R中的geotiff文件中均匀着色完美平坦的表面(连续像素)

时间:2013-09-06 04:24:45

标签: r gis gdal

我正在使用RGDAL读入并绘制地理文件文件:

df.gtiff = readOGR("/df.tif")
image(df.gtiff, red=1, green=2, blue=3)

地图包含一些完美平坦的表面,它们是湖泊或海洋部分,即有连续像素具有相同高度的斑块。有没有办法识别这些补丁,比如多边形,这样我就可以统一着色(例如蓝色)? [注意:文件太大而无法上传,因此下面提供了gdalinfo(df.tif)):

Driver: GTiff/GeoTIFF
Files: ~/df.tif
Size is 2928, 2285
Coordinate System is:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433],
    AUTHORITY["EPSG","4326"]]
Origin = (81.381111111111110,22.564444444444447)
Pixel Size = (0.002083333333333,-0.002083333333333)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (  81.3811111,  22.5644444) ( 81d22'52.00"E, 22d33'52.00"N)
Lower Left  (  81.3811111,  17.8040278) ( 81d22'52.00"E, 17d48'14.50"N)
Upper Right (  87.4811111,  22.5644444) ( 87d28'52.00"E, 22d33'52.00"N)
Lower Right (  87.4811111,  17.8040278) ( 87d28'52.00"E, 17d48'14.50"N)
Center      (  84.4311111,  20.1842361) ( 84d25'52.00"E, 20d11' 3.25"N)
Band 1 Block=2928x1 Type=Byte, ColorInterp=Red
Band 2 Block=2928x1 Type=Byte, ColorInterp=Green
Band 3 Block=2928x1 Type=Byte, ColorInterp=Blue

1 个答案:

答案 0 :(得分:2)

你可能只需要做

image( df.gtiff , col = rep( "cornflowerblue" , ncell(df.gtiff) ) )

但如果不起作用,这里有一些有用的例子:

require( raster )

#  Observe difference between a brick and a raster
logoB <- brick(system.file("external/rlogo.grd", package="raster"))
logoR <- raster(system.file("external/rlogo.grd", package="raster"))

#  Use a brick and plotRGB to get intended colours (image uses heat.colors)
par(mfrow=c(1,2))
image( logoR , axes = FALSE , labels = FALSE )
plotRGB( logoB )

enter image description here

#  To manually mask and colour to a single value:
#  This file doesn't have NA values so setting e.g. plot( logoR , col = "blue" ) won't work...

#  Example of cell values for background colour
logoB[1]
     red green blue
[1,] 255   255  255

#  Make binary mask with some threshold of 'close to white' or 'close to black' values
newLogo <- calc( logoB , function(x) sum( x ) < 725 + 0 | sum( x ) < 50 + 0 )

#  Then plot, supplying a colour for each binary level
plot(newLogo , col = c( "white" , "cornflowerblue" ) , main = "binary mask" )

enter image description here

# If your file has background cells set to NA you can just plot directly setting the colour equal to number of cells in raster:

require(maptools)
r <- raster(system.file("external/test.grd", package="raster"))
image( r , col = rep("cornflowerblue" , ncell(r)) , axes = FALSE )

enter image description here