A lot of options are possible!

Thanks everybody for all these answers.

2011/9/18 Robert J. Hijmans <r.hijm...@gmail.com>

> Nice problem:
>
> library(raster)
> cds1 <- rbind(c(-100,91), c(-140,15), c(-10, 40), c(-140,-91))
> lines <- SpatialLines(list(Lines(list(Line(cds1)), "1")))
> r <- raster(ncols=50, nrows=45, xmx=10)
> r <- rasterize(lines, r)
>
> rc <- rowColFromCell(r, cellFromXY(r, rasterToPoints(r)))
> west <- tapply(rc[,2], rc[,1], min)-1
> east <- tapply(rc[,2], rc[,1], max)+1
>
> # option 1 (an improved? version of your approach)
> # note artifacts if line goes up and down
> nc <- ncol(r)
> for (i in 1:nrow(r)) {
> r[i, 1:west[i]] <- 2
>  r[i, east[i]:nc] <- 3
> }
> plot(r)
>
> # option 2 ( a variation on 1)
> ww <- cbind(as.integer(names(west)), west)
> ee <- cbind(as.integer(names(east)), east)
> wrc <- apply(ww, 1, function(x) cbind(x[1], 1:x[2]) )
> wrc <- do.call(rbind, wrc)
> erc <- apply(ee, 1, function(x) cbind(x[1], 1:x[2]) )
> erc <- do.call(rbind, erc)
>
> r[wrc] <- 2
> r[erc] <- 3
>
>
> # option 3 (Following Barry)
> library(rgeos)
> pol <- as(extent(r), 'SpatialPolygons')
> m <- gUnion(lines, as(pol, 'SpatialLines'))
> p <- gPolygonize(m)
> x <- rasterize(p, r)
> plot(x)
>
>
> # option 4 (After Jacbob)
> r[] <- 2
> r <- rasterize(lines, r, update=T)
> r[r==1] <- NA
> z <- clump(r)
> plot(z)
>
>
>
>
> On Fri, Sep 16, 2011 at 1:38 PM, Jacob van Etten 
> <jacobvanet...@yahoo.com>wrote:
>
>> Try the clump() function in raster.
>>
>>
>> 1. Convert the railroad in a raster, using rasterize().
>>
>> 2. Set the line to NA or 0.
>>
>> 3. Use clump() with directions=4.
>> Jacob
>>
>>
>> ________________________________
>> From: Barry Rowlingson <b.rowling...@lancaster.ac.uk>
>> To: Mathieu Rajerison <mathieu.rajeri...@gmail.com>
>> Cc: r-sig-...@stat.math.ethz.ch
>> Sent: Friday, 16 September 2011, 17:51
>> Subject: Re: [R-sig-Geo] [raster] a railroad, a raster with a different
>> value on each side of it
>>
>> If your railroad is just a single line feature running approx N-S then:
>>
>> Create a box polygon for your study area, make sure the railroad just
>> crosses it at N and S edges
>>
>> Use rgeos functions overlaying the RR line with the box polygon to
>> create the E and W polygons
>>
>> Job done?
>>
>> On Fri, Sep 16, 2011 at 1:11 PM, Mathieu Rajerison
>> <mathieu.rajeri...@gmail.com> wrote:
>> > Hi List,
>> >
>> >
>> > I've got a railroad shapefile and a corine land cover raster.
>> >
>> > I'd like to perform a land cover comparative analysis between the west
>> side
>> > of the railroad and the right side.
>> >
>> > To do that, I tried first to have a raster with values=1 when on the
>> east
>> > and 0 when on the west.
>> > But my calculation is very slow.
>> >
>> > Maybe anyone has a better idea on how to accomplish that?
>> >
>> > Here is the code, for the moment:
>> >
>> > #* *I create a raster of the same extent and resolution as the corine
>> land
>> > cover raster
>> >> track.r <- raster(extent(clc.ov))
>> >> res(track.r) <- res(clc.ov)
>> >
>> > # I rasterize it
>> >> track.r <- rasterize(track, track.r)
>> >
>> > # I extract the row-col pairs of cells with value 1
>> >> rowCol <- rowColFromCell(track.r, which(track.r[]==1))
>> >
>> > # I give the value 1 to each cell which col number is > to that of the
>> > rasterized line cell and of same row
>> >> for (i in seq(1,length(rowCol))) {
>> >  track.r[rowCol[i,1], rowCol[i,2]:ncols] <- 1
>> > }
>> > # too slow.....
>> >
>> >
>> > Any help would be greatly appreciated,
>> >
>> > Mathieu
>> >
>> >        [[alternative HTML version deleted]]
>> >
>> > _______________________________________________
>> > R-sig-Geo mailing list
>> > R-sig-Geo@r-project.org
>> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> >
>>
>>
>>
>> --
>> blog: http://geospaced.blogspot.com/
>> web: http://www.maths.lancs.ac.uk/~rowlings
>> web: http://www.rowlingson.com/
>> twitter: http://twitter.com/geospacedman
>> pics: http://www.flickr.com/photos/spacedman
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo@r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>         [[alternative HTML version deleted]]
>>
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo@r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>>
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Reply via email to