[R-sig-Geo] Raster Layers same resolution but not the same coordinates over the same area

Robert J. Hijmans r.hijmans at gmail.com
Mon Sep 1 19:13:24 CEST 2014


Justin,

I would not call this a slight difference (several rows and columns
difference, whereas these numbers have to be the same to align the
cells):

>>> dimensions  : 1287, 1321, 1700127, 12  (nrow, ncol, ncell, nlayers)
>>> resolution  : 0.008333333, 0.008333333  (x, y)

>>> dimensions  : 1258, 1291, 1624078, 12  (nrow, ncol, ncell, nlayers)
>>> resolution  : 0.008526982, 0.008525437  (x, y)


You need to tell GDALwarp not only the extent you want, but also the
number of rows and columns (or the resolution, but that is more prone
to creating slight differences due to imprecision).
But by far the easiest solution is to use this:

NDVI <- projectRaster("original ndivi data stack", rainStack)

( that takes a little longer to run, but it should not be too bad for
these data (not that much), and would have been much faster overall,
of course, if you take all your time into account).

Robert


On Mon, Sep 1, 2014 at 3:06 AM, Justin Michell <jwm302 at gmail.com> wrote:
> Hi Robert
>
> All right, I see. Do my layers not match because my resolution is slightly different between the rasters (do they have to be exact to some decimal level?) or because my dimensions are not the same? Or do these differences (resolutions/dimensions) amount to the the same thing? I apologize if this has been dealt with previously somewhere on this list.
>
> I thought they would match after changing the projection for NDVI because my MODIS NDVI data is at 1-kilometer spatial resolution and so is my worldclim data.
>
> I was using this approach (advised by a previous post that gdalwarp reprojects/resamples all at once):
>
>> # Warp
>> gdalwarp(srcfile=sdsList, t_srs="+proj=longlat +datum=WGS84 +no_defs",
>> dstfile=file.path(dir, ’NDVI.tif'))
>
>
> Would I still need afterwards to complete resampling correctly set the smallest nrow (x) and ncol (y) among the rasters. So something like this:
>
> s <- raster(nrow=x, nrow=y)
>
> NDVIRaster <- resample(NDVIRaster, s, method=“ngb”)
>
> Justin
>
>
> On Aug 31, 2014, at 12:46 AM, Robert J. Hijmans <r.hijmans at gmail.com> wrote:
>
>> Justin,
>>
>> The RasterStack approach is fine, but as you show, the layers do not
>> match. The resolution of NDVIStack is larger than the resolution of
>> rainStack; it seems that you did not resample the NDVI data correctly.
>>
>> Robert
>>
>>
>>
>> On Sat, Aug 30, 2014 at 4:31 AM, Justin Michell <jwm302 at gmail.com> wrote:
>>> My apologies, I forgot to send as plain text.
>>>
>>> On Aug 30, 2014, at 1:27 PM, Justin Michell <jwm302 at gmail.com> wrote:
>>>
>>>> Hi Jonathan
>>>>
>>>> I tried that.. But I am but the results don’t make sense:
>>>>
>>>>> head(NDVIStackDf)
>>>>        x         y     NDVI1     NDVI2    NDVI3     NDVI4    NDVI5     NDVI6     NDVI7     NDVI8     NDVI9    NDVI10    NDVI11    NDVI12
>>>> 1 29.42809 -1.002081 0.7196462 0.6093929 0.614475 0.7851786 0.799475 0.7241929 0.5946000 0.5948500 0.6585143 0.7874357 0.8305786 0.7774929
>>>> 2 29.43662 -1.002081 0.7103154 0.5823500 0.572225 0.7867429 0.794050 0.7445643 0.6033786 0.5635429 0.6421857 0.7802786 0.8300643 0.7886786
>>>> 3 29.44514 -1.002081 0.7303692 0.6254571 0.599350 0.7771143 0.785850 0.7510071 0.6404571 0.6197286 0.6770929 0.7686786 0.8169571 0.7957786
>>>> 4 29.45367 -1.002081 0.7641923 0.6890714 0.657850 0.7936786 0.809975 0.7805429 0.6769643 0.6590714 0.7165357 0.7843357 0.8283286 0.8231500
>>>> 5 29.46220 -1.002081 0.8057769 0.7500714 0.718175 0.8208786 0.838575 0.8126500 0.7345786 0.7359571 0.7661214 0.8087214 0.8442643 0.8380071
>>>> 6 29.47073 -1.002081 0.8065615 0.7627071 0.730550 0.8142214 0.841100 0.8252000 0.7574786 0.7425071 0.7590571 0.8025071 0.8518357 0.8430286
>>>>> head(rainStackDf)
>>>>        x         y rain1 rain2 rain3 rain4 rain5 rain6 rain7 rain8 rain9 rain10 rain11 rain12
>>>> 1 29.42917 -1.004167    66    68    85   116   105    61    40    85   110    125    118     85
>>>> 2 29.43750 -1.004167    66    69    85   117   106    61    40    85   110    125    119     86
>>>> 3 29.44583 -1.004167    66    68    84   116   106    60    39    84   110    124    118     85
>>>> 4 29.45417 -1.004167    65    67    83   116   105    60    39    83   108    123    117     84
>>>> 5 29.46250 -1.004167    65    68    83   117   106    59    38    83   109    124    118     84
>>>> 6 29.47083 -1.004167    66    68    84   118   107    59    38    83   110    125    119     85
>>>>> mystackDf <- stack(NDVIStackDf, rainStackDf)
>>>>>
>>>>> head(mystackDf)
>>>>   values ind
>>>> 1 29.42809   x
>>>> 2 29.43662   x
>>>> 3 29.44514   x
>>>> 4 29.45367   x
>>>> 5 29.46220   x
>>>> 6 29.47073   x
>>>>
>>>> Is there a better way maybe without using data frames?
>>>>
>>>> I tried stacking the stacks too:
>>>>
>>>>> s1 <- stack(NDVIStack, rainStack)
>>>> Error in compareRaster(x) : different number or columns
>>>>> rainStack
>>>> class       : RasterStack
>>>> dimensions  : 1287, 1321, 1700127, 12  (nrow, ncol, ncell, nlayers)
>>>> resolution  : 0.008333333, 0.008333333  (x, y)
>>>> extent      : 29.425, 40.43333, -11.725, -1  (xmin, xmax, ymin, ymax)
>>>> coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
>>>> names       : rain1, rain2, rain3, rain4, rain5, rain6, rain7, rain8, rain9, rain10, rain11, rain12
>>>> min values  :     7,     5,    31,    13,     0,     0,     0,     0,     0,      0,     22,     32
>>>> max values  :   391,   392,   563,   746,   473,   159,   109,   106,   164,    247,    349,    464
>>>>
>>>>> NDVIStack
>>>> class       : RasterStack
>>>> dimensions  : 1258, 1291, 1624078, 12  (nrow, ncol, ncell, nlayers)
>>>> resolution  : 0.008526982, 0.008525437  (x, y)
>>>> extent      : 29.425, 40.43333, -11.725, -1  (xmin, xmax, ymin, ymax)
>>>> coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
>>>> names       :      NDVI1,      NDVI2,      NDVI3,      NDVI4,      NDVI5,      NDVI6,      NDVI7,      NDVI8,      NDVI9,     NDVI10,     NDVI11,     NDVI12
>>>> min values  : -0.1524615, -0.1353071, -0.1970500, -0.1632071, -0.1919250, -0.1647286, -0.1665000, -0.1693357, -0.1852643, -0.1733714, -0.1555357, -0.1682571
>>>> max values  :  0.9153615,  0.9013000,  0.9295500,  0.9134643,  0.9421750,  0.9158357,  0.9000071,  0.9009786,  0.8733571,  0.8792429,  0.9151357,  0.9233000
>>>>
>>>> Thanks
>>>> Justin
>>>>
>>>> On Jun 30, 2014, at 5:55 PM, Jonathan Greenberg <jgrn at illinois.edu> wrote:
>>>>
>>>>> Justin:
>>>>>
>>>>> It would make more sense, if you insist on working with data.frames,
>>>>> for you to create a single stack with all of your predictor and
>>>>> response variables, and then do your extraction -- that way you don't
>>>>> need to worry about a merge after the fact.  The chances of the
>>>>> coordinates being EXACT I suspect are very low, which is why the
>>>>> merge() is failing.
>>>>>
>>>>> Basically:
>>>>>
>>>>> mystack <- stack(meanTempStackDf, rainStackDf)
>>>>>
>>>>> ... Then you can extract the data.
>>>>>
>>>>> --j
>>>>>
>>>>> On Mon, Jun 30, 2014 at 6:22 AM, Justin Michell <jwm302 at gmail.com> wrote:
>>>>>> Dear Niandou
>>>>>>
>>>>>> No I have not received any feedback.
>>>>>>
>>>>>> I do have some thoughts though. Is it possible/or at least would it make sense to get values in NDVI layer (average of nearby cells?) at the coordinates of raster values in other layers which one wants to merge with?
>>>>>>
>>>>>> I am not very well versed in these things so it’s just a thought- not sure how it would be implemented in R.
>>>>>>
>>>>>> Regards
>>>>>> Justin
>>>>>>
>>>>>>
>>>>>> On Jun 27, 2014, at 3:58 PM, ISSAKA NIANDOU, Yacouba <niandouy at who.int> wrote:
>>>>>>
>>>>>>> Niandou
>>>>>>
>>>>>> _______________________________________________
>>>>>> R-sig-Geo mailing list
>>>>>> R-sig-Geo at r-project.org
>>>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Jonathan A. Greenberg, PhD
>>>>> Assistant Professor
>>>>> Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
>>>>> Department of Geography and Geographic Information Science
>>>>> University of Illinois at Urbana-Champaign
>>>>> 259 Computing Applications Building, MC-150
>>>>> 605 East Springfield Avenue
>>>>> Champaign, IL  61820-6371
>>>>> Phone: 217-300-1924
>>>>> http://www.geog.illinois.edu/~jgrn/
>>>>> AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007
>>>>
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list