Convert SpatialPointsDataFrame to SpatialLinesDataFrame to R

I am working with a HURDAT dataset to create hurricane tracks. I am currently creating a SpatialPointsDataFrame object in R that looks something like this in 2004.

> str(cluster.2004.sdf) Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots ..@ data :'data.frame': 2693 obs. of 4 variables: .. ..$ Sid : int [1:2693] 1331 1331 1331 1331 1331 1331 1331 1331 1331 1331 ... .. ..$ clusterid: num [1:2693] 2 2 2 2 2 2 2 2 2 2 ... .. ..$ name : Factor w/ 269 levels "","ABBY ",..: 6 6 6 6 6 6 6 6 6 6 ... .. ..$ WmaxS : num [1:2693] 78.9 82.8 80.9 70.9 76.9 ... ..@ coords.nrs : num(0) ..@ coords : num [1:2693, 1:2] 754377 612852 684956 991386 819565 ... .. ..- attr(*, "dimnames")=List of 2 .. .. ..$ : NULL .. .. ..$ : chr [1:2] "lon" "lat" ..@ bbox : num [1:2, 1:2] -3195788 1362537 4495870 9082812 .. ..- attr(*, "dimnames")=List of 2 .. .. ..$ : chr [1:2] "lon" "lat" .. .. ..$ : chr [1:2] "min" "max" ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots .. .. ..@ projargs: chr "+proj=lcc +lat_1=60 +lat_2=30 +lon_0=-60 +ellps=WGS84" > summary(cluster.2004.sdf) Object of class SpatialPointsDataFrame Coordinates: min max lon -3195788 4495870 lat 1362537 9082812 Is projected: TRUE proj4string : [+proj=lcc +lat_1=60 +lat_2=30 +lon_0=-60 +ellps=WGS84] Number of points: 2693 Data attributes: Sid clusterid name WmaxS Min. :1331 Min. :1.000 IVAN :517 Min. : 14.83 1st Qu.:1334 1st Qu.:2.000 FRANCES :403 1st Qu.: 31.35 Median :1337 Median :3.000 JEANNE :379 Median : 50.04 Mean :1337 Mean :2.898 KARL :283 Mean : 61.66 3rd Qu.:1339 3rd Qu.:4.000 DANIELLE :271 3rd Qu.: 90.40 Max. :1341 Max. :4.000 BONNIE :253 Max. :142.52 (Other) :587 

Each storm has a unique storm reference designated as "Sid." I would like to group SpatialPointsDataFrame with "Sid" and convert all points to a string.

I had a way out of the plyr package, but to be honest, I have no idea what I'm doing. I know I can do this by looping every row in the data frame and adding coordinates to the list, and then converting this list using the Lines function from the sp package.

However, I would prefer a more R-way conversion. thanks Richard

+6
source share
2 answers

The problem with mdsumner's solution is that the resulting data.frame should have one row for each row, but there is one row for each point in its code. Corrected Code:

 ## example data d <- data.frame(x=runif(7), y=runif(7), id = c(rep("a", 3), rep("b", 4))) library(sp) coordinates(d) <- ~x+y ## list of Lines per id, each with one Line in a list x <- lapply(split(d, d$id), function(x) Lines(list(Line(coordinates(x))), x$id[1L])) # the corrected part goes here: lines <- SpatialLines(x) data <- data.frame(id = unique(d$id)) rownames(data) <- data$id l <- SpatialLinesDataFrame(lines, data) 

So the problem is that you need to create data.frame for the rows grouped by id (one row for each row). In the case above, when there is no data other than id , it is quite simple. If you need to group some other data for the original SpatialPointDataFrame , although you should use some grouping functions like tapply , aggregate , or use my favorite - sqldf :

 data <- sqldf(' select id, max(something), sum(something_else) from d group by id ') 
+6
source
 ## example data d <- data.frame(x=runif(7), y=runif(7), id = c(rep("a", 3), rep("b", 4))) ##split(d, d$id) library(sp) coordinates(d) <- ~x+y ## list of Lines per id, each with one Line in a list x <- lapply(split(d, d$id), function(x) Lines(list(Line(coordinates(x))), x$id[1L])) ## or one Lines in a list, with all Line objects ## x <- list(Lines(lapply(split(d, d$id), function(x) Line(coordinates(x))), paste(unique(d$id), collapse = "_"))) ## etc. SpatialLines(x, CRS(as.character(NA))) ## need to be careful here, assuming one Lines per original row ## and we trash the original rownames . . . SpatialLinesDataFrame(SpatialLines(x, CRS(as.character(NA))), d[,"id", drop = FALSE], match.ID = FALSE) 
+3
source

Source: https://habr.com/ru/post/971002/


All Articles