2013-03-13 46 views
0

我有一个函数,可根据输入斜率和距离计算价格。我想将价格写入栅格作为栅格值。我怎么做? 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 
+0

您可以在'R'中做到这一点。你是否熟悉它?如果你能给我们提供你的价格的详细信息,这将有助于提供一个完整的解决方案.Func – 2013-03-14 17:32:28

+0

我还没有使用R. priceFunc超长。我认为这不重要。它基本上采用参数斜率和距离并返回一个价格。 – ustroetz 2013-03-14 17:54:09

+0

如果您想要一个完整的解决方案,这非常重要。我只能带你到目前为止,而不知道如何根据你的输入值来计算价格。 – 2013-03-14 18:05:11

回答

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") 
+0

感谢您在R中的详细示例。但是我想保留Python中的代码。成本函数非常长,不能在R中轻松地重写。 – ustroetz 2013-03-14 18:08:46

+0

那么为什么说* OpenSource *解决方案会起作用?如果只有Python解决方案可行,我浪费了我的时间。然而,所有R的函数都可以使用RPy包进行调用。也许你想看看它,并使用该包将这些函数调用到Python中。 – 2013-03-14 18:10:33

+0

我的意思是Python中的OpenSource。 Sorr不够具体。在你发布你的答案之前,我说过这个函数很长。所以很明显,我不会重写我的功能。 – ustroetz 2013-03-14 18:12:38