[R-sig-Geo] converting line segment into polyline

Roger Bivand Roger.Bivand at nhh.no
Mon Dec 19 11:30:32 CET 2011


On Mon, 19 Dec 2011, Rolf Turner wrote:

>
> As it stands, your question does not make a lot of sense.  A psp object
> is a collection of (in general) unrelated line segments. A *polyline*
> in my understanding is a collection of *related* line segments ``joined
> end to end''.  I.e. a polyline is effectively a sequence of points (x_i,y_i)
> where (conceptually at least) the i-th point is joined to the (i+1)-st
> point by a line segment.
>
> Hence, in a sense, there is an enormous number of ways in which
> a psp object could be turned into a polyline --- arrange the segments
> in arbitrary order and then "join" the second point of the i-th segment
> to the first point of the (i+1)-st segment.  (The first point of each segment
> is ``already joined'' to the second point of that segment.)
>
> Few if any of these ``ways'' would be meaningful or useful.
>
> You will have to be much more clear and explicit in your question if
> you want any real help.

Yes, it would be useful to know where the psp objects come into your 
workflow, and if you do need them. A possible answer to your question is:

library(maptools)
xx <- readShapeSpatial(system.file("shapes/fylk-val.shp",
  package="maptools")[1], proj4string =
  CRS("+proj=utm +zone=33 +datum=WGS84"))
dim(xx)
xxPSP <- as.psp(xx[,"VALINJE_"])
str(xxPSP)
xx1 <- as(xxPSP, "SpatialLines")
# using the version of maptools on R-forge or wait for it to reach CRAN, 
# submitted today
library(rgeos)
xx2 <- gLineMerge(xx1)

There isn't a way yet to merge the lines respecting the marks in the psp 
object, but this might be possible to accommodate, xx2 here has one Lines 
object containing 93 Line objects.

Hope this helps,

Roger

>
> BTW what do you mean by ``***back*** into a polyline''?
> Please note the dictum:  "R lacks a mind_read() function."
> As do I.
>
>    cheers,
>
>        Rolf Turner
>
> P. S.  I do not know if there is formal "polyline" class (either S3 or S4)
> defined in any R package.  If there is, please enlighten me.
>
> The PBSmapping package has a class "PolySet"; a PolySet object is in
> effect a collection of polylines.
>
>        R. T.
>
> On 19/12/11 10:40, Miaoying Shi wrote:
>> Hello, Lists,
>>
>>   I have been searching a method to convert psp format line segment back
>> into polyline, as the psp data (p1) is like below, is there anyone can help
>> me?  Thanks a lot!
>> 
>> Erin
>> 
>> 
>> 
>>> str(p1)
>> List of 5
>>   $ ends      :'data.frame':     22511 obs. of  4 variables:
>>    ..$ x0: num [1:22511] 233994 233966 233949 233929 233885 ...
>>    ..$ y0: num [1:22511] 3355530 3355519 3355515 3355508 3355533 ...
>>    ..$ x1: num [1:22511] 233946 233918 233911 233873 233889 ...
>>    ..$ y1: num [1:22511] 3355510 3355511 3355512 3355513 3355514 ...
>>   $ window    :List of 4
>>    ..$ type  : chr "rectangle"
>>    ..$ xrange: Named num [1:2] 228691 236822
>>    .. ..- attr(*, "names")= chr [1:2] "min" "max"
>>    ..$ yrange: Named num [1:2] 3347926 3356435
>>    .. ..- attr(*, "names")= chr [1:2] "min" "max"
>>    ..$ units :List of 3
>>    .. ..$ singular  : chr "unit"
>>    .. ..$ plural    : chr "units"
>>    .. ..$ multiplier: num 1
>>    .. ..- attr(*, "class")= chr "units"
>>    ..- attr(*, "class")= chr "owin"
>>   $ n         : int 22511
>>   $ marks     : int [1:22511] 37 37 37 37 37 37 37 37 100 100 ...
>>   $ markformat: chr "vector"
>>   - attr(*, "class")= chr [1:2] "psp" "list"
>>
>> 	[[alternative HTML version deleted]]
>> 
>> _______________________________________________
>> R-sig-Geo mailing list
>> 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
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
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



More information about the R-sig-Geo mailing list