Raster stream network to lines

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Raster stream network to lines

Andy Bunn-3
I have a raster of streams with 0 being non-stream and 1 being stream. Is there a way to convert this into a SpatialLinesDataFrame? rasterToContour() almost does what I want but fails to draw the line on queens rules, etc. Example here:

library(raster)
foo <- matrix(0,ncol=9,nrow=9)
foo[1:4,3] <- 1
foo[5,4] <- 1
foo[6:9,5] <- 1
foo <- raster(foo)
plot(foo)
bar <- rasterToContour(foo,nlevels=1)
plot(bar)

How can I get a continuous line in this example? Many thanks, Andy

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Raster stream network to lines

Joseph Stachelek
Hi Andy,

I have posted some code showing one way of doing this
with the `sf` package:

https://gist.github.com/jsta/d8d8e8d79877c720b6842ae8efa2
b9b8

If you are doing this a lot I recommend picking up GRASS:

https://grasswiki.osgeo.org/wiki/R.stream.*_modules
https://grass.osgeo.org/grass74/manuals/r.to.vect.html

--Joe

-----Original Message-----
From: Andy Bunn <[hidden email]>
To: R-sig-Geo <[hidden email]>
Subject: [R-sig-Geo] Raster stream network to lines
Date: Wed, 18 Apr 2018 19:03:21 +0000

I have a raster of streams with 0 being non-stream and 1
being stream. Is there a way to convert this into a
SpatialLinesDataFrame? rasterToContour() almost does what
I want but fails to draw the line on queens rules, etc.
Example here:

library(raster)
foo <- matrix(0,ncol=9,nrow=9)
foo[1:4,3] <- 1
foo[5,4] <- 1
foo[6:9,5] <- 1
foo <- raster(foo)
plot(foo)
bar <- rasterToContour(foo,nlevels=1)
plot(bar)

How can I get a continuous line in this example? Many
thanks, Andy

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Raster stream network to lines

Andy Bunn-3
Brilliant! And a heck of a workaround. But what if foo is a projected raster? I can add a projection system but all the coordinate info is lost in the shuffle.

On 4/18/18, 1:17 PM, "Joseph Stachelek" <[hidden email]> wrote:

    Hi Andy,
   
    I have posted some code showing one way of doing this
    with the `sf` package:
   
    https://gist.github.com/jsta/d8d8e8d79877c720b6842ae8efa2
    b9b8
   
    If you are doing this a lot I recommend picking up GRASS:
   
    https://grasswiki.osgeo.org/wiki/R.stream.*_modules
    https://grass.osgeo.org/grass74/manuals/r.to.vect.html
   
    --Joe
   
    -----Original Message-----
    From: Andy Bunn <[hidden email]>
    To: R-sig-Geo <[hidden email]>
    Subject: [R-sig-Geo] Raster stream network to lines
    Date: Wed, 18 Apr 2018 19:03:21 +0000
   
    I have a raster of streams with 0 being non-stream and 1
    being stream. Is there a way to convert this into a
    SpatialLinesDataFrame? rasterToContour() almost does what
    I want but fails to draw the line on queens rules, etc.
    Example here:
   
    library(raster)
    foo <- matrix(0,ncol=9,nrow=9)
    foo[1:4,3] <- 1
    foo[5,4] <- 1
    foo[6:9,5] <- 1
    foo <- raster(foo)
    plot(foo)
    bar <- rasterToContour(foo,nlevels=1)
    plot(bar)
   
    How can I get a continuous line in this example? Many
    thanks, Andy
   
    [[alternative HTML version deleted]]
   
    _______________________________________________
    R-sig-Geo mailing list
    [hidden email]
    https://stat.ethz.ch/mailman/listinfo/r-sig-geo
   

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Raster stream network to lines

Michael Sumner-2
Not much to add but if you didn't notice there is a degenerate line in that
isolated pixel, so if you were looking at sets of connected pixels in turn
you could possibly re-process the output of contourLines to join things up:

cl <- contourLines(list(x = xFromCol(foo), y  = rev(yFromRow(foo)), z =
as.matrix(t(flip(foo, "y")))), levels = 1)

## the third one is a nearly-degenerate line in the centre of the isolated
cell
cl[[3]]

Cheers, Mike.

On Thu, 19 Apr 2018 at 08:22 Andy Bunn <[hidden email]> wrote:

