使用基于函数输出的值创建Raster

时间:2013-03-13 23:31:50

标签: python r function raster arcmap

我有一个根据输入斜率和距离计算价格的功能。我想把价格写成栅格值作为栅格值。我怎么做? OpenSource和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 个答案:

答案 0 :(得分:3)

您可以在R中轻松完成此操作。我建议你download and install it(它是免费的和开源的)。你唯一要做的就是找出如何在R中编写价格函数的代码,这就是我建议你发布代码的原因。一旦定义了pricefunc,就可以从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" )