"Neighbourhood modifier" from r.mapcalc in R?

5 messages Options
Embed this post
Permalink
Rainer M. Krug-3

"Neighbourhood modifier" from r.mapcalc in R?

Reply Threaded More More options
Print post
Permalink
Hi

I want to do some map calculations which involve binominal distributed
random numbers which I can not obtain in r.mapcalc. Therefore I import
the raster via readRAST6() into R and I want to do the calculations
there. But I am stuck...

In r.mapcalc I use somthing like

OUTPUT=INPUT1[-2,-2]*INPUT2+INPUT1[+2,+2]*INPUT2

How can I translate this into R?

rA <- readRAST6("INPUT1")
rB <- readRAST6("INPUT2")
but now?

rA$A gives me all cell values,
rA[1:2,1:2]$A gives me the cell values from columns and row 1 and 2,

But I have no idea where I should put the offset in.

Rainer

_______________________________________________
statsgrass mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/statsgrass
Roger Bivand

Re: "Neighbourhood modifier" from r.mapcalc in R?

Reply Threaded More More options
Print post
Permalink
On Tue, 16 Oct 2007, Rainer M Krug wrote:

> Hi
>
> I want to do some map calculations which involve binominal distributed random
> numbers which I can not obtain in r.mapcalc. Therefore I import the raster
> via readRAST6() into R and I want to do the calculations there. But I am
> stuck...
>
> In r.mapcalc I use somthing like
>
> OUTPUT=INPUT1[-2,-2]*INPUT2+INPUT1[+2,+2]*INPUT2
>
> How can I translate this into R?
>
> rA <- readRAST6("INPUT1")
> rB <- readRAST6("INPUT2")
> but now?
>
> rA$A gives me all cell values,
> rA[1:2,1:2]$A gives me the cell values from columns and row 1 and 2,
>
> But I have no idea where I should put the offset in.

As designed, the SpatialGridDataFrame objects are intended to have the
same regions in the g.region sense. They do have grid slots, and you could
shift the whole grid by changing the cellcentre.offset slot in the
GridTopology object in the grid slot, and update the bbox. This would give
grids that are offset in relation to each other, but to get the values
across all offset grids, you'd need to back the original SpatialGrid out
to SpatialPoints, and use the overlay() method for SpatialPoints and
SpatialGridDataFrame objects.

An alternative that would need coding is to look inside the "[" method for
SpatialGridDataFrame objects (which you refer to above) and see how one
might write a relative reference function which would not subset on the i
and j arguments, but shift by row and column.

Probably the first alternative involves less work.

Roger

>
> Rainer
>
> _______________________________________________
> statsgrass mailing list
> [hidden email]
> http://grass.itc.it/mailman/listinfo/statsgrass
>
>

--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: [hidden email]

_______________________________________________
statsgrass mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/statsgrass
Roger Bivand
Economic Geography Section
Department of Economics
Norwegian School of Economics and Business Administration
Helleveien 30
N-5045 Bergen, Norway
Agustin Lobo-2

Re: "Neighbourhood modifier" from r.mapcalc in R?

Reply Threaded More More options
Print post
Permalink
In reply to this post by Rainer M. Krug-3
Have you tried shift.im() from spat.stats to create the two
shifted rasters first and then operate with them?
Or perhaps extracting the matrix from the sp object and doing
the same in matrix notation. For example, if INPUT1 is a matrix,
then
INPUT1NW <- cbind(INPUT1[,-(1:2)],NA*INPUT1[,1:2])
INPUT1NW <- rbind(INPUT1NW[-(1:2),],NA*INPUT1NW[1:2,])
and an equivalent for the SE shift (INPUT1SE)

Then
OUTPUT <- INPUT1NW*INPUT2 + INPUT1SE*INPUT2

and make an sp object with OUTPUT

Agus



Rainer M Krug escribió:

> Hi
>
> I want to do some map calculations which involve binominal distributed
> random numbers which I can not obtain in r.mapcalc. Therefore I import
> the raster via readRAST6() into R and I want to do the calculations
> there. But I am stuck...
>
> In r.mapcalc I use somthing like
>
> OUTPUT=INPUT1[-2,-2]*INPUT2+INPUT1[+2,+2]*INPUT2
>
> How can I translate this into R?
>
> rA <- readRAST6("INPUT1")
> rB <- readRAST6("INPUT2")
> but now?
>
> rA$A gives me all cell values,
> rA[1:2,1:2]$A gives me the cell values from columns and row 1 and 2,
>
> But I have no idea where I should put the offset in.
>
> Rainer
>
> _______________________________________________
> statsgrass mailing list
> [hidden email]
> http://grass.itc.it/mailman/listinfo/statsgrass
>

