[R-sig-Geo] problems with plotting STFDF

Edzer Pebesma edzer.pebesma at uni-muenster.de
Sun Jan 5 20:32:47 CET 2014


Thanks, Erin; the first function should be available when loading
package spacetime; I adapted kml_layer.STIDF in plotkml (on r-forge
svn), to accomodate to your second problem.

On 01/01/2014 02:55 AM, Hodgess, Erin wrote:
> Hello again!  Happy New Year!
> 
> Here is the solution for this particular situation.  Note:  thanks to many people for the help.  Anyhow, I started with looking at page 28 in the gstat vignette for krigeST.  If you work through that, you obtain an object called DE_kriged, which is an STFDF.
> 
> I copied some code from the following website:
> 
> (https://r-forge.r-project.org/scm/viewvc.php/pkg/R/coerce.R?view=markup&root=spacetime
> <https://r-forge.r-project.org/scm/viewvc.php/pkg/R/coerce.R?view=markup&root=spacetime&pathrev=25>)
> 
> to get the as.STIDF.STFDF lines:
> 
> 
> 
> # STFDF -> STIDF
> as.STIDF.STFDF = function(from) {
>         as(as(from, "STSDF"), "STIDF")
> }
> setAs("STFDF", "STIDF", as.STIDF.STFDF)
> 
> 
> Now:
> 
>>  DE1 <- as.STIDF.STFDF(DE_kriged)
> 
> DE1 is an STIDF object, which is good, but the @sp section is a Spatial Pixels object.
> 
> Next I copied over the function kml_layer.STIDF to new_STIDF.R and added the following section (see the note)
> 
> new.STIDF <-
> function (obj, dtime = "", ...)
> {
>     if (all(dtime == 0)) {
>         TimeSpan.begin = format(time(obj at time), "%Y-%m-%dT%H:%M:%SZ")
>         TimeSpan.end = TimeSpan.begin
>     }
>     else {
>         if (length(obj at time) > 1 & !nzchar(dtime)) {
>         print(obj at time)
>             period <- periodicity(obj at time)
>             dtime <- period$frequency
>         }
>         TimeSpan.begin <- format(as.POSIXct(unclass(as.POSIXct(time(obj at time))) -
>             dtime/2, origin = "1970-01-01"), "%Y-%m-%dT%H:%M:%SZ")
>         TimeSpan.end <- format(as.POSIXct(unclass(as.POSIXct(time(obj at time))) +
>             dtime/2, origin = "1970-01-01"), "%Y-%m-%dT%H:%M:%SZ")
>     }
>     if (class(obj at sp) == "SpatialPoints" | class(obj at sp) == "SpatialPointsDataFrame") {
>         sp <- SpatialPointsDataFrame(obj at sp, obj at data)
>         kml_layer.SpatialPoints(obj = sp, TimeSpan.begin = TimeSpan.begin,
>             TimeSpan.end = TimeSpan.end, ...)
>     }
>     else {
>         if (class(obj at sp) == "SpatialPolygons" | class(obj at sp) ==
>             "SpatialPolygonsDataFrame") {
>             sp <- SpatialPolygonsDataFrame(obj at sp, obj at data)
>             kml_layer.SpatialPolygons(obj = sp, TimeSpan.begin = TimeSpan.begin,
>                 TimeSpan.end = TimeSpan.end, ...)
>         }
>         else {
>             if (class(obj at sp) == "SpatialLines" | class(obj at sp) ==
>                 "SpatialLinesDataFrame") {
>                 sp <- SpatialLinesDataFrame(obj at sp, obj at data)
>                 kml_layer.SpatialLines(obj = sp, TimeSpan.begin = TimeSpan.begin,
>                   TimeSpan.end = TimeSpan.end, ...)
>             }
> 
> ###########################################################
> #  New for Spatial Pixels                                 #
> ##########################################################
>         else {
>             if (class(obj at sp) == "SpatialPixels" | class(obj at sp) ==
>                 "SpatialPixelsDataFrame") {
>                 sp <- SpatialPixelsDataFrame(obj at sp, obj at data)
>                 kml_layer.SpatialPoints(obj = sp, TimeSpan.begin = TimeSpan.begin,
>                   TimeSpan.end = TimeSpan.end, ...)
>             }
> 
> 
> 
>             else {
>                 stop("The STIDF object does not extend SpatialPoints*, SpatialLines* or SpatialPolygons*")
>             }
>         }
>     }
> }
> }
> 
> Finally, I ran this:
>> library(plotKML)
> plotKML version 0.4-0 (2013-11-15)
> URL: http://plotkml.r-forge.r-project.org/
> Warning message:
> replacing previous import by ‘zoo::as.zoo’ when loading ‘gstat’
>> library(gstat)
> Loading required package: sp
>> kml_open("stuff2.kml")
> KML file opened for writing...
>> new.STIDF(DE1,dtime=24*3600,colour=var1.pred)
> Parsing to KML...
>> kml_close("stuff2.kml")
> Closing  stuff2.kml
>>
> 
> All is well.  Google Earth lets you run the kml file and show the time change, as it should.
> 
> Hope this might help someone!
> 
> Thanks,
> Erin
> 
> 
> 
> 
> 
> 
> 
> 	[[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
> 

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Heisenbergstraße 2, 48149 Münster, Germany. Phone: +49 251
83 33081 http://ifgi.uni-muenster.de GPG key ID 0xAC227795

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 555 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20140105/d7288dd9/attachment.bin>


More information about the R-sig-Geo mailing list