[R-sig-Geo] Drawing lines in raster cells

Robert J. Hijmans r.hijmans at gmail.com
Mon May 13 18:42:32 CEST 2013


Lionel, perhaps this is what you are after. Robert

library(raster)
library(rgeos)

r <- raster(ncols=10,nrows=10)
set.seed(0)
r[] <- sample(0:1,100,replace=TRUE)

p <- rasterToPolygons(r, fun=function(x){x==1}, dissolve=TRUE)

plot(r)
plot(p, add=T, density=4)



On Sun, May 12, 2013 at 8:49 AM, Lionel <s6lihert at uni-bonn.de> wrote:
> Hi Oscar,
>
> Thanks for your help, what I would like to get is something similar to this:
> http://www.google.fr/imgres?q=species+range+shift&start=280&um=1&sa=N&hl=fr&biw=1366&bih=647&tbm=isch&tbnid=MdhZVMKVLMSH3M:&imgrefurl=http://www.scielo.sa.cr/scielo.php%3Fscript%3Dsci_arttext%26pid%3DS0034-77442008000200028&docid=9u3QDKVd0hdQqM&imgurl=http://www.scielo.sa.cr/img/revistas/rbt/v56n2/art28i2.jpg&w=363&h=432&ei=cbiPUe_4BqO80QX-hoCIBw&zoom=1&iact=hc&vpx=588&vpy=170&dur=342&hovh=180&hovw=152&tx=83&ty=124&page=10&tbnh=159&tbnw=134&ndsp=30&ved=1t:429,r:84,s:200,i:256
>
> Where each raster cells with a certain values (say 1) get crossed by a
> diagonal line.
>
> Your code is achieving something different, concerning the object created
> the best would be a Spatial* object, however I think I could be able to find
> a way around using ggmap.
>
> Thanks again,
> Lionel
>
>
> On 12/05/2013 16:23, Oscar Perpiñán Lamigueiro wrote:
>>
>> Hi Lionel,
>>
>> With this code you will draw lines connecting raster cells with a
>> certain value. I am not sure if you also need to build a SpatialLines
>> object with them (which you won't get with my code):
>>
>> library(raster)
>> library(latticeExtra) ## to use +.trellis and layer()
>>
>> r1<-raster(ncols=10,nrows=10)
>> r1[]<-sample(0:1,100,replace=TRUE)
>>
>> ## extract coordinates of those cells with a condition
>> myCells <- Which(r1==1, cells=TRUE)
>> xy <- xyFromCell(r1, myCells)
>>
>> ## border and border.lwd are only needed to highlight the cells boundaries
>> spplot(r1, at=c(0, 0.5, 1), border='black', border.lwd=0.3) +
>>      layer(llines(xy, type='b', col='black'))
>>
>>
>> Best,
>>
>> Oscar.
>>
>> Lionel <s6lihert at uni-bonn.de> writes:
>>
>>> Dear List,
>>>
>>> I would like to draw lines in raster cells with a certain values, I can
>>> work around this with a for loop but as always this solution only works
>>> for small datasets (which is not my case), I would therefore like to
>>> have your opinion on this;
>>>
>>> #the code
>>> library(maptools) #to use spRbind
>>> out<-NULL
>>> r1<-raster(ncols=10,nrows=10)
>>> r1[]<-sample(0:1,100,replace=TRUE)
>>> for(i in 1:ncell(r1)){
>>>     if(r1[i]==1){
>>>
>>> lines<-SpatialLines(list(Lines(list(Line(matrix(c(xyFromCell(r1,i)+(res(r1)/2),xyFromCell(r1,i)-(res(r1)/2)),ncol=2,byrow=TRUE))),ID=as.character(i))))
>>>       if(is.null(out)){
>>>         out<-lines }
>>>          else {out<-spRbind(out,lines)}
>>>       }
>>>     print(i)
>>> }
>>>
>>> Thank you in advance,
>>
>>
>>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list