--
Dr. Agustin Lobo
Institut de Ciencies de la Terra "Jaume Almera" (CSIC)
LLuis Sole Sabaris s/n
08028 Barcelona
Spain
Tel. 34 934095410
Fax. 34 934110012
email: [hidden email]
http://www.ija.csic.es/gt/obster

_______________________________________________
statsgrass mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/statsgrass
Rainer M. Krug-3

Re: "Neighbourhood modifier" from r.mapcalc in R?

Reply Threaded More More options
Print post
Permalink
In reply to this post by Roger Bivand
Thanks - I'll look into your suggestions later this week.

Rainer

Roger Bivand wrote:

> On Tue, 16 Oct 2007, Rainer M Krug wrote:
>
>> Hi
>>
>> I want to do some map calculations which involve binominal distributed
>> random numbers which I can not obtain in r.mapcalc. Therefore I import
>> the raster via readRAST6() into R and I want to do the calculations
>> there. But I am stuck...
>>
>> In r.mapcalc I use somthing like
>>
>> OUTPUT=INPUT1[-2,-2]*INPUT2+INPUT1[+2,+2]*INPUT2
>>
>> How can I translate this into R?
>>
>> rA <- readRAST6("INPUT1")
>> rB <- readRAST6("INPUT2")
>> but now?
>>
>> rA$A gives me all cell values,
>> rA[1:2,1:2]$A gives me the cell values from columns and row 1 and 2,
>>
>> But I have no idea where I should put the offset in.
>
> As designed, the SpatialGridDataFrame objects are intended to have the
> same regions in the g.region sense. They do have grid slots, and you
> could shift the whole grid by changing the cellcentre.offset slot in the
> GridTopology object in the grid slot, and update the bbox. This would
> give grids that are offset in relation to each other, but to get the
> values across all offset grids, you'd need to back the original
> SpatialGrid out to SpatialPoints, and use the overlay() method for
> SpatialPoints and SpatialGridDataFrame objects.
>
> An alternative that would need coding is to look inside the "[" method
> for SpatialGridDataFrame objects (which you refer to above) and see how
> one might write a relative reference function which would not subset on
> the i and j arguments, but shift by row and column.
>
> Probably the first alternative involves less work.
>
> Roger
>
>>
>> Rainer
>>
>> _______________________________________________
>> statsgrass mailing list
>> [hidden email]
>> http://grass.itc.it/mailman/listinfo/statsgrass
>>
>>
>

_______________________________________________
statsgrass mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/statsgrass
Rainer M. Krug-3

Re: "Neighbourhood modifier" from r.mapcalc in R?

Reply Threaded More More options
Print post
Permalink
In reply to this post by Agustin Lobo-2
Hi

Thanks for pointing out shift.im() and the matrix notation - I was
thinking along the same lines and will look into it again later this week

Rainer


Agustin Lobo wrote:

> Have you tried shift.im() from spat.stats to create the two
> shifted rasters first and then operate with them?
> Or perhaps extracting the matrix from the sp object and doing
> the same in matrix notation. For example, if INPUT1 is a matrix,
> then
> INPUT1NW <- cbind(INPUT1[,-(1:2)],NA*INPUT1[,1:2])
> INPUT1NW <- rbind(INPUT1NW[-(1:2),],NA*INPUT1NW[1:2,])
> and an equivalent for the SE shift (INPUT1SE)
>
> Then
> OUTPUT <- INPUT1NW*INPUT2 + INPUT1SE*INPUT2
>
> and make an sp object with OUTPUT
>
> Agus
>
>
>
> Rainer M Krug escribió:
>> Hi
>>
>> I want to do some map calculations which involve binominal distributed
>> random numbers which I can not obtain in r.mapcalc. Therefore I import
>> the raster via readRAST6() into R and I want to do the calculations
>> there. But I am stuck...
>>
>> In r.mapcalc I use somthing like
>>
>> OUTPUT=INPUT1[-2,-2]*INPUT2+INPUT1[+2,+2]*INPUT2
>>
>> How can I translate this into R?
>>
>> rA <- readRAST6("INPUT1")
>> rB <- readRAST6("INPUT2")
>> but now?
>>
>> rA$A gives me all cell values,
>> rA[1:2,1:2]$A gives me the cell values from columns and row 1 and 2,
>>
>> But I have no idea where I should put the offset in.
>>
>> Rainer
>>
>> _______________________________________________
>> statsgrass mailing list
>> [hidden email]
>> http://grass.itc.it/mailman/listinfo/statsgrass
>>
>

_______________________________________________
statsgrass mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/statsgrass