为像素分配新值
Assign new values for pixels
对于任何栅格数据集,我想连续为最大像素值分配 1
,为其他像素值分配 0
。
我尝试了很多方法,但到目前为止我找不到任何解决方案。
这是一个示例代码:
library(dplyr)
library(raster)
library(rgdal)
ras=raster("D:/Rtool/DATA/DigitalTerrainModel/SJER2013_DTM.tif")
这个栅格有 5060 col and 4299 row
。我想要的是为 rowMax 值分配 "1"
,为其他值分配 "0"
。
我试过了:
df=data.frame(ras[])
summarise_each(df, funs(max(., na.rm=TRUE)))
然而,我只得到了整个栅格高程值的一个最大值,这不是我想要的。
在 python 上,我使用了 df.eq(df.max(1), 0).astype(int)
那么我怎么能在 R 上做同样的事情呢?
希望对您有所帮助。
# create fake raster
fakeRaster <- raster(matrix(ncol = 10, nrow = 10))
# place values 1 to number of cells; so the right most value
# of each row should be the highest
values(fakeRaster) <- 1:ncell(fakeRaster)
# introduce higher values somewhere; test points
values(fakeRaster)[75] <- 300
values(fakeRaster)[1] <- 300
values(fakeRaster)[100] <- 300
values(fakeRaster)[81] <- 300
values(fakeRaster)[45] <- 300
# extract values as matrix
dataRaster <- values(fakeRaster, format = "matrix")
# get every row; evalute if equal to max, if yes give 1, if no give 0
# then transpose the output
dataRaster <- t(apply(dataRaster, 1, FUN = function(x) ifelse(x == max(x, na.rm = T), 1, 0) ))
# reassign value to raster
values(fakeRaster) <- dataRaster
顺便说一句,当你这样做时:
df=data.frame(ras[])
你最终得到一个向量(我认为),这给你带来了问题。您也可以尝试我在上面的示例中使用的值(光栅,格式 = "matrix")。
数据准备
# Load packages
library(raster)
## Create example raster layer
# create an empty raster layer
r <- raster(ncol = 10, nrow = 10)
# assign values to cells
values(r) <- 1:ncell(r)
# Inspect the raster layer
plot(r)
解决方案 1:此解决方案使用 raster
和 dplyr
包中的函数。
library(dplyr)
# extract values as matrix, and then converted to data frame
data_df <- as.data.frame(values(r, format = "matrix"))
# Calculate the maximum and save the values to the "Max" column
data_df$Max <- apply(data_df, 1, max)
# Define a function to replace the values to be 1 or zero
# x is the input vector, M is the maximum
max_replace <- function(x, M){
x[x != M] <- 0
x[x == M] <- 1
return(x)
}
# Use mutate_each to replace values to be 1 and 0
data_df2 <- data_df %>%
dplyr::mutate_each(funs(max_replace(., M = Max)), - Max) %>%
dplyr::select(-Max)
# Assign the values back to the raster layer
values(r) <- as.matrix(data_df2)
# Examine the results
plot(r)
解决方案 2:这只使用 raster
包
# Initialize an empty list to store the results of each row
raster_list <- list()
# A for loop to crop the image and assign 0 or 1 to see if it is the maximum of that row
for (i in 1:nrow(r)){
# Get the extent of each row
temp_ext <- extent(r, r1 = i, r2 = i, c1 = 1, c2 = ncol(r))
# Crop the raster for only one row
temp_r <- crop(r, temp_ext)
# Get the maximum of one row
temp_max <- max(values(temp_r), na.rm = TRUE)
# Use raster algebra
temp_r[temp_r != temp_max] <- 0
temp_r[temp_r == temp_max] <- 1
# Save the results to the raster_list
raster_list <- c(raster_list, temp_r)
}
# Merge all rasters
m <- do.call(merge, raster_list)
# Examine the result
plot(m)
对于任何栅格数据集,我想连续为最大像素值分配 1
,为其他像素值分配 0
。
我尝试了很多方法,但到目前为止我找不到任何解决方案。
这是一个示例代码:
library(dplyr)
library(raster)
library(rgdal)
ras=raster("D:/Rtool/DATA/DigitalTerrainModel/SJER2013_DTM.tif")
这个栅格有 5060 col and 4299 row
。我想要的是为 rowMax 值分配 "1"
,为其他值分配 "0"
。
我试过了:
df=data.frame(ras[])
summarise_each(df, funs(max(., na.rm=TRUE)))
然而,我只得到了整个栅格高程值的一个最大值,这不是我想要的。
在 python 上,我使用了 df.eq(df.max(1), 0).astype(int)
那么我怎么能在 R 上做同样的事情呢?
希望对您有所帮助。
# create fake raster
fakeRaster <- raster(matrix(ncol = 10, nrow = 10))
# place values 1 to number of cells; so the right most value
# of each row should be the highest
values(fakeRaster) <- 1:ncell(fakeRaster)
# introduce higher values somewhere; test points
values(fakeRaster)[75] <- 300
values(fakeRaster)[1] <- 300
values(fakeRaster)[100] <- 300
values(fakeRaster)[81] <- 300
values(fakeRaster)[45] <- 300
# extract values as matrix
dataRaster <- values(fakeRaster, format = "matrix")
# get every row; evalute if equal to max, if yes give 1, if no give 0
# then transpose the output
dataRaster <- t(apply(dataRaster, 1, FUN = function(x) ifelse(x == max(x, na.rm = T), 1, 0) ))
# reassign value to raster
values(fakeRaster) <- dataRaster
顺便说一句,当你这样做时:
df=data.frame(ras[])
你最终得到一个向量(我认为),这给你带来了问题。您也可以尝试我在上面的示例中使用的值(光栅,格式 = "matrix")。
数据准备
# Load packages
library(raster)
## Create example raster layer
# create an empty raster layer
r <- raster(ncol = 10, nrow = 10)
# assign values to cells
values(r) <- 1:ncell(r)
# Inspect the raster layer
plot(r)
解决方案 1:此解决方案使用 raster
和 dplyr
包中的函数。
library(dplyr)
# extract values as matrix, and then converted to data frame
data_df <- as.data.frame(values(r, format = "matrix"))
# Calculate the maximum and save the values to the "Max" column
data_df$Max <- apply(data_df, 1, max)
# Define a function to replace the values to be 1 or zero
# x is the input vector, M is the maximum
max_replace <- function(x, M){
x[x != M] <- 0
x[x == M] <- 1
return(x)
}
# Use mutate_each to replace values to be 1 and 0
data_df2 <- data_df %>%
dplyr::mutate_each(funs(max_replace(., M = Max)), - Max) %>%
dplyr::select(-Max)
# Assign the values back to the raster layer
values(r) <- as.matrix(data_df2)
# Examine the results
plot(r)
解决方案 2:这只使用 raster
包
# Initialize an empty list to store the results of each row
raster_list <- list()
# A for loop to crop the image and assign 0 or 1 to see if it is the maximum of that row
for (i in 1:nrow(r)){
# Get the extent of each row
temp_ext <- extent(r, r1 = i, r2 = i, c1 = 1, c2 = ncol(r))
# Crop the raster for only one row
temp_r <- crop(r, temp_ext)
# Get the maximum of one row
temp_max <- max(values(temp_r), na.rm = TRUE)
# Use raster algebra
temp_r[temp_r != temp_max] <- 0
temp_r[temp_r == temp_max] <- 1
# Save the results to the raster_list
raster_list <- c(raster_list, temp_r)
}
# Merge all rasters
m <- do.call(merge, raster_list)
# Examine the result
plot(m)