Extracting raster pixels in qGIS - raster

I have a raster file with two bands and a vector shapefile. I used raster extraction by mask and created the desired area. Now I want to calculate the number of pixels for Band 1 and Band 2 of the new clipped mask file. What would be the best way to do that?

Related

Determining landcover %'s (polygon layer) that is not within burned areas (raster layer)

I am trying to extract information on the % of different vegetation cover types within areas that have not been burned. However, the data I was given for the park includes areas that have been burned as a raster file in NAD_83 projection, and the vegetation cover as a polygon layer in WGS_84 projection. Essentially, I'm trying to erase the overlap between areas that have been burned and the vegetation layer to only look at vegetation cover types in areas that have not been burned. The vegetation is broken into 12 class labels based on understory/shrub cover type, and I want a percentage of how much each cover type occurs in unburned area. Can anyone help with this transformation?
I have access to ArcGIS Desktop v 10.8.2 with an Advanced licence.
I have tried converting the raster to points which worked, but I don't know how to make the points into polygons that perfectly match the size/shape of the original raster. I don't know another way to "erase" the two layers other than by trying to convert the raster to a polygon with the same coordinate system.
ArcMap is probably reprojecting the data on the fly, but you could start by transforming the raster and vector layers to a common datum.
If I understand correct, you want to know the area of veg types represented as polygons that are within unburned areas which are represented as pixels in a raster.
If that is correct, I would convert the unburned areas to a polygon layer. It might help to convert the burn raster such that unburned = 1 all else = 0 or NA, then convert unburned to a polygon.
With the unburned areas represented as polygons, intersect this layer with the veg layer. Doing so will cut the veg layer by the unburn polygon layer. The result should allow you to only select polygons that intersected with the unburned layer once it is vectorized. Get the area of these polygons grouped by veg type.

How to calculate area of shaded polygon on map in r?

I generate a raster map in R with some shaded portion, then i plot my shape file on the raster file to show boundaries of the map. I can calculate the the overall shaded area with a code but I want to calculate the shaded region coming under the separate polygons when i plot shape file on raster. Please help me with the code.
I am using maxent in R to have an idea of suitable area of certain crop for whole country. when I generate map, it is a raster file and I can calculate suitable area for whole country with a code, but I want to calculate the area for provinces as well for which i plot province vise shape file on the raster map.
I want help with the area calculation for each shaded polygon when i plot shape file on raster
pred_me2 [pred_me2 <=0.33] <- NA
pred_me2 [pred_me2 >0.66] <- NA
cell_size<-area (pred_me2, na.rm=TRUE, weights=FALSE)
cell_size<-cell_size[!is.na (cell_size)]
suitable<-length (cell_size)*median(cell_size)
You can try with this:
cell_size <- xres(pred_me2)*yres(pred_me2)
area_NA<- sum(is.na(values(pred_me2))) * cell_size
area_non_NA <- sum(!is.na(values(pred_me2))) * cell_size

Query raster brick layer based on another raster in R

