Description Usage Arguments Value Examples
View source: R/latlong2region.R
Match coordinates to regions in a shapefile. Also includes the possibility to find the region a point is closest to if, for example, the point is the water.
1 | latlong2region(p, shp, closest = TRUE)
|
p |
Longitudes and latitudes of points. Can be a vector of two points or
a matrix or |
shp |
The |
closest |
Whether to find the closest region for points that don't fall
in any region in |
Returns a data.frame
with p
and the corresponding elements in shp
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # Download, extract and load the US counties shapefile:
us <- "http://www2.census.gov/geo/tiger/GENZ2015/shp/cb_2015_us_county_500k.zip"
download.file(us, "us_counties.zip")
unzip("us_counties.zip")
shp <- rgdal::readOGR(".", "cb_2015_us_county_500k")
# Points to use, the Chicago point is in Lake Michigan so it won't be matched.
# It will be matched to the closest county.
df <- data.frame(name = c("New York", "San Francisco", "Chicago"),
lon = c(-74.00594, -122.41942, -87.61),
lat = c(40.71278, 37.77493, 41.87811))
p <- df[, c("lon", "lat")]
latlong2region(p, shp)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.