I have an image as below. It is 2579*2388 pixels. Lets assume that it's bottom left corner is at 0,0. From that image I want to create multiple images as follows and save them in the working folder. Each image will have size of 100*100 pixels. Each image will be saved by it's bottom left hand coordinates.
- first image will have its bottom left hand corner at 0,0. Top right hand corner will be at 100,100 and the image will be saved as 0-0.jpg
- second will have its bottom left hand corner at 10,0. Top right hand corner will be at 110,100 and the image will be saved as 10-0.jpg
- Once the bottom row is completed, Y coordinate will move by 10. In case of second row, the first image will be at 0,10 and that image will be saved as 0-10.jpg
what is the fastest way to do this? is there any R package which could do it very fast?
I understand that in the case of the current image, it will split it into around 257*238 images. But I have sufficient disk space and i need each image to perform text detection.
Here's one way to do it, using GDAL via
gdalUtils
, and parallelizing if desired.Example applying the function to a single window:
Example applying it to the first 10 windows:
Example using parLapply to run in parallel:
Not finding a straight-forward implementation using exclusively r I used the following approach using raster operations which may be interesting for others. It generates extents and crops the original raster to them. Hope this helps!
You can use gdal and r, as shown in this link.
You would then modify line 23 to make a suitable offset to allow overlap among tiles generated.
Here another approach using "raster" package. The function spatially aggregates the raster to be chopped, the aggregated raster cells are turned into polygons, then each polygon's extent is used to crop the input raster.
I am sure there are sophisticated and compact ways to do this but this approach works for me and I found it intuitive as well. I hope you find it useful too. Notice Part 4 & 5 below are only for testing and they are not part of the function.
Part 1: Load and plot sample raster data
Part 2: The function itself:
Part 3: Test the function
Part 4: Run a code on each piece & save them back in directory
Part 5: Let us put the pieces back together
I think what you need is to create a function for your processing part (let's call it "fnc") and a table that lists the number of tiles that you have made (let's call it "tile.tbl") and also let's assume that your geobig data called "obj"
and then parallelize it using snowfall package. Here is an example:
For a detailed explanation please see HERE
This comes a bit late but may be useful to others coming across this question. The SpaDES package has a handy function called splitRaster() which does what you're after.
An example:
Which gives you this: Now do the splitting using the SpaDES package. Set
nx
andny
according to the number of tiles you want along the x and y axis - if we want 4 tiles, set them asnx=2
andny=2
. If you don't setpath
, it should write the files to your current directory. There are other things on offer too like buffering - see?splitRaster
:The variable
sections
is a list of rasters, one for each section ofthe_grid
- access them as:If you want to save them specifically, just use writeRaster().
To create a combined raster again, use mergeRaster().