[GRASS-dev] Creating / reading in random sample of raster values in Python script

Hi,

For a script I am working on, I am looking for a way to select a X number of raster cells and their values into a numpy array. I can create a layer with r.random, and read in that layer into a numpy array, but I rather avoid having to read in the whole raster layer. I can also from within the script run r.random to generate a raster layer, r.stats to export the resulting raster layer as a text file (with n flag), and than read in the text file in Python. But I wonder if there is not a more direct way to accomplish this..

Cheers,

Paulo

Hi,

You can read col and row from GRASS Region and use random python function to calculate random x and y and later query a pygrass raster object

Cheers
Luca

···

Il 25/mar/2017 20:07, “Paulo van Breugel” <p.vanbreugel@gmail.com> ha scritto:

Hi,

For a script I am working on, I am looking for a way to select a X number of raster cells and their values into a numpy array. I can create a layer with r.random, and read in that layer into a numpy array, but I rather avoid having to read in the whole raster layer. I can also from within the script run r.random to generate a raster layer, r.stats to export the resulting raster layer as a text file (with n flag), and than read in the text file in Python. But I wonder if there is not a more direct way to accomplish this…

Cheers,

Paulo

On 25 March 2017 20:21:18 Luca Delucchi lucadeluge@gmail.com wrote:

Il 25/mar/2017 20:07, “Paulo van Breugel” p.vanbreugel@gmail.com ha
scritto:

Hi,

Hi,

For a script I am working on, I am looking for a way to select a X number
of raster cells and their values into a numpy array. I can create a layer
with r.random, and read in that layer into a numpy array, but I rather
avoid having to read in the whole raster layer. I can also from within the
script run r.random to generate a raster layer, r.stats to export the
resulting raster layer as a text file (with n flag), and than read in the
text file in Python. But I wonder if there is not a more direct way to
accomplish this…

You can read col and row from GRASS Region and use random python function
to calculate random x and y and later query a pygrass raster object

Thanks. And what if I have a mask or null values in the target raster.

Cheers,

Paulo

Cheers
Luca

Hello,

If you need a fixed number of points, you might do a while loop that randomly generate coordinates then add the point only if the array is not null at those coordinates.

Laurent

···

On 25 Mar 2017 13:55, “Paulo van Breugel” <p.vanbreugel@gmail.com> wrote:

On 25 March 2017 20:21:18 Luca Delucchi <lucadeluge@gmail.com> wrote:

Il 25/mar/2017 20:07, “Paulo van Breugel” <p.vanbreugel@gmail.com> ha
scritto:

Hi,

Hi,

For a script I am working on, I am looking for a way to select a X number
of raster cells and their values into a numpy array. I can create a layer
with r.random, and read in that layer into a numpy array, but I rather
avoid having to read in the whole raster layer. I can also from within the
script run r.random to generate a raster layer, r.stats to export the
resulting raster layer as a text file (with n flag), and than read in the
text file in Python. But I wonder if there is not a more direct way to
accomplish this…

You can read col and row from GRASS Region and use random python function
to calculate random x and y and later query a pygrass raster object

Thanks. And what if I have a mask or null values in the target raster.

Cheers,

Paulo

Cheers
Luca


grass-dev mailing list
grass-dev@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-dev

Thanks, I’ll give that a try. Will also see how this compares with using r.random/r.what in terms of speed.

···

On 25-03-17 23:11, Laurent C. wrote:

Hello,

If you need a fixed number of points, you might do a while loop that randomly generate coordinates then add the point only if the array is not null at those coordinates.

Laurent

On 25 Mar 2017 13:55, “Paulo van Breugel” <p.vanbreugel@gmail.com> wrote:

On 25 March 2017 20:21:18 Luca Delucchi <lucadeluge@gmail.com> wrote:

Il 25/mar/2017 20:07, “Paulo van Breugel” <p.vanbreugel@gmail.com> ha
scritto:

Hi,

Hi,

For a script I am working on, I am looking for a way to select a X number
of raster cells and their values into a numpy array. I can create a layer
with r.random, and read in that layer into a numpy array, but I rather
avoid having to read in the whole raster layer. I can also from within the
script run r.random to generate a raster layer, r.stats to export the
resulting raster layer as a text file (with n flag), and than read in the
text file in Python. But I wonder if there is not a more direct way to
accomplish this…

You can read col and row from GRASS Region and use random python function
to calculate random x and y and later query a pygrass raster object

Thanks. And what if I have a mask or null values in the target raster.

Cheers,

Paulo

Cheers
Luca


grass-dev mailing list
grass-dev@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-dev

Let us know the results of your tests

Ciao
Luca

···

Il 26/mar/2017 10:58, “Paulo van Breugel” <p.vanbreugel@gmail.com> ha scritto:

Thanks, I’ll give that a try. Will also see how this compares with using r.random/r.what in terms of speed.