I have a list of Raster Objects, with different extents and dimensions (columns/rows), namely (0, 1400, 0, 2000) and (500,350). The resolution is 4 (dimensions * resolution equals extent). The data values in each raster are either NA or 1.
I would like to index in the object, but using its extent, and not dimensions (so index in the range 0-2000 instead of 0-500). So, I would like to change the raster dimensions to match the extent of the object. Adjusting the resolution does not seem to apply in this case.
I tried to use resample function after creating a new raster with the appropriate dimensions, but after resampling the new raster had correct dimensions but the extent and values (all NA) were messed up. I suppose the solution might be adjusting this approach
ref
a <- raster(nrow = ymax(ref), ncol = xmax(ref))
a <- resample(ref,a)
If the raster was 500x350 pixels before, I would like it to be 2000x1400. So I would guess that where 1 pixel was, there will be 4. And I would like those 4 pixels to all have the same value as the one before.
Any ideas would be appreciated!
I have attached an example raster: https://ucarecdn.com/7cc71657-c829-4a3c-b522-7024b7996efe/
Related
I am currently trying to calculate the distance a set of points and their closes raster cell of a certain value. So far I have tried to convert the raster file into Points, but I keep getting an error message:
Cannot allot Vector of this size. Is there any other way how I can go around this. My datasets are very large (20.000 Points and a raster layer of an entire country).
so far I have tried:
library(raster)
water_points <- rasterToPoints(land_cover , fun = function(x) {x == 405}) #405 are cells that contain water
then I would continue like this:
df$water_dist <- gDistance(df, water_points)
I have also tried to use rastertoPolygons but it seems to show the same problem
Thank you very much
When using the raster package, are resample's or projectRaster's bilinear interpolation always taking the four nearest centroids, or does it account for differences in cell sizes?
In the explanation for bilinear interpolation in resampling found at http://desktop.arcgis.com/en/arcmap/latest/extensions/spatial-analyst/performing-analysis/cell-size-and-resampling-in-analysis.htm, it says the four nearest centroids are used. In the example here, the cell sizes are similar. This explanation is not directly for the raster package in R, but it appeared to be the most accessible explanation for how bilinear interpolation might work in these cases.
Are a different amount of centroids used when the cell sizes differ by more than a factor of 2 in either (or both) dimensions?
For example, here is a raster with a higher resolution than what it is going to be resampled to:
library(raster)
R1 <- raster(resolution = 13, vals = sample(x = 1:5, size = 392, replace = TRUE))
plot(R1)
High Resolution Raster
Created here is the raster to be projectRaster to, and then R1 after projectRaster to match the new raster:
R2 <- raster(resolution = 50)
R3 <- projectRaster(from = R1, to = R2, method = "bilinear")
plot(R3)
New Low Resolution Raster
Which cells from the high resolution raster are being taken into consideration when projectRaster is being used to create the fewer, larger cells? Since the cells in the high resolution raster are more than four times as small as the cells in the low resolution one, if bilinear interpolation just used the four closest centroids, it may not be as accurately representative as it could be if more cells were taken into consideration.
Prior to the actual resampling with bilinear interpolation, the raster package first aggregates the values to create cells with an approximately equal resolution as the target. It is from this aggregated raster that the four cells are taken.
This is not done with projectRaster. Tis will be added in a future version, but for now you could first aggregate the input data yourself.
I would like to obtain the extent of raster layer conditional on certain cell values. Consider the following example:
raster1 is a large raster object, filled with values between 1 and 1000. However, I only want to obtain the extent for pixels with value 100. Since this subset of cells should crowd in a small region, the extent should be rather narrow. Once I know the coordinates of that box, I can crop this minor area.
My approach so far is to replace all values != 100 with NA - as suggested in related questions. Considering the raster object's overall size, this step takes an enormous amount of time and invests a lot of computational capacity in regions that I would like to crop anyways.
Does anyone know how to obtain the extent conditional on a certain pixel value which does not require to reclassify the entire object beforehand?
Here is an alternative way to do that
Example data:
library(raster)
r <- raster(ncol=18,nrow=18)
values(r) <- 1
r[39:45] <- 100
r[113:115] <- 100
r[200] <- 100
"Standard" way:
x <- r == 100
s <- trim(x, values=FALSE)
Alternate route by creating an extent:
xy <- rasterToPoints(r, function(x){ x ==100 })
e <- extent(xy[,1:2])
e <- alignExtent(e, r, snap='out')
v <- crop(r, e)
Either way, all cells need to be looked at, but at least you do not need to create another large raster.
Is there a simple way to crop a specified pixel location of a png image, and raster that image onto a plot.
Currently i am working with dissociated cells (up to 1000 cells in a field of view). Each cell has a center x (in pixels) and a center y (in pixels). We gather data in trace format, so i would like a way of displaying the picture of the cells next to the trace.
I have tried rasterImage, and grid.raster. Unfortunately i have no idea how to call upon the image array to specify the location.
Also, i do not want to use the zoom package since the functions work extremely slow and are not able to raster onto a current plot.
Take a look at the raster package. The raster::raster function, for importing or coercing your png and raster::crop for subsetting to a specific extent.
Here is an example from the crop functions help.
Create an example raster
r <- raster(nrow=45, ncol=90)
r[] <- 1:ncell(r)
Subset the raster based on a define extent
e <- extent(-160, 10, 30, 60)
rc <- crop(r, e)
Plot results
par(mfrow=c(1,2))
plot(r)
plot(rc)
It was alot easier than expected. My PNG image was an array with 3 layers.
img[xpos, ypos, layer]
Therefore i could simply specify the location of my cells increased by a factor,
rasterImage(img[(x-x*.2):(x+x*.2), (y-y*.2):(y+y*.2),],-2, 4, 3, 1)
Now i need to code around for cells at the limits of my plots.
At the moment I'm working with the raster package. I've different polygons with certain values (let's say 100), which I managed to rasterize. The problem is that when I rasterize each raster cell result with a value of 100, but I want the polygon value (100), to be equally divided per each cell overlaying the polygon. For example, if the polygon overlay 100 raster cells, I want each raster cell to have a value of 1, instead of 100. Could anyone help me?
Here the raster abd the shp I created: https://drive.google.com/drive/folders/0B6-UFgI67v99c3ZhUFp0eWpzOGM
I tried to do something like that:
ncell<-freq(union,digits=6)
ncell[,"value"]/ncell[,"count"]
new<-rep(c(union[,"value"],ncell[,"count"]))
union$new<-c(new)
but I cannot join the column I obtain in the raster associating the raster cells with the new values.
There are two ways I can think of:
compute the number you want for the polygons, before using rasterize
use freq as you did, but then use subs
for example:
r <- subs(union, data.frame(ncell))
x <- union / r