OneMapSGAPI

Getting Started

Introduction

The OneMapSGAPI package provides useful wrappers for the OneMapSG API client. It allows users to easily query spatial data from the API in a tidy format and provides additional functionalities to allow easy data manipulation.

:tada: Version 2.0.0 is now available! Version update is now compatible with the changes to the OneMapSG API released in late-2023.

The OneMapSg API package is published on CRAN-R! :tada: :confetti_ball: To download the release version, run:

install.packages("onemapsgapi")

The development version can be downloaded via:

# install.packages("devtools")
devtools::install_github("jolene-lim/onemapsgapi")

The development version is recommended as this package is early in its lifecycle. Please submit any problems via a github issues. Pull requests are also encouraged for anyone looking to contribute!

Features

Status

Currently, the following API endpoints are supported:

API Coverage Supported Endpoints
Authentication :white_check_mark: Full getToken
Search :white_check_mark: Full search
Themes :white_check_mark: Full checkThemeStatus, getThemesInfo, getAllThemesInfo, retrieveTheme
Planning Area :white_check_mark: Full getPlanningarea, getPlanningareaNames, getPlanningarea
Population Query :white_check_mark: Full Check Population Query Docs for the full list of endpoints. All endpoints are supported in this package.
Routing Service :white_check_mark: Full route

Usage

Authentication

In order to query data, most API endpoints in the OneMapSG API require a token. First-time users can register themselves using the OneMapSG registration form. Subsequently, they can retrieve their tokens using the get_token() function with their email and password, for example:

token <- get_token("user@email.com",  "password")

The function will also print a message informing users of the token's expiry date and time.

Search

This can be used to geocode data. Users inputs a dataframe with a column of information (e.g. postal codes, names of places of interest). The function returns an enriched dataframe with coordinates and/ or address information. Users also have the option to return the data as an sf dataframe. The option for parallel processing is available for large datasets.

# example: returns a sf dataframe with coordinates and address details
geocode_onemap(df, "address",
   return_geom=TRUE, address_details = TRUE, return_spatial = TRUE)

Themes

Themes in the OneMap SG API refer to types of locations, such as kindergartens, parks, hawker centres etc. This package provides functions related to querying themes:

# example: returns named logical if theme has been updated at time of query

get_theme_status(token, "hotels")
# example: returns character vector related to kindergaterns theme

get_theme_info(token, "kindergartens")
# example: return all themes related to "hdb" or "parks"
search_themes(token, "hdb", "parks")

# example: return all possible themes
search_themes(token)
# example: return the location coordinates + other data of all hotels in Singapore
get_theme(token, "hotels")

Planning Area

Planning Area API endpoints allow users to get spatial data and data related to the planning areas in Singapore. This package provides users with the ability to query the data and optionally handles necessary spatial data wrangling on behalf of the user.

# example: return dataframe of class "sf" for planning areas in 2014
get_planning_areas(token, 2014, read = "sf")
# example: return dataframe of planning areas in 2014
get_planning_names(token, 2014)
# example: return spatial polygon of class "sf" for planning area matching the input point in 2014
get_planning_polygon(token, 1.3, 103.8, 2014, read = "sf")

Population Query

Population Query API endpoints allow users to pull socio-economic datasets by planning area, which each endpoint representing a dataset (e.g. getPopulationAgeGroup provides age group summary statistics by planning area). This package combines querying different Popquery API endpoints into a single function.

# example: return occupation summary data and literacy summary data for Bedok and Yishun towns in year 2010
get_pop_queries(token, c("getReligion", "getLanguageLiterate"), c("Bedok", "Yishun"), "2010")

Route Service

The Route Service API provides users a way to query the route taken from one point to another. It provides information about the total time and distance taken for the route, route instructions and other infomation e.g. elevation, for a variety of routes (public transport, drive, walk, cycle). This package provides two different functions associated with this API, each serving different purposes.

# example: return route data only in tibble format
get_route(token, c(1.319728, 103.8421), c(1.319728905, 103.8421581), "drive")

# example: return route data (tibble) and status information (list) as list of 2
get_route(token, c(1.319728, 103.8421), c(1.319728905, 103.8421581), "drive", status_info = TRUE)

For large queries, allowing parallel iterations is recommended as it can halve the return time. Note that for queries involving a large volume of API calls, return time will still be limited by the API call speed.

# example: return tibble named df
get_travel(token, df,
    "start_lat", "start_lon", "end_lat", "end_lon",
    routes = c("cycle", "walk"))