chronosphere-info / r_client

R client for the chronosphere
https://chronosphere.info/r_client/
GNU General Public License v3.0
6 stars 4 forks source link

Problem reconstructing SpatialLinesDataFrame #6

Closed LPDagallier closed 3 years ago

LPDagallier commented 3 years ago

Hi, Thanks a lot for this wonderful package !

I have an issue while reconstructing a SpatialLinesDataFrame:

As a reproducible example:

adminlines = fetch(dat = "paleomap", var = "adminlines")
chile <- adminlines[which(adminlines$ADMIN == "Chile"),] #subset to have a small object
plot(chile) # just check that Chile has been well selected
reconstruct(chile, age = 50)

I got the following message: "Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘coordinates’ for signature ‘"NULL"’ "

I also tried with a very simple SpatialLinesDataFrame object created from scratch (code from sp vignette):

x <- c(1,5,4,8)
y <- c(1,3,4,7)
SL <- SpatialLines(list(Lines(Line(cbind(x,y)), ID="a")))
reconstruct(SL, 140)
l1 <- cbind(c(1, 2, 3), c(3, 2, 2))
l2 <- cbind(c(1, 2, 3), c(1, 1.5, 1))
Sl1 <- Line(l1)
Sl2 <- Line(l2)
S1 <- Lines(list(Sl1), ID = "a")
S2 <- Lines(list(Sl2), ID = "b")
Sl <- SpatialLines(list(S1, S2))
## sample data: line lengths
library(rgeos)
df <- data.frame(len = sapply(1:length(Sl), function(i) gLength(Sl[i, ])))
rownames(df) <- sapply(1:length(Sl), function(i) Sl@lines[[i]]@ID)
## SpatialLines to SpatialLinesDataFrame
Sldf <- SpatialLinesDataFrame(Sl, data = df)

reconstruct(Sldf, 50)

And got the same error.

I check through the reconstruction.R script, and it seems that the problem comes from the fact that for "SpatialLinesDataFrame" objects, the reconstruction is passed to gplates_reconstruct_polygon internal function (line 320). And when this function (line 848) tries to retrieve the attributes from the sp input object, it tries to retrieve the 'polygons' attributes and not the 'lines' attributes (polys = attr(sp,'polygons') line 853), which causes coordinate extractions to be "NULL".

adamkocsis commented 3 years ago

Hi,

Thanks for getting in touch and for the report.

Yes, the reason why you get the errors is because for the online reconstruction method the SpatialLinesDataFrame-specific routines are not implemented yet. At the moment these are only added for the offline reconstructions that use the GPlates Desktop Application. If you install GPlates (https://www.gplates.org/), the following script should work:

# download the paleomap model (same version as the online method)
mod <- chronosphere::fetch("paleomap", "model", ver="v3-GPlates")  

# present day admin lines
adminlines = chronosphere::fetch(dat = "paleomap", var = "adminlines")
# subset
chile <- adminlines[which(adminlines$ADMIN == "Chile"),] #subset to have a small object
plot(chile) # just check that Chile has been well selected

# 50ma reconstruction background
world50 <- reconstruct("plates", age=50, model=mod)
plot(world50, col="gray", border=NA)

chile50 <- reconstruct(chile, age = 50, model=mod)
plot(chile50, add=T)

I will try to incorporate the missing method (using @lines) in the next update. I can try to prioritize this if you need it fast. Please let me know if the solution above does not work for you for some reason!

As a side note, please be aware that the paleocoordinate reconstruction-related functions will be separated from the main chronosphere package in the next update for easier maintenance (the downloads will remain where they are). The coordinate reconstructions (i.e. reconstruct()) have already been copied to the rgplates package (https://cran.r-project.org/package=rgplates) and will be maintained there in the foreseeable future.

Best,

Adam

LPDagallier commented 3 years ago

Hi, Thank you a lot for your reply. It works very well with GPlates locally installed. I will use this method instead of the online reconstruction method. Thanks a lot for this incredible tool, very useful ! Best, Léo-Paul