I have a NetCDF file of global oceanographic (OmegaA) data at relatively coarse spatial resolution with 33 depth levels. I also have a global bathymetry raster at much finer resolution. My goal is to use get the seabed OmegaA data from the NetCDF file, using the bathymetry data to determine the desired depth. My code so far;
library(raster)
library(rgdal)
library(ncdf4)
# Aragonite data. Defaults to CRS WGS84
ncin <- nc_open("C:/..../GLODAPv2.2016b.OmegaA.nc")
ncin.depth <- ncvar_get(ncin, "Depth")# 33 depth levels
omegaA.brk <- brick("C:/.../GLODAPv2.2016b.OmegaA.nc")
omegaA.brk <-rotate(omegaA.bkr)# because netCDF is in Lon 0-360.
# depth raster. CRS WGS84
r<-raster("C:/....GEBCO.tif")
# resample the raster brick to the resolution that matches the bathymetry raster
omegaA.brk <-resample(omegaA.brk, r, method="bilinear")
# create blank final raster
omegaA.rast <- raster(ncol = r#ncols, nrow = r#nrows)
extent(omegaA.rast) <- extent(r)
omegaA.rast[] <- NA_real_
# create vector of indices of desired depth values
depth.values<-getValues(r)
depth.values.index<-which(!is.na(depth.values))
# loop to find appropriate raster brick layer, and extract the value at the desired index, and insert into blank raster
for (p in depth.values.index) {
dep.index <-which(abs(ncin.depth+depth.values[p]) == min(abs(ncin.depth+depth.values[p]))) ## this sometimes results in multiple levels being selected
brk.level <-omegaA.brk[[dep.index]] # can be more than on level if multiple layers selected above.
omegaA.rast[p] <-omegaA.brk[[1]][p] ## here I choose the first level if multiple levels have been selected above
print(paste(p, "of", length(depth.values.index))) # counter to look at progress.
}
The problem: The result is a raster with massive gaps (NAs) in it where there should be data. The gaps often take a distinctive shape - eg, follow a contour, or along a long straight line. I've pasted a cropped example.
enter image description here
I think this could be because either 1) for some reason the 'which' statement in the loop is not finding a match or 2) a misalignment of the projections is created which I've read can happen when using 'Rotate'.
I've tried to make sure all the extents, resolutions, number of cells, and CRS's are all the same, which they seem to be.
To speed up the process I've cropped the global brick and bathy raster to my area of interest, again checking that all the spatial resolutions, etc etc match - I've not included those steps here for simplicity.
At a loss. Any help welcome!
Without a reproducible example, this kind of problems is hard to solve. I can't tell where your problem is but I'll present to you the approach I would try. Maybe it's good, maybe it's bad, I don't know but it may inspire you to find a way to go around your problem.
To my understanding, you have a brick of OmegaA (33 layers/depth) and a bathymetry raster. You want to get the OmegaA value at the bottom of the sea. Here is how I would do:
Make OmegaA raster to the same resolution and extent to the bathymetry one
Transforme the bathymetry raster into a raster brick of 33 three layers of 0-1. e.g. If the sea bottom is at 200m for one particular pixel, than this pixel on all depth layer other than 200 is 0 and 1 for the 200. To program this, I would go the long way, something like
:
r_1 <- r
values(r_1) <- values(r)==10 # where 10 is the depth (it could be a range with < or >)
r_2 <- r
values(r_2) <- values(r)==20
...
r_33 <- r
values(r_33) <- values(r)==250
r_brick <- brick(r_1, r_2, ..., r_33)
then you multiple both your raster bricks. They have the same dimension, it should be easy. The output should be a raster brick of 33 layers with 0 everywhere where it isn't the bottom of the sea and the value of OmegaA anywhere else.
Combine all the layer of the brick obtained previously into a simple raster with a sum.
This should work. If you have problem with dealing with raster brick, you could make the data into base R arrays, it could be simpler.
Good luck.

R Converting contour lines to elevation plot

I would like to be able to create an elevation plot from contour lines in R. I am very new to using shape files
At the moment I have downloaded data from here
which provides .shp files for all of the UK.
It also provides the contour lines, summarising the topology of the UK.
For the elevation plot I would like a data.frame or data.table of evenly spaced points (100m apart from each other) to produce a data output giving an x, y and z value. Where x and y represent the latitude and longitude (or Eastings and Northings), and z represent the height (in meters above sea-level).
I think there are probably some tools that will automatically carry out the interpolation for you, but am unsure how it would work with geo-spatial data.
This is my basic start...
require(maptools)
xx <- readShapeSpatial("HP40_line.shp")
Choose "ASCII Grid and GML (Grid)" as download format for the "OS Terrain 50" product, and download the file. This will give you a zip file containing many directories of zip files, each of which contains portions of a 50 m elevation grid of the UK (the portion I looked at had 200 x 200 cells, meaning 10 km x 10 km). I went into the directory data/su, unzipped the zip file there, and did
library(raster)
r = raster("SU99.asc")
plot(r)
to aggregate this to a 100 m grid, I did
r100 = aggregate(r) # default is factor 2: 50 -> 100 m
As mentioned above, the advice is to work on the grids as contour lines are derived from grids, working the other way around is a painful and a great loss of information.
Getting grid values in longitude latitude as a data.frame can be done in two ways:
df = as.data.frame(projectRaster(r, crs = CRS("+proj=longlat")), xy = TRUE)
unprojects the grid to a new grid in longitude / latitude. As these grids cannot coincide, it minimally moves points (see ?projectRaster).
The second option is to convert the grid to points, and unproject these to longitude latitude, by
df2 = as.data.frame(spTransform(as(r, "SpatialPointsDataFrame"), CRS("+proj=longlat")))
This does not move points, and as a consequence does not result in a grid.

polygon to raster with raster as cell size

I have a shapefile and a raster file which I want to merge. Normally I would Extract by mask the rasterfile to obtain delineation of the shapefile. But now the shapefile has sub-delineation inside which I want to keep. One way to do this is Polygon to raster where I choose the raster-file inside cell size. However when I do this the right uppper coordiante is off the chart with values of 10^4 for a WGS 1984 decimal degree georeference. The projection of the two files do not seem to make any difference.
ArcGis 10.1

Resources