[GRASS-dev] C function which returns raster and additional info?

If this mail has made it already to the lst, I apologise - I just could not find it anywhere.

Hi

I am thinking about the possibility of loading raster directly from GRASS into R, to avoid the overhang of saving to disk and loading again or having gdal in between GRASS and R.

As R (using the sp package) holds the raster in memory, I was thinking about reading the data from GRASS by using a C function, returning the cell values and additional info (at least the one provided in esri ACSII file, i.e. NCOLS, NROWS, XLLCORNER, YLLCORNER, CELLSIZE, NODATA_VALUE) plus geo-referencing info, so that, back in R, this data can be assembled to become a spatial object (SpatialGridDataFrame, as defined in the package sp).

The part in R should be quite easy, as soon as I have the data - so is there a C function which could help me to get the data?

This would be a first step - in the next step, I would be looking into the possibility to use a similar way of writing the data to GRASS,

Any ideas, suggestions?

Thanks,

Rainer


Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel : +33 - (0)9 53 10 27 44
Cell: +33 - (0)6 85 62 59 98
Fax (F): +33 - (0)9 58 10 27 44

Fax (D): +49 - (0)3 21 21 25 22 44

email: Rainer@krugs.de

Skype: RMkrug

On 18/10/11 10:26, Rainer M Krug wrote:

If this mail has made it already to the lst, I apologise - I just could
not find it anywhere.

Hi

I am thinking about the possibility of loading raster directly from
GRASS into R, to avoid the overhang of saving to disk and loading again
or having gdal in between GRASS and R.

Have you had a look at:
http://cran.r-project.org/web/packages/spgrass6/

Moritz

On Tue, Oct 18, 2011 at 2:13 PM, Moritz Lennert <mlennert@club.worldonline.be> wrote:

On 18/10/11 10:26, Rainer M Krug wrote:

If this mail has made it already to the lst, I apologise - I just could
not find it anywhere.

Hi

I am thinking about the possibility of loading raster directly from
GRASS into R, to avoid the overhang of saving to disk and loading again
or having gdal in between GRASS and R.

Have you had a look at:
http://cran.r-project.org/web/packages/spgrass6/

Yes - I am using it regularly and am in more or less regular contact with Roger, the author of the package.

In my opinion, there are a few disadvantages in the approach used (which works perfectly fine in most cases):

The linking to GRASS si done via GDAL, the gdal-grass plugin or via export from grass to file and import from this file.

So one needs either a middle layer to be installed (GDAL) or go via the more time intensive way via writing to file – loading from file. If this has to be done a few times, it can cost a considerable amount of time. This is why I was thinking about direct linking the two. The C or C++ code could be called from R to get the cell values and the related metadata, and in R one could construct the spatial layer. This would be considerably faster.

That is my reasoning to the question. As I am not involved in GRASS development, I do not know if there is a function which I could use, or if it could be easily be written.

Cheers,

Rainer

Moritz


Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel : +33 - (0)9 53 10 27 44
Cell: +33 - (0)6 85 62 59 98
Fax (F): +33 - (0)9 58 10 27 44

Fax (D): +49 - (0)3 21 21 25 22 44

email: Rainer@krugs.de

Skype: RMkrug

On Tue, Oct 18, 2011 at 10:26 AM, Rainer M Krug <r.m.krug@gmail.com> wrote:

If this mail has made it already to the lst, I apologise - I just could not
find it anywhere.

Hi

I am thinking about the possibility of loading raster directly from GRASS
into R, to avoid the overhang of saving to disk and loading again or having
gdal in between GRASS and R.

As R (using the sp package) holds the raster in memory, I was thinking about
reading the data from GRASS by using a C function, returning the cell values
and additional info (at least the one provided in esri ACSII file, i.e.
NCOLS, NROWS, XLLCORNER, YLLCORNER, CELLSIZE, NODATA_VALUE) plus
geo-referencing info, so that, back in R, this data can be assembled to
become a spatial object (SpatialGridDataFrame, as defined in the package
sp).

