question on r.out.gdal in a loop

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

question on r.out.gdal in a loop

Gabriel Cotlier
Dear grass users,

I'm using grass 7.4.1 trying to export  all raster layers in the current mapset out of grass to a folder as GeoTiff files though a for loop from the python shell  as follows and I'm getting the error in red...
What could be the problem?
Thanks a lot in advance.
Regards,
Gabriel

>>>import grass.script as gscript
>>>rastlist=grass.read_command("g.list",type="rast")
>>>rastlist
F121996
F121997
F141999
F142000

>>>os.chdir('C:\\Users\\Gabriel\\Desktop\\out')
None
>>>for raster in rastlist:
...         gscript.run_command('r.out.gdal', input=raster,
...                                             output=raster + '.tiff', format='GTiff')
                       
Traceback (most recent call last):
  File "<input>", line 3, in <module>
  File "C:\OSGEO4~1\apps\grass\grass-7.4.1\etc\python\grass\script\core.py", line 421, in run_command
    return handle_errors(returncode, returncode, args, kwargs)
  File "C:\OSGEO4~1\apps\grass\grass-7.4.1\etc\python\grass\script\core.py", line 332, in handle_errors
    returncode=returncode)
CalledModuleError: Module run None ['r.out.gdal', 'input=F', 'output=F.tiff', 'format=GTiff'] ended with error
Process ended with non-zero return code 1. See errors in the (error) output.


_______________________________________________
grass-user mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/grass-user
Reply | Threaded
Open this post in threaded view
|

Re: question on r.out.gdal in a loop

Micha Silver-2


On 07/11/2018 02:20 AM, Gabriel Cotlier wrote:
Dear grass users,

I'm using grass 7.4.1 trying to export  all raster layers in the current mapset out of grass to a folder as GeoTiff files though a for loop from the python shell  as follows and I'm getting the error in red...
What could be the problem?
Thanks a lot in advance.
Regards,
Gabriel

>>>import grass.script as gscript
>>>rastlist=grass.read_command("g.list",type="rast")
>>>rastlist
F121996
F121997
F141999
F142000

Try:
rastlist=grass.read_command("g.list",type="rast").split()


>>>os.chdir('C:\\Users\\Gabriel\\Desktop\\out')
None
>>>for raster in rastlist:
...         gscript.run_command('r.out.gdal', input=raster,
...                                             output=raster + '.tiff', format='GTiff')
                       
Traceback (most recent call last):
  File "<input>", line 3, in <module>
  File "C:\OSGEO4~1\apps\grass\grass-7.4.1\etc\python\grass\script\core.py", line 421, in run_command
    return handle_errors(returncode, returncode, args, kwargs)
  File "C:\OSGEO4~1\apps\grass\grass-7.4.1\etc\python\grass\script\core.py", line 332, in handle_errors
    returncode=returncode)
CalledModuleError: Module run None ['r.out.gdal', 'input=F', 'output=F.tiff', 'format=GTiff'] ended with error
Process ended with non-zero return code 1. See errors in the (error) output.



_______________________________________________
grass-user mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/grass-user

-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918

_______________________________________________
grass-user mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/grass-user
Reply | Threaded
Open this post in threaded view
|

Re: question on r.out.gdal in a loop

NikosAlexandris

Gabriel Cotlier wrote:

>
>     I'm using grass 7.4.1 trying to export  all raster layers in the current
>     mapset out of grass to a folder as GeoTiff files though a for loop from
>     the python shell  as follows and I'm getting the error in red...
>     What could be the problem?
>     Thanks a lot in advance.
>     Regards,
>     Gabriel
>     >>>import grass.script as gscript
>     >>>rastlist=grass.read_command("g.list",type="rast")
>     >>>rastlist
>     F121996
>     F121997
>     F141999
>     F142000
Micha Silver:
>   Try:
>   rastlist=grass.read_command("g.list",type="rast").split()

[..]

For me it works like:

rasters=grass.read_command('g.list', type='raster', separator='comma').split(',')

