[R-sig-Geo] R 3.0.0 and spatial classes

Roger Bivand Roger.Bivand at nhh.no
Thu Dec 20 09:56:36 CET 2012


On Thu, 20 Dec 2012, Francis Markham wrote:

> Is there interest in something like this code for generating SpatialLines?
> I use a variation of this fairly regularly:
>

If the geometry is simple, why not construct a WKT string and use 
available functions? Try:

library(rgeos)
set.seed(1)
coords.1 <- runif(10)
coords.2 <- runif(10)
crds <- paste(coords.1, coords.2)
crdstr <- paste(crds, collapse=",", sep="")
WKT <- paste("LINESTRING(", crdstr, ")", sep="")
g2 <- readWKT(WKT)
class(g2)

The same use of readWKT() of course extends to other WKT flavours.

Hope this helps,

Roger

>
> SimpleSpatialLines <- function(coords.1, coords.2, proj4string =
> CRS(as.character(NA))){
>  coords.1 <- coordinates(coords.1)
>  coords.2 <- coordinates(coords.2)
>  if (nrow(coords.1) != nrow(coords.2)) stop("coords.1 must be of same
> length as coords.2")
>  coords.both <- cbind(coords.1, coords.2)
>  coords.both <- cbind(coords.both, 1:nrow(coords.both))
>  lines <- apply(X=coords.both,
>                 MARGIN=1,
>                 FUN=function(x){
>                   Lines(list(Line(cbind(c(x[1], x[3]),
>                                         c(x[2], x[4])))),
>                         ID=x[5])})
>  SpatialLines(LinesList=lines, proj4string=proj4string)
> }
>
>
>
> Regards,
>
> Francis Markham
> Australian National University
> francis.markham at anu.edu.au
>
>
>
>
>
>
> On 17 December 2012 06:58, Barry Rowlingson <b.rowlingson at lancaster.ac.uk>wrote:
>
>> On Sun, Dec 16, 2012 at 6:53 PM, Chris English <sglish at hotmail.com> wrote:
>>>
>>> Edzer:
>>> On Tue, Jul 17, 2012 at 2:34 PM, Agustin Lobo ~ from Barry Rowlingson
>>> wrote:
>>> To convert to SpatialLines, get the coordinates and build in the
>>> usual convoluted manner:
>>>
>>> > s=data.frame(x=runif(10),y=1:10,z=rnorm(10))
>>> > coordinates(s)=~x+y
>>> > L = SpatialLines(list(Lines(list(Line(coordinates(s))),"X")))
>>> > plot(L)
>>> Convoluted is not the same as orphaned, certainly, but one gets the
>> sense that
>>> 'Line' owes its existence to matters of plotting rather than line as
>> line, independent of
>>> drawing it, and this may have some import upon line analysis and the
>> possibility of
>>> arriving at topology and dispensing with shared lines and the like.
>>
>>  "Line" is a non-spatial Line, made up from an ordered set of (x,y)
>> coordinate pairs. It cannot have a coordinate system assigned to it.
>>
>>  "Lines" is a list of Lines, making a non-spatial set of Line
>> segments. It too cannot have a coordinate system assigned.
>>
>>  "SpatialLines" is a spatial set of Lines, for when you have a number
>> of features each of which may be composed of several disconnected
>> segments. It can have a coordinate system assigned.
>>
>>  You are making a SpatialLines object where a single feature has a
>> single line segment. The slightly annoying
>> list(Lines(list(Line(...)))) dance is unavoidable because the lists
>> are necessary since the lists can have more than one element, but you
>> could easy create a 'SimpleSpatialLine' function that did all that. It
>> might even make sp for R 3.0.0! (SimpleSpatialPolygon might be handy
>> too...)
>>
>> Another possibility might be to write methods for SpatialLines that
>> takes a matrix and skips the complexity for simple cases...
>>
>> Barry
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
> 	[[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
>

-- 
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