基于函数的输出值与创建栅格(Creating Raster with values based on

2019-10-29 13:55发布

我有一个计算基于输入斜率和距离的价格的功能。 我想价格写入光栅作为rastervalue。 我怎么做? 开源和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

Answer 1:

你可以做到这一点很容易地R 我建议你下载并安装它 (它的自由和开放源码)。 你将不得不做的唯一一件事情就是解决如何编写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" ) 


文章来源: Creating Raster with values based on function output