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?
+proj=longlat +datum=WGS84 +no_defs
. Otherwise, see edit. Thanks.+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.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?