Learn R Programming

ggOceanMaps (version 1.1)

transform_coord: Transform spatial coordinates to another projection

Description

Transforms spatial coordinates from original projection (decimal degrees assumed) to another projection.

Usage

transform_coord(
  x = NULL,
  lon = NULL,
  lat = NULL,
  new.names = "auto",
  proj.in = "+init=epsg:4326",
  proj.out = NULL,
  verbose = FALSE,
  bind = FALSE,
  na = "ignore"
)

Arguments

x

Data frame to be transformed. Can be omitted if numeric vectors are assigned to lon and lat.

lon, lat

Either a name of the longitude and latitude columns in x or a numeric vector containing longitude and latitude coordinates. Use NULL to guess the longitude and/or latitude columns in x.

new.names

Character vector of length 2 specifying the names of transformed longitude and latitude columns, respectively. Alternatively NULL, which returns column names from x or "auto", which uses NULL if bind = FALSE and c("lon.proj", "lat.proj") if bind = TRUE.

proj.in

Original proj4string projection. If NULL, the projection is taken from x. x must be a Spatial object in that case.

proj.out

Character. Either NULL, proj4string projection the coordinates should be transformed to or a name of shapefiles in shapefile_list. If NULL, the output projection will be automatically determined from data. This option requires decimal degrees as input option.

verbose

Logical indicating whether information about the projection should be returned as message. Set to FALSE to make the function silent.

bind

logical. Should only transformed coordinates be returned (FALSE, default) or should x be returned with transformed coordinates (TRUE)?

na

character specifying the NA action for missing coordinates. The "ignore" option ignores the coordinates and returns NAs to transformed coordinates. The "remove" option removes missing values from x returning a message while doing it. Any other character argument will trigger na.fail stopping the function in case of missing coordinates.

Value

Returns a data frame with transformed spatial coordinates.

Details

If x is specified, the function guesses longitude and latitude columns from x by default.

See Also

Other basemap functions: basemap(), qmap(), shapefile_list()

Examples

Run this code
# NOT RUN {
# Coordinates are automatically transformed to the pre-made shapefile 
# projections:
dt <- data.frame(lon = c(-150, 150), lat = c(60, 90))
transform_coord(dt)
transform_coord(dt, bind = TRUE)

dt <- data.frame(lon = c(-150, 150), lat = c(20, 50))
transform_coord(dt, bind = TRUE) # no transformation required.
# }

Run the code above in your browser using DataLab