[R-sig-Geo] attribing x-coordinates to map cells

Roger Bivand Roger.Bivand at nhh.no
Sat Feb 9 19:30:35 CET 2008


On Sat, 9 Feb 2008, Roger Bivand wrote:

> On Fri, 8 Feb 2008, Milton Cezar Ribeiro wrote:
>
>> Dear all,
>>
>> I have some ascii/grid files and I would like to save the x-coordinates into a new ascii/grid cells with the same dimentions that my input map. Below follow a temptative code.
>>
>> (file1 <-  paste(system.file(package = "adehabitat"),
>>               "ascfiles/elevation.asc", sep = "/"))
>> el <- import.asc(file1)
>> image(el)
>> ##el.xcoord<-  ### here is what I need
>
> Roughly, using the sp SpatialGridDataFrame class:
>
> library(adehabitat)
> (file1 <-  paste(system.file(package = "adehabitat"),
>                "ascfiles/elevation.asc", sep = "/"))
> el <- import.asc(file1)
> image(el)
> library(sp)
> cd <- dim(el)
> cs <- rep(attr(el, "cellsize"), 2)
> co <- c(attr(el, "xll"), attr(el, "yll"))
> grd <- GridTopology(co, cs, cd)
> SGDF <- SpatialGridDataFrame(grd,
>   data=data.frame(asc=as.vector(el[,ncol(el):1])))
> image(SGDF, "asc")
> SGDF$x <- coordinates(SGDF)[,1]
> co1 <- slot(slot(SGDF, "grid"), "cellcentre.offset")
> el1 <- as.asc(as.matrix(SGDF["x"]), xll=co1[1], yll=co1[2],
>   type="numeric")
> image(el1)

A correction to coercion from SGDF to asc which wasn't obvious in the 
example, but is so if we convert the input data, rather than the 
coordinate values; here we need:

co1 <- slot(slot(SGDF, "grid"), "cellcentre.offset")
cs1 <- slot(slot(SGDF, "grid"), "cellsize")[1]
M <- as.matrix(SGDF["asc"])
el1 <- as.asc(M[,ncol(M):1], xll=co1[1], yll=co1[2], cellsize=cs1,
  type="numeric")
all.equal(el, el1)

Roger

>
> If you only need the x coordinates of the non-missing cells, use the
> SpatialPixelsDataFrame class in addition. I guess that you could get there
> directly too.
>
> Roger
>
>>
>> Kind regards,
>>
>> Miltinho
>>
>>
>>
>> para armazenamento!
>>
>> 	[[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>

-- 
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: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list