Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于函数输出使用值创建光栅_Python_R_Function_Raster_Arcmap - Fatal编程技术网

Python 基于函数输出使用值创建光栅

Python 基于函数输出使用值创建光栅,python,r,function,raster,arcmap,Python,R,Function,Raster,Arcmap,我有一个基于输入斜率和距离计算价格的函数。我想将价格作为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 loc

我有一个基于输入斜率和距离计算价格的函数。我想将价格作为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

您可以在
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" ) 
#安装所需的软件包
安装包(c(“光栅”、“spatstat”、“sp”、“rgdal”)、dep=TRUE)
#加载所需的包
需要(光栅)
需要(spatstat)
需要(sp)
需要(rgdal)
#读入数据文件(您可能必须在此处更改目录路径,默认情况下,R将在$USERHOME$目录中查找R使用/不\来分隔目录

slp您可以在
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" ) 
#安装所需的软件包
安装包(c(“光栅”、“spatstat”、“sp”、“rgdal”)、dep=TRUE)
#加载所需的包
需要(光栅)
需要(spatstat)
需要(sp)
需要(rgdal)
#读入数据文件(您可能必须在此处更改目录路径,默认情况下,R将在$USERHOME$目录中查找R使用/不\来分隔目录

slp您可以在
R
中执行此操作。您熟悉它吗?如果您能向我们提供priceFunc的详细信息,这将有助于提供完整的解决方案。priceFunc没有使用R。priceFunc非常长。我认为它不重要。它基本上需要参数斜率和距离,并返回它的价格。如果你想要一个完整的解决方案。我只能带你到目前为止,还不知道如何根据你的输入值计算价格。你能澄清一下“光栅”的意思吗?这是二维网格吗?你想根据两个参数存储函数值
f
x
y
,所以你基本上想存储
f(x,y)
?你可以用字典来完成。你可以在
R
中完成。你熟悉吗?如果你能告诉我们你的priceFunc的详细信息,这将有助于提供一个完整的解决方案。priceFunc没有使用R。priceFunc非常长。我认为它不重要。它基本上需要参数斜率和距离,并返回一个值价格。如果你想要一个完整的解决方案,这非常重要。我只能带你到目前为止,还不知道如何根据你的输入值计算价格。你能澄清一下“光栅”是什么意思吗?这是2D网格吗?是否要根据两个参数
x
y
存储函数值
f
,因此基本上要存储
f(x,y)
?你可以用字典来实现这一点。感谢你在R中的详细示例。但是我想用Python来保存代码。代价函数非常长,在R中无法如此轻松地重写。那么为什么说开源解决方案可以工作呢?如果只有Python解决方案可以工作,我就浪费了时间。但是,所有R的函数都可以使用RPy包。也许你想看看它,并使用该包将这些函数调用到Python中。我是说Python.Sorr中的开源不够具体。在你发布答案之前,我说过函数很长。所以很明显,我不会重写我的函数。很明显,我不会重写我的f函数不是真的。哦,很好。谢谢你在R中的详细示例。但是我想用Python保留代码。代价函数非常长,在R中无法如此轻松地重写。那么为什么说开源解决方案可以工作呢?如果只有Python解决方案可以工作,我就浪费了时间。但是,所有R的函数都可以使用RPy包调用。也许你想看看它,然后用这个包把这些函数调用到Python中。我指的是Python.Sorr中的开源,因为它不够具体。在你发布答案之前,我说过函数很长。所以很明显,我不会重写我的函数。很明显,我不会重写我的函数y、 哦,好吧。