在 python 中使用 gdb 中的栅格数据集

发布于 2024-12-24 17:18:42 字数 788 浏览 2 评论 0原文

我正在尝试编写一个脚本,用于在 python 中将度数转换为弧度。这是一项常见的任务,随身携带它会很有用。我遇到的问题是输入栅格似乎被作为字符串读取。见下文:

import arcpy
from arcpy import env
from arcpy.sa import *
import math
arcpy.CheckOutExtension("Spatial")
env.workspace = "C:\Users\OJB\Desktop\University\UsingRAS\UsingRas.gdb"


degrad = math.pi / 180
PythonRad = "Aspect_Deg" * degrad

outCos = Cos("PythonRad")
outCos.save("C:\Users\OJB\Desktop\University\UsingRAS\UsingRas.gdb\PyTest")

我得到的错误是:

Traceback (most recent call last):
File "C:/Users/OJB/Desktop/University/UsingRAS/Scripts/DegtoRad", line 11, in <module>
PythonRad = "Aspect_Deg" * degrad
TypeError: can't multiply sequence by non-int of type 'float'

我不确定如何在不使用引号的情况下使用此栅格。我对 Python 很陌生,所以任何帮助将不胜感激。

一切顺利

I'm trying to write a script for converting degrees to radians in python. A common task and it would just be useful to have around. The problem I'm getting is that the input raster appears to be being read as a string. See below:

import arcpy
from arcpy import env
from arcpy.sa import *
import math
arcpy.CheckOutExtension("Spatial")
env.workspace = "C:\Users\OJB\Desktop\University\UsingRAS\UsingRas.gdb"


degrad = math.pi / 180
PythonRad = "Aspect_Deg" * degrad

outCos = Cos("PythonRad")
outCos.save("C:\Users\OJB\Desktop\University\UsingRAS\UsingRas.gdb\PyTest")

The error I get is:

Traceback (most recent call last):
File "C:/Users/OJB/Desktop/University/UsingRAS/Scripts/DegtoRad", line 11, in <module>
PythonRad = "Aspect_Deg" * degrad
TypeError: can't multiply sequence by non-int of type 'float'

I'm not sure how to use this raster without using the quote marks. I'm very new to Python so any help would be appreciated.

All the best

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素罗衫 2024-12-31 17:18:42

使用 arcpy 模块中的栅格对象实例化栅格:

from arcpy import Raster

...

ras = Raster('path_to_raster') 
PythonRad = ras * degrad

此外,完成后不要忘记检查您的空间分析扩展。

Use a raster object from the arcpy module to instantiate the raster:

from arcpy import Raster

...

ras = Raster('path_to_raster') 
PythonRad = ras * degrad

Also, don't forget to check your spatial analyst extension back in when you are done.

自控 2024-12-31 17:18:42

第一:当您想在字符串中使用 \ 字符时,您必须使用另一个 \ 对其进行转义。所以你的路径看起来像这样: "C:\\Users\\OJB\\Desktop\\University\\UsingRAS\\UsingRas.gdb"

这行:

PythonRad = "Aspect_Deg" * degrad

没有意义,因为你将字符串文字与浮点值相乘。您必须首先检索一个可与您的浮点数相乘的对象。

First: When you want to use the \ character in your strings, you have to escape it with another \. So your path would look like this: "C:\\Users\\OJB\\Desktop\\University\\UsingRAS\\UsingRas.gdb"

This line:

PythonRad = "Aspect_Deg" * degrad

Doesn't make sense, as you are multiplying a string literal with a float value. You have to first retrieve an object that is multipliable with your float.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文