1

I have an existing Python script that processes raster images with Gdal / Numpy. Now I want to extend the script with Grass. The python script is developed on Windows 7 with Eclipse/Pydev. The script is executed from within Eclipse. I installed Grass with the OSGeo4W installer (Version 6.4.3). I’m new to Grass, but as far as I understand I first need to create a new GRASS location. I try to do this with:

grass.core.create_location("C:\grassdata", “test”, epsg=21781)

however this throws the following error:

IOError: [Errno 2] No such file or directory:'C:\\grassdata\\test\\PERMANENT\\MYNAME'

create_location first runs g.gisenv, then g.proj and then again g.gisenv and finally tries to open C:\grassdata\test\PERMANENT\MYNAME that does not exists. Actually nothing was created in C:\grassdata. This is the complete script:

import os, sys
import numpy as np
import gdal

def get_env(env):
  value = ""
  if env in os.environ:
    value = os.environ[env] + os.pathsep
  return value

input_data = r"C:\data\dom_eth\DOM_Clip.tif"

# start configure grass
gisbase = os.environ['GISBASE'] = r'C:\OSGeo4W64\apps\grass\grass-6.4.3'

os.environ['GISRC'] = r'C:\Users\Username\AppData\Roaming\GRASS6\grassrc6'
grass_dirs = [os.path.join(os.environ['GISBASE'], i) for i in ['etc', os.path.join('etc', 'python'), 'lib', 'bin', 'scripts']]
path = get_env('PATH') + os.pathsep.join(grass_dirs)
os.environ['PATH'] = path
os.environ['GRASS_SH'] = r'C:\OSGeo4W64\apps\msys\bin\sh.exe'

if not os.path.exists(gisbase):
  os.mkdir(gisbase)

sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "python"))
import grass.script as grass
import grass.script.array as garray
import grass.script.setup as gsetup

gisdbase = r"C:\grassdata"
location = "test"

grass.core.create_location(gisdbase, location, epsg=21781)

A grep -rl "MYNAME" over the source code of Grass did not reveal something useful to me. What is the correct way to use Grass with Python from the "outside"?

2

you have forgotten an essential part, the initiation of GRASS with setup:

import grass.script as grass
import grass.script.setup as gsetup
gisbase = os.environ['GISBASE']
gisdb="your_path_to/grassdata"
# initiation of GRASS
gsetup.init(gisbase, gisdb)
grass.core.create_location(gisdb,"test", epsg=21781)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.