Skip to content

Commit 66f2c61

Browse files
committed
Add raster mosaic method
1 parent 007d1d8 commit 66f2c61

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/main/groovy/geoscript/layer/Raster.groovy

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,48 @@ class Raster implements Renderable {
978978
new Raster(gridCov)
979979
}
980980

981+
/**
982+
* Mosaic a List of Rasters into a single Raster
983+
* @param options options Optional named parameters:
984+
* <ul>
985+
* <li>bounds = The final Bounds</li>
986+
* <li>size = The final size of the Raster</li>
987+
* </ul>
988+
* @param rasters The List of Rasters
989+
* @return A new Raster
990+
*/
991+
static Raster mosaic(Map options = [:], List<Raster> rasters) {
992+
Bounds bounds = options.get("bounds")
993+
if (!bounds) {
994+
rasters.each { Raster r ->
995+
if (!bounds) {
996+
bounds = r.bounds
997+
} else {
998+
bounds.expand(r.bounds)
999+
}
1000+
}
1001+
}
1002+
List size = options.get("size")
1003+
if (!size) {
1004+
int width = 0
1005+
int height = 0
1006+
rasters.each { Raster r ->
1007+
width += r.size[0]
1008+
height += r.size[1]
1009+
}
1010+
size = [width, height]
1011+
}
1012+
GridGeometry2D gg = new GridGeometry2D(new GridEnvelope2D(0, 0, size[0], size[1]), bounds.env)
1013+
def processor = new CoverageProcessor()
1014+
def params = processor.getOperation("Mosaic").parameters
1015+
params.parameter("Sources").value = rasters.collect { Raster raster -> raster.coverage}
1016+
params.parameter("geometry").value = gg
1017+
def newCoverage = processor.doOperation(params)
1018+
new Raster(newCoverage)
1019+
}
1020+
1021+
1022+
9811023
/**
9821024
* Create a new Raster with styling baked in.
9831025
* @param style The Style/Symobolizer

src/test/groovy/geoscript/layer/RasterTestCase.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,17 @@ class RasterTestCase {
10741074
assertNotNull rgb
10751075
}
10761076

1077+
@Test void mosaic() {
1078+
File file = new File(getClass().getClassLoader().getResource("alki.tif").toURI())
1079+
GeoTIFF geoTIFF = new GeoTIFF(file)
1080+
Raster raster = geoTIFF.read()
1081+
Bounds bounds = raster.bounds
1082+
Raster mosaicRaster = Raster.mosaic(
1083+
bounds.tile(0.5).collect { Bounds b -> raster.crop(b) }
1084+
)
1085+
assertNotNull mosaicRaster
1086+
}
1087+
10771088
@Test void extractFootPrint() {
10781089
File file = new File(getClass().getClassLoader().getResource("raster.tif").toURI())
10791090
GeoTIFF geoTIFF = new GeoTIFF(file)

0 commit comments

Comments
 (0)