2013-03-13 48 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