mapview and plot show different output for the same rasterLayer, why? - r

Below is the rasterLayer RASTER_slope in a plot (4 NAs are shown in white):
Here is the metadata of RASTER_slope
class : RasterLayer
dimensions : 4, 4, 16 (nrow, ncol, ncell)
resolution : 500, 500 (x, y)
extent : 2227000, 2229000, 1316500, 1318500 (xmin, xmax, ymin, ymax)
crs : +proj=lcc +lat_0=12 +lon_0=-102 +lat_1=17.5 +lat_2=29.5 +x_0=2500000 +y_0=0 +datum=WGS84 +units=m +no_defs
source : memory
names : values
values : -143.2145, 214.1981 (min, max)
Then I used mapview to get a dynamic visualization of RASTER_slope:
> mapview(RASTER_slope)
Warning messages:
1: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=#null +wktext +no_defs +type=crs
2: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
Discarded datum World Geodetic System 1984 in Proj4 definition
3: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=#null +wktext +no_defs +type=crs
4: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
Discarded datum World Geodetic System 1984 in Proj4 definition
The resulting mapview object looks like this:
The mapview object is displaying only 2 pixels with NA.
Probably I am wrong but I have the feeling that the key here is to pass a crs to RASTER_slope in a WKT format but I do not know how to do that.
Any help will be appreciated.

Following #dww, the key is in adding method="ngb" when calling mapview, no need to re project the rasterLayer, though. Here the code and output
mapview(RASTER_slope, method="ngb", na.color="transparent")

Related

how do I read a shapefile into r in a format that leaflet can use

