2

I have loaded a shapefile and NetCDF into R, and I want to extract data based on the extent of the shapefile. I have done the following, but when I want to extract it I get NA's as the extent differs between the file:

library(rgdal)
library(sf)
library(raster)
library(ncdf4)

shp = readOGR("shapefile.shp")
net = brick("netCDF.nc")
extent(net)
extent(shp)

crs(shp)
crs(net)

net.mask = mask(net.brick, shp)
net.df = as.data.frame(net.mask[[1]], xy = TRUE)
head(temp.df)
> shp
class       : SpatialPolygonsDataFrame 
features    : 1 
extent      : -2939454, -1610437, 3261153, 4333895  (xmin, xmax, ymin, ymax)
crs         : +proj=longlat +datum=WGS84 +no_defs 
variables   : 8
names       : NA_L2CODE,              NA_L2NAME, NA_L1CODE, NA_L1NAME,                    NA_L2KEY, NA_L1KEY,    Shape_Leng,   Shape_Area 
value       :       3.1, ALASKA BOREAL INTERIOR,         3,     TAIGA, 3.1  ALASKA BOREAL INTERIOR, 3  TAIGA, 11989512.2816, 423471100811 

> crs(shp)
CRS arguments: +proj=longlat +datum=WGS84 +no_defs 
> crs(net)
CRS arguments: +proj=longlat +datum=WGS84 +no_defs 

> extent(net)
class      : Extent 
xmin       : -180.125 
xmax       : -51.875 
ymin       : 40.875 
ymax       : 90.125 

> extent(shp)
class      : Extent 
xmin       : -2939454 
xmax       : -1610437 
ymin       : 3261153 
ymax       : 4333895 

How can I change the extent of the shapefile so it gets latitude and longitude rather than X and Y coordinates?

EDIT: untitled

8
  • Which CRS is the shapefile in?
    – Erik
    May 3, 2021 at 12:07
  • They are actually both the following +proj=longlat +datum=WGS84 +no_defs. Otherwise, see edit. Thanks.
    – Thomas
    May 3, 2021 at 12:09
  • The CRS of the shapefile can't be WGS 84/EPSG 4326.
    – Erik
    May 3, 2021 at 12:12
  • No, a shapefile with extents outside +/-180,+/-90 is not +proj=longlat, so your shapefile has been corrupted by an incorrect projection. You need to specify the correct projection, the project one of the two into a common reference. Note that projection is not a simple "change of extent", but a complex math formula that alters every vertex.
    – Vince
    May 3, 2021 at 12:12
  • @Vince & @Erik I find it very strange. The shapefile I downloaded has the projection and extent I added in the question. It is the Level II Ecoregions of North America shapefile downloaded from here: epa.gov/eco-research/ecoregions-north-america. Can I somehow change that and then perform what I want?
    – Thomas
    May 3, 2021 at 12:31

1 Answer 1

3

Maybe this will help:

library(sf)
eco_l2 <- st_read("na_cec_eco_l2/NA_CEC_Eco_Level2.shp")
Reading layer `NA_CEC_Eco_Level2' from data source `/home/micha/work/tmp/na_cec_eco_l2/NA_CEC_Eco_Level2.shp' using driver `ESRI Shapefile'
Simple feature collection with 2261 features and 8 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -4334052 ymin: -3313739 xmax: 3324076 ymax: 4267265
Projected CRS: Sphere_ARC_INFO_Lambert_Azimuthal_Equal_Area

# Clearly NOT WGS84. Here's the correct projection of the original shapefile:

st_crs(eco_l2)
Coordinate Reference System:
  User input: Sphere_ARC_INFO_Lambert_Azimuthal_Equal_Area 
  wkt:
PROJCRS["Sphere_ARC_INFO_Lambert_Azimuthal_Equal_Area",
    BASEGEOGCRS["GCS_Sphere_ARC_INFO",
        DATUM["D_Sphere_ARC_INFO",
            ELLIPSOID["Sphere_ARC_INFO",6370997,0,
                LENGTHUNIT["metre",1,
                    ID["EPSG",9001]]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["Degree",0.0174532925199433]]],
    CONVERSION["unnamed",
        METHOD["Lambert Azimuthal Equal Area (Spherical)",
            ID["EPSG",1027]],
        PARAMETER["Latitude of natural origin",45,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",-100,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["False easting",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",0,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["(E)",east,
            ORDER[1],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]],
        AXIS["(N)",north,
            ORDER[2],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]]]

# Now just reproject to the projection of the netcdf raster
eco_l2_wgs84 <- st_transform(eco_l2, "EPSG:4326")

Now you should be able to do your extract.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.