如何在R中读取GeoJSONP数据?

3

我正在学习R语言,想要下载美国地质调查局提供的地震数据用于在R中进行探索。以下是我下载该数据的方法:

>USGSdata<-fromJSON("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojsonp")

但如果我按照下面的步骤获取文件的属性:
> names(USGSdata)

我理解为:

[1] "type"     "metadata" "features" "bbox"

这不是我要找的... 我在寻找地震数据的字段 / 属性名称(例如位置、震级、深度等)。

有什么想法可以将GeoJSONP数据转换为普通的JSON,以便我可以在R中操作它吗?我知道GeoJSONP与GeoJSON不同。


1个回答

5
使用rgdal包中的readOGR函数:
> download.file("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson",destfile="/tmp/all_month.geojson")
> all_month=readOGR("/tmp/all_month.geojson","OGRGeoJSON")
OGR data source with driver: GeoJSON 
Source: "/tmp/all_month.geojson", layer: "OGRGeoJSON"
with 8923 features and 26 fields
Feature type: wkbPoint with 3 dimensions

那样你就得到了可以像数据框一样绘制和处理的内容:
> plot(all_month)
> names(all_month)
 [1] "mag"     "place"   "time"    "updated" "tz"      "url"     "detail" 
 [8] "felt"    "cdi"     "mmi"     "alert"   "status"  "tsunami" "sig"    
[15] "net"     "code"    "ids"     "sources" "types"   "nst"     "dmin"   
[22] "rms"     "gap"     "magType" "type"    "title"  
> range(all_month$mag)
[1] -0.73  7.80
> plot(all_month[all_month$mag>7,])
> plot(all_month[all_month$mag>6,])

这是一个SpatialPointsDataFrame,是sp包中定义的空间数据类之一。


运行得很好,谢谢!其他人可能想知道,要安装rgdal,他们必须使用library(rgdal)。 - mfroese

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接