0

I am using Pygrass in a a Python Grass GIS stand alone application. I am using GRASS GIS 7.0.3 on a Windows machine and Python 2.7.4. I am initializing a new Grass environment. I can work in this environment, and apply grass functions (e.g. load shapefiles,...)

I would like to access the Vector data directly using this example:

from pygrass.vector import VectTopo
municip = VectTopo('boundary_municp_sqlite')
municip.is_open()
False
municip.mapset
''
municip.exist()  # check if exist, and if True set mapset
True
municip.mapset
'user1'

Unfortunately I get the following Error during the command municip.exist():

ERROR: MAPSET is not set

But I can work in the environment, the variables Location and Mapset("PERMANENT") are set. I also used g.region to set the region. I tried to use the module from grass.pygrass.gis import Mapset. This also gives me an error when I try to access the function:

from grass.pygrass.gis import Mapset
m = Mapset()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-59-73310da5f35b> in <module>()
----> 1 m = Mapset()

C:\Program Files (x86)\GRASS GIS 7.0.3\etc\python\grass\pygrass\gis\__init__.py
in __init__(self, mapset, location, gisdbase)
    265         self.gisdbase = gisdbase
    266         self.location = location
--> 267         self.name = mapset
    268         self.visible = VisibleMapset(self.name, self.location, self.gisd
base)
    269

C:\Program Files (x86)\GRASS GIS 7.0.3\etc\python\grass\pygrass\gis\__init__.py
in _set_name(self, name)
    290
    291     def _set_name(self, name):
--> 292         self._name = _check(name, join(self._gisdb, self._loc), "MAPSET"
)
    293
    294     name = property(fget=_get_name, fset=_set_name,

C:\PROGRA~2\QGISPI~1\apps\Python27\lib\ntpath.pyc in join(a, *p)
     71             b_wins = 1
     72
---> 73         elif isabs(b):
     74             # This probably wipes out path so far.  However, it's more
     75             # complicated if path begins with a drive letter:

C:\PROGRA~2\QGISPI~1\apps\Python27\lib\ntpath.pyc in isabs(s)
     55 def isabs(s):
     56     """Test whether a path is absolute"""
---> 57     s = splitdrive(s)[1]
     58     return s != '' and s[:1] in '/\\'
     59

C:\PROGRA~2\QGISPI~1\apps\Python27\lib\ntpath.pyc in splitdrive(p)
    123     """Split a pathname into drive and path specifiers. Returns a 2-tupl
e
    124 "(drive,path)";  either part may be empty"""
--> 125     if p[1:2] == ':':
    126         return p[0:2], p[2:]
    127     return '', p

TypeError: 'NoneType' object has no attribute '__getitem__'

I am not sure if both errors are connected somehow.

1

I have no problem on Mac OS X or Linux (GRASS 7.0.3 and GRASS 7.1)

from grass.pygrass.gis import Mapset
m = Mapset()
m
Mapset('Hamoir')
print m
Hamoir
m.glist('vector')
['axes_plis', 'barre_giv_lignes', 'failles_hamoir', 'hydro']
from grass.pygrass.vector import VectorTopo
axes = VectorTopo('axes_plis')
axes.exist()
True
axes.mapset
'Hamoir'

It may be a problem of the path of your gisdb. The error is in the file ntpath.py (specific to Windows = common operations on Windows pathnames), line 125 because p is empty.

    123     """Split a pathname into drive and path specifiers. Returns a 2-tuple
    124 "(drive,path)";  either part may be empty"""
--> 125     if p[1:2] == ':':
    126         return p[0:2], p[2:]
    127     return '', p

   TypeError: 'NoneType' object has no attribute '__getitem__'
  • Thanks for testing it, i think you were right with the comments. I opened an existing location in the GUI and printed all environmental variables, and added them to my PATH in the standalone script. Now it works fine! – Jonathan Mar 10 '16 at 12:26

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.