Skip to content

Distribute points along lines

5 messages · Agustin Lobo, Kamran Safi, Roger Bivand

#
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
#
Hi,

I think you can use spsample() together with type=random and n=your 
sample size.

Kami
Agustin Lobo wrote:
#
On Fri, 3 Jul 2009, Kamran Safi wrote:

            
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

  
    
#
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

Also, which are the units for offset? 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.

Agus
Roger Bivand wrote:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: alobolistas.vcf
Type: text/x-vcard
Size: 251 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20090703/48fbe622/attachment.vcf>
#
On Fri, 3 Jul 2009, Agustin Lobo wrote:

            
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:
+ 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.
For samples from lines, it is used as:

# default is: ..., offset = runif(1), ...
((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.
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.