Unlimited learning, half price | 50% off

Last chance! 50% off unlimited learning

Sale ends in


tidygeocoder

Tidygeocoder makes getting data from geocoding services easy. A unified high-level interface is provided for a selection of supported geocoding services and results are returned in tibble (dataframe) format.

Note that you should exercise due diligence when geocoding sensitive data as tidygeocoder utilizes third party web services to perform geocoding. Refer to the documentation on your selected geocoding service for information on how your data will be utilized and stored. See further information on this subject here.

Features:

  • Forward geocoding (addresses ⮕ coordinates)
  • Reverse geocoding (coordinates ⮕ addresses)
  • Batch geocoding (geocoding multiple addresses or coordinates in a single query) is automatically used if applicable.
  • Duplicate, NA, and blank input data is handled elegantly; only unique inputs are submitted in queries, but the rows in the original data are preserved by default.
  • The maximum rate of querying is automatically set according to the usage policies of the selected geocoding service.

In addition to the usage examples below, see the Getting Started Vignette and blog posts on tidygeocoder.

Installation

To install the stable version from CRAN (the official R package servers):

install.packages('tidygeocoder')

Alternatively, you can install the latest development version from GitHub:

devtools::install_github("jessecambon/tidygeocoder")

Usage

In this first example we will geocode a few addresses using the geocode() function and plot them on a map with ggplot.

library(dplyr, warn.conflicts = FALSE)
library(tidygeocoder)

# create a dataframe with addresses
some_addresses <- tibble::tribble(
~name,                  ~addr,
"White House",          "1600 Pennsylvania Ave NW, Washington, DC",
"Transamerica Pyramid", "600 Montgomery St, San Francisco, CA 94111",     
"Willis Tower",         "233 S Wacker Dr, Chicago, IL 60606"                                  
)

# geocode the addresses
lat_longs <- some_addresses %>%
  geocode(addr, method = 'osm', lat = latitude , long = longitude)
#> Passing 3 addresses to the Nominatim single address geocoder
#> Query completed in: 3 seconds

The geocode() function geocodes addresses contained in a dataframe. The Nominatim (“osm”) geocoding service is used here, but other services can be specified with the method argument. Only latitude and longitude are returned from the geocoding service in this example, but full_results = TRUE can be used to return all of the data from the geocoding service. See the geo() function documentation for details.

nameaddrlatitudelongitude
White House1600 Pennsylvania Ave NW, Washington, DC38.89770-77.03655
Transamerica Pyramid600 Montgomery St, San Francisco, CA 9411137.79519-122.40279
Willis Tower233 S Wacker Dr, Chicago, IL 6060641.87874-87.63596

Now that we have the longitude and latitude coordinates, we can use ggplot to plot our addresses on a map.

library(ggplot2)

ggplot(lat_longs, aes(longitude, latitude), color = "grey99") +
  borders("state") + geom_point() +
  ggrepel::geom_label_repel(aes(label = name)) +
  theme_void()

To perform reverse geocoding (obtaining addresses from geographic coordinates), we can use the reverse_geocode() function. The arguments are similar to the geocode() function, but now we specify the input data columns with the lat and long arguments. The input dataset used here is the results of the geocoding query above.

The single line address is returned in a column named by the address argument and all columns from the geocoding service results are returned because full_results = TRUE. See the reverse_geo() function documentation for more details.

reverse <- lat_longs %>%
  reverse_geocode(lat = latitude, long = longitude, method = 'osm',
                  address = address_found, full_results = TRUE) %>%
  select(-addr, -licence)
#> Passing 3 coordinates to the Nominatim single coordinate geocoder
#> Query completed in: 3 seconds
#> New names:
#> • `name` -> `name...1`
#> • `name` -> `name...17`
name…1latitudelongitudeaddress_foundplace_idosm_typeosm_idosm_latosm_lonclasstypeplace_rankimportanceaddresstypename…17officehouse_numberroadboroughcitystateISO3166-2-lvl4postcodecountrycountry_codeboundingboxtourismquarterbuildingneighbourhoodsuburbcounty
White House38.89770-77.03655White House, 1600, Pennsylvania Avenue Northwest, Ward 2, Washington, District of Columbia, 20500, United States321631063way23824102238.897699700000004-77.03655315officegovernment300.6863356officeWhite HouseWhite House1600Pennsylvania Avenue NorthwestWard 2WashingtonDistrict of ColumbiaUS-DC20500United Statesus38.8974908 , 38.8979110 , -77.0368537, -77.0362519NANANANANANA
Transamerica Pyramid37.79519-122.40279Transamerica Pyramid, 600, Montgomery Street, Financial District, San Francisco, California, 94111, United States300093323way2422297337.7951883-122.40279022107812tourismattraction300.4485801tourismTransamerica PyramidNA600Montgomery StreetNASan FranciscoCaliforniaUS-CA94111United Statesus37.7948854 , 37.7954472 , -122.4031399, -122.4024317Transamerica PyramidFinancial DistrictNANANANA
Willis Tower41.87874-87.63596Willis Tower, 233, South Wacker Drive, Printer’s Row, Loop, Chicago, Cook County, Illinois, 60606, United States344062520way38086821641.878738049999995-87.63596121188081buildingcommercial300.5331678buildingWillis TowerNA233South Wacker DriveNAChicagoIllinoisUS-IL60606United Statesus41.8781922 , 41.8792768 , -87.6365297, -87.6353939NANAWillis TowerPrinter’s RowLoopCook County

In the Wild

For inspiration, here are a few articles (with code) that leverage tidygeocoder:

Contributing

Contributions to the tidygeocoder package are welcome. File an issue for bug fixes or suggested features. If you would like to contribute code such as adding support for a new geocoding service, reference the developer notes for instructions and documentation.

Citing tidygeocoder

Use the citation() function:

citation('tidygeocoder')
To cite tidygeocoder use:

  Cambon J, Hernangómez D, Belanger C, Possenriede D (2021).
  tidygeocoder: An R package for geocoding. Journal of Open Source
  Software, 6(65), 3544, https://doi.org/10.21105/joss.03544 (R package
  version 1.0.6)

A BibTeX entry for LaTeX users is

  @Article{,
    title = {tidygeocoder: An R package for geocoding},
    author = {Jesse Cambon and Diego Hernangómez and Christopher Belanger and Daniel Possenriede},
    year = {2021},
    journal = {Journal of Open Source Software},
    publisher = {The Open Journal},
    doi = {10.21105/joss.03544},
    url = {https://doi.org/10.21105/joss.03544},
    volume = {6},
    number = {65},
    pages = {3544},
    note = {R package version 1.0.6},
  }

Or refer to the citation page.

Copy Link

Version

Install

install.packages('tidygeocoder')

Monthly Downloads

11,122

Version

1.0.6

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Jesse Cambon

Last Published

March 31st, 2025