[GRASS-user] GRASS Environment Variables

Blumentrath, Stefan Stefan.Blumentrath at nina.no
Wed Apr 5 05:47:01 PDT 2017


Hi Joseph,

You just have to import the module:

import grass.script as gscript

See also:
https://grass.osgeo.org/grass72/manuals/libpython/script_intro.html

Cheers
Stefan


From: grass-user [mailto:grass-user-bounces at lists.osgeo.org] On Behalf Of Joseph Kariuki
Sent: onsdag 5. april 2017 14.43
To: Andres Solarte <e.andres.solartec at gmail.com>
Cc: GRASS user list <grass-user at lists.osgeo.org>
Subject: Re: [GRASS-user] GRASS Environment Variables

Hi Andres, I have run the code in pycharm but still raises an error
/usr/bin/python2.7 /home/hempire/PycharmProjects/GRASS/ingrass.py
Traceback (most recent call last):
  File "/home/hempire/PycharmProjects/GRASS/ingrass.py", line 43, in <module>
    gscript.run_command('g.region', n=float(n), s=float(s), e=float(e), w=float(w), ewres=float(res), nsres=float(res))
NameError: name 'gscript' is not defined

Process finished with exit code 1

However on terminal it executes with no errors. I will stick to using terminal. PyCharm is limiting.



Kind Regards,
Joseph Kariuki

Geospatial Engineer | GIS / Web Developer

On Wed, Apr 5, 2017 at 2:12 AM, Andres Solarte <e.andres.solartec at gmail.com<mailto:e.andres.solartec at gmail.com>> wrote:
Hello Joseph,

This is the way I work with grass from python:


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys

#1. Define function

def conectar(location,mapset):
# path to the GRASS GIS launch script
grass7bin = '/usr/bin/grass72'

# Set GISDBASE environment variable
gisdb = "/media/andres/Andres/TesisGrass"
os.environ['GISDBASE'] = gisdb
# query GRASS 7 itself for its GISBASE
gisbase = "/usr/lib/grass72"
os.environ['GISBASE'] = gisbase

# define GRASS-Python environment
gpydir = "/usr/lib/grass72/etc/python"
sys.path.append(gpydir)
# import GRASS Python bindings
import grass.script.setup as gsetup

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

#2. Now you can connect with your projects

location = "your location"
mapset = "your mapset"

conectar(location,mapset)

#3. Call function (examples)

gscript.run_command('g.region', n=float(n), s=float(s), e=float(e), w=float(w), ewres=float(res), nsres=float(res))

gscript.run_command("r.in<http://r.in>.gdal", input="/home/..../image.tif", flags='e',output = "image")

2017-04-04 11:12 GMT-03:00 Joseph Kariuki <joehene at gmail.com<mailto:joehene at gmail.com>>:
Hi?

I have been trying to call GRASS modules using python outside GRASS session, I am coding in PyCharm however, I am having a problem importing grass scripts and the solution being provided is setting GRASS environment variables. How do I go about that so to import GRASS modules in Pycharm?


Kind Regards,
Joseph Kariuki

Geospatial Engineer | GIS / Web Developer

_______________________________________________
grass-user mailing list
grass-user at lists.osgeo.org<mailto:grass-user at lists.osgeo.org>
https://lists.osgeo.org/mailman/listinfo/grass-user


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20170405/4862c121/attachment-0001.html>


More information about the grass-user mailing list