基于函数输出创建栅格值

0 投票
1 回答
1025 浏览
提问于 2025-04-17 19:01

我有一个函数,它根据输入的坡度和距离来计算价格。我想把这个价格写入一个栅格中,作为栅格值。我该怎么做呢?开源的解决方案和ArcMap的解决方案都可以。

slopeRaster = "slope.tif"
emptyRaster = "emptyraster.tif" # How do I create an empty raster?
road = "road.shp"

for cell in emptyraster:
    # get slope from sloperaster at cell location
    ...
    slope = ...

    # get distance to nearest road from center of cell
    ...
    distance = ...

    # calculate price for cell
    price = pricefunc(slope, distance)

    # write price to cell as value  # How do I write a value to a raster

1 个回答

3

语言中,你可以很轻松地做到这一点。我建议你下载并安装它(它是免费的,且是开源的)。你需要做的唯一一件事就是弄清楚如何在R中编写你的价格函数,这也是我建议你把那段代码发出来的原因。一旦你定义了你的价格函数,就可以在R的命令行中运行这些命令。

# Install required packages
install.packages( c("raster" , "spatstat" , "sp" , "rgdal") , dep = TRUE )

# Load required packages
require( raster )
require( spatstat )
require( sp )
require( rgdal )

# Read in your data files (you might have to alter the directory paths here, the R default is to look in your $USERHOME$ directory R uses / not \ to delimit directories
slp <- raster( "slope.tif" )
roads <- readShapeLines( "road.shp" )


# Create point segment pattern from Spatial Lines
distPSP <- as.psp( roads )


#   Create point pattern from slope raster values
slpPPP <- as.ppp( values(slp) )


#   Calculate distances from lines for each cell
distances <- nncross( slpPPP , distPSP )


# Create raster with calcualted distances
rDist <- raster( slp )
values( rDist ) <- distances


# Define your princefunc() here. It should take two input values, slope and distance and return one value, which I have called price
pricefunc <- function( slp , dist ){
    ...my code
        ... more code
    ...more code
    return( price )
}


# Calculate price raster using your price function and save as output.tif
rPrice <- overlay( slp , rDist , fun = function( x , y ){ pricefunc( x , y ) } , filename = "output.tif" ) 

撰写回答