geocode_onemap: Geocode a dataframe of keywords

View source: R/geocode_onemap.R

geocode_onemapR Documentation

Geocode a dataframe of keywords

Description

This function is a wrapper for the Search API. It allows for geocoding of data such as postal codes or address information. Users input a dataframe with a column to geocode (e.g. postal codes, address information). It returns a tibble with additional columns of coordinate data. Optionally, it can also return the output as an sf object.

Usage

geocode_onemap(
  df,
  search_val,
  return_geom = FALSE,
  address_details = FALSE,
  return_spatial = FALSE,
  spatial_lnglat = TRUE,
  parallel = FALSE
)

Arguments

df

Input tibble with column to be geocoded

search_val

Column name containing keyword(s) to be geocoded, e.g. column of postal codes

return_geom

Default = FALSE. Whether to return the coordinate information

address_details

Default = FALSE. Whether to return address information

return_spatial

Default = FALSE. Whether to return the output as an sf object. Please ensure

spatial_lnglat

Default = TRUE. If TRUE, the WGS84 coordinates will be used to create the sf tibble. If FALSE, the SVY21 coordinates will be used.

parallel

Default = FALSE. Whether to run API calls in parallel or sequentially (default).

Value

Please note only the top result matching the search will be returned. If no error occurs:

SEARCH_VAL

Detailed search name

X

Longitude in SVY21. Returned only if return_geom = TRUE

Y

Latitude in SVY21. Returned only if return_geom = TRUE

LONGITUDE

Longitude in WGS84. Returned only if return_geom = TRUE

LATITUDE

Latitude in WGS84. Returned only if return_geom = TRUE

BLK_NO

Block number

ROAD_NAME

Road Name

BUILDING

Building Name

ADDRESS

Address

POSTAL

Postal Code

If an error occurs, an empty result will be returned for that row. A warning message will be printed with the serach value, API error message and status code.

Examples

# sample dataframe. the last record does not return any API results.
df <- data.frame(
  places = c("a", "b", "c", "d"),
  address = c("raffles place mrt", "suntec city", "nus", "100353")
)

# Returns the original df with additional columns
## Not run: geocode_onemap(df, "address",
  return_geom=TRUE, address_details = TRUE, return_spatial=TRUE)

## End(Not run)
# If an error occurs for any of the rows, an empty row will be returned.

onemapsgapi documentation built on June 8, 2025, 10:05 a.m.