[GRASS-user] r.shade python issues

Hi – I’m having issues with r.shade module in grass 7.0.3 when trying to interact with data via python.

r.shade runs absolutely fine through the UI, however, when I run the syntax through the python console, I get a syntax error.

I have tried a variety of different datasets and syntax but it keeps coming back with the syntax error, I went back to manual for the basics but I still get the following:

r.shade shade=aspect color=elevation output=elevation_aspect_shaded

File “”, line 1

r.shade(shade=aspect color=elevation output=elevation_aspect_shaded)

^

SyntaxError: invalid syntax

Other modules (r.in.gdal, r.relief, r.slope.aspect, g.region etc) all run fine through python, I am only having issues with r.shade.

Does anybody have any idea what might be going wrong?

Cheers,

Gareth

Hei,

Maybe the issue is missing commas?

r.shade(shade=aspect, color=elevation, output=elevation_aspect_shaded)

Cheers

Stefan

···

Hi – I’m having issues with r.shade module in grass 7.0.3 when trying to interact with data via python.

r.shade runs absolutely fine through the UI, however, when I run the syntax through the python console, I get a syntax error.

I have tried a variety of different datasets and syntax but it keeps coming back with the syntax error, I went back to manual for the basics but I still get the following:

r.shade shade=aspect color=elevation output=elevation_aspect_shaded

File “”, line 1

r.shade(shade=aspect color=elevation output=elevation_aspect_shaded)

^

SyntaxError: invalid syntax

Other modules (r.in.gdal, r.relief, r.slope.aspect, g.region etc) all run fine through python, I am only having issues with r.shade.

Does anybody have any idea what might be going wrong?

Cheers,

Gareth

Hi Stefan - thanks for getting back to me.

I had tried that before but still got issues, this time returning: ‘NameError: name ‘r’ is not defined’

r.shade(shade=aspect, color=elevation, output=elevation_aspect_shaded)
Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘r’ is not defined

Any ideas what might be causing that?

Cheers,
Gareth

···

On Mon, May 23, 2016 at 12:47 PM, Blumentrath, Stefan <Stefan.Blumentrath@nina.no> wrote:

Hei,

Maybe the issue is missing commas?

r.shade(shade=aspect, color=elevation, output=elevation_aspect_shaded)

Cheers

Stefan

From: grass-user [mailto:grass-user-bounces@lists.osgeo.org] On Behalf Of Gareth Grewcock
Sent: 23. mai 2016 11:49
To: grass-user@lists.osgeo.org
Subject: [GRASS-user] r.shade python issues

Hi – I’m having issues with r.shade module in grass 7.0.3 when trying to interact with data via python.

r.shade runs absolutely fine through the UI, however, when I run the syntax through the python console, I get a syntax error.

I have tried a variety of different datasets and syntax but it keeps coming back with the syntax error, I went back to manual for the basics but I still get the following:

r.shade shade=aspect color=elevation output=elevation_aspect_shaded

File “”, line 1

r.shade(shade=aspect color=elevation output=elevation_aspect_shaded)

^

SyntaxError: invalid syntax

Other modules (r.in.gdal, r.relief, r.slope.aspect, g.region etc) all run fine through python, I am only having issues with r.shade.

Does anybody have any idea what might be going wrong?

Cheers,

Gareth

Hello,

I answered off-list by mistake.

Normally, when "non-zero return code1" occurs, you can see the error
generated by the module on the terminal.
Alternatively, you can try the solution given in the wiki to retrieve the error:

def read2_command(*args, **kwargs):
   kwargs['stdout'] = grass.PIPE
   kwargs['stderr'] = grass.PIPE
   ps = grass.start_command(*args, **kwargs)
   return ps.communicate()

Could you check this out and post the result here?

Regards,
Laurent

2016-05-23 9:58 GMT-05:00 Gareth Grewcock <garethgrewcock@gmail.com>:

Thanks for getting in touch Laurent.

I have been working on a data processing script written in python which I
want to use Grass modules in. So I have actually been using
gscript.run_command() already. I was doing this externally and not actually
loading Grass explicitly. I can do this successfully as follows:

grass.run_command('r.in.gdal',input = ffile, output = name, quiet = 'True',
overwrite = 'True', flags = 'o')

grass.run_command('r.relief',input = name,output = hillshade_output,altitude
= '20', azimuth = '315', zscale = '6', overwrite = 'True')

grass.run_command('r.slope.aspect',elevation = name, slope = "dem_slope",
aspect = "dem_aspect", overwrite = 'True')