I'm trying to do some leaflet plots with San Francisco precinct shapefiles. It's worked before, but the precincts and their shapefiles have been updated & I can't seem to load them in a form that leaflet recognizes.
If I use st_read, the shapefile seems to load okay, but leaflet objects. If I do
sfprecincts <- readOGR(dsn=".",layer = "SF_DOE_PREC_2022_07_18_pg"), I get this warning message:
Warning messages:
1: In OGRSpatialRef(dsn, layer, morphFromESRI = morphFromESRI, dumpSRS = dumpSRS, :
Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=#null +wktext +no_defs
2: In OGRSpatialRef(dsn, layer, morphFromESRI = morphFromESRI, dumpSRS = dumpSRS, :
Discarded datum WGS_1984 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=#null +wktext +no_defs
3: In showSRID(wkt2, "PROJ") :
Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=#null +wktext +no_defs +type=crs
4: In showSRID(wkt2, "PROJ") :
Discarded datum World Geodetic System 1984 in Proj4 definition

Trouble reading a netcdf file and convert it into a SpatRaster

I am struggling to open a NetCDF file and convert it into a raster using R. The
data is supposed to be on a regular grid of 25 km by 25 km. It contains sea
ice concentration in the Arctic.
library(terra)
#> terra 1.5.21
library(ncdf4)
file <- "~/Downloads/data_sat_Phil_changt_grid/SIC_SMMR_month_2015.nc"
I am getting a warning about the extent not found.
r <- rast(file)
#> Error in R_nc4_open: No such file or directory
#> Warning: [rast] GDAL did not find an extent. Cells not equally spaced?
We can see that there is a problem with the coordinates/extent.
r
#> class : SpatRaster
#> dimensions : 448, 304, 12 (nrow, ncol, nlyr)
#> resolution : 0.003289474, 0.002232143 (x, y)
#> extent : 0, 1, 0, 1 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84
#> source : SIC_SMMR_month_2015.nc:sic
#> varname : sic
#> names : sic_1, sic_2, sic_3, sic_4, sic_5, sic_6, ...
I can open the nc file with nc_open() and I see that the coordinates are present.
nc <- nc_open(file)
names(nc$var)
#> [1] "lat" "lon" "sic"
lat <- ncvar_get(nc, "lat")
lon <- ncvar_get(nc, "lon")
dim(lat)
#> [1] 304 448
dim(lon)
#> [1] 304 448
dim(r)
#> [1] 448 304 12
Is it possible to assemble this data (the SIC values and the coordinates) to create a SpatRaster?
The nc file can be downloaded here: https://easyupload.io/pfth0s
Created on 2022-05-20 by the reprex package (v2.0.1)
The data are gridded, but the file does not specify the coordinates, nor the coordinate reference system. The file specifies the lon/lat values associated with the cells, but does not help us much, as these are clearly not on a regular grid. That is easy to see from plot(r)
NAflag(r) = -9999
plot(r,1)
And also from
p = cbind(as.vector(lon), as.vector(lat))
plot(p, cex=.1, xlab="lon", ylab="lat")
So what you need to find out, is which coordinate reference system (crs) is used, clearly some kind of polar crs. And what the extent of the data set is.
From the website you point to, I take it we can use:
crs(r) = "+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +k=1 +x_0=0 +y_0=0 +a=6378273 +b=6356889.449 +units=m +no_defs"
ext(r) = c(-3850000, 3750000, -5350000, 5850000)
r
#class : SpatRaster
#dimensions : 448, 304, 12 (nrow, ncol, nlyr)
#resolution : 25000, 25000 (x, y)
#extent : -3850000, 3750000, -5350000, 5850000 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +x_0=0 +y_0=0 +a=6378273 +b=6356889.449 +units=m +no_defs
#source : SIC_SMMR_month_2015.nc:sic
#varname : sic
#names : sic_1, sic_2, sic_3, sic_4, sic_5, sic_6, ...
The results look good:
g = geodata::gadm("Greenland", level=0, path=".")
gg = project(g, crs(r))
plot(r,1)
lines(gg)
But this is of course not a good way to do such things; the ncdf file should have contained all the metadata required.

Unmatch raster and polygon data in R

The polygons (read in from a shapefile) are :
class : SpatialPolygonsDataFrame
features : 653
extent : -7115213, 4895580, 1368240, 7805331 (xmin, xmax, ymin,
ymax)
coord. ref. : +proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154
+x_0=0 +y_0=0 +datum=NAD27 +units=us-ft +no_defs +ellps=clrk66
+nadgrids=#conus,#alaska,#ntv2_0.gsb,#ntv1_can.dat
variables : 6
names : cat, NAME, AREA_MI, lccount, lcsum,
lcmean
min values : 1, Alaska, 1.006402e+00, 1.000000e+01, 0.000000e+00,
0.00000000
max values : 99, Alaska, 9.945810e-01, 9.900000e+01, 9.960000e+02,
12.00000000
The raster (elevation) data is
class : RasterLayer
dimensions : 6800, 9200, 62560000 (nrow, ncol, ncell)
resolution : 1305.521, 946.6311 (x, y)
extent : -7115213, 4895580, 1368240, 7805331 (xmin, xmax, ymin,
ymax)
coord. ref. : +proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154
+x_0=0 +y_0=0 +datum=NAD27 +units=us-ft +no_defs +ellps=clrk66
+nadgrids=#conus,#alaska,#ntv2_0.gsb,#ntv1_can.dat
data source : /Users/hong/Documents/GitHub/Alaska/akshd300m.tif
names : akshd300m
values : 0, 255 (min, max)
Does anyone how to solve this? I use tmap to plot this figure
Although the coordinate reference system is reported as +proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154 +x_0=0 +y_0=0 +datum=NAD27 +units=us-ft +no_defs +ellps=clrk66 for both data sets, clearly that cannot be true. It should be easy to figure out which one is correct by transforming known polygons to that crs. For example
library(raster)
library(rgdal)
usa <- getData('GADM', country='USA', level=1)
ak <- usa[usa$NAME_1 == 'Alaska', ]
ak_asa <- spTransform(ak, '+proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154 +x_0=0 +y_0=0 +datum=NAD27 +units=us-ft +no_defs +ellps=clrk66')
And plotting that one as well. Having established which one is correct (hopefully one of the two is), you can then try to trace back what went wrong with the other one. With the information you have provided we cannot figure that out for you.

"Failure during raster IO" when converting raster to data.frame?

Anyone have any idea what may be happening? Here is my console printout. What's weird is that the first four as.data.frame lines run without issue but the last one gives me the "Failure during raster IO" error.
library("raster", lib.loc="~/R/win-library/3.3")
library("rgdal", lib.loc="~/R/win-library/3.3")
#> rgdal: version: 1.1-10, (SVN revision 622)
#> Geospatial Data Abstraction Library extensions to R successfully loaded
#> Loaded GDAL runtime: GDAL 2.0.1, released 2015/09/15
#> Path to GDAL shared files: C:/Users/JV-Desktop/Documents/R/win-library/3.3/rgdal/gdal
#> Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
#> Path to PROJ.4 shared files: C:/Users/JV-Desktop/Documents/R/win-library/3.3/rgdal/proj
liMax <- as.data.frame(raster("D:/LiDAR/LiDAR/30_m/m_30_max.tif"), xy=TRUE)
CanopyBase <- as.data.frame(raster("D:/LiDAR/LiDAR/30_m/m_30_avg.tif") - raster("D:/LiDAR/LiDAR/30_m/m_30_std.tif"), xy=TRUE)
rawData.li <- merge(liMax,CanopyBase)
processed.li <- rawData.li[complete.cases(rawData.li),]
satMax <- as.data.frame(stack("D:/LiDAR/Landsat/For_Joe/Max_2015.tif"), xy=TRUE)
satMed <- as.data.frame(stack("D:/LiDAR/Landsat/For_Joe/Med_2015.tif"), xy=TRUE)
#> Error: Failure during raster IO
In case it helps here is the printout for the info on the last two files:
stack("D:/LiDAR/Landsat/For_Joe/Max_2015.tif")
# class : RasterStack
# dimensions : 5640, 8763, 49423320, 7 (nrow, ncol, ncell, nlayers)
# resolution : 30, 30 (x, y)
# extent : -1491300, -1228410, 2709840, 2879040 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
# names : Max_2015.1, Max_2015.2, Max_2015.3, Max_2015.4, Max_2015.5, Max_2015.6, Max_2015.7
stack("D:/LiDAR/Landsat/For_Joe/Med_2015.tif")
# class : RasterStack
# dimensions : 5640, 8763, 49423320, 7 (nrow, ncol, ncell, nlayers)
# resolution : 30, 30 (x, y)
# extent : -1491300, -1228410, 2709840, 2879040 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
# names : Med_2015.1, Med_2015.2, Med_2015.3, Med_2015.4, Med_2015.5, Med_2015.6, Med_2015.7
# min values : -0.3208517, -0.5767694, -0.5474564, ?, ?, ?, ?
# max values : 1, 1, 1, ?, ?, ?, ?
I hope this is enough info and I am asking the question correctly. I am new to stackoverflow and relatively new to R and coding in general. Thanks for any insight you can give.

Layer won't rasterize using raster package in r

I'm having trouble rasterizing a shapefile using the raster package in R.
shp<-shapefile(ZoneShape);
lcRas<-raster(lcRaster);
r<-raster(ncol=ncol(lcRas), nrow=nrow(lcRas), crs=CRS);
res(r)<-res(lcRas);
extent(r)<-extent(lcRas);
>r
class : RasterLayer
dimensions : 22610, 27959, 632152990 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 554739, 582698, 3837197, 3859807 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=15 +ellps=GRS80 +datum=NAD83 +units=m +no_defs +towgs84=0,0,0
shp$GID<-1:nrow(shp);
> shp
class : SpatialPolygonsDataFrame
nfeatures : 1
extent : 554838, 582597.6, 3837297, 3859707 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=15 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0
nvariables : 14
names : SP_ID, NAME, Shape_Leng, Shape_Area, GID
>zoneRas<-rasterize(shp, r, "GID")
The following error is returned:
trying to get slot "coords" from an object of a basic class ("NULL") with no slots.
Can anyone see what I'm missing/screwing up here?
Thanks
This is a bug that happens when the SpatialPolygonsDataFrame object only has a single geometry. Here is a work-around:
zoneRas <- rasterize(shp, r, shp$GID)

Resources