![]() Rast2_length <- sqrt(length(original_map.rast2)/3) # find the number of cells in compressed raster Original_map.rast2 <- aggregate(original_map.rast, 2) #compress raster Original_map.rast1 <- ggmap_rast(original_map) #extract RasterStack from ggmap object #ggmap_rast function courtesy of Robin Lovelace Hope this is useful: original_map <- get_map("New York", scale = 1) #download a map at lowest resolution This involves extracting and transforming the ggmap raster (using Robin Lovelace's ggmap_rast()), aggregating the raster as discussed in this thread, and then replacing the ggmap high resolution raster with the lower resolution raster below. ![]() I recently had a requirement to reduce the resolution of a ggmap object. WriteRaster(resampledRaster,filename="ResampleProduct.tif",format="GTiff",datatype="INT1U",overwrite=TRUE) # Or, use writeRaster method to create the output file. ResampledRaster <- resample(inputRaster,resampledRaster,datatype="INT1U",method='bilinear',filename="testOutResamp.tif",overwrite=TRUE) # The resample method will write the resampled raster image to a NEW disk file. ResampledRaster <- raster(ncol=(inCols / resampleFactor), nrow=(inRows / resampleFactor))Įxtent(resampledRaster) <- extent(inputRaster) InputRaster <- raster("TmB50MosaicImg1.tif") 5 # reduce the cell size by 50% and double the number of rows and columns. # 2) Set the extent (geographic 'bounding box') of the new raster # 1) Create a new raster object with the correct 'resampled' number of cells. # both dimensions (four times the number of cells) # into a NEW raster with 2x spatial resolution in # Resample a satellite image, stored in a GeoTiff file # s prints a vector of resampled cell values. # This code produces a resampled raster, 's', ![]() # second method: use the resample() method from raster package WriteRaster(FineResampRaster,filename=sOutFile,format="GTiff",datatype="INT1U",overwrite=TRUE) SOutFile <- sprintf("FineSubsamp%s",inFiles) WriteRaster(CoarseResampRaster,filename=sOutFile,format="GTiff",datatype="INT1U",overwrite=TRUE)įineResampRaster <- disaggregate(inputRaster,fact=resampleFactor,fun=mean) SOutFile <- sprintf("CoarseSubsamp%s",inFiles) # and FINER (smaller cells) resolutions, respectivelyĬoarseResampRaster <- aggregate(inputRaster,fact=resampleFactor,fun=mean) # The aggregate() / disaggregate methods resample rasters to COARSER (bigger cells) Message(sprintf("resampling file: %s",inFiles)) # Note: these images do not have same spatial extent, so they cant be stored # Within the same loop, obtain the (geographic) extent of each component. # Read the mosaic components, stored in a subfolder, into a raster object list. ResampleFactor <- 4 # For test, subsample incoming image by factor of 10 # spatial resolution using the R raster package. # This function demonstrates resampling of raster images to a new This would normally be a SpatRaster that you already have from another data source. ![]() Therefore, the preferred approach is to provide a template to which you want the output to align with. You can do that like this: u1 <- project(r, "+proj=utm +zone=32")īut, unlike for vector data, the transformation of raster data is not well-defined. It could also be that you want to transform raster data to a geometry with another coordinate reference system ("map projection"). In that case you can use resampleĮxample of a non-aligned SpatRaster: x <- rast(r) But to combine raster data from different sources, you may need to match the geometry of a raster that does not align. You can also go the other way and disaggregate: d <- disagg(a2, 2)Īggregate can only combine entire cells. You can use a different factor for the rows and columns, and also different aggregation functions (the default is "mean") a2 <- aggregate(r, c(2,3), fun=sum) To go from 30 to 120m is a factor of 4 a1 <- aggregate(r, 4) These days we can use terra, the replacement for the raster packageį <- system.file("ex/elev.tif", package="terra")Īggregate raster cells. ![]()
0 Comments
Leave a Reply. |