However, when I come to use r.shade, I get the 'non-zero return code1', so I
have since gone back into the Grass UI to try and simply run python snippets
but can't get r.shade to work with Python either externally or through the
UI, I really have no idea what the problem is as all of the other modules
work fine.

Cheers,

Gareth

On Mon, May 23, 2016 at 3:38 PM, Laurent C. <lrntct@gmail.com> wrote:

Hello,

Try this:

import grass.script as gscript
gscript.run_command("r.shade", shade=aspect, color=elevation,
output=elevation_aspect_shaded)

Have a look at the wiki:
https://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library

Laurent

2016-05-23 8:01 GMT-05:00 Gareth Grewcock <garethgrewcock@gmail.com>:
> Hi Stefan - thanks for getting back to me.
>
> I had tried that before but still got issues, this time returning:
> 'NameError: name 'r' is not defined'
>
> r.shade(shade=aspect, color=elevation, output=elevation_aspect_shaded)
> Traceback (most recent call last):
> File "<input>", line 1, in <module>
> NameError: name 'r' is not defined
>
> Any ideas what might be causing that?
>
> Cheers,
> Gareth
>
> On Mon, May 23, 2016 at 12:47 PM, Blumentrath, Stefan
> <Stefan.Blumentrath@nina.no> wrote:
>>
>> Hei,
>>
>>
>>
>> Maybe the issue is missing commas?
>>
>>
>>
>> r.shade(shade=aspect, color=elevation, output=elevation_aspect_shaded)
>>
>>
>>
>> Cheers
>>
>> Stefan
>>
>>
>>
>> From: grass-user [mailto:grass-user-bounces@lists.osgeo.org] On Behalf
>> Of
>> Gareth Grewcock
>> Sent: 23. mai 2016 11:49
>> To: grass-user@lists.osgeo.org
>> Subject: [GRASS-user] r.shade python issues
>>
>>
>>
>> Hi – I’m having issues with r.shade module in grass 7.0.3 when trying
>> to
>> interact with data via python.
>>
>>
>>
>> r.shade runs absolutely fine through the UI, however, when I run the
>> syntax through the python console, I get a syntax error.
>>
>>
>>
>> I have tried a variety of different datasets and syntax but it keeps
>> coming back with the syntax error, I went back to manual for the basics
>> but
>> I still get the following:
>>
>>
>>
>> r.shade shade=aspect color=elevation output=elevation_aspect_shaded
>>
>> File "<input>", line 1
>>
>> r.shade(shade=aspect color=elevation
>> output=elevation_aspect_shaded)
>>
>> ^
>>
>> SyntaxError: invalid syntax
>>
>>
>>
>> Other modules (r.in.gdal, r.relief, r.slope.aspect, g.region etc) all
>> run
>> fine through python, I am only having issues with r.shade.
>>
>>
>>
>> Does anybody have any idea what might be going wrong?
>>
>>
>>
>> Cheers,
>>
>> Gareth
>>
>>
>
>
>
> _______________________________________________
> grass-user mailing list
> grass-user@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user

On 23/05/16 15:01, Gareth Grewcock wrote:

Hi Stefan - thanks for getting back to me.

I had tried that before but still got issues, this time returning:
'NameError: name 'r' is not defined'

/r.shade(shade=aspect, color=elevation, output=elevation_aspect_shaded)/
/Traceback (most recent call last):confused:
/ File "<input>", line 1, in <module>/
/NameError: name 'r' is not defined/
/
Any ideas what might be causing that?

If you are using pygrass, you have to create the r.shade module object before using it. See [1].

If you are not using pygrass, please give us more information about what happens in your script before the call that fails.

Moritz

[1] https://grass.osgeo.org/grass70/manuals/libpython/pygrass_modules.html

Again - thanks for all the help so far, it is much appreciated.

Laurent - I ran the read2_command and it returned the following:

Traceback (most recent call last):
File “”, line 254, in run_nodebug
File “C:\Users\ggrewcock\Dropbox\PythonScripts\gmt\grass-interface-v2.py”, line 85, in
grass.read2_command(‘r.shade’, shade=‘hillshade_output_python’,color=‘dem_slope’, output=‘shaded_raster_hillshade_map’, brighten=‘25’, overwrite=True)
AttributeError: ‘module’ object has no attribute ‘read2_command’

Moritz - I have managed to get r.shade to run in the UI by creating the module object however, when I run through the script I get a parsing error. Is this because python env variables are all ‘buried’ within the ArcGIS folder?

