[R-sig-Geo] Create SpatialLinesDataFrame

Michael Sumner mdsumner at gmail.com
Tue Feb 16 02:49:18 CET 2010


Using apply makes it difficult to set an appropriate ID for each Line,
so I've done it in a loop:

ranlines <- list()

for (irow in 1:nrow(temp)) {
	ranlines[[irow]] <- Lines(Line(rbind(as.numeric(temp[irow, c("x",
"y")]), as.numeric(temp[irow, c("X2", "Y2")]))),
		ID = as.character(irow))
}

sldf <- SpatialLinesDataFrame(SpatialLines(ranlines), temp, match.ID = FALSE)


I've also used named arguments as your indexes don't match the table
as you presented it, I presume that start is "x/y" and the end is
"X2/Y2"?

Then you can use this to write a shapefile to the current directory:

library(rgdal)
writeOGR(sldf, ".", "ranlines", "ESRI Shapefile")

I agree there needs to be more examples, the fact of multi-part lines,
polygons and points can be hard to grasp and the need for them makes
the structure of sp objects difficult to understand. It's not hard
once you do though. I posted this as I thought it might be of use:

https://stat.ethz.ch/pipermail/r-sig-geo/2010-February/007619.html

It's not a great example, but it shows how I explore these objects to
get an understanding. In case it's of use I also put this together
this morning, to work with the results of package "alphahull":

This example creates a (possibly multi-branched) line object and
writes to shapefile.

#R

library(alphahull)

library(rgdal)

# Uniform sample of size n=300 in the annulus B(c,0.5)\B(c,0.25),

# with c=(0.5,0.5).

n <- 300

theta<-runif(n,0,2*pi)

r<-sqrt(runif(n,0.25^2,0.5^2))

x<-cbind(0.5+r*cos(theta),0.5+r*sin(theta))



x <- do.call('cbind', locator())

# Value of alpha

alpha <- 0.1

# alpha-shape

x.shape <- ashape(x, alpha = alpha)

## construct a GIS-line object

l <- list()

for (i in 1:nrow(x.shape$edges)) {

 l[[i]] <-  Line(rbind(x.shape$edges[i, 3:4], x.shape$edges[i, 5:6]))

}

## one line object, one ID value

l <- list(Lines(l, as.character("1")))

## equivalent to a drawing with a single line object:

sldf <- SpatialLinesDataFrame(SpatialLines(l), data.frame(name =
"ashape"), match.ID = FALSE)



## write to desired OGR driver (see ogrDrivers() for available ones)

## this creates lines.shp/shx/dbf in current directory

writeOGR(sldf, ".", "lines", "ESRI Shapefile")


Cheers, Mike.





On Tue, Feb 16, 2010 at 11:42 AM, Tyler Dean Rudolph
<tylerdeanrudolph at gmail.com> wrote:
> I have searched extensively for some good examples or documentation on how
> to create a SpatialLinesDataFrame and for the life of me have found zilch.
> This does not mean there is no such thing available, it just means that
> wherever it is it is not readily accessible through the circuitous pathways
> of R help documentation or via a simple web search.  As someone who
> struggles daily to achieve highly technical things with scarcely the savvy
> required, I wish there were more (or any?) examples in the documentation for
> most of the spatial packages available.  (I also wish that managing and
> manipulating Spatial objects was about 65% less complicated, but for the
> moment let's start with this)...
>
> I have a data.frame (temp) containing endpoints of animal trajectories with
> which I would like to create a SpatialLinesDataFrame for export into a GIS.
>
>> temp
>       id                 day       x        y        X2       Y2
> 1 2002007 2005-04-04 12:01:01 -300358 748992.6 -300450.8 748764.8
> 2 2002007 2005-04-04 12:01:01 -300358 748992.6 -301694.2 751728.4
> 3 2002007 2005-04-04 12:01:01 -300358 748992.6 -299983.1 749528.8
> 4 2002007 2005-04-04 12:01:01 -300358 748992.6 -300569.4 749640.7
> 5 2002007 2005-04-04 12:01:01 -300358 748992.6 -300439.0 748308.8
>
> I seem reasonably able to create a Lines object (though I can't get the ID
> argument to work for me where there are multiple records involved)....
>
>> ranlines<-apply(temp, 1,  function(x)
> Lines(Line(list(rbind(as.numeric(x[7:8]), as.numeric(x[9:10]))))))
>
>> str(ranlines[[1]])
> Formal class 'Lines' [package "sp"] with 2 slots
>  ..@ Lines:List of 1
>  .. ..$ :Formal class 'Line' [package "sp"] with 1 slots
>  .. .. .. ..@ coords: num [1:2, 1:2] -300358 -300451 748993 748765
>  .. .. .. .. ..- attr(*, "dimnames")=List of 2
>  .. .. .. .. .. ..$ : NULL
>  .. .. .. .. .. ..$ : chr [1:2] "X1" "X2"
>  ..@ ID   : chr NA
>
> Am I at least on the right track?   Unfortunately now I'm clueless as to
> what to do next because apparently Lines cannot be coerced to SpatialLines,
> and while I am normally able to figure my way out of things there is just
> nothing intuitive about this as far as I can tell.  I have tried to create
> SLDFs numerous times in the past and I have always been discouraged from
> continuing.  I can create a psp object in Spatstat but I am equally unable
> to convert that into a SLDF shape.  Terribly sorry about the rant but it's
> difficulties like these that prevent a lot of people more clueless than me
> from ever getting anywhere with R and more specifically the spatial
> stuff....
>
> Tyler
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list