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 <[email protected]>: > 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 < > [email protected]> 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 <[email protected]>: >> >>> 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 >>> [email protected] >>> https://lists.osgeo.org/mailman/listinfo/grass-user >>> >> >> >
_______________________________________________ grass-user mailing list [email protected] https://lists.osgeo.org/mailman/listinfo/grass-user