Traceback (most recent call last):
File “”, line 254, in run_nodebug
File “”, line 3, in
File “C:\OSGEO4~1\apps\grass\grass-7.0.3\etc\python\grass\pygrass\modules\interface\module.py”, line 471, in init
tree = fromstring(self.xml)
File “C:\Python27\ArcGIS10.4\Lib\xml\etree\ElementTree.py”, line 1301, in XML
return parser.close()
File “C:\Python27\ArcGIS10.4\Lib\xml\etree\ElementTree.py”, line 1654, in close
self._raiseerror(v)
File “C:\Python27\ArcGIS10.4\Lib\xml\etree\ElementTree.py”, line 1506, in _raiseerror
raise err
xml.etree.ElementTree.ParseError: no element found: line 1, column 0

Cheers,
Gareth

···

On Mon, May 23, 2016 at 6:26 PM, Moritz Lennert <mlennert@club.worldonline.be> wrote:

On 23/05/16 15:01, Gareth Grewcock wrote:

Hi Stefan - thanks for getting back to me.

I had tried that before but still got issues, this time returning:
‘NameError: name ‘r’ is not defined’

/r.shade(shade=aspect, color=elevation, output=elevation_aspect_shaded)/
/Traceback (most recent call last):confused:
/ File “”, line 1, in /
/NameError: name ‘r’ is not defined/
/
/
Any ideas what might be causing that?

If you are using pygrass, you have to create the r.shade module object before using it. See [1].

If you are not using pygrass, please give us more information about what happens in your script before the call that fails.

Moritz

[1] https://grass.osgeo.org/grass70/manuals/libpython/pygrass_modules.html

2016-05-24 4:36 GMT-05:00 Gareth Grewcock <garethgrewcock@gmail.com>:

Again - thanks for all the help so far, it is much appreciated.

Laurent - I ran the read2_command and it returned the following:

Traceback (most recent call last):
  File "<string>", line 254, in run_nodebug
  File "C:\Users\ggrewcock\Dropbox\PythonScripts\gmt\grass-interface-v2.py",
line 85, in <module>
    grass.read2_command('r.shade',
shade='hillshade_output_python',color='dem_slope',
output='shaded_raster_hillshade_map', brighten='25', overwrite=True)
AttributeError: 'module' object has no attribute 'read2_command'

You have to define the function yourself with the code from the wiki,
it is not part of the GRASS scripting library.
Laurent

On Tue, May 24, 2016 at 1:11 PM, Laurent C. <lrntct@gmail.com> wrote:

2016-05-24 4:36 GMT-05:00 Gareth Grewcock <garethgrewcock@gmail.com>:
> Again - thanks for all the help so far, it is much appreciated.
>
> Laurent - I ran the read2_command and it returned the following:
>
> output='shaded_raster_hillshade_map', brighten='25', overwrite=True)
> AttributeError: 'module' object has no attribute 'read2_command'

You have to define the function yourself with the code from the wiki,
it is not part of the GRASS scripting library.

Hi all,

let me enter the conversation here.

Gareth, using custom function might be quite an overkill here, the original
problem was wrong Python syntax (unrelated to any GRASS GIS functionality).

Laurent, if you think you must consistently use some custom function, then
you should consider opening a enhancement ticket and ideally provide a
patch as well. What you are using might be useful for others.

Best,
Vaclav

Hi Vaclav - I agree, the GRASS GIS functionality itself is brilliant and is just what I need and the issue does lie with Python.

That said, it is a bit strange with what is going on, using the module object works for other modules apart from r.shade, I seem to get an XML parsing error: xml.etree.ElementTree.ParseError: no element found: line 1, column 0

Can anybody advise what the correct syntax should be for using r.shade with pygrass please?

I can’t even seem to be able to initialize the r.shade module as per the wiki but I can others.

This works:

slope_aspect = Module(‘r.slope.aspect’)

This doesn’t:

shade = Module(‘r.shade’)

Thanks,
Gareth

···

On Tue, May 24, 2016 at 10:29 PM, Vaclav Petras <wenzeslaus@gmail.com> wrote:

On Tue, May 24, 2016 at 1:11 PM, Laurent C. <lrntct@gmail.com> wrote:

2016-05-24 4:36 GMT-05:00 Gareth Grewcock <garethgrewcock@gmail.com>:

Again - thanks for all the help so far, it is much appreciated.

Laurent - I ran the read2_command and it returned the following:

output=‘shaded_raster_hillshade_map’, brighten=‘25’, overwrite=True)
AttributeError: ‘module’ object has no attribute ‘read2_command’

You have to define the function yourself with the code from the wiki,
it is not part of the GRASS scripting library.

Hi all,

let me enter the conversation here.

Gareth, using custom function might be quite an overkill here, the original problem was wrong Python syntax (unrelated to any GRASS GIS functionality).