An option may be to
- open the raster map,
- set computational region to the map
- read G_get_window()

http://grass.osgeo.org/programming6/get__window_8c.html#ada28852284056440f08c032e64a80765

Markus

On Wed, Oct 19, 2011 at 8:36 AM, Markus Neteler <neteler@osgeo.org> wrote:

On Tue, Oct 18, 2011 at 10:26 AM, Rainer M Krug <r.m.krug@gmail.com> wrote:

If this mail has made it already to the lst, I apologise - I just could not
find it anywhere.

Hi

I am thinking about the possibility of loading raster directly from GRASS
into R, to avoid the overhang of saving to disk and loading again or having
gdal in between GRASS and R.

As R (using the sp package) holds the raster in memory, I was thinking about
reading the data from GRASS by using a C function, returning the cell values
and additional info (at least the one provided in esri ACSII file, i.e.
NCOLS, NROWS, XLLCORNER, YLLCORNER, CELLSIZE, NODATA_VALUE) plus
geo-referencing info, so that, back in R, this data can be assembled to
become a spatial object (SpatialGridDataFrame, as defined in the package
sp).

An option may be to

  • open the raster map,

What do you mean by "open the raster map)?

  • set computational region to the map

Good idea - but it would also be an option to use the active region, to enable the reading of certain selected regions.

  • read G_get_window()

http://grass.osgeo.org/programming6/get__window_8c.html#ada28852284056440f08c032e64a80765

Thanks - that’s great.
That enables me to get the header date to construct a raster with the same info in R as a SpatialGrisDataFrame.

Now I would need a function, which reads the cell values and returns it - either the complete raster, or the by column / row, and then I could assemble it - any ideas?

Rainer

Markus


Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel : +33 - (0)9 53 10 27 44
Cell: +33 - (0)6 85 62 59 98
Fax (F): +33 - (0)9 58 10 27 44

Fax (D): +49 - (0)3 21 21 25 22 44

email: Rainer@krugs.de

Skype: RMkrug

On Wed, Oct 19, 2011 at 10:03 AM, Rainer M Krug <r.m.krug@gmail.com> wrote:

On Wed, Oct 19, 2011 at 8:36 AM, Markus Neteler <neteler@osgeo.org> wrote:

On Tue, Oct 18, 2011 at 10:26 AM, Rainer M Krug <r.m.krug@gmail.com>
wrote:
> If this mail has made it already to the lst, I apologise - I just could
> not
> find it anywhere.
>
>
> Hi
>
> I am thinking about the possibility of loading raster directly from
> GRASS
> into R, to avoid the overhang of saving to disk and loading again or
> having
> gdal in between GRASS and R.
>
> As R (using the sp package) holds the raster in memory, I was thinking
> about
> reading the data from GRASS by using a C function, returning the cell
> values
> and additional info (at least the one provided in esri ACSII file, i.e.
> NCOLS, NROWS, XLLCORNER, YLLCORNER, CELLSIZE, NODATA_VALUE) plus
> geo-referencing info, so that, back in R, this data can be assembled to
> become a spatial object (SpatialGridDataFrame, as defined in the package
> sp).

An option may be to
- open the raster map,

What do you mean by "open the raster map)?

- set computational region to the map

Good idea - but it would also be an option to use the active region, to
enable the reading of certain selected regions.

- read G_get_window()

http://grass.osgeo.org/programming6/get__window_8c.html#ada28852284056440f08c032e64a80765

Thanks - that's great.
That enables me to get the header date to construct a raster with the same
info in R as a SpatialGrisDataFrame.

Now I would need a function, which reads the cell values and returns it -
either the complete raster, or the by column / row, and then I could
assemble it - any ideas?

In the grass source tree, see doc/raster/r.example/main.c (or any
raster C module) about how to fetch cell values.

See also

http://grass.osgeo.org/programming6/gisrasterlib.html

for an introduction to C functions for raster processing.

Is it possible that Roger Bivand tried that approach in an earlier
version of spgrass, maybe for GRASS 5? If yes, he must have abandoned
that approach for good reason.

Markus M