> Brilliant! And a heck of a workaround. But what if foo is a projected
> raster? I can add a projection system but all the coordinate info is lost
> in the shuffle.
>
> On 4/18/18, 1:17 PM, "Joseph Stachelek" <[hidden email]>
> wrote:
>
>     Hi Andy,
>
>     I have posted some code showing one way of doing this
>     with the `sf` package:
>
>     https://gist.github.com/jsta/d8d8e8d79877c720b6842ae8efa2
>     b9b8
>
>     If you are doing this a lot I recommend picking up GRASS:
>
>     https://grasswiki.osgeo.org/wiki/R.stream.*_modules
>     https://grass.osgeo.org/grass74/manuals/r.to.vect.html
>
>     --Joe
>
>     -----Original Message-----
>     From: Andy Bunn <[hidden email]>
>     To: R-sig-Geo <[hidden email]>
>     Subject: [R-sig-Geo] Raster stream network to lines
>     Date: Wed, 18 Apr 2018 19:03:21 +0000
>
>     I have a raster of streams with 0 being non-stream and 1
>     being stream. Is there a way to convert this into a
>     SpatialLinesDataFrame? rasterToContour() almost does what
>     I want but fails to draw the line on queens rules, etc.
>     Example here:
>
>     library(raster)
>     foo <- matrix(0,ncol=9,nrow=9)
>     foo[1:4,3] <- 1
>     foo[5,4] <- 1
>     foo[6:9,5] <- 1
>     foo <- raster(foo)
>     plot(foo)
>     bar <- rasterToContour(foo,nlevels=1)
>     plot(bar)
>
>     How can I get a continuous line in this example? Many
>     thanks, Andy
>
>         [[alternative HTML version deleted]]
>
>     _______________________________________________
>     R-sig-Geo mailing list
>     [hidden email]
>     https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Raster stream network to lines

Andy Bunn-3
While Joe’s solution worked great (except the projection info) I know when I’m beat. I used the channel network tool in QGIS.

From: Michael Sumner <[hidden email]>
Date: Wednesday, April 18, 2018 at 8:30 PM
To: Andy Bunn <[hidden email]>
Cc: Joseph Stachelek <[hidden email]>, R-sig-Geo <[hidden email]>
Subject: Re: [R-sig-Geo] Raster stream network to lines

Not much to add but if you didn't notice there is a degenerate line in that isolated pixel, so if you were looking at sets of connected pixels in turn you could possibly re-process the output of contourLines to join things up:

cl <- contourLines(list(x = xFromCol(foo), y  = rev(yFromRow(foo)), z = as.matrix(t(flip(foo, "y")))), levels = 1)

## the third one is a nearly-degenerate line in the centre of the isolated cell
cl[[3]]

Cheers, Mike.

On Thu, 19 Apr 2018 at 08:22 Andy Bunn <[hidden email]<mailto:[hidden email]>> wrote:
Brilliant! And a heck of a workaround. But what if foo is a projected raster? I can add a projection system but all the coordinate info is lost in the shuffle.

On 4/18/18, 1:17 PM, "Joseph Stachelek" <[hidden email]<mailto:[hidden email]>> wrote:

    Hi Andy,

    I have posted some code showing one way of doing this
    with the `sf` package:

    https://gist.github.com/jsta/d8d8e8d79877c720b6842ae8efa2
    b9b8

    If you are doing this a lot I recommend picking up GRASS:

    https://grasswiki.osgeo.org/wiki/R.stream.*_modules
    https://grass.osgeo.org/grass74/manuals/r.to.vect.html

    --Joe

    -----Original Message-----
    From: Andy Bunn <[hidden email]<mailto:[hidden email]>>
    To: R-sig-Geo <[hidden email]<mailto:[hidden email]>>
    Subject: [R-sig-Geo] Raster stream network to lines
    Date: Wed, 18 Apr 2018 19:03:21 +0000

    I have a raster of streams with 0 being non-stream and 1
    being stream. Is there a way to convert this into a
    SpatialLinesDataFrame? rasterToContour() almost does what
    I want but fails to draw the line on queens rules, etc.
    Example here:

    library(raster)
    foo <- matrix(0,ncol=9,nrow=9)
    foo[1:4,3] <- 1
    foo[5,4] <- 1
    foo[6:9,5] <- 1
    foo <- raster(foo)
    plot(foo)
    bar <- rasterToContour(foo,nlevels=1)
    plot(bar)

    How can I get a continuous line in this example? Many
    thanks, Andy

        [[alternative HTML version deleted]]

    _______________________________________________
    R-sig-Geo mailing list
    [hidden email]<mailto:[hidden email]>
    https://stat.ethz.ch/mailman/listinfo/r-sig-geo


_______________________________________________
R-sig-Geo mailing list
[hidden email]<mailto:[hidden email]>
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
--
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo