Rで解析:「raster」パッケージのコマンド紹介

Rの解析に役に立つ記事
スポンサーリンク

rasterパッケージは「セル(ドット)の並びでデータを表現する」多くのコマンドが収録されています。パッケージの中から、日常用途に使いそうなコマンドをまとめました。

パッケージのバージョンは3.5-15。実行コマンドはwindows 11のR version 4.1.3で確認しています。

スポンサーリンク

パッケージのインストール

下記コマンドを実行してください。

#パッケージのインストール
install.packages("raster")

実行コマンド

詳細はコマンド、パッケージヘルプを確認してください。

#パッケージの読み込み
library("raster")

###簡単な例#####
#rasterレイヤーの作成:rasterコマンド
x <- raster()
##&#27083;&#25104;&#12398;&#30906;&#35469;
#&#12487;&#12540;&#12479;&#12399;&#31354;&#12391;&#12377;
x
class       : RasterLayer 
dimensions  : 180, 360, 64800  (nrow, ncol, ncell)
resolution  : 1, 1  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 

#&#12500;&#12463;&#12475;&#12523;&#12469;&#12452;&#12474;&#12398;&#22793;&#26356;:res&#12467;&#12510;&#12531;&#12489;
#&#20516;&#12434;&#22823;&#12365;&#12367;&#12377;&#12427;&#12392;&#30011;&#36074;&#12364;&#33618;&#12367;&#12394;&#12426;&#12414;&#12377;
#&#12524;&#12452;&#12516;&#12540;&#12469;&#12452;&#12474;&#12364;&#22793;&#12431;&#12426;&#12414;&#12377;
res(x) <- 30
x
class       : RasterLayer 
dimensions  : 6, 12, 72  (nrow, ncol, ncell)
resolution  : 30, 30  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 

#&#12524;&#12452;&#12516;&#12540;&#12395;&#12487;&#12540;&#12479;&#12434;&#20195;&#20837;:values&#12467;&#12510;&#12531;&#12489;
#data source,names,values
values(x) <- matrix(sample(1:300, 72, replace = TRUE), 6, 12)
x
class       : RasterLayer 
dimensions  : 6, 12, 72  (nrow, ncol, ncell)
resolution  : 30, 30  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 5, 298  (min, max)

#&#12487;&#12540;&#12479;&#12398;&#35336;&#31639;&#12467;&#12510;&#12531;&#12489;:calc&#12467;&#12510;&#12531;&#12489;
#&#12467;&#12510;&#12531;&#12489;&#12434;&#23455;&#34892;&#12377;&#12427;&#12392;&#26032;&#35215;&#12395;&#12521;&#12473;&#12479;&#12540;&#12487;&#12540;&#12479;&#12364;&#20316;&#25104;&#12373;&#12428;&#12414;&#12377;
calc(x, function(x) { x * 2})
class       : RasterLayer 
dimensions  : 6, 12, 72  (nrow, ncol, ncell)
resolution  : 30, 30  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 10, 596  (min, max)

#&#12487;&#12540;&#12479;&#12398;&#20998;&#24067;&#12434;&#30906;&#35469;:densty,hist&#12467;&#12510;&#12531;&#12489;
density(x)
hist(x)

#&#29305;&#23450;&#12475;&#12523;&#12398;&#12487;&#12540;&#12479;&#12434;&#21462;&#24471;&#12377;&#12427;&#26041;&#27861;
#RasterLayer[i]&#12391;&#25351;&#23450;:i&#12399;&#12475;&#12523;&#30058;&#21495;
x[23]
120
#RasterLayer[rowNo, ColNo]
x[2, 11]
120

#x,y&#12398;&#12487;&#12540;&#12479;&#31684;&#22258;&#12363;&#12425;&#12475;&#12523;&#30058;&#21495;&#12434;&#21462;&#24471;:cellFromXY&#12467;&#12510;&#12531;&#12489;
#&#12487;&#12540;&#12479;&#12434;&#12503;&#12525;&#12483;&#12488;&#12377;&#12427;&#12392;&#29702;&#35299;&#12375;&#12420;&#12377;&#12356;&#12391;&#12377;
cellFromXY(x, c(120, 50))
[1] 23

