为像素分配新值

时间:2017-04-08 14:08:45

标签: python r r-raster

对于任何栅格数据集,我想为最大像素值分配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上做同样的事情?

2 个答案:

答案 0 :(得分:3)

希望这有帮助。

# 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

BTW,当你这样做时:

df=data.frame(ras[])

你最终得到一个矢量(我认为),这给你带来了问题。您也可以尝试我在上面的例子中使用的值(栅格,格式=&#34;矩阵&#34;)。

答案 1 :(得分:1)

数据准备

# 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 ​​:此解决方案使用rasterdplyr包中的函数。

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)