r - How to filter a dataframe of geocoordinates using a KML polygon? -


i have csv of data points span entire earth (the geological service's earthquake feed), , want filter earthquakes within united states' border.

the kml file i've pulled u.s. census bureau:

https://www.census.gov/geo/maps-data/data/kml/kml_nation.html

in r, rgdal library can load kml files:

library(rgdal) kml = readogr("kmls/cb_2014_us_nation_20m.kml", 'cb_2014_us_nation_20m') 

how use dplyr/plyr/etc. filter data.frame (the columns geo data latitude , longitude) rows fall within boundaries specified kml?


edit, post-answer:

here's used @hrbrmstr's answer quick visualization:

library(sp) library(rgdal) # download earthquakes url <- "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.csv" fil <- "all_week.csv" if (!file.exists(fil)) download.file(url, fil) quakes <- read.csv("all_week.csv", stringsasfactors=false) # create spatial object sp::coordinates(quakes) <- ~longitude+latitude  # download nation kml url <- "http://www2.census.gov/geo/tiger/genz2014/kml/cb_2014_us_nation_20m.zip" fil <- "uskml.zip" if (!file.exists(fil)) download.file(url, fil) unzip(fil, exdir="uskml") # read kml file <- rgdal::readogr("./uskml/cb_2014_us_nation_20m.kml", "cb_2014_us_nation_20m") sp::proj4string(quakes) <- sp::proj4string(us)  length(quakes) # 1514  usquakes = quakes[us,] length(usquakes) # 1260  ### visualize plot(us)  # plot quakes points(quakes$longitude, quakes$latitude) # plot points(usquakes$longitude, usquakes$latitude) 

resulting image:

us geological services detected earthquakes 1 week; quakes plotted in red

thanks @hrbrmstr!

this'll cpl ways:

library(sp) library(maptools) library(rgeos) # not entirely necessary library(rgdal) # not entirely necessary  url <- "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.csv" fil <- "all_week.csv" if (!file.exists(fil)) download.file(url, fil)  quakes <- read.csv("all_week.csv", stringsasfactors=false) coordinates(quakes) <- ~longitude+latitude  url <- "http://www2.census.gov/geo/tiger/genz2014/shp/cb_2014_us_nation_20m.zip" fil <- "us.zip" if (!file.exists(fil)) download.file(url, fil) unzip(fil, exdir="us") <- readshapepoly("us/cb_2014_us_nation_20m.shp")  # alternatively # <- rgdal::readogr("us/cbcb_2014_us_nation_20m.shp", "cb_2014_us_nation_20m")  # true if in in_us_rgeos <- rgeos::gintersects(quakes, us, byid=true)  # <na> if in in_us_over <- quakes %over% 

gintersects takes longer. rgdal , rgeos can bear working on systems. ymmv

using kml require (for part) rgdal:

# wanted kml shapefile tho url <- "http://www2.census.gov/geo/tiger/genz2014/kml/cb_2014_us_nation_20m.zip" fil <- "uskml.zip" if (!file.exists(fil)) download.file(url, fil) unzip(fil, exdir="uskml")  <- rgdal::readogr("uskml/cb_2014_us_nation_20m.kml", "cb_2014_us_nation_20m") proj4string(quakes) <- proj4string(us) rgeos::gintersects(quakes, us, byid=true) head(quakes %over% us) 

Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -