Distribute points along lines
On Fri, 3 Jul 2009, Agustin Lobo wrote:
Thanks, I'm actually doing: plot(fl1) points(spsample(fl1[4,],5,offset=0.001,type="regular")) but why offset=0 does not work?
points(spsample(fl1[4,],5,offset=0.0,type="regular"))
Error in validObject(.Object) : invalid class "SpatialPoints" object: bbox should never contain NA values
With the meuse.riv data set, this cannot be reproduced. Please provide a reproducible example. The problem is occurring in the output from sp:::.bboxCoords(), with an NA bounding box value, but that means that sample.Line() is probably getting a Line with no length:
sp:::sample.Line(Line(matrix(c(0,0,0,0), ncol=2)), 5, offset=0.001,
+ type="regular") Error in validObject(.Object) : invalid class "SpatialPoints" object: bbox should never contain NA values which means that no sample points are found. This suggests that the user error ought to be caught earlier, and maybe that Line objects with no length and all identical coordinates should be made invalid (this requirement was removed two years ago), but the onus is on the user to provide sensible data.
Also, which are the units for offset?
For samples from lines, it is used as: # default is: ..., offset = runif(1), ... ((1:n) - offset)/n * maxl
n <- 5 maxl <- 10 offset <- 0 ((1:n) - offset)/n * maxl
[1] 2 4 6 8 10 so a proportion in [0,1], but it seems to work backwards at present, with an offset of 0 anchoring to the final, not the first, point.
I'm actually trying to set real field sampling sites, so this matters. The best would be being able to set the first point in the line and then the nb of points for random sites (as it is currently done) and the length interval for regular sampling. By now I can adjust the nb. of points for the interval I want if the length of the line is known, but how can I calculate the length of the lines? calcLength requires another type of spatial object.
calcLength() is in PBSmapping. Adding a length slot to Line objects is an option, or rather providing length methods which would be less invasive. I'll take a look. Roger PS. The answers to these relatively detailed questions are in the source, which can easily be browsed on: http://r-spatial.cvs.sourceforge.net/viewvc/r-spatial/sp/ or from the source bundle. Suggestions with patches are appreciated.
Agus Roger Bivand wrote:
On Fri, 3 Jul 2009, Kamran Safi wrote:
Hi, I think you can use spsample() together with type=random and n=your sample size.
Exactly: library(sp) data(meuse.riv) SL <- SpatialLines(list(Lines(list(Line(meuse.riv[1:80,])), ID="1"))) plot(SL) pts <- spsample(SL, n=50, type="random") plot(pts, add=TRUE) Here we only have one Line object in the single Lines object, but the principle is the same. Roger
Kami Agustin Lobo wrote:
Hi! Is there any way to distribute a set of random or regularly-spaced points along a line imported from a shape file into a SpatialLinesDataFrame? I've been searching, but could not find it. Should I use coordinates() to get the vertex, then calculate the a&b parameters for each line and distribute the points? Or is it there something already done? Thanks, Agus Dr. Agustin Lobo Institut de Ciencies de la Terra "Jaume Almera" (CSIC) LLuis Sole Sabaris s/n 08028 Barcelona Spain Tel. 34 934095410 Fax. 34 934110012 email: Agustin.Lobo at ija.csic.es http://www.ija.csic.es/gt/obster
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no