How to adjust two maps with different extent in R - r

Two maps with different extents
I am using R to process two maps (raster package). I have two maps with both WGS84 coordinates see figure.
But one layer is from -180 to 180 in longitude (the map on surface) another one is from 0 to 360 (the map on the bottom).
I need to adjust the bottom layer to -180 to 180 so that they can match. How can I do that?
I think I need to find all grid greater than 180 and then minus 360. But I cannot find the longitude vector and cannot modify it from the Raster data.
Description of the map is:
class : RasterLayer
dimensions : 94, 192, 18048 (nrow, ncol, ncell)
resolution : 1.865234, 1.883872 (x, y)
extent : 0, 358.125, -88.542, 88.542 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : TMAX_2015_01_01
values : 220.9, 324.3 (min, max)

spTransform(ws_soil,CRS=CRS("+proj=longlat +ellps=WGS84 +pm=+360 +datum=WGS84 +no_defs"))
You need to play with "+pm=+360" or "+pm=-360"

Others told me a good function (raster::rotate)
Simply use CLIM_NEW<-rotate (CLIM_old). That will work.

Related

match coordinates and projection of a raster layer and shapefile in R

I have two spatial objects that I'm working with - a raster layer and a shapefile of my study region. I'm trying to crop my raster to my shapefile, but I'm having trouble getting the coordinate systems to match.
Here is a summary of my spatial objects:
Raster Layer
> testxx
class : RasterLayer
dimensions : 450, 3245, 1460250 (nrow, ncol, ncell)
resolution : 0.1109257, 0.3826826 (x, y)
extent : -179.9964, 179.9576, -89.98608, 82.22108 (xmin, xmax, ymin, ymax)
crs : NA
source : memory
names : layer
values : 5.389717e-06, 0.0002052335 (min, max)
Shapefile
class : SpatialPolygonsDataFrame
features : 1
extent : 5951759, 7591708, 649338.9, 2306575 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
variables : 8
names : PRUID, PRNAME, PRENAME, PRFNAME, PREABBR, PRFABBR, BUFF_DIST, ORIG_FID
value : 35, Ontario, Ontario, Ontario, Ont., Ont., 10000, 5
This is the code I tried:
# Add a coordinate system to the raster
crs(testxx) <- "+proj=longlat +datum=WGS84 +no_defs"
# change shapefile coordinate system
aoi <- spTransform(on_shp, crs(testxx))
But the coordinates themselves still don't match - the shapefile is still in meters, while the raster is in lat and long.
Thanks!
The coordinate reference system for the SpatialPolygonsDataFrame is clearly wrong. Given this extent:
extent : 5951759, 7591708, 649338.9, 2306575 (xmin, xmax, ymin, ymax)
It cannot be
crs : +proj=longlat +datum=WGS84 +no_defs
So you need to find out what it actually is. To test whether it is correct you could set it to what you think it is and then transform to lon/lat and see if ends up in the right place.

Converting .adf files into NetCDF

I have a set of .adf files which contain spatially distributed data. The files look like this:
dblbnd.adf
hdr.adf
metadata.xml
prj.adf
sta.adf
w001001.adf
w001001x.adf
I can read in the large file (w001001.adf) into R as a raster. This is the raster characteristics:
class : RasterLayer
dimensions : 1692, 3611, 6109812 (nrow, ncol, ncell)
resolution : 6, 6 (x, y)
extent : -10833.68, 10832.32, -4713.677, 5438.323 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : C:/Users/ahsa361/Documents/CEDS_Data/gridding-preprocessing/input/Corbett/w001001.adf
names : w001001
values : 0, 5637.337 (min, max)
I would like to convert this to NetCDF with dimensions of 180 deg lat. x 360 deg lon. at 0.5 deg resolution. How do I go about doing this?
You have an extent that does not match your coordinate reference system
#extent : -10833.68, 10832.32, -4713.677, 5438.323 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +no_defs
Without more information it not really possible to help you much. Most likely the crs is wrong and you need to replace it with the correct one. Did you set it to the wrong value? (that is the most common cause of this problem). If you know what it should be, you can set it like this
crs(x) <- "correct crs definition"
It is also possible that the extent is wrong, and you could also set that to the right value. But based on the what you say, you do not know, so you need to figure these things out before we can help with how to fix your problem.
To get a global extent, and a resolution close to 0.1 degrees, you can do
extent(x) <- c(-180,180,-90,90)
Given the number of rows and columns you have, that would get you:
library(raster)
raster(nrow=1692, ncol=3611)
#class : RasterLayer
#dimensions : 1692, 3611, 6109812 (nrow, ncol, ncell)
#resolution : 0.09969538, 0.106383 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +no_defs

R Raster Merge Changes Values

I have a series of GTiff images that I am trying to merge into a single larger extent. 6 small tiles need to be combined to generate my larger extent. My original 6 tiles have values which range from 0 to 255.
For example:
> tiff.list[[1]]
class : RasterLayer
dimensions : 1200, 1200, 1440000 (nrow, ncol, ncell)
resolution : 926.6254, 926.6254 (x, y)
extent : -10007555, -8895604, 2223901, 3335852 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs
data source : D:\Scratch\Data\MOD15A2.A2016153.h09v06.005.2016166083754.tif
names : MOD15A2.A2016153.h09v06.005.2016166083754
values : 0, 255 (min, max)
However, when merging the tiles using the code detailed here, I get a new image file and the values have changed:
> xx
class : RasterLayer
dimensions : 2400, 3600, 8640000 (nrow, ncol, ncell)
resolution : 926.6254, 926.6254 (x, y)
extent : -10007555, -6671703, 1111951, 3335852 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs
data source : D:\Scratch\Modis\A2016161.tif
names : A2016161
values : 0, 25 (min, max)
Does anyone know why this is happening? I've tried changing the file format and dataType ('INT1U') but it keeps happening. It's important the values don't change from 0 to 255 as the original data comes from NASAs MODIS satellite and certain values (i.e. 248-255) have specific fill values associated with them (for example, land cover assigned as water or snow). This change from a max value of 255 to 25 is removing important information from the original files.
Any assistance provided would be most welcome.
This suggests that these values are absent in the original files. The min and max values reported for the original files are based on the metadata provided therein. The metadata was likely wrong (showing the range of possible, not the actual values). To investigate do
setMinMax(tiff.list[[1]])
or
tiff.list[[1]] * 1

