[R-sig-Geo] Raster stream network to lines
Andy Bunn
Andy.Bunn at wwu.edu
Thu Apr 19 19:01:47 CEST 2018
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 <mdsumner at gmail.com>
Date: Wednesday, April 18, 2018 at 8:30 PM
To: Andy Bunn <Andy.Bunn at wwu.edu>
Cc: Joseph Stachelek <joseph.stachelek at gmail.com>, R-sig-Geo <r-sig-geo at r-project.org>
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 <Andy.Bunn at wwu.edu<mailto:Andy.Bunn at wwu.edu>> 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" <joseph.stachelek at gmail.com<mailto:joseph.stachelek at gmail.com>> 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 <Andy.Bunn at wwu.edu<mailto:Andy.Bunn at wwu.edu>>
To: R-sig-Geo <r-sig-geo at r-project.org<mailto:r-sig-geo at r-project.org>>
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
R-sig-Geo at r-project.org<mailto:R-sig-Geo at r-project.org>
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org<mailto:R-sig-Geo at r-project.org>
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]]
More information about the R-sig-Geo
mailing list