This function converts geographic coordinates (latitude and longitude) into projected coordinates based on a specified Coordinate Reference System (CRS). The function can automatically detect the CRS from provided metadata (dataInfo) or use a specified CRS code. It is designed to work with datasets obtained from rerddap::griddap() but can be used with any geographic data that requires coordinate transformation.

latlon_to_xy(
  dataInfo,
  longitude,
  latitude,
  xName = "rows",
  yName = "cols",
  crs = NULL
)

Arguments

dataInfo

Metadata object containing CRS information, typically obtained from a rerddap::info() call on a dataset. It should include global attributes that contain CRS information.

longitude

Numeric vector of longitudes to be converted.

latitude

Numeric vector of latitudes to be converted.

xName

Name of the longitude coordinate in the output projection. Defaults to 'longitude'.

yName

Name of the latitude coordinate in the output projection. Defaults to 'latitude'.

crs

Optional. A character string specifying the CRS to use for the projection. This can be an EPSG code (e.g., 'EPSG:4326' for WGS84) or a PROJ string. If NULL, the function attempts to detect the CRS from dataInfo.

Value

A matrix with columns corresponding to the projected x and y coordinates (in the order specified by xName and yName). Each row in the matrix corresponds to a pair of input latitude and longitude values.

Examples

myURL <- 'https://polarwatch.noaa.gov/erddap/'
myInfo <- rerddap::info('noaacwVIIRSn20icethickNP06Daily', url = myURL)
latitude <- c( 80., 85.)
longitude <- c(-170., -165)
coords <- latlon_to_xy(myInfo,  longitude, latitude)