[R-sig-Geo] Aggregation to
Miluji Sb
milujisb at gmail.com
Sat Sep 2 00:46:44 CEST 2017
Dear Loic,
Thank you for your reply and the suggestions - didn't realize that the
factor needs to be an integer.
The original data was at 0.5 degree, I aggregated it to 1 degree to match
to another data.
The original 0.5 degree data is available as a shapefile, I read it in
using readOGR and the information is as follows:
class : SpatialPolygonsDataFrame
features : 75228
extent : -180, 180, -59.48429, 83.62742 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84
+towgs84=0,0,0
variables : 4
names : gID, ISO3, px, py
min values : 1, AFG, -100.07055, -10.00004
max values : 75228, ZWE, 179.93522, 83.55961
Then I convert it to raster for aggregation using:
###
pgeo <- spTransform(poly, CRS('+proj=longlat +datum=WGS84'))
ext <- floor(extent(pgeo))
rr <- raster(ext, res=0.5)
rr <- rasterize(pgeo, rr, field=1)
This is the information on the converted raster:
class : RasterLayer
dimensions : 288, 720, 207360 (nrow, ncol, ncell)
resolution : 0.5, 0.5 (x, y)
extent : -180, 180, -60, 84 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : layer
values : 1, 1 (min, max)
Then I aggregate it to 2.5 degree using:
###
aggregate(rr,fact=5, fun=sum)
This seems to have aggregate it to the desired 2.5 degree:
df2_5
class : RasterLayer
dimensions : 58, 144, 8352 (nrow, ncol, ncell)
resolution : 2.5, 2.5 (x, y)
extent : -180, 180, -61, 84 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : layer
values : 1, 25 (min, max)
My goal is to use this to extract data from a rasterbrick (time series), so
I convert the raster to a spatialpointsdataframe:
###
spts <- rasterToPoints(df2_5, spatial = TRUE)
Then use extract from the the following rasterbrick:
class : RasterBrick
dimensions : 72, 144, 10368, 2880 (nrow, ncol, ncell, nlayers)
resolution : 2.5, 2.5 (x, y)
extent : 0, 360, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
names : X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13,
X14, ...
z-value : 0, 2879 (min, max)
varname : beta
level : 1
But I get a lot of missing values.Is it due to different extent? How can I
change the extent of the RasterBrick to (-180 180 -90 90)?
I realize that I have probably provided a lot of redundant information. Any
help will be highly appreciated. Thank you.
Sincerely,
Milu
On Thu, Aug 31, 2017 at 9:57 PM, Loïc Dutrieux <loic.dutrieux at conabio.gob.mx
> wrote:
>
>
> On 31/08/17 14:22, Miluji Sb wrote:
>
>> I have a set of coordinates:
>>
>> temp <- dput(head(gcp,10))
>> structure(list(lon = c(-180, -180, -179, -179, -178, -178, -177,
>> -176, -176, -175), lat = c(67, 68, 67, 68, 67, 68, 67, 66, 67,
>> 66)), datalabel = "", time.stamp = "11 Aug 2017 16:10", .Names = c("lon",
>> "lat"), formats = c("%9.0g", "%9.0g"), types = c(255L, 255L), val.labels =
>> c("",
>> ""), var.labels = c("lon", "lat"), version = 12L, row.names = c("1",
>> "2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "data.frame")
>>
>> These are at 1 degree, I would like to aggregate them to 2.5 degree to use
>> them to extract data from a netcdf file (which is at 2.5 degree). This is
>> what I have done:
>>
>> rasterDF <- rasterFromXYZ(gcp)
>> gcp2_5 <- aggregate(rasterDF,fact=2.5, fun=sum)
>>
>
> The aggregation factor in raster::aggregate has to be an integer (2, 3,
> etc). Perhaps raster::resample is what you're looking for then. When using
> 'bilinear', resample actually aggregates to the nearest multiple of the
> original raster's resolution (2 degrees in your case), and finishes with
> some interpolation.
>
> Cheers,
> Loïc
>
>
>> However, this seems to return aggregated coordinates at 2 degree.
>>
>> class : RasterLayer
>> dimensions : 67, 180, 12060 (nrow, ncol, ncell)
>> resolution : 2, 2 (x, y)
>> extent : -180.5, 179.5, -54.5, 79.5 (xmin, xmax, ymin, ymax)
>> coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
>>
>> What am I doing wrong? Is there another way? Thank you.
>>
>> Sincerely,
>>
>> Milu
>>
>> [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>> Email secured by Check Point
>>
>>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list