rasters with same crs, extent, dimension, resolution do not align

I am finding the average production days per year for maple syrup. My maple distribution data is in an ascii file. I have a raster (created from NetCDF files) called brick.Tmax. I want to match the specs of brick.Tmax to my maple distribution data.
## These are the specs I want to use for my maple distribution
brick.Tmax
class : RasterBrick
dimensions : 222, 462, 102564, 366 (nrow, ncol, ncell, nlayers)
resolution : 0.125, 0.125 (x, y)
extent : -124.75, -67, 25.125, 52.875 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : E:\all_files\gridded_obs.daily.Tmax.1980.nc
names : X1980.01.01, X1980.01.02, X1980.01.03, X1980.01.04, X1980.01.05, X1980.01.06, X1980.01.07, X1980.01.08, X1980.01.09, X1980.01.10, X1980.01.11, X1980.01.12, X1980.01.13, X1980.01.14, X1980.01.15, ...
Date : 1980-01-01, 1980-12-31 (min, max)
varname : Tmax
## reading in red maple data from ascii file into rasterLayer
red_raster <- raster("E:/all_files/Maple_Data/redmaple.asc")
red_raster
class : RasterLayer
dimensions : 140, 150, 21000 (nrow, ncol, ncell)
resolution : 20000, 20000 (x, y)
extent : -1793092, 1206908, -1650894, 1149106 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : E:\all_files\Maple_Data\redmaple.asc
names : redmaple
values : -2147483648, 2147483647 (min, max)
How can I project all specs (dimension, crs, resoluion, and extent) from brick.Tmax onto red_raster, while still preserving the values of red_raster? It seems like the two are mutually exclusive.
NOTE: In an attempt to streamline my question, I edited my question quite a bit from my original post, so apologies if the comments below are confusing in the current context. (I removed the raster prodavg_rastwhich was acting like a middleman).
The two rasters clearly do not have the same extent. In fact are in different universes (coordinate reference systems). brick.Tmax has angular (longitude/latitude) coordinates: +proj=longlat +datum=WGS84
but red_raster clearly does not given extent : -1793092, 1206908, -1650894, 1149106. So to use these data together, one of the two needs to be transformed (projected into the the coordinate reference system of the other). The problem is that we do not know what the the crs of red_raster is (esri ascii files do not store that information!). So you need to find out what it is from your data source, or by guessing giving the area covered and conventions. After you find out, you could do something like:
library(raster)
tmax <- raster(nrow=222, ncol=462, xmn=-124.75, xmx=-67, ymn=25.125, ymx=52.875, crs="+proj=longlat +datum=WGS84")
red <- raster(nrow=140, ncol=150, xmn=-1793092, xmx=1206908, ymn=-1650894, ymx=1149106, crs=NA)
crs(red) <- " ?????? "
redLL <- projectRaster(red, tmax)
Projectiong rasters takes time. A good way to test whether you figured out the crs would be to transform some polygons that can show whether things align.
library(rgdal)
states <- shapefile('states.shp')
sr <- spTransform(states, crs(red)
plot(red)
plot(sr, add=TRUE)

Rasterize spatialpolygons in R giving raster with NA values

I am having issues converting this spatialpolygondataframe to a raster. When I do ther conversion, the raster has NA as its values. As shown below:
DL3
[1]
class : SpatialPolygonsDataFrame
features : 126
extent : -15.04001, 46.1036, 3.759985, 31.71804 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +towgs84=0,0,0 +ellps=WGS84
variables : 1
names : LFRP
min values : 14
max values : 335.2
This is how I rasterize it:
##TO CONVERT TO RASTER
FunR<-function(r){
ext<-raster(extent(r))
crs(ext)<-crs(r)
D<-rasterize(r,ext,field=1,update=T)
D}
DL4<-lapply(DL3,FunR)
DL4
[1]
class : RasterLayer
dimensions : 45, 40, 1800 (nrow, ncol, ncell)
resolution : 1.52859, 0.6212901 (x, y)
extent : -15.04001, 46.1036, 3.759985, 31.71804 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +towgs84=0,0,0 +ellps=WGS84
data source : in memory
names : layer
values : NA, NA (min, max)
What can I be doing wrongly? I need help with a method to ensure the values in the dataframe reflect in the raster, please.
The principle works, as illustrated below.
library(raster)
SPP <- shapefile(system.file("external/lux.shp", package="raster"))
r <- raster(SPP, ncol=40, nrow=45)
SPP2 <- rasterize(SPP, r, "ID_1")
As this does not work for you, I assume that your polygons are very small relative to the raster cell size such that none of the cells is covered. Can you try with much smaller grid cells? If indeed the polygons are that small, it might make more sense to use their centroids (coordinates(SPP)) and rasterize these (as points).

Resources