4

I have a dataframe containing U.S. latitude and longitude coordinates.

I would like to create a variable that shows the respective U.S. county that includes these coordinates.

How can I do that in R/Python?

Thanks!

ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
  • The best way is using Google Maps API. But, you should register your app for getting api-key. Here is link https://developers.google.com/maps/documentation/geocoding/start Allso `geopy` is good lib for it – amarynets Sep 13 '17 at 21:33
  • 1
    Something like this: https://stackoverflow.com/questions/13316185/r-convert-zipcode-or-lat-long-to-county – Ryan Morton Sep 13 '17 at 21:33
  • Possible duplicate of [R convert zipcode or lat/long to county](https://stackoverflow.com/questions/13316185/r-convert-zipcode-or-lat-long-to-county) – G5W Sep 13 '17 at 21:42
  • not a duplicate. zip codes can span multiple counties while lat loc does not. my question is more specific – ℕʘʘḆḽḘ Sep 13 '17 at 21:56

1 Answers1

4

You could use geopy.

Example from the documentation using Nominatim (Open Street Map):

>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim(user_agent="http")
>>> location = geolocator.reverse("52.509669, 13.376294")
>>> print(location.address)
Potsdamer Platz, Mitte, Berlin, 10117, Deutschland, European Union
>>> print((location.latitude, location.longitude))
(52.5094982, 13.3765983)
>>> print(location.raw)
{'place_id': '654513', 'osm_type': 'node', ...}

The raw output has a dict key country, if one can be found.

gcamargo
  • 3,683
  • 4
  • 22
  • 34
agtoever
  • 1,669
  • 16
  • 22