#x&#36600;&#12398;&#12487;&#12540;&#12479;&#31684;&#22258;&#12363;&#12425;&#21015;&#30058;&#21495;&#12434;&#21462;&#24471;:colFromX&#12467;&#12510;&#12531;&#12489;
colFromX(x, 120)
[1] 11

#y&#36600;&#12398;&#12487;&#12540;&#12479;&#31684;&#22258;&#12363;&#12425;&#21015;&#30058;&#21495;&#12434;&#21462;&#24471;:rowFromY&#12467;&#12510;&#12531;&#12489;
rowFromY(x, 50)
[1] 2

#&#12487;&#12540;&#12479;&#31684;&#22258;&#12395;&#21547;&#12414;&#12428;&#12427;&#12475;&#12523;&#12398;&#20516;&#12434;&#21462;&#24471;:fourCellsFromXY&#12467;&#12510;&#12531;&#12489;
fourCellsFromXY(x, cbind(c(-150, 150), c(-50, 50)))
     [,1] [,2] [,3] [,4]
[1,]   50   62   61   49
[2,]   24   12   11   23

#&#34892;&#30058;&#21495;&#12434;&#25351;&#23450;&#12375;&#12390;&#12475;&#12523;&#12398;&#30058;&#21495;&#12434;&#21462;&#24471;:cellFromRow&#12467;&#12510;&#12531;&#12489;
cellFromRow(x, 2)
[1] 13 14 15 16 17 18 19 20 21 22 23 24

#&#21015;&#30058;&#21495;&#12434;&#25351;&#23450;&#12375;&#12390;&#12475;&#12523;&#12398;&#30058;&#21495;&#12434;&#21462;&#24471;:cellFromCol&#12467;&#12510;&#12531;&#12489;
cellFromCol(x, 11)
[1] 11 23 35 47 59 71

#&#34892;&#21015;&#12434;&#25351;&#23450;&#12375;&#12390;&#12475;&#12523;&#12398;&#30058;&#21495;&#12434;&#21462;&#24471;:cellFromRowCol&#12467;&#12510;&#12531;&#12489;
cellFromRowCol(x, 2, 11)
[1] 23

#x, y&#36600;&#12398;&#31684;&#22258;&#12434;&#22793;&#26356;:extent
extent(x) <- c(0, 1, 0, 1)
x
class       : RasterLayer 
dimensions  : 6, 12, 72  (nrow, ncol, ncell)
resolution  : 0.08333333, 0.1666667  (x, y)
extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 5, 298  (min, max)

#&#12524;&#12452;&#12516;&#12540;&#12434;&#12503;&#12525;&#12483;&#12488;
plot(x)

#&#12475;&#12523;&#33394;&#12398;&#22793;&#26356;
SetCol = colorRampPalette(c("#00bfd4", "#6e5f72", "#e3deda"))
plot(x, col = SetCol(10))
########

###&#20197;&#38477;&#12289;&#19979;&#35352;raster&#12434;&#20351;&#29992;#####
##&#12469;&#12452;&#12474;&#12434;&#25351;&#23450;&#12375;&#12524;&#12452;&#12516;&#12540;&#12434;&#20316;&#25104;
TESTRaster <- raster(nrow = 10, ncol = 10, 
                     xmn = -1000, xmx = 1000, ymn = -100, ymx = 900)
values(TESTRaster) <- sample(1:10, 10 * 10, replace = TRUE)
plot(TESTRaster, col = SetCol(10))
########

#&#12488;&#12522;&#12511;&#12531;&#12464;:crop&#12467;&#12510;&#12531;&#12489;
#&#31684;&#22258;&#12398;&#25351;&#23450;:extent&#12458;&#12503;&#12471;&#12519;&#12531;;(xmin, xmax, ymin, ymax)
#&#12488;&#12522;&#12511;&#12531;&#12464;&#12487;&#12540;&#12479;1
CropRas1 <- crop(TESTRaster, extent(-300, 300, 100, 300))
plot(CropRas1)
#&#12488;&#12522;&#12511;&#12531;&#12464;&#12487;&#12540;&#12479;2
CropRas2 <- crop(TESTRaster, extent(-1000, -500, 600, 800))
plot(CropRas2)

