[R-sig-Geo] Find nearest downstream value of a river network
Jon Olav Skoien
jon.skoien at jrc.ec.europa.eu
Tue May 24 14:54:40 CEST 2011
Hi Edzer,
Thanks for the suggestion! It is a much simpler way of doing it, but
unfortunately gDistance seems rather slow for larger data sets. For a
data set with 1200 segments (the full data set has around 8000), it took
around 4 minutes on my computer, which is considerably more than the
method I already had. But gDistance might be what I need for something
else...
Cheers,
Jon
On 19-May-11 17:17, Edzer Pebesma wrote:
> Hi Jon (thanks for reminding off-list!), I haven't looked into finding
> the line with an ID, but in general finding the nearest SpatialLines
> index (which of course might be ambiguous) might be done by:
>
> library(rgeos)
> d = gDistance(nres, nres, byid=TRUE)
> diag(d)=1e9
> apply(d,2,function(x)which(x==min(x))[1])
>
> Does that get you any further? Or are there more clever solutions?
> --
> Edzer
>
> On 04/13/2011 09:59 AM, Jon Olav Skoien wrote:
>> Hi,
>>
>> I have a SpatialLinesDataFrame with predictions on different locations
>> of a river network that I would like to plot. However, I have many more
>> line segments in the network than I have predictions, so most of the
>> data.frame has NA-values. Does anyone know a simple way of finding the
>> nearest downstream value for a line segment with an NA-value, so that I
>> can get a continuous river network with predictions?
>>
>> Below is a simple example of what I want to do, based on a shapefile
>> ("nres") that can be downloaded from:
>> http://intamap.geo.uu.nl/~jon/sarp/tempaacf/
>>
>> nres = readOGR(".","nres")
>> nin = nres
>> nin$pred[!(nin$OBJECTID %in% c(1015, 1020, 1366, 1369, 4981))] = NA
>>
>> "nres" is the SpatialLinesDataFrame that I would like to have as result
>> after associating all NA-values to the nearest downstream value. "nin"
>> is a simplified version of the SpatialLinesDataFrame that I have after
>> predicting, and that I would like to use for creating "nres". "pred" is
>> the prediction column in these SLDFs. The following plot shows the
>> results (with colors) and thick black lines for the segments where I
>> have predictions.
>>
>> spplot(nres, "pred", col.regions = bpy.colors(),
>> panel = function(x,y, ...) {
>> panel.polygonsplot(x,y, ...)
>> sp.lines(nin[!is.na(nin$pred),], col = "black", lwd = 2)
>> })
>>
>> So far I have got the result in this plot from an iterative procedure
>> using the FROMJCT and TOJCT (from and to junction) IDs of the data.frame:
>>
>> ichange = 1
>> while (ichange> 0) {
>> ichange = 0
>> for (i in 1:dim(nin)[1]) {
>> if (!is.na(nin$pred[i])) {
>> tt = which(nin$TOJCT == nin$FROMJCT[i])
>> if (length(tt)> 0&& is.na(nin$pred[tt])) {
>> nin$pred[tt] = nin$pred[i]
>> ichange = ichange + 1
>> }
>> }
>> }
>> }
>>
>> But I think it should be either possible to simplify this (quite slow
>> for a large river network), or preferably take advantage of topology of
>> SpatialLines objects? Any clues?
>>
>> Thanks,
>> Jon
>>
>> _______________________________________________
>> 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