I wonder why the 'comma' is not mentioned for the separator option, when
asking for the module's help:
```
Lists available GRASS data base files of the user-specified data type optionally using the search pattern.

Usage:
 g.list [-iretmpf] type=datatype[,datatype,...] [pattern=string]
   [exclude=string] [mapset=name[,name,...]] [separator=character]
   [region=name] [output=name] [--overwrite] [--help] [--verbose]
   [--quiet] [--ui]

Flags:
  -i   Ignore case
  -r   Use basic regular expressions instead of wildcards
  -e   Use extended regular expressions instead of wildcards
  -t   Print data types
  -m   Print fully-qualified map names (including mapsets)
  -p   Pretty printing in human readable format
  -f   Verbose listing (also list map titles)

Parameters:
       type   Data type(s)
              options: raster,raster_3d,vector,label,region,group,all
    pattern   Map name search pattern (default: all)
    exclude   Map name exclusion pattern (default: none)
     mapset   Name of mapset to list (default: current search path)
  separator   Field separator
              default: newline
     region   Name of saved region for map search (default: not restricted)
     output   Name for output file
```

This on
```
g.version -r
GRASS 7.5.svn (2018)
libgis Revision: 72327
libgis Date: 2018-03-06 12:12:44 +0100 (Tue, 06 Mar 2018)
```

Nikos

_______________________________________________
grass-user mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/grass-user