#&#12521;&#12473;&#12479;&#12540;&#12487;&#12540;&#12479;&#12398;&#32113;&#21512;:merge&#12467;&#12510;&#12531;&#12489;
CropMerge <- merge(CropRas1, CropRas2, overlap = TRUE)
plot(CropMerge, col = SetCol(10))

#&#20999;&#12426;&#25244;&#12365;:&#12475;&#12523;&#30058;&#21495;&#12434;&#21462;&#24471;&#12377;&#12427;cellsFromExtent&#12467;&#12510;&#12531;&#12489;&#12398;&#21033;&#29992;
#raster&#12398;&#35079;&#35069;
CutOutRaster <- TESTRaster
#&#20999;&#12426;&#25244;&#12365;&#31684;&#22258;&#12434;&#20516;&#12391;&#25351;&#23450;
CellExtent <- cellsFromExtent(CutOutRaster, extent(-500, 500, 200, 600))
#&#31684;&#22258;&#12434;NA&#12391;&#32622;&#12365;&#25563;&#12360;
CutOutRaster[CellExtent] <- NA
plot(CutOutRaster, col = SetCol(10))

#&#21608;&#22258;&#12395;&#12475;&#12523;&#12434;&#36861;&#21152;:extend&#12467;&#12510;&#12531;&#12489;
#&#36861;&#21152;&#12377;&#12427;&#12475;&#12523;&#25968;&#12434;&#25351;&#23450;:y&#12458;&#12503;&#12471;&#12519;&#12531;
plot(extend(CutOutRaster, y = 1), col = SetCol(10))

#&#12487;&#12540;&#12479;&#12398;&#21453;&#36578;:flip&#12467;&#12510;&#12531;&#12489;
#&#22522;&#28310;&#36600;&#12434;&#25351;&#23450;:direction&#12458;&#12503;&#12471;&#12519;&#12531;;"x" or "y"
FlipCutOutRaster <- flip(CutOutRaster, direction = "y")
plot(FlipCutOutRaster, col = SetCol(10))

#&#12487;&#12540;&#12479;&#12398;&#20998;&#24067;&#12434;&#30906;&#35469;:layerize&#12467;&#12510;&#12531;&#12489;
plot(layerize(CutOutRaster), col = SetCol(10))

#&#12475;&#12523;&#12391;&#12399;&#12394;&#12367;&#12471;&#12531;&#12508;&#12523;&#12391;&#12503;&#12525;&#12483;&#12488;:rasterToPoints&#12467;&#12510;&#12531;&#12489;
plot(rasterToPoints(CutOutRaster), col = SetCol(10), pch = 17)

#&#12487;&#12540;&#12479;&#12364;&#28961;&#12356;&#31623;&#25152;&#12434;0&#12391;&#22475;&#12417;&#12390;&#12521;&#12473;&#12479;&#12540;&#12434;&#32080;&#21512;:mosaic&#12467;&#12510;&#12531;&#12489;
MosaRaster <- mosaic(CropRas1, CropRas2, fun = sum)
plot(MosaRaster, col = SetCol(10))

出力例

・denstyコマンド

density

・histコマンド

hist

・ディフォルトでプロット

Rasterplot

・セル色を変更してプロット

RasterCellCol

・cropコマンド

crop

・mergeコマンド

RasterMerge

・cellsFromExtentコマンドの利用

CutOutRaster

・extendコマンド

ExtendRaster

・flipコマンド

RasterFlip

・layerizeコマンド

layerize

・rasterToPointsコマンド

rasterToPoints

・mosaicコマンド

mosaic

少しでも、あなたの解析が楽になりますように!!

タイトルとURLをコピーしました