[R-sig-Geo] POSIX* objects and writeOGR()
Roger Bivand
Roger.Bivand at nhh.no
Fri Jan 1 19:54:06 CET 2010
On Thu, 31 Dec 2009, Agustin Lobo wrote:
> Thanks!
>
> I see that creating a temporary SPDF with that variable casted
> as character is the only way to run writeOGR().
>
> Would it be possible adding
> an option i.e. POSIXtoCHAR=T to writeOGR() so that such a conversion is
> automated?
POSIXt *is* converted to character by default, and has been since the
writeOGR() function was added to the package. The issues with time and
data handling were in readOGR(), where these are now converted to String
before reading.
If the identification of POSIXt columns hasn't worked for you with
writeOGR(), please provide a reproducible example and I'll try to fix it,
but I think that this "just works".
Best wishes,
Roger
>
> Agus
>
> Michael Sumner wrote:
>> POSIXlt stores the date-times as a list of vectors decomposed into the
>> (still numeric) component parts ("sec", "min", "hour", "mday", "mon",
>> "year", "wday", "yday", "isdst") so that cannot be stored as a single
>> column in the data frame - you would need 9 columns.
>>
>>
>> Compare these to see the underlying structure:
>>
>> tm <- Sys.time()
>> ## POSIXct
>> unclass(tm)
>> ## POSIXlt
>> unclass(as.POSIXlt(tm))
>>
>> You can just cast to character, directly or with format - see
>> ?strptime for the full set of format tokens available. For example:
>>
>> as.character(tm)
>> ##[1] "2009-12-31 07:28:01"
>> format(tm, "%Y/%b/%a %H:%M:%S")
>> ##[1] "2009/Dec/Thu 07:28:01"
>>
>> Hope that helps.
>>
>> Regards, Mike.
>>
>>
>> On Thu, Dec 31, 2009 at 7:15 AM, Agustin Lobo <alobolistas at gmail.com>
>> wrote:
>>> This might be a bit off-topic.
>>> I have to export as shape an Sp.Poly.DF in which
>>> one of the variables records date and time as POSIXtc. As
>>> writeOGR() complains about the POSIXct variable, I'm
>>> trying to convert to POSIXlt, which I think that will be
>>> accepted (another alternative would be coverting to an
>>> string char, but prefer to keep the SPDF object with
>>> a correct class definition for that variable).
>>> Let's call patata the table of the SPDF.
>>>
>>>> class(patata$Tima)
>>> [1] "POSIXt" "POSIXct"
>>>
>>>> patata$Tima[1]
>>> [1] "2009-09-26 09:57:24 CEST"
>>>
>>>> as.POSIXlt(patata$Tima[1])
>>> [1] "2009-09-26 09:57:24 CEST"
>>>
>>>> class(as.POSIXlt(patata$Tima[1]))
>>> [1] "POSIXt" "POSIXlt"
>>>
>>> So I was happy, but:
>>>
>>>> patata$Tima <- as.POSIXlt(patata$Tima)
>>> Error in `$<-.data.frame`(`*tmp*`, "Tima", value = list(sec = c(24, 33, :
>>> replacement has 9 rows, data has 44
>>> Calls: $<- -> $<-.data.frame
>>>
>>>
>>> Is there any way I can put the POSIXlt object in the table (patata) of the
>>> SPDF object?
>>>
>>> Thanks!
>>>
>>> Agus
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at stat.math.ethz.ch
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at 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.Bivand at nhh.no
More information about the R-sig-Geo
mailing list