tidygeocoder R Package

repository·main·Indexed 43 days ago

https://github.com/jessecambon/tidygeocoder

An R package providing a unified, high-level interface for forward and reverse geocoding services. It returns results in tidy tibble format and includes features such as batch geocoding, automatic rate limiting, and handling of duplicate or NA inputs via the geocode() and reverse_geocode() functions.

Tokens
780
Snippets
3
Records
3
Agent score
5%

What's inside tidygeocoder

  1. Install tidygeocoder

    main

    You can install the stable version of tidygeocoder from CRAN or the latest development version from GitHub using devtools.

    # Install stable version from CRAN
    install.packages('tidygeocoder')
    
    # Install latest development version from GitHub
    devtools::install_github("jessecambon/tidygeocoder")
  2. Reverse geocoding with reverse_geocode()

    main

    The reverse_geocode() function converts geographic coordinates into addresses. It accepts similar arguments to geocode(), but requires specifying the input columns for latitude and longitude.

    To retrieve the full address string, use the address argument to name the output column. Setting full_results = TRUE will include all metadata returned by the geocoding service.

    library(dplyr)
    library(tidygeocoder)
    
    # Assuming lat_longs contains latitude and longitude columns
    reverse <- lat_longs %>%
      reverse_geocode(
        lat = latitude, 
        long = longitude, 
        method = 'osm',
        address = address_found, 
        full_results = TRUE
      ) %>%
      select(-addr, -licence)
  3. Forward geocoding with geocode()

    main

    The geocode() function converts addresses into geographic coordinates (latitude and longitude). It works on dataframes and supports various geocoding services via the method argument.

    Key features:

    • Batch geocoding: Automatically used if the service supports it.
    • Rate limiting: Automatically respects the usage policies of the selected service.
    • Data handling: Handles duplicate, NA, and blank inputs by only submitting unique values while preserving original row counts.
    • Full results: By default, only latitude and longitude are returned. Set full_results = TRUE to retrieve all available data from the service.
    library(dplyr, warn.conflicts = FALSE)
    library(tidygeocoder)
    
    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 addresses using the OpenStreetMap (osm) method
    lat_longs <- some_addresses %>%
      geocode(addr, method = 'osm', lat = latitude, long = longitude)