[GRASS-user] GRASS Environment Variables

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

Hello Joseph,

There is a solution, but it is not straight forward:
https://grasswiki.osgeo.org/wiki/Working_with_GRASS_without_starting_it_explicitly

Another option is to start your editor from the GRASS command line.

Regards,
Laurent

2017-04-04 9:12 GMT-05:00 Joseph Kariuki <joehene@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@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

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.gdal”, input=“/home/…/image.tif”, flags=‘e’,output = “image”)

···

2017-04-04 11:12 GMT-03:00 Joseph Kariuki <joehene@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@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

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
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.

···

On Wed, Apr 5, 2017 at 2:12 AM, Andres Solarte <e.andres.solartec@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.gdal”, input=“/home/…/image.tif”, flags=‘e’,output = “image”)

Kind Regards,
Joseph Kariuki

Geospatial Engineer | GIS / Web Developer

2017-04-04 11:12 GMT-03:00 Joseph Kariuki <joehene@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@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

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

···

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
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@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.gdal”, input=“/home/…/image.tif”, flags=‘e’,output = “image”)

2017-04-04 11:12 GMT-03:00 Joseph Kariuki <joehene@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@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

Hello Joseph,

my fault,

add this line after #3.

#3. Call function (examples)

import grass.script as gscript

now it should works!

remember set your coordinates and resolution (example)
res = 0.008626704385179

n = 14.
s = -2.
w = -76
e = -65

Regards

···

2017-04-05 9:43 GMT-03:00 Joseph Kariuki <joehene@gmail.com>:

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
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@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.gdal”, input=“/home/…/image.tif”, flags=‘e’,output = “image”)

2017-04-04 11:12 GMT-03:00 Joseph Kariuki <joehene@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@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user