Creating Raster with values based on function output

守給你的承諾、 提交于 2019-12-12 04:37:21

问题


I have a function that calculates a price based on the input slope and distance. I want to write the price to a raster as the rastervalue. How do I do that? OpenSource and ArcMap solutions would work.

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:


You can do this pretty easily in R. I recommend you download and install it (it's free and Open Source). The only thing you will have to do is to work out how to code your price function in R which is why I suggested you post that code. Once you have your pricefunc defined you can then run these commands from the R command line.

# 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" ) 


来源:https://stackoverflow.com/questions/15398532/creating-raster-with-values-based-on-function-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!