Last data update: 2014.03.03

R: Convert a vector/matrix of coordinates to JSON format
coords2JSONR Documentation

Convert a vector/matrix of coordinates to JSON format

Description

Similar to toJSON from jsonlite, this function takes a set of coordinates as input and converts them to proper JSON format. Note that the function is powered by Rcpp which makes it a convenient alternative to existing methods when it comes to processing big datasets.

Usage

## S4 method for signature 'numeric'
coords2JSON(x)

## S4 method for signature 'character'
coords2JSON(x, xy = c(1, 2))

## S4 method for signature 'matrix'
coords2JSON(x, xy = c(1, 2))

Arguments

x

A 'numeric' vector with a single pair of coordinates or a matrix with multiple pairs of input coordinates, typically projected in EPSG:4326 (http://spatialreference.org/ref/epsg/wgs-84/).

xy

An 'integer' vector specifying the coordinate columns.

Value

A single 'character' object in JSON format.

Author(s)

Florian Detsch

Examples

crd <- matrix(ncol = 3, nrow = 12)

# x-coordinates
set.seed(10)
crd[, 1] <- rnorm(nrow(crd), 10, 3)

# y-coordinates
set.seed(10)
crd[, 2] <- rnorm(nrow(crd), 50, 3)

# additional data
crd[, 3] <- month.abb

# reformat a single pair of coordinates
coords2JSON(crd[1, ])

# reformat multiple pairs of coordinates at once
coords2JSON(crd)

Results