gdal_translate error when cropping sentinel 1 image - r

I downloaded a Sentinel1 product from the esa open acces hub. In the downloaded directory is a tif-file containing the measurement data. I would now like to crop this tif-file to the window that I need.
For this I use the gdal_translate function in R
gdal_translate( 'from.tiff', to.tiff , projwin = c( 4.5, 52.4, 4.7, 52.2 ))
This returns an error
ERROR 1: Error: Computed -srcwin 4.5 52 0 0 has negative width and/or height.
I figured it might be a coordinate reference frame problem, but when I open the file in qgis it shows nice wgs84.
When I read in the file using
raster('from.tiff')
I notice that the raster extent is just the pixel dimensions. (As opposed to when I open it in Qgis)
How can I crop this 'from.tiff' file to the desired extent?

It seems you have mixed up your minimum and maximum latitude. The correct command would be:
gdal_translate( 'from.tiff', to.tiff , projwin = c( 4.5, 52.2, 4.7, 52.4 ))

Related

Resize raster for dimensions to match extent

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/

Find the width and density for each class.

So I have to find the density and the width for each of the following class. I have the solution but i am confused on something. I am confused on if the answer is correct or incorrect because some sources are saying uppperLimit - lowerLimit = class Width while some are saying it should be lowerLimit2 - lowerLimit1 = Class width. So please have a look at my data and solution and tell me if i am doing it correctly so i can proceed to find the density of it.
CLASS FREQUENCY
30.0-32.0 8
32.0-33.0 7
33.0-34.0 10
34.0-34.5 25
34.5-35.0 30
35.0-35.5 40
35.5-36.0 45
36.0-50.0 5
My Solution.
We first need to find the class boundaries. In this case, they are 30.0, 32.0, 33.0, 34.0, 34.5, 35.0, 35.5 and 36.0. The class widths are therefore c2 – c1 (i.e., 32.0 – 30.0 = 2.0)
So the class width should be --> 2.0, 1.0, 1.0, 0.5, 0.5, 0.5 and 14.0
Looks to me that you are doing it correctly -- the quantity you want in this case is the width of the bin, which is the distance from the lower bound to the upper bound.
More generally, what you need is the ordinary (Lebesgue) measure of the bin -- your density estimate is essentially comparing the observed mass (i.e. bin count) to the mass of the bin. This generalizes your example to other cases in a natural way. The Lebesgue measure of an interval is just the length of an interval, so that's the right thing whether the intervals touch each other (as in your example) or they don't touch at the endpoints (more generally). Also if you are working in two or more dimensions, the Lebesgue measure of the bin is its area or n-dimensional volume -- therefore in any dimensions, it's easy to know what you need to compute.

How to transfer specific colors to a raster in r

I wonder if it is possible to assign specific colors to the cells of a raster in r.
I know that “rasterize” in the package "raster" allows transferring values to a raster given the coordinates of the given value. However, I wonder if is possible to transfer just a color to the specific position of a cell?
In short, I have a given raster and an external database with three columns (x, y, and color). I would like to transfer the color in the third column to the cell in the raster at the x-y position.
This is the code that I have:
library(raster)
BaseRaster<-raster(xmn=1, xmx=4, ymn=1, ymx=4,resolution=1) #create an empty raster
Database<-data.frame(Xcols=c(1.5, 2.5,3.5, 1.5, 2.5,3.5, 1.5, 2.5,3.5) ,
Ycols= c(1.5, 1.5,1.5, 2.5, 2.5,2.5, 3.5, 3.5,3.5),
Cols=c("#FFF423","#FFFF14","#FFE20F","#FFF80A","#FFCB07","#FFE100","#FFEE0B","#FDEE0A","#FFE209"))
The question is how do I transfer the colors in the third column of the database to the raster, given the coordinates provided in columns 1 and 2 in the database?
Unfortunately, rasterize does not do the job
ColorRaster= rasterize(c(Database$Xcols, Database$Ycols), BaseRaster, Database $Cols)
Assuming BaseRaster is your raster object and database has the color the following should work:
plot(BaseRaster, col=colorRampPalette(database$color))(255)
for more information refer
plot{raster}

Png image crop, and plot

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.

Error using getNOAA.bathy, are there restrictions on coordinates values?

I am trying to execute getNOAA.bathy from package marmap.
I can successfully execute the following (from here):
library(marmap)
getNOAA.bathy(lon1=-20,lon2=-90,lat1=50,lat2=20, resolution=10) -> a
plot(a, image=TRUE, deep=-6000, shallow=0, step=1000)
However, when I execute the following:
getNOAA.bathy(lon1=-80,lon2=-79.833333,lat1=32.7,lat2=32.833333, resolution=10) -> a
plot(a, image=TRUE, deep=-6000, shallow=0, step=1000)
I get the error:
Error in getNOAA.bathy(lon1 = -80, lon2 = -79.833333, lat1 = 32.7, lat2 = 32.833333, : The NOAA server cannot be reached
Questions:
Are there special restrictions to LAT/LON values? Am I
miscalculating something here?
Are there "better" packages that can support my LAT/LON values?
As stated in the help file for getNOAA.bathy(), the resolution argument is expressed in minutes. So resolution=10 means that the cells of your grid will have a dimension of 10 minutes in longitude by 10 minutes in latitude. The bigger the number, the worse the resolution. So considering your region, you need to use the highest resolution possible for the ETOPO1 dataset (i.e. the database that's fetched by getNOAA.bathy()):
getNOAA.bathy(lon1=-80, lon2= 79.833333, lat1=32.7, lat2=32.833333, res=1)
That's definitely not hi-res (you get a grid of 80 values: 10 latitude values by 8 longitude values), but that's the maximum you can get with getNOAA.bathy().
If you need a higher resolution, you should try other sources such as the GEBCO database and the readGEBCO() function of marmap. You should also have a look at sections 2 and 3 of the marmap-ImportExport vignette where other sources are listed.
vignette("marmap-ImportExport")

Resources