Laurent, if you think you must consistently use some custom function, then you should consider opening a enhancement ticket and ideally provide a patch as well. What you are using might be useful for others.

Best,

Vaclav

On Wed, May 25, 2016 at 5:36 AM, Gareth Grewcock
<garethgrewcock@gmail.com> wrote:

Hi Vaclav - I agree, the GRASS GIS functionality itself is brilliant and is
just what I need and the issue does lie with Python.

That said, it is a bit strange with what is going on, using the module
object works for other modules apart from r.shade, I seem to get an XML
parsing error: xml.etree.ElementTree.ParseError: no element found: line 1,
column 0

Can anybody advise what the correct syntax should be for using r.shade with
pygrass please?

I can't even seem to be able to initialize the r.shade module as per the
wiki but I can others.

This works:

slope_aspect = Module('r.slope.aspect')

this module is written in C

This doesn't:

shade = Module('r.shade')

and this in Python. Also in the error you posted, the ArcGIS Python is
used instead of the one bundled with OSGeo4W, so there seems to be
some Python mixup going on, but I don't why or how to fix it...

Anna

Thanks,
Gareth

On Tue, May 24, 2016 at 10:29 PM, Vaclav Petras <wenzeslaus@gmail.com>
wrote:

On Tue, May 24, 2016 at 1:11 PM, Laurent C. <lrntct@gmail.com> wrote:

2016-05-24 4:36 GMT-05:00 Gareth Grewcock <garethgrewcock@gmail.com>:
> Again - thanks for all the help so far, it is much appreciated.
>
> Laurent - I ran the read2_command and it returned the following:
>
> output='shaded_raster_hillshade_map', brighten='25', overwrite=True)
> AttributeError: 'module' object has no attribute 'read2_command'

You have to define the function yourself with the code from the wiki,
it is not part of the GRASS scripting library.

Hi all,

let me enter the conversation here.

Gareth, using custom function might be quite an overkill here, the
original problem was wrong Python syntax (unrelated to any GRASS GIS
functionality).

Laurent, if you think you must consistently use some custom function, then
you should consider opening a enhancement ticket and ideally provide a patch
as well. What you are using might be useful for others.

Best,
Vaclav

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

Thanks Anna - that’s really helpful. The fact it was written in python got me thinking.

I was running OSGeo4W64, I have since installed GRASS standalone and OSGeo4W and the r.shade module now runs successfully.

Thanks everybody for your help and giving up your time to look into this.

Cheers,
Gareth

···

On Wed, May 25, 2016 at 1:42 PM, Anna Petrášová <kratochanna@gmail.com> wrote:

On Wed, May 25, 2016 at 5:36 AM, Gareth Grewcock
<garethgrewcock@gmail.com> wrote:

Hi Vaclav - I agree, the GRASS GIS functionality itself is brilliant and is
just what I need and the issue does lie with Python.

That said, it is a bit strange with what is going on, using the module
object works for other modules apart from r.shade, I seem to get an XML
parsing error: xml.etree.ElementTree.ParseError: no element found: line 1,
column 0

Can anybody advise what the correct syntax should be for using r.shade with
pygrass please?

I can’t even seem to be able to initialize the r.shade module as per the
wiki but I can others.

This works:

slope_aspect = Module(‘r.slope.aspect’)

this module is written in C

This doesn’t:

shade = Module(‘r.shade’)

and this in Python. Also in the error you posted, the ArcGIS Python is
used instead of the one bundled with OSGeo4W, so there seems to be
some Python mixup going on, but I don’t why or how to fix it…

Anna

Thanks,
Gareth

On Tue, May 24, 2016 at 10:29 PM, Vaclav Petras <wenzeslaus@gmail.com>
wrote:

On Tue, May 24, 2016 at 1:11 PM, Laurent C. <lrntct@gmail.com> wrote:

2016-05-24 4:36 GMT-05:00 Gareth Grewcock <garethgrewcock@gmail.com>:

Again - thanks for all the help so far, it is much appreciated.

Laurent - I ran the read2_command and it returned the following:

output=‘shaded_raster_hillshade_map’, brighten=‘25’, overwrite=True)
AttributeError: ‘module’ object has no attribute ‘read2_command’

You have to define the function yourself with the code from the wiki,
it is not part of the GRASS scripting library.

Hi all,

let me enter the conversation here.

Gareth, using custom function might be quite an overkill here, the
original problem was wrong Python syntax (unrelated to any GRASS GIS
functionality).

Laurent, if you think you must consistently use some custom function, then
you should consider opening a enhancement ticket and ideally provide a patch
as well. What you are using might be useful for others.

Best,
Vaclav


grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user