Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read GeoJSONP data in R?

Tags:

r

geojson

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.

like image 384
mfroese Avatar asked Dec 05 '25 21:12

mfroese


1 Answers

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.

like image 153
Spacedman Avatar answered Dec 08 '25 11:12

Spacedman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!