signature.asc (235 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: question on r.out.gdal in a loop

NikosAlexandris
* Nikos Alexandris <[hidden email]> [2018-07-11 09:14:47 +0200]:

>
>Gabriel Cotlier wrote:
>>
>>    I'm using grass 7.4.1 trying to export  all raster layers in the current
>>    mapset out of grass to a folder as GeoTiff files though a for loop from
>>    the python shell  as follows and I'm getting the error in red...
>>    What could be the problem?
>>    Thanks a lot in advance.
>>    Regards,
>>    Gabriel
>>    >>>import grass.script as gscript
>>    >>>rastlist=grass.read_command("g.list",type="rast")
>>    >>>rastlist
>>    F121996
>>    F121997
>>    F141999
>>    F142000
>
>Micha Silver:
>>  Try:
>>  rastlist=grass.read_command("g.list",type="rast").split()
>
>[..]

>For me it works like:
>
>rasters=grass.read_command('g.list', type='raster', separator='comma').split(',')

For splitting on newlines, there is splitlines(), i.e.

```
rasters = grass.read_command('g.list', type='raster').splitlines()
for raster in rasters:
    print "Raster:", raster
    raster += '.tif'
    print "Raster with extension:", raster
```

See https://docs.python.org/2/library/stdtypes.html#str.splitlines

Nikos

_______________________________________________
grass-user mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/grass-user

signature.asc (235 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: question on r.out.gdal in a loop

Gabriel Cotlier
Dear Nikos and Micha,

I tried both and worked ways for constructing the list and worked well:



and also:



but still have an error problem with the loop

why could be this happening?
Thanks a lot.
Gabriel

On Wed, Jul 11, 2018 at 5:00 AM, Nikos Alexandris <[hidden email]> wrote:
* Nikos Alexandris <[hidden email]> [2018-07-11 09:14:47 +0200]:


Gabriel Cotlier wrote:

   I'm using grass 7.4.1 trying to export  all raster layers in the current
   mapset out of grass to a folder as GeoTiff files though a for loop from
   the python shell  as follows and I'm getting the error in red...
   What could be the problem?
   Thanks a lot in advance.
   Regards,
   Gabriel
   >>>import grass.script as gscript
   >>>rastlist=grass.read_command("g.list",type="rast")
   >>>rastlist
   F121996
   F121997
   F141999
   F142000

Micha Silver:
 Try:
 rastlist=grass.read_command("g.list",type="rast").split()

[..]

For me it works like:

rasters=grass.read_command('g.list', type='raster', separator='comma').split(',')

For splitting on newlines, there is splitlines(), i.e.

```
rasters = grass.read_command('g.list', type='raster').splitlines()
for raster in rasters:
   print "Raster:", raster
   raster += '.tif'
   print "Raster with extension:", raster
```

See https://docs.python.org/2/library/stdtypes.html#str.splitlines

Nikos

_______________________________________________
grass-user mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/grass-user


_______________________________________________
grass-user mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/grass-user
Reply | Threaded
Open this post in threaded view
|

Re: question on r.out.gdal in a loop

Gabriel Cotlier
solved with correcting spelling mistake at file extension, now the for loop worked for me as: 



Gabriel 

On Wed, Jul 11, 2018 at 6:39 PM, Gabriel Cotlier <[hidden email]> wrote:
Dear Nikos and Micha,

I tried both and worked ways for constructing the list and worked well:



and also:



but still have an error problem with the loop

why could be this happening?
Thanks a lot.
Gabriel

On Wed, Jul 11, 2018 at 5:00 AM, Nikos Alexandris <[hidden email]> wrote:
* Nikos Alexandris <[hidden email]> [2018-07-11 09:14:47 +0200]:


Gabriel Cotlier wrote:

   I'm using grass 7.4.1 trying to export  all raster layers in the current
   mapset out of grass to a folder as GeoTiff files though a for loop from
   the python shell  as follows and I'm getting the error in red...
   What could be the problem?
   Thanks a lot in advance.
   Regards,
   Gabriel
   >>>import grass.script as gscript
   >>>rastlist=grass.read_command("g.list",type="rast")
   >>>rastlist
   F121996
   F121997
   F141999
   F142000

Micha Silver:
 Try:
 rastlist=grass.read_command("g.list",type="rast").split()

[..]

For me it works like:

rasters=grass.read_command('g.list', type='raster', separator='comma').split(',')

For splitting on newlines, there is splitlines(), i.e.

```
rasters = grass.read_command('g.list', type='raster').splitlines()
for raster in rasters:
   print "Raster:", raster
   raster += '.tif'
   print "Raster with extension:", raster
```

See https://docs.python.org/2/library/stdtypes.html#str.splitlines

Nikos

_______________________________________________
grass-user mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/grass-user



_______________________________________________
grass-user mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/grass-user
Reply | Threaded
Open this post in threaded view
|

Re: question on r.out.gdal in a loop

Micha Silver-2
Thanks for reporting
I didn't realize that exporting could fail due to a  file extension.

On 07/12/2018 01:31 AM, Gabriel Cotlier wrote:
solved with correcting spelling mistake at file extension, now the for loop worked for me as: 



Gabriel 

On Wed, Jul 11, 2018 at 6:39 PM, Gabriel Cotlier <[hidden email]> wrote:
Dear Nikos and Micha,

I tried both and worked ways for constructing the list and worked well:



and also:



but still have an error problem with the loop

why could be this happening?
Thanks a lot.
Gabriel

On Wed, Jul 11, 2018 at 5:00 AM, Nikos Alexandris <[hidden email]> wrote:
* Nikos Alexandris <[hidden email]> [2018-07-11 09:14:47 +0200]:


Gabriel Cotlier wrote:

   I'm using grass 7.4.1 trying to export  all raster layers in the current
   mapset out of grass to a folder as GeoTiff files though a for loop from
   the python shell  as follows and I'm getting the error in red...
   What could be the problem?
   Thanks a lot in advance.
   Regards,
   Gabriel
   >>>import grass.script as gscript
   >>>rastlist=grass.read_command("g.list",type="rast")
   >>>rastlist
   F121996
   F121997
   F141999
   F142000

Micha Silver:
 Try:
 rastlist=grass.read_command("g.list",type="rast").split()

[..]

For me it works like:

rasters=grass.read_command('g.list', type='raster', separator='comma').split(',')

For splitting on newlines, there is splitlines(), i.e.

```
rasters = grass.read_command('g.list', type='raster').splitlines()
for raster in rasters:
   print "Raster:", raster
   raster += '.tif'
   print "Raster with extension:", raster
```

See https://docs.python.org/2/library/stdtypes.html#str.splitlines

Nikos

_______________________________________________
grass-user mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/grass-user



-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918

_______________________________________________
grass-user mailing list
[hidden email]
https://lists.osgeo.org/mailman/listinfo/grass-user