0
votes

I have two rasters and one shape file, all with 100m resolution grids but different extents. The shapefile is slightly smaller extent. I want to make sure they line up exactly so my calculations are correct for each grid cell in future analysis.

Raster 1

day class : RasterLayer dimensions : 2367, 2909, 6885603 (nrow, ncol, ncell) resolution : 0.0008333333, 0.0008333333 (x, y) extent : -123.6325, -121.2083, 36.8925, 38.865 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 names : DAY_BA values : 0, 14917 (min, max)

Raster 2

night class : RasterLayer dimensions : 2365, 2909, 6879785 (nrow, ncol, ncell) resolution : 0.0008333333, 0.0008333333 (x, y) extent : -123.6325, -121.2083, 36.89417, 38.865 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 names : NIGHT_BA values : 0, 1744 (min, max)

Shapefile

mgrs class : SpatialPolygonsDataFrame features : 1186800 extent : -122.6511, -121.594, 37.10124, 38.27151 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 variables : 12

The files are large and loading them and plotting them for visual comparison is yielding nothing interesting.

I tried calculating the distance in meters between the upper and lower extents for each using the functions from https://eurekastatistics.com/calculating-a-distance-matrix-for-geographic-points-using-r/, thinking that increments of 100m would indicate that they are on 100m increment distances from each other, but this did not appear to be the case.

distance.100m <- GeoDistanceInMetresMatrix(df.lims)/100 distance.100m DayMin DayMax NightMin NightMax MSMin MSMax DayMin 0.000000 3056.1968 1.906129 3056.1968 903.7839357 2363.0676716 DayMax 3056.196849 0.0000 3054.546060 0.0000 2332.1390496 739.6121652 NightMin 1.906129 3054.5461 0.000000 3054.5461 902.8710503 2361.5160232 NightMax 3056.196849 0.0000 3054.546060 0.0000 2332.1390496 739.6121652 MSMin 903.783936 2332.1390 902.871050 2332.1390 0.0000000 1598.8812655 MSMax 2363.067672 739.6122 2361.516023 739.6122 1598.8812655 0.0000000

Any ideas how to compare that the pixels line up? I want to keep the original values if possible and not resample.

1
Have you tried posting this question on GIS stackexchage gis.stackexchange.com ? I think this'll be more suitable there than on SO.snair.stack

1 Answers

0
votes

Given that all extent coordinates are the same, except one (ymin), and the resolution is the same, they should line up.

We can first look at the extents

d <- raster(nrow=2367, ncol=2909, ext=extent(c(-123.6325, -121.2083, 36.8925, 38.865)))
n <- raster(nrow=2365, ncol=2909, ext=extent(c(-123.6325, -121.2083, 36.89417, 38.865)))  
e <- extent(c(-122.6511, -121.594, 37.10124, 38.27151))

plot(extent(d), col='green', lwd=2)
plot(extent(n), add=TRUE, col="red")
plot(e, add=TRUE, col="blue")

Clearly, the rasters are similar, and the polygons are inside the rasters extent.

We can check the origin of the rasters, to see if they align:

origin(n)
#[1] 3.331042e-05 6.573362e-05
origin(d)
#[1]  3.331042e-05 -7.105427e-14

Not quite, but that is probably because of rounding. If we do

res(n) <- 1/1200
res(d) <- 1/1200

To (probably) get what you really (should) have:

origin(n)
[1] -9.947598e-14  4.263256e-14
origin(d)
[1] -9.947598e-14 -7.105427e-14

As the extent of d is larger, you can do crop it to n, so that things line up

d <- crop(d, n)