Search code examples
rbigdatarasternetcdfcoordinate-transformation

How to convert longlat coordinate into Lambert conformal conic in R


I have a big .nc file for daily climate data with 365 layers for north America. I want to use R to extract the timeseries of one climate variables for a specific location (a point with specific lat and lon).

I tried ncvar_get(“filename.nc', "variable") but the memory limitation does not let me to use this function.

I can only use brick function as: brick.nc <- brick(“filename.nc', "variable")

The coordinate system and extent of the RasterBrick made by brick function are as:

extent: -5802750, -5518750, -622500, -38500 (xmin, xmax, ymin, ymax)

crs: +proj=lcc +lat_0=42.5 +lon_0=-100 +lat_1=25 +lat_2=60 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs

The location I need to extract has lat and lon: (18.241755, -158.844437)
the problem is that I don’t know how to convert longlat into lcc.

I tried this answer, but it does not give correct answer.

Any help would be appreciated.


Solution

  • You might want to re-think your workflow, but to transform a single lat/lon point to projection described by provided proj-string, you could first create the point (note the order: X (longitude, easting), Y (latitude, northing)), convert it to something that's aware of CRS (like sfc), transform to target CRS and extract coordinates:

    library(sf)
    #> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
    
    st_point(c(-158.844437, 18.241755)) |>
      st_sfc(crs = "WGS84") |>
      st_transform("+proj=lcc +lat_0=42.5 +lon_0=-100 +lat_1=25 +lat_2=60 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs") |>
      st_coordinates()
    #>             X       Y
    #> [1,] -5954329 -453417
    

    Again, make sure you handle latitude / longitude and X / Y in the correct order when you deal with coordinates; you often need to swap those when moving between geographic coordinates (usually lat/lon) and projected coordinates (usually X,Y).

    Created on 2023-08-10 with reprex v2.0.2