gdal_calc.py 在 sbt 下不起作用

gdal_calc.py does not work under sbt

I 运行 Scala 脚本中的命令行 gdal_calc.py。我 运行 这个脚本在我的终端上有 'sbt run'。我收到此错误:

Traceback (most recent call last):
  File "/usr/local/bin/gdal_calc.py", line 329, in <module>
    main()
  File "/usr/local/bin/gdal_calc.py", line 326, in main
    doit(opts, args)
  File "/usr/local/bin/gdal_calc.py", line 282, in doit
    myResult = ((1*(myNDVs==0))*myResult) + (myOutNDV*myNDVs)
TypeError: only integer arrays with one element can be converted to an index

运行 我终端中的命令 gdal_calc.py 运行良好。 运行 直接在我的终端中完全相同的命令行不起作用。环境是一样的:使用的gdal库是一样的

命令行 运行ned 如下:

gdal_calc.py --outfile=outfile.tiff -A infile.tiff --overwrite --calc="3*(A==2)"

谁能解释一下?谢谢!

我终于找到了解决问题的方法: 我正在将我的 gdal_calc.py 命令行写入 shell 脚本,然后执行它认为 shell。 它不是解决问题而是绕过 ;)

我对 Scala 帮不上忙,但和 Samuel 一样,我在使用 'subprocess.call()'.

从 Python 脚本调用 gdal_calc.py 时遇到了问题

我怀疑 'subprocess.call()' 将参数移交给 'gdal_calc.py' 时一定有问题。

解决方案是使用 'subprocess.call' 参数 'shell = True',通过 shell:

作为单个字符串执行调用
subprocess.call('gdal_calc.py --outfile=outfile.tiff -A rasterA.tiff
                -B rasterB.tiff --calc="A+B"', shell = True)

也许 'sbt run' 在 Scala 中有类似的选项?

除了使用 shell 脚本,您始终可以从 python 脚本启动它,如下所示:

# import section
import os

# Manual input
a_source = "aaa.tif"
b_source = "bbb.tif"
output = "out.tif"
calc = '"(A**B)*(B==1)"'

# The command itself
gdal_calc = 'python {Path root}/Lib/site-packages/osgeo/scripts/gdal_calc.py ' \
            '-A {0} ' \
            '-B {1} ' \
            '--outfile={2} ' \
            '--calc={3} ' \
            '--type=Float32 ' \
            '--overwrite'.format(a_source, b_source, output, calc)

# Launch the command
os.system(gdal_calc)

我发现这更容易、更清晰。