[GRASS-dev] pyGRASS find number of rows and columns

Pietro peter.zamb at gmail.com
Mon Sep 16 03:35:32 PDT 2013


Hi all!

On Mon, Sep 16, 2013 at 10:43 AM, Sören Gebbert
<soerengebbert at googlemail.com> wrote:
> Hi Yann,
> you can use the raster info-oject to access the raster specific region
> information's:
>
> ras = raster.RasterRow("Bla")
> ras.info.rows
>
> You don't need to open the map to gather the info.

just some example to be more clear...

I'm on North Carolina dataset, my region is:

{{{
projection: 99 (Lambert Conformal Conic)
zone:       0
datum:      nad83
ellipsoid:  a=6378137 es=0.006694380022900787
north:      220750
south:      220000
west:       638300
east:       639000
nsres:      0.2
ewres:      0.2
rows:       3750
cols:       3500
cells:      13125000
}}}

now with python:

{{{
>>> from grass.pygrass.raster import RasterRow
>>> elev = RasterRow('elevation')
>>> elev.info
elevation at PERMANENT
rows: 1350
cols: 1500
north: 228500.0 south: 215000.0 nsres:10.0
east:  645000.0 west: 630000.0 ewres:10.0
range: 56, 156
proj: 99
>>> elev.info.rows
1350
}}}

So the info return just the main data of the raster.

If you open the raster map:

{{{
>>> elev.open('r')
>>> elev.info.rows
1350
>>> elev.info.cols
1500
>>> elev._rows
3750
>>> elev._cols
3500
>>> len(elev)
3750
>>> len(elev[0])
3500
}}}

I put the underscore in front of the rows and cols attribute because
the number of rows and columns depend from the active region that you
are using...

and to discourage the user to work/use these data...

Why do you need the number of rows and columns? If you want to do a
cycle you can do something like:

{{{
>>> for row in elev:
...     dosomething(row)
...
}}}

if you need the row number you can just use enumerate

{{{
>>> for irow, row in enumerate(elev):
...     dosomething(irow, row)
...
}}}

of course you can nest two cycle, something like

{{{
>>> for irow, row in enumerate(elev):
...     for icol, col in enumerate(row):
...         dosomething(irow, icol, row, col)
...
}}}

But I discourage to cycle over the column of a row, because it is too
slow, the row is a numpy array and you should use the numpy API to
work with the row...

Any way If you think that rows and cols are useful attributes I can
rename them from _rows, _cols => row, cols :-)

Best regards

Pietro


More information about the grass-dev mailing list