How to change number of layers of a raster brick - r

When I load a tiff image in R as raster brick then I have the expected number of pixels (number rows and columns) when I check afterwards the details of the image. But I expect 44 layers. I think I can change the number of layers with the nlayers function. Unfortunately, this does not work. The number of layers remain at 4.
sample_brick = brick("file_example_TIFF_1MB.tiff", nlayers = 44)
Can someone help me how to adapt the number of layers?
Here you can find a example tiff image.
https://file-examples.com/wp-content/uploads/2017/10/file_example_TIFF_1MB.tiff

The brick function creates a RasterBrick that has all the layers that are in your file.
Your expectation is probably wrong.
You cannot add imaginary layers when creating an object from file.

Related

raster:: extract produces an empty list

I am trying to extract the values of pixels in a DSM(CHM) within digitized tree crowns.
first I set my working directory read in the shapefile and raster.
TreeCrowns <-shapefile("plot1sag_shape/plot1sag.shp")
CHM <- raster('272280split4.tif')
Then I try to extract the pixel values
pixel <- raster::extract(CHM, TreeCrowns, method= 'simple', weights=FALSE, fun=NULL)
But I get an empty list with all NULL values for every polygon. I have confirmed that the CHM and polygons are in the same location. What can I do to fix this?
Since your shapefile consists of polygon, the extract() function need to know how to summarise the pixel values across a polygon via the fun= argument. Since you provide fun=NULL, the function interpret as returning NULL values to summarise the pixel values.
Try fun=mean or fun=sum (and they mean different thing so see which one suits you).
That probably happens because the polygons and the raster do not overlap. Can you show(CHM) and TreeCrowns? Have you looked at
plot(CHM)
lines(TreeCrowns)
Or are your polygons very small relative to the raster cells? In that case try argument small=TRUE

Calculating the percentage of the area that a polygon covers a cell in a raster in QGIS

I'm having trouble with using QGIS.
I have a GRIB file containing meteorlogical data, this is loaded in QGIS as rasterdata if I'm not mistaken.
I also have a shape-file containing a polygon, describing regions within the area covered in the GRIB file.
What I need to know, is how many % of a raster-cell is covered by a region within the polygon. I'm using QGIS.
What I have done so far, is I rasterized the polygon, and used zonal statistics to calculate the SUM and COUNT on the shape-file, and then used the field calculator to calculate the percentage covered. This does not seem to provide the result I hoped for.
Can anyone push me in the right direction?

Create stage height raster using least cost path and r

I have a point shapefile of Station IDs and stageheights. I would like to create a raster where each cell has the stage height value (in meters) of the closest in situ station to that cell.
I want this raster to match up with another raster. So I would like it if I could input both a raster I have created (dataset 3 described below) and my point shapefile (1).
Datasets:
1) Point Shapefile with stage heights of a river delta
2) Shapefile of the river delta extent
3) Raster of the delta where NA's represent land (could also have them be zero's if need be) and 1's are water. Two datasets 10 meter resolution and 30 meter resolution.
One conceptual issue I am having is with the amount of small streams I have.
For example (pictured in image below), station 1 (circled in blue) is technically closer to the black x region than station 2 (circled in red), but the stage height value in red is more representative of point x. There are NA's in between the two streams, does that mean that the value will not jump across streams?
How can I reassign the values in my Raster (all the 1's) to the stage height of the nearest station and make sure that these values are not jumping from stream to stream? Do I need to use least cost path? What is the best way to do this?
I would like to use R, but can use ArcMap if I must.
So I'm not sure what tools you have available to you but I think this answer may be useful:
Calculating attribute for network distance between multiple points in ArcGIS Desktop?
Here the questioner was looking to calculate distances on roads to some points, but your problem seems similar. I think the main point I would make here is that you should do your network distance classification prior to worrying about the raster layer. You may have to convert from polygon to lines or some workaround to get your data into a format that works, but this is the kind of job the tool is designed to do.
After you have reclassified your river shapefile based on their network distance to a given point, then convert the polygons to raster and use this to classify your original raster. You could do this in R or Arcmap. Arcmap will probably be faster.

Determining the size of grid cells in a raster

Is there a good method for determining the size of individual grid cells in a RasterLayer? I can access the resolution but I need the actual size in km^2. I've tried using the area() with my raster but it's not finding the window. I've also tried creating a SpatialGridDataFrame from my raster but have had no luck computing the area of each individual grid cell. Any help would be appreciated.
The cells size is the product of the x and y resolution prod(res(x)) if you have a planar coordinate reference system. Otherwise, if your crs is longitude/latitude, cell size will change with latitude, and you can indeed use the area function to get the size of each cell.
I take it that "it's not finding the window." refers to this error message Error in as.owin.default(w) : Can't interpret W as a window.
This is a name conflict with spatstat. So either do not load spatstat or call the raster function explicitly raster::area(x).

Finding the image boundary

While I use R quite a bit, just started an image analysis project and I am using the EBImage package. I need to collect a lot of data from circular/elliptical images. The built-in function computeFeatures gives the maximum and minimum radius. But I need all of the radii it computes.
Here is the code. I have read the image, thresholded and filled.
actual.image = readImage("xxxx")
image = actual.image[,2070:4000]
image1 = thresh(image)
image1 = fillHull(image1)
As there are several objects in the image, I used the following to label
image1 = bwlabel(image1)
I generated features using the built in function
features = data.frame(computeFeatures(image1,image))
Now, computeFeatures gives max radius and min radius. I need all the radii of all the objects it has computed for my analysis. At least if I get the coordinates of boundaries of all objects, I can compute the radii through some other code.
I know images are stored as matrices and can come up with a convoluted way to find the boundaries and then compute radii. But, was wondering if there a more elegant method?
You could try extracting each object + some padding, and plotting the x and y axis intensity profiles for each object. The intensity profiles is simply the sum of rows / columns which can be computed using rowSums and colSums in R
Then you could find where it dropps by splitting each intensity profiles in half and computing the nearest minimum value.
Maybe an example would help clear things up:
Hopefully this makes sense

Resources