1

I took this as reference, Moving raster to different mapset GRASS GIS, and I use pyscripter to use GRASS module g.copy from outside,

import os
import sys

#set up GRASS environment variables
sys.path.append(os.path.join(os.environ['GISBASE'], 'etc', 'python'))
import grass.script as g
import grass.script.setup as gsetup
gisbase = os.environ['GISBASE']
gisdb = 'C:\Users\Heinz\Documents\grassdata'
location = 'newLocation'
mapset = 'watershed_analysis'
gsetup.init(gisbase, gisdb, location, mapset)

#copy raster data
g.run_command('g.copy', rast = 'hc@TC, hc')

#check result
print g.read_command('g.list', _type = 'rast')

The output is raster files in mapset PERMANENT, thus not what I want. (current mapset is watershed_analysis)

*** Remote Interpreter Reinitialized  ***
>>> 
----------------------------------------------

raster files available in mapset <PERMANENT>:
hc

----------------------------------------------

Seems I don't successfully copy the raster file to the mapset I want.

How to fix this problem? I am using GRASS 6.4.3, python 2.7.6 under Windows 8.1 x64.


Update#1 @gene, I do the same thing in the python shell of the GRASS layer manager,

Python 2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>import grass.script as g
>>>g.run_command('g.copy', rast = 'acc@TC, acc@watershed_analysis')
0

>>>g.read_command('g.list', _type = 'rast')
----------------------------------------------

raster files available in mapset <PERMANENT>:
hc

----------------------------------------------

g.read_command('g.mlist', _type = 'rast')
hc

In the output above, I want to copy the file acc from mapset TC to current mapset watershed_analysis, but the output still tells me about another mapset. (g.list and g.mlist outputs the same except the later one wouldn't tell me the info of mapset.)

And I go to check results in the file of this mapset, there are no new file copied to it. Maybe there are problems in my script.


Update#2 If I tried the module in GRASS GUI, here's result,

enter image description here enter image description here

No output but the error message,

Traceback (most recent call last):


File "C:\OSGeo4W\apps\grass\grass-6.4.3\etc\wxpython\gui_c
ore\forms.py", line 655, in OnRun

self.goutput.RunCmd(cmd, onDone = self.OnDone)
  File "C:\OSGeo4W\apps\grass\grass-6.4.3\etc\wxpython\gui_c
ore\goutput.py", line 554, in RunCmd

task = GUI(show = None).ParseCommand(command)
  File "C:\OSGeo4W\apps\grass\grass-6.4.3\etc\wxpython\gui_c
ore\forms.py", line 1871, in ParseCommand

blackList = _blackList)
  File "C:\OSGeo4W\apps\grass\grass-6.4.3\etc\python\grass\s
cript\task.py", line 470, in parse_interface

tree = etree.fromstring(get_interface_description(name))
  File
"C:\OSGeo4W\apps\Python27\lib\xml\etree\ElementTree.py",
line 1300, in XML

parser.feed(text)
  File
"C:\OSGeo4W\apps\Python27\lib\xml\etree\ElementTree.py",
line 1642, in feed

self._raiseerror(v)
  File
"C:\OSGeo4W\apps\Python27\lib\xml\etree\ElementTree.py",
line 1506, in _raiseerror

raise err
xml.etree.ElementTree
.
ParseError
:
unknown encoding: line 1, column 30

I don't know if I did something wrong in inputs.

  • 1
    strange, I have no problem on Mac OS X or Linux. – gene Mar 31 '14 at 17:27
  • @gene some other modules like g.remove works fine, but some are returning strange outputs. I hope it's not problem relates to OS. – Heinz Apr 1 '14 at 2:07
  • all the outputs are correct in my case. Try to execute your script in the Python shell of the GRASS Layer manager – gene Apr 1 '14 at 19:55
  • @gene I have added the output from the built-in python shell. – Heinz Apr 3 '14 at 9:37
2

For the g.copy problem you should set two variables for the "from" and "to" maps then the command should be:

g.run_command('g.copy', rast = (from, to))

Have a look here how to format it correctly: http://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library#Interface_to_copying_maps_.28g.copy.29

Regarding the g.mremove part, you need to add the '-f' flag (i.e. "force") to actually perform the remove. Without that flag g.mremove just outputs what it would have done

May I suggest that you separate two questions into two posts. Makes it easier for future generations to search and find relevant information.

  • thank you for your reply, and I will separate it into two posts, by the way, the link in your answer I can't open. – Heinz Apr 3 '14 at 9:23
  • 1
    There is some issue with the whole "osgeo.org" servers since yesterday, so the grass wiki is down for a while. Try again in a day or so, I'm sure it will be up again. – Micha Apr 4 '14 at 13:07
  • (GRASS GIS server is back, system maintenance should be done.) – markusN Apr 8 '14 at 5:42

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.