I am trying to align two raster grids in R. Once aligned I would like to be able to add them together.
I have tried to check whether making a stack would work:
grid_snap <- stack(GLC2000_sdw, afriPop_sdw)
And I get the following error:
Error in compareRaster(x) : different extent
The raster grids have the following properties:
show(habi_sdw)
# class : RasterLayer
# dimensions : 9187, 9717, 89270079 (nrow, ncol, ncell)
# resolution : 0.00892857, 0.00892857 (x, y)
# extent : -28.83706, 57.92186, -36.02464, 46.00214 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs
# data source : C:\Users\di39\AppData\Local\Temp\R_raster_di39\raster_tmp_2015-08-12_172902_12860_17067.grd
# names : layer
# values : 0, 333707.6 (min, max)
show(Pop_sdw)
# class : RasterLayer
# dimensions : 10143, 8858, 89846694 (nrow, ncol, ncell)
# resolution : 0.008333333, 0.008333333 (x, y)
# extent : -17.53524, 56.28143, -46.97893, 37.54607 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
# data source : C:\Users\di39\AppData\Local\Temp\R_raster_di39\raster_tmp_2015-08-12_170421_12860_12760.grd
# names : pop2010ppp
# values : 0, 128925.9 (min, max)
Using alignExtent() in the raster package seems not to be the correct approach.
Do I need to resample because the resolutions are slightly different?
(0.00892857 x 0.00892857) vs (0.008333333 vs 0.008333333)
First use resample(GLC2000_sdw, afriPop_sdw, method="ngb") and then crop (GLC2000_sdw, afriPop_sdw) to make sure they have the same extent.
Related
I have SpatialPointsDataFrame object loaded in R with with extent in meters. I want them to convert into degree decimals. Please help how can I do that.
Below is how my data looks in console:
class : SpatialPointsDataFrame
features : 23
extent : 351912.5, 457807, 3236835, 3367232 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=44 +datum=WGS84 +units=m +no_defs
variables : 1
names : Id
min values : 0
max values : 0
I want to convert them as my rasterStack object which has extent as below:
class : RasterStack
dimensions : 7554, 8341, 63007914, 2 (nrow, ncol, ncell, nlayers)
resolution : 0.0002777778, 0.0002777778 (x, y)
extent : 78.72589, 81.04284, 28.70956, 30.80789 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
names : dem_kumaun1, slope
min values : -32768, 0
max values : 32767.00000, 88.75196
please help guys!
Use spTransform function
new_shp <- spTransform(old_shp, CRS("+proj=longlat +datum=WGS84"))
Following code worked fro me.
transformed_shapefile <- spTransform(SpatialPointsDataFrame , CRS(RasterStack#crs#projargs))
I'm trying the following procedure (I apologize for mistakes but it is the very first time here). I've the raster dtm.temp expressed as follows:
> dtm.temp
class : RasterLayer
dimensions : 668, 965, 644620 (nrow, ncol, ncell)
resolution : 64.9, 92.6 (x, y)
extent : 437230.1, 499858.6, 5138842, 5200699 (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:32632 +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : elev
values : 362.8404, 3584.865 (min, max)
Using this extent I convert it in longlat in the following way
# I build a spatial dataframe with extent data from dtm.temp
df <- data.frame(ID = 1:2, X = c(dtm.temp#extent#xmin, dtm.temp#extent#xmax),
Y = c(dtm.temp#extent#ymin, dtm.temp#extent#ymax))
coordinates(df) <- c("X", "Y")
crs_text <- crs(dtm.temp, asText=TRUE) # extracting crs from dtm.temp
proj4string(df) <- CRS(crs_text)
ext.lonlat <- spTransform(df, CRS("+proj=longlat +datum=WGS84"))
ext.lonlat
> ext.lonlat
class : SpatialPointsDataFrame
features : 2
extent : 8.183449, 8.998142, 46.40024, 46.95982 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
variables : 1
names : ID
min values : 1
max values : 2
at this point I use the extent (expressed as longlat) to crop the following raster dtm.temp1 (always in longlat but with a different initial extent and higher resolution)
> dtm.temp1
class : RasterLayer
dimensions : 9956, 14656, 145915136 (nrow, ncol, ncell)
resolution : 0.0002777778, 0.0002777778 (x, y)
extent : 7.857917, 11.92903, 44.27042, 47.03597 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : elev
values : -18, 4543 (min, max)
then
dtm.temp1.crop <- crop(dtm.temp1, ext.lonlat)
> dtm.temp1.crop
class : RasterLayer
dimensions : 2015, 2933, 5909995 (nrow, ncol, ncell)
resolution : 0.0002777779, 0.0002777772 (x, y)
extent : 8.183473, 8.998195, 46.40014, 46.95986 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
and then I project again in UTM the raster cropped at the same extent (or at least, so I believed..)
# crs_text as defined above
dtm.temp1.crop.proj <- projectRaster(dtm.temp1.crop, crs=crs_text)
>dtm.temp1.crop.proj
class : RasterLayer
dimensions : 2033, 2968, 6033944 (nrow, ncol, ncell)
resolution : 21.2, 30.9 (x, y)
extent : 437083.4, 500005, 5138362, 5201182 (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:32632 +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : elev
values : 360, 3603.655 (min, max)
as you can see the two extent dtm.temp (the starting one) e the final dtm.temp1.crop.proj seems to be too much different.
extent dtm.temp : 437230.1, 499858.6, 5138842, 5200699
extent dtm.temp1.crop.proj: 437083.4, 500005, 5138362, 5201182
The error (in meters) is:
> 437230.1 - 437083.4
[1] 146.7
> 499858.6-500005
[1] -146.4
> 5138842 - 5138362
[1] 480
> 5200699-5201182
[1] -483
I'm sure I'm doing something wrong but I'm going crazy trying to understand what...
Someone is so patient to help me? Thanks in advance!
I add some more information trying to clearify on the basis of the comment of Mike.
Using a different approach (easier but computationally time consuming) I follow this steps:
> dtm.temp.extent <- extent(dtm.temp) # is the extent fixed
> dtm.temp.extent
class : Extent
xmin : 437230.1
xmax : 499858.6
ymin : 5138842
ymax : 5200699
# then I project directly the second raster dtm.temp1 using the crs
# with projectRaster (very slow)
> dtm.temp1.proj <- projectRaster(dtm.temp1,crs=crs_text)
# then I crop to the fixed extent
> dtm.temp1.proj.crop <- crop(dtm.temp1.proj, dtm.temp.extent)
# this is what I obtain
> extent(dtm.temp)
class : Extent
xmin : 437230.1
xmax : 499858.6
ymin : 5138842
ymax : 5200699
> extent(dtm.temp1.proj.crop)
class : Extent
xmin : 437233.6
xmax : 499852
ymin : 5138857
ymax : 5200688
the error now appears to me very reasonable:
> 437230.1 - 437233.6
[1] -3.5
> 499858.6 - 499852
[1] 6.6
> 5138842 - 5138857
[1] -15
> 5200699 - 5200688
[1] 11
The reason of the first approach is only because I'm trying to speedup the code (very time consuming in the second approach).
(Edited) I add, it may be useful for someone, an image of the problem in the first workflow (the two points of the extent - LL and UR - far from raster even if on a different raster respect to the one above in the question) and the solution suggested by Mkennedy that works fine (using four points unprojecting all four corners of the raster, LL, UL, UR, LR and then taking the min/max of the 8 values). I was not catching the rotation that occurs when unprojecting to lat/lon using only the LL and UR coordinates. Taking the true min/max, the results are more closely (as in the 2nd workflow)
Position of the raster vertex following a different approach
Hi i been trying to reproject a raster image from Equirectangular to EPSG:4326 (Latlon), the issue is that every time i run my code on R, i get the wrong coordinates on the new image; i don´t know where is the error in the code, also i do the same process with Qgis, and i got the same result, it´s strange, i got the opportunity to do the same reprojection process in ENVI, and the result was succesful, help please!!!
a <- raster("C:/Users/<username>/Documents/imageexample.tif")
> a
class : RasterLayer
dimensions : 1800, 1800, 3240000 (nrow, ncol, ncell)
resolution : 1100, 1100 (x, y)
extent : -988900, 991100, 1677577, 3657577 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=eqc +lat_ts=0 +lat_0=24 +lon_0=-112 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:/Users/<username>/Documents/imageexample.tif
names : imageexample
g1 <- projectRaster(a, crs="+init=epsg:4326")
> g1
class : RasterLayer
dimensions : 1810, 1810, 3276100 (nrow, ncol, ncell)
resolution : 0.00988, 0.00988 (x, y)
extent : -120.9328, -103.05, 39.02317, 56.90597 (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0
data source : in memory
names : imageexample.tif
values : -5.000117, 39.87529 (min, max)
The correct coordinates should be like this:
class : RasterLayer
dimensions : 1793, 1803, 3232779 (nrow, ncol, ncell)
resolution : 0.0108098, 0.009931556 (x, y)
extent : -121.735, -102.245, 15.08612, 32.8934 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:/Users/<username>/Documents/CORRECTimageexample.tif
names : CORRECTimageexample
Thanks!!!
This is a bit of a special case but generally there is not defined (by the data) extent and resolution for raster reprojection. You need to specify these. For example, you can do:
library(raster)
r <- raster(xmn=-121.735, xmx=-102.245, ymn=15.08612, ymx=32.8934, nrow=1793, ncol=1803, crs='+proj=longlat +ellps=WGS84')
g2 <- projectRaster(a, r)
I have two geotiff raster files, one has all the metadata information and in the other the metadata information was lost. I know that all the metadata information was exactly the same so I want to copy it from one file to the other. I tried to use raster, because I made all the processing in R.
This is the file with metadata
af1_patch <-raster(a_files[6])
af1_patch
class : RasterLayer
dimensions : 38400, 38400, 1474560000 (nrow, ncol, ncell)
resolution : 231.656, 231.656 (x, y)
extent : -2223901, 6671703, -4447802, 4447802 (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 : Forest_patches_AF_1.tif
names : Forest_patches_AF_1
values : 0, 255 (min, max)
And this is the file without the metadata
af1_area <-raster(a_files[1])
af1_area
class : RasterLayer
dimensions : 38400, 38400, 1474560000 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 0, 38400, 0, 38400 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : africa_AF_1.tif
names : africa_AF_1
values : 0, 255 (min, max)
I tried to copy the metadata using:
res(af1_area) <- res(af1_patch)
crs(af1_area) <- crs(af1_patch)
extent(af1_area) <- extent(af1_patch)
but it doesn't work, dimensions and resolution are incorrect and the data values are lost:
af1_area
class : RasterLayer
dimensions : 166, 166, 27556 (nrow, ncol, ncell)
resolution : 53588, 53588 (x, y)
extent : -2223901, 6671703, -4447802, 4447802 (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
hist(af1_area)
Error in .hist1(x, maxpixels = maxpixels, main = main, plot = plot, ...) :
cannot make a histogram; need data on disk or in memory
Thanks!
I think that this is occuring because you are changing the resolution before changeing the extent, because this, the extent is bound by the resolution that you have assigned to it. I was able to reproduce your problem and solve it by changing the order of the process. Hope it works for you!
library(raster)
x <- raster(matrix(1:10))
proj4string(x) <- CRS("+proj=longlat")
extent(x) <- extent(-10,10,-10,10)
y <- raster(matrix(1:10))
z <- raster(matrix(1:10))
#Current Process
res(y) <- res(x)
crs(y) <- crs(x)
extent(y) <- extent(x)
#Working Process
extent(z) <- extent(x)
res(z) <- res(x)
crs(z) <- crs(x)
Output:
> x
class : RasterLayer
dimensions : 10, 1, 10 (nrow, ncol, ncell)
resolution : 20, 2 (x, y)
extent : -10, 10, -10, 10 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84
data source : in memory
names : layer
values : 1, 10 (min, max)
> y
class : RasterLayer
dimensions : 1, 1, 1 (nrow, ncol, ncell)
resolution : 20, 20 (x, y)
extent : -10, 10, -10, 10 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84
> z
class : RasterLayer
dimensions : 10, 1, 10 (nrow, ncol, ncell)
resolution : 20, 2 (x, y)
extent : -10, 10, -10, 10 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84
data source : in memory
names : layer
values : 1, 10 (min, max)
I would just reassign the values which will preserve the metadata, and re-write the file:
af1_patch <-raster(a_files[6])
af1_area <-raster(a_files[1])
af1_patch[] <- af1_area[]
#writeRaster(af1_patch, a_files[1], ...)
Hi i been trying to reproject a raster image from Equirectangular to EPSG:4326 (Latlon), the issue is that every time i run my code on R, i get the wrong coordinates on the new image; i don´t know where is the error in the code, also i do the same process with Qgis, and i got the same result, it´s strange, i got the opportunity to do the same reprojection process in ENVI, and the result was succesful, help please!!!
a <- raster("C:/Users/<username>/Documents/imageexample.tif")
> a
class : RasterLayer
dimensions : 1800, 1800, 3240000 (nrow, ncol, ncell)
resolution : 1100, 1100 (x, y)
extent : -988900, 991100, 1677577, 3657577 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=eqc +lat_ts=0 +lat_0=24 +lon_0=-112 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:/Users/<username>/Documents/imageexample.tif
names : imageexample
g1 <- projectRaster(a, crs="+init=epsg:4326")
> g1
class : RasterLayer
dimensions : 1810, 1810, 3276100 (nrow, ncol, ncell)
resolution : 0.00988, 0.00988 (x, y)
extent : -120.9328, -103.05, 39.02317, 56.90597 (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0
data source : in memory
names : imageexample.tif
values : -5.000117, 39.87529 (min, max)
The correct coordinates should be like this:
class : RasterLayer
dimensions : 1793, 1803, 3232779 (nrow, ncol, ncell)
resolution : 0.0108098, 0.009931556 (x, y)
extent : -121.735, -102.245, 15.08612, 32.8934 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:/Users/<username>/Documents/CORRECTimageexample.tif
names : CORRECTimageexample
Thanks!!!
This is a bit of a special case but generally there is not defined (by the data) extent and resolution for raster reprojection. You need to specify these. For example, you can do:
library(raster)
r <- raster(xmn=-121.735, xmx=-102.245, ymn=15.08612, ymx=32.8934, nrow=1793, ncol=1803, crs='+proj=longlat +ellps=WGS84')
g2 <- projectRaster(a, r)