Unmatch raster and polygon data in R - 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.

Related

Calculate area in hectares for different land cover classes in a raster in R

How can I get the the total area in hectares of my land cover classes 1,2,3,4,5,6 given the following example:
class : RasterLayer
dimensions : 9257, 8348, 77277436 (nrow, ncol, ncell)
resolution : 25, 25 (x, y)
extent : 146600, 355300, 164575, 396000 (xmin, xmax, ymin, ymax)
crs : +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs
source : memory
names : Layer_5
values : 0, 6 (min, max)
I have tried:
nrow(change2) * ncol(change2) * xres(change2) * yres(change2)
But this gives me a different area result to when I use:
rgeos::gArea(area2)
on a polygon as the same area as the raster:
class : SpatialPolygonsDataFrame
features : 114
extent : 146598, 396590, 164568, 401725.8 (xmin, xmax, ymin, ymax)
crs : +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs
variables : 7
Many thanks for any assistance!
You can get the area of each target class using:
##area by class
targ_class = 1:6
area_class = table(change2[which(change2[]%in%targ_class)])*xres(change2)*yres(change2)/10000
area_class
And the total area of the target classes using:
##total area
sum(area_class)
The calculated area may still differ slightly from that calculated using a polygon.

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

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")

Loading multiple rasters in R results in some with attributes

I am trying to load multiple rasters (87 in total) into R for analysis. The rasters are being loaded direct to a stack using this code:
filelist <- list.files(path="mypath", pattern="tif$", full.names=TRUE)
stk1 <- raster::stack(filelist)
The data largely load correctly apart from 9 problematic bands that seem to have the same issue. These bad bands are loading with 'attributes'. See example below
goodband
> goodband
class : RasterLayer
dimensions : 1768, 3674, 6495632 (nrow, ncol, ncell)
resolution : 40, 40 (x, y)
extent : 135800.4, 282760.4, 22297.69, 93017.69 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs +ellps=airy +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894
data source : goodband.tif
names : Good
values : 0.5, 36 (min, max)
badband
> badband
class : RasterLayer
dimensions : 1768, 3674, 6495632 (nrow, ncol, ncell)
resolution : 40, 40 (x, y)
extent : 135800.4, 282760.4, 22297.69, 93017.69 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs +ellps=airy +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894
data source : badband.tif
names : Bad
values : 33, 6824 (min, max)
attributes :
ID OID Value Count
from: 0 0 33 433
to : 529 529 6824 1307
As you can see the badband has additional 'attributes' at the bottom. When trying to analyse bad bands they are just full of null values and don't operate like the good bands.
Could you advise how best to fix this problem?

"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