【问题标题】:R - Date is in Scientific Notation when I pulled Data from ArcGIS Server, how do I change the value to a date?R - 当我从 ArcGIS Server 中提取数据时,日期采用科学记数法,如何将值更改为日期?
【发布时间】:2020-06-23 23:29:03
【问题描述】:

我正在从 ArcGIS Online 服务器中提取数据,并且日期以数字形式加载,我需要将其转换回格式为 d/m/y h:m 的日期。

library(sf)
library(tmap)
library(httr)
library(sp)

url <- list(hostname = "gis.blm.gov/arcgis/rest/services",
            scheme = "https",
            path = "hydrography/BLM_Natl_AIM_AquADat/MapServer/0/query",
            query = list(
              where = "1=1",
              outFields = "*",
              returnGeometry = "true",
              f = "geojson")) %>% 
            setattr("class", "url")
request <- build_url(url)
#Field DT and DATECHANGE need to be dates not num 
BLM <- st_read(request) 
str(BLM$DT)

Date from the imported file look like:

【问题讨论】:

    标签: r date import arcgis arcgis-server


    【解决方案1】:

    这个也让我摸不着头脑!

    我决定在浏览器中打开查询 URL,看看我是不是疯了。 下面的结果表明问题不是我的代码。

    {
      "attributes" : {
        ...
    
        "Date":1596326400000
    
        ...
      }
    }
    

    我最终开始梳理 ArcGIS 文档页面,直到我发现他们如何根据该数字计算日期。
    我在下面的页面上找到了答案:

    https://developers.arcgis.com/documentation/common-data-types/feature-object.htm

    attributes - 要素属性。它是一个 JSON 对象,包含名称-值对的字典。名称是特征字段名称。这些值是字段值,它们可以是任何标准 JSON 类型:字符串、数字和布尔值。 请注意,日期值被编码为数字。该数字表示自 UTC 纪元(1970 年 1 月 1 日)以来的毫秒数。

    示例:

    "attributes" : {
     "OWNER" : "Joe Smith", //string
     "VALUE" : 94820.37, //number
     "APPROVED" : true, //boolean
     "LASTUPDATE" : 1227663551096 //date
    }
    

    知道了这一点,我们可以轻松地将值从毫秒转换为秒以获得 UNIX 纪元时间 - 然后将其转换为原生 r 格式,如下面这个超级有用的 stackoverflow 问题所述:
    Convert UNIX epoch to Date object

    r 代码:

    > myDate <- 1.595394e+12 / 1000
    > as.POSIXct(myDate, origin="1970-01-01")
    

    输出:

    [1] "2020-07-22 01:00:00 EDT"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-02
      • 2018-08-14
      • 2017-12-30
      • 1970-01-01
      • 2014-11-20
      • 2017-02-20
      • 2014-04-09
      • 2014-10-30
      相关资源
      最近更新 更多