Learn R Programming

googlePolylines (version 0.8.7)

encode: Encode

Description

Encodes coordinates into an encoded polyline.

Usage

encode(obj, ...)

# S3 method for sf encode(obj, strip = FALSE, ...)

# S3 method for data.frame encode(obj, lon = NULL, lat = NULL, byrow = FALSE, ...)

Value

sfencoded object

Arguments

obj

either an sf object or data.frame

...

other parameters passed to methods

strip

logical indicating if sf attributes should be stripped. Useful if you want to reduce the size even further, but you will lose the spatial attributes associated with the sf object

lon

vector of longitudes

lat

vector of latitudes

byrow

logical indicating if the encoding should be done for each row

Details

The function assumes Google Web Mercator projection (WSG 84 / EPSG:3857 / EPSG:900913) for inputs and outputs.

Will work with

  • sf and sfc objects directly

  • data.frames - It will attempt to find lat & lon coordinates, or you can explicitely define them using the lat and lon arguments

See Also

encodeCoordinates

Examples

Run this code

## data.frame
df <- data.frame(polygonId = c(1,1,1,1),
  lineId = c(1,1,1,1),
  lon = c(-80.190, -66.118, -64.757, -80.190),
  lat = c(26.774, 18.466, 32.321, 26.774))
  
## on a data.frame, it will attemp to find the lon & lat columns
encode(df)

## use byrow = TRUE to convert each row individually
encode(df, byrow = TRUE)


if (FALSE) {

## sf objects
library(sf)
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))

encoded <- encode(nc)

## view attributes
attributes(encoded) 

## view attributes of subset object
attributes(encoded[, c("AREA", "PERIMETER", "geometry")])

## view attributes without encoded column
attributes(encoded[, c("AREA", "PERIMETER")])

## strip attributes
encodedLite <- encode(nc, strip = TRUE)

attributes(encodedLite)

## view attributes of subset lite object
attributes(encodedLite[, c("AREA", "PERIMETER", "geometry")])

## view attributes without encoded column
attributes(encodedLite[, c("AREA", "PERIMETER")])
}

Run the code above in your browser using DataLab