3

I am learning R and I wanted to download earthquake data provided by the USGS to explore it with R. This is how I downloaded the data:

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

But if I do the following to get the attributes of the file:

> names(USGSdata)

I get :

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

Which is not what I am looking for... I am looking for the name of the fields/attributes of the seismic data (something like location, magnitude, depth etc).

Any ideas of how can I convert the GeoJSONP data into plain JSON so I can manipulate it in R? I know GeoJSONP is not the same as GeoJSON.

mfroese
  • 309
  • 1
  • 5
  • 18
  • possible duplicate of [Is it possible to read geoJSON or topoJSON file in R to draw a choropleth map?](http://stackoverflow.com/questions/24183007/is-it-possible-to-read-geojson-or-topojson-file-in-r-to-draw-a-choropleth-map) –  Jun 04 '15 at 00:58

1 Answers1

5

Use readOGR from the rgdal package:

> 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

That gives you something you can plot and treat like a data frame:

> 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,])

This is a SpatialPointsDataFrame and is one of the spatial data classes defined in the sp package.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Works well thanks! Other people may want to know that to install rgdal they have to use library(rgdal) – mfroese Jun 03 '15 at 23:18