Reputation: 31
I have 6000 object with only lng and lat. Using GADM dataset for Poland i can find where the point is. Here i have LargeSpdataframe with boundries of voivoships
library(raster)
voiv<-getData("GADM", country = "PL", level = 1)
counities
county<-getData("GADM", country = "PL", level = 2)
and on level down, more dedailed breakdown
gm<-getData("GADM", country = "PL", level = 3)
I want to find where the points are, like this.( its only eg.)
lng lat voiv county gm
20.28222 50.72641 Lubuskie Sieradz Sieradz
How to define Ggps points. I was using over
function but doesn't work.
Upvotes: 1
Views: 142
Reputation: 47631
You can use raster::extract
library(raster)
voiv <- getData("GADM", country = "POL", level = 1)
xy <- cbind(20.28222, 50.72641)
e <- extract(voiv[, c("NAME_0", "NAME_1")], xy)
e
# point.ID poly.ID NAME_0 NAME_1
#1 1 13 Poland Swietokrzyskie
Upvotes: 1
Reputation: 73782
The coordinates and names are in different slots. Extract the former using coordinates
. After that you could cbind
them together.
r <- cbind(gm[grep("^NAME", names(gm))], `colnames<-`(coordinates(gm), c("lng", "lat")))
Find Lubuskie in result:
head(r[r$NAME_1 %in% "Lubuskie", ])
# NAME_0 NAME_1 NAME_2 NAME_3 lng lat
# 41070 Poland Lubuskie Gorzów Bogdaniec 15.09408 52.68379
# 41116 Poland Lubuskie Gorzów Deszczno 15.29237 52.65471
# 40825 Poland Lubuskie Gorzów Klodawa 15.26856 52.82939
# 41258 Poland Lubuskie Gorzów Kostrzyn Nad Odra 14.63813 52.61470
# 40855 Poland Lubuskie Gorzów Lubiszyn 14.98341 52.79892
# 40950 Poland Lubuskie Gorzów Santok 15.43891 52.74680
Upvotes: 0
Reputation: 486
This should be possible using the revgeocode function from ggmap, but you need your own API key
library(ggmap)
ggmap::register_google(key = "your api key")
revgeocode(location = c(20.28222, 50.72641), output = c("address", "all"), force = FALSE, urlonly = FALSE, override_limit = FALSE)
"Mniszek 4, 28-366, Poland"
Upvotes: 0