>> flop<-Lines(Line(list(rbind(as.numeric(temp[1,7:8]), > as.numeric(temp[1,9:10])))), ID=row.names(temp)) >> SpatialLines(flop) > Error in as.list.default(X) : > no method for coercing this S4 class to a vector >
You need to put "flop" in a list of its own, as SpatialLines is a collection class that may contain more than one "Lines" (think of multi-branched line sequences that may not be connected, but exist as a single entity/feature - a "Line" is a matrix of a coordinates that are connected in sequence, a "Lines" is a collection of those, and a SpatialLines is a collection of Lines - each Lines will have a row in the attribute table). So SpatialLines(list(flop)) should work. See in my code how I start with a generic empty list, then populate it with many Lines: ranlines <- list() for (irow in 1:nrow(temp)) { ranlines[[irow]] <- Lines(Line > Why is this? Also to export my SLDF as a shapefile I would normally use the > following >> writeSpatialShape(ranslines, "C:/GIS/ranslines") > > ...but this is different from what Michael has proposed. >> writeOGR(sldf, ".", "ranlines", "ESRI Shapefile") > > Is one method better than the other? > I use the rgdal package as it was my first introduction to read/write for GIS data in R, and it is very general. It can be hard to install as there are 3rd-party dependencies even for the base package (very easy on Windows with a binary/zip package), though when you compile from source you have greater control and the potental for adding new drivers (formats) over and above the base ones. There may be important differences between the way shapefiles are written but it's personal preference and ease of use/installation as far as I can see. cheers, Mike. On Wed, Feb 17, 2010 at 10:28 AM, Tyler Dean Rudolph <tylerdeanrudo...@gmail.com> wrote: > Much thanks to all for these useful and informative responses. > > I had come across the book in my searches but I had not stumbled onto its > homepage with the additional resources Roger has provided, which are > indispensable. Perhaps to circumvent the potential conflict of interest you > could add a URL to your personal signature referring interested parties to a > website containing information on your related books and projects? This to > me seems entirely reasonable as it does not solicit interest, it merely > provides access to that information. > > I realize that have a different way of accessing the package vignettes (or > so I thought: http://cran.r-project.org/web/packages/sp/sp.pdf) so I haven't > necessarily been working with all the available information either. > > I will keep Robert's handy conversion code nearby, which promises to make > creating sp objects quick and easy. > > Thank you to Michael for his solution and encouragement. I gathered that > the "ID" argument was critical to successfully passing the SpatialLines > command. I considered doing a "for" loop, but when the following failed to > work with one mere record I decided there was no solution there to be found. > >> flop<-Lines(Line(list(rbind(as.numeric(temp[1,7:8]), > as.numeric(temp[1,9:10])))), ID=row.names(temp)) >> SpatialLines(flop) > Error in as.list.default(X) : > no method for coercing this S4 class to a vector > > Why is this? Also to export my SLDF as a shapefile I would normally use the > following >> writeSpatialShape(ranslines, "C:/GIS/ranslines") > > ...but this is different from what Michael has proposed. >> writeOGR(sldf, ".", "ranlines", "ESRI Shapefile") > > Is one method better than the other? > > Tyler > > > On Tue, Feb 16, 2010 at 4:06 AM, Roger Bivand <roger.biv...@nhh.no> wrote: > >> On Mon, 15 Feb 2010, Tyler Dean Rudolph 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. >>> >> >> I have discussed with Achim Zeilis, who faces the same problem, the ethical >> appropriateness of mentioning our book on the Spatial Task View, and we've >> found until now that we shouldn't mention our own books. If opinion is that >> we should, and because they are in a number of university libraries, I can >> do that, and encourage Achim to do the same for Econometrics. >> >> If you visit the book website (http://www.asdar-book.org), you will find >> links to code showing how to do this (chapter 2). In addition, under >> "Additional materials", you find links to course materials, especially to a >> course given at Imperial College in 2007, in which a good deal of these >> questions are covered (see "Representing Spatial Data"). >> >> Because the classes were designed as interface containers, most users do >> not need to know how to construct them from scratch, typically reading in >> data or coercing from other classes of objects. While the help pages could >> certainly be improved, we do provide a vignette that ships with the package, >> and where section 6 covers Line objects etc. See: >> >> http://cran.r-project.org/web/packages/sp/vignettes/sp.pdf >> >> or: >> >> vignette("sp", package="sp") >> >> in R. >> >> I'm sure that Mike's answer resolves your immediate problem - this is more >> of an answer to your general question about where to look for information >> about sp classes and how to make and use them. >> >> Hope this helps, >> >> Roger >> >> 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@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.biv...@nhh.no >> >> > > [[alternative HTML version deleted]] > > _______________________________________________ > R-sig-Geo mailing list > R-sig-Geo@stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > _______________________________________________ R-sig-Geo mailing list R-sig-Geo@stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo