Windows10 下GRASS GIS Python 环境配置问题

因为想要在Spyder中直接调用GRASS的Python包,实现在外部的IDE中运行GRASS,直接在Spyder中输入官网给出的实现代码,根据实际情况更改gisdb,grassbin目录等:

import os
import sys
import subprocess


#定义GRASS数据库
#添加自己的grassdb数据库路径
gisdb = os.path.join(os.path.expanduser("~"), "grassdata")
# the following path is the default path on MS Windows
# gisdb = os.path.join(os.path.expanduser("~"), "Documents/grassdata")

#指定现有location和mapset
location = "nc_spm_08"
mapset = "user1"

#指定GRASS的启动文件
grass7bin = 'grass78'
if sys.platform.startswith('win'):
    # MS Windows
    grass7bin = r'C:Program FilesGRASS GIS 7.8grass78.bat'
    # uncomment when using standalone WinGRASS installer
    # grass7bin = r'C:Program Files (x86)GRASS GIS 7.8.0grass78.bat'
    # this can be avoided if GRASS executable is added to PATH
elif sys.platform == 'darwin':
    # Mac OS X
    # TODO: this have to be checked, maybe unix way is good enough
    grass7bin = '/Applications/GRASS/GRASS-7.8.app/'

# query GRASS GIS itself for its GISBASE

startcmd = [grass7bin, '--config', 'path']
try:
    p = subprocess.Popen(startcmd, shell=False,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = p.communicate()

except OSError as error:
    sys.exit("ERROR: Cannot find GRASS GIS start script"
             " {cmd}: {error}".format(cmd=startcmd[0], error=error))
if p.returncode != 0:
    sys.exit("ERROR: Issues running GRASS GIS start script"
             " {cmd}: {error}"
             .format(cmd=' '.join(startcmd), error=err))

#gisbase = out.strip(os.linesep.encode())
gisbase= r'C:Program FilesGRASS GIS 7.8'

# set GISBASE environment variable
os.environ['GISBASE'] = gisbase

# define GRASS-Python environment
grass_pydir = os.path.join(gisbase, "etc", "python")
sys.path.append(grass_pydir)

# import (some) GRASS Python bindings
import grass.script as gscript
import grass.script.setup as gsetup

# launch session
rcfile = gsetup.init(gisbase, gisdb, location, mapset)

# example calls
gscript.message('Current GRASS GIS 7 environment:')
print(gscript.gisenv())

gscript.message('Available raster maps:')
for rast in gscript.list_strings(type='raster'):
    print(rast)

gscript.message('Available vector maps:')
for vect in gscript.list_strings(type='vector'):
    print(vect)

# clean up at the end
gsetup.cleanup()

但在运行过程中,报了如下错误:

CalledModuleError: Module run None g.list --q -m type=raster ended with error
Process ended with non-zero return code 3221225785. See errors in the (error) output.`

以及弹出提示:

up-627570d46ef2d4bc9408b619bcc22eb7e0c.pngup-0b2c6ff1a808f77d897111e35d61f04d606.png

通过搜索了解到出现这个错误的主要原因是在GRASS运行时可能调用了错误的.dll动态链接库,参考 GRASS WiKi 网上给出的解决方法是:

找到系统中的同名的.dll文件,将其重命名,然后再运行程序:

几个主要的参考:

Is it possible to use GRASS GIS in Python stand alone scripts?

WinGRASS errors

Error on libcurl.dll when using GDAL of OSGeo4W in Django

【问题解决】无法定位程序输入点~于动态链接库上

DLL目录查找顺序

  • 解决步骤如下:

需要用到的软件:

以解决“无法定位程序输入点locale_charset于动态链接库C:\Program Files\GRASS GIS 7.8\spatialite.dll上”为例:

  1. 首先打开Dependencies,直接将spatialite.dll拖入软件

up-82ab463965d8029bcb3ba8d3807af36f4a1.png

然后可以看到locale_charset这个变量,是需要由 C:\Program Files\GRASS GIS 7.8\extrabin\iconv.dll 这个dll提供的

所以问题很有可能就是在程序运行的时候没有调用到这个路径下的iconv.dll,而是用了其他路径的iconv.dll

up-551fb369278b7b94b49521277b4e1c9b88f.png

2. 打开Process Explorer 64,查看此时内存中载入了哪些iconv.dll

up-2539f22a07465b259467a4f9d16f3aa3bdc.png

可以看到,现在调用了D:\ProgramData\Anaconda3\Library\bin\iconv.dll这个dll,来自Anaconda虚拟环境的Library,将其重命名为iconv.dll.bak,然后重启spyder,再次用Process Explorer搜索,发现没有再次载入了

up-99eef49faf20d352493bc2d9f42429f5d43.png

至此,解决了iconv.dll的问题,同样的方法,可以解决gdal300.dll的问题

最后重启spyder,运行程序,成功:

up-b912731db704595e27b03f9ff54cffed1fd.png

PS:这样重命名虚拟环境中dll的方法,可能会影响虚拟环境中其他软件的使用,所以最好在使用完GRASS GIS之后,把重命名的文件改回来。

(或者直接把Anaconda卸载了

后续应该要找到怎么样让程序直接找到对应的.dll文件的方法,从根本上解决问题

附:spyder的启动脚本:

# filename: grass_spyder.sh
# directory where GRASS GIS lives
# GRASS GIS 的安装信息
export GISBASE=`C:Program FilesGRASS GIS 7.8grass78.bat --config path`   # or define path to binaries like /usr/local/grass-7.4.svn
#echo $GISBASE

# generate GISRC
# Defines the system wide value while in a GRASS session
# 设置GISDBASE LOCATION MAPSET

MYGISDBASE=G:grass_data # Change this path to reflect your own 
MYLOC=futures_triangle_nc # Change this location name to reflect your own 
MYMAPSET=PERMANENT

# Set the global grassrc file to individual file name
MYGISRC="C:Usersddd.grassrc"

echo "GISDBASE: $MYGISDBASE" > "$MYGISRC"
echo "LOCATION_NAME: $MYLOC" >> "$MYGISRC"
echo "MAPSET: $MYMAPSET" >> "$MYGISRC"
echo "GRASS_GUI: text" >> "$MYGISRC"

# path to GRASS settings file
export GISRC=$MYGISRC

export LD_LIBRARY_PATH=$GISBASE/lib:$LD_LIBRARY_PATH
export PYTHONPATH=$GISBASE/etc/python:$PYTHONPATH
export PATH=$GISBASE/bin:$GISBASE/scripts:$PATH

# start the notebook in the notebook folder
cd G:grass_data # change to notebooks folder (example; update to your path)

spyder

cd到文件目录,执行

sh grass_spyder.sh

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值