Skip to content

Commit 96e5919

Browse files
committed
Add raster mosaic command
1 parent 97bfe08 commit 96e5919

File tree

8 files changed

+75
-0
lines changed

8 files changed

+75
-0
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ raster stylize --name raster --output-format raster_stylized --output-name raste
257257

258258
raster shadedrelief --name raster --output-format shaded --output-name shaded --scale 1.0 --altitude 25.0 --azimuth 35.0
259259

260+
raster mosaic --name1 raster1 --name2 raster2 --output-format mosaic --output-name mosaic
261+
260262
style
261263
-----
262264
style create --params "stroke=navy stroke=width=5" --file earth_outline.sld

examples/alki2.tif

473 KB
Binary file not shown.

examples/alki3.tif

473 KB
Binary file not shown.

examples/raster_mosaic.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
format open --input alki2.tif --name alki2
2+
3+
raster open --format alki2 --raster alki2 --name alki2
4+
5+
format open --input alki3.tif --name alki3
6+
7+
raster open --format alki3 --raster alki3 --name alki3
8+
9+
format open --input mosaic.tif --name mosaic
10+
11+
raster mosaic --name1 alki2 --name2 alki3 --output-format mosaic --output-name mosaic
12+
13+
map open --name map
14+
15+
map add raster --name map --raster mosaic
16+
17+
map draw --name map
18+
19+
map close --name map
20+
21+
open --file image.png

src/main/groovy/org/geoshell/raster/RasterCommands.groovy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,4 +635,28 @@ class RasterCommands implements CommandMarker {
635635
}
636636
}
637637

638+
@CliCommand(value = "raster mosaic", help = "Mosaic two Rasters together")
639+
String mosaic(
640+
@CliOption(key = "name1", mandatory = true, help = "The Raster name") RasterName name1,
641+
@CliOption(key = "name2", mandatory = true, help = "The Raster name") RasterName name2,
642+
@CliOption(key = "output-format", mandatory = true, help = "The output Format Workspace") FormatName formatName,
643+
@CliOption(key = "output-name", mandatory = false, help = "The output Raster name") String outputRasterName
644+
) throws Exception {
645+
Raster raster1 = catalog.rasters[name1]
646+
Raster raster2 = catalog.rasters[name2]
647+
Format format = catalog.formats[formatName]
648+
if (format) {
649+
Raster mosaicRaster = Raster.mosaic([raster1, raster2])
650+
format.write(mosaicRaster)
651+
if (!outputRasterName) {
652+
outputRasterName = formatName.name
653+
}
654+
catalog.rasters[new RasterName(outputRasterName)] = format.read(outputRasterName)
655+
"Mosaic ${name1} and ${name2} to create ${outputRasterName}!"
656+
} else {
657+
"Unable to find Raster Format ${formatName}"
658+
}
659+
660+
}
661+
638662
}

src/test/groovy/org/geoshell/raster/RasterCommandsTest.groovy

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.geoshell.raster
22

3+
import geoscript.geom.Bounds
34
import geoscript.layer.Format
45
import geoscript.layer.Layer
56
import geoscript.layer.Raster
@@ -509,4 +510,31 @@ class RasterCommandsTest {
509510
assertEquals(1.0, outRaster.getValue(30,20,0), 0.1)
510511
}
511512

513+
@Test void mosaic() {
514+
515+
Catalog catalog = new Catalog()
516+
File file1 = new File(getClass().getClassLoader().getResource("mosaic/alki2.tif").toURI())
517+
Format format1 = Format.getFormat(file1)
518+
catalog.formats[new FormatName("raster1")] = format1
519+
catalog.rasters[new RasterName("raster1")] = catalog.formats[new FormatName("raster1")].read()
520+
521+
File file2 = new File(getClass().getClassLoader().getResource("mosaic/alki3.tif").toURI())
522+
Format format2 = Format.getFormat(file2)
523+
catalog.formats[new FormatName("raster2")] = format2
524+
catalog.rasters[new RasterName("raster2")] = catalog.formats[new FormatName("raster2")].read()
525+
526+
File outFile = new File(temporaryFolder.newFolder("mosaic"), "mosaic.tif")
527+
Format outFormat = Format.getFormat(outFile)
528+
catalog.formats[new FormatName("mosaic")] = outFormat
529+
530+
RasterCommands cmds = new RasterCommands(catalog: catalog)
531+
cmds.open(new FormatName("raster1"), new RasterName("raster1"), "raster1")
532+
cmds.open(new FormatName("raster2"), new RasterName("raster2"), "raster2")
533+
534+
String result = cmds.mosaic(new RasterName("raster1"), new RasterName("raster2"), new FormatName("mosaic"), "mosaic")
535+
assertEquals("Mosaic raster1 and raster2 to create mosaic!", result)
536+
Raster outRaster = catalog.rasters[new RasterName("mosaic")]
537+
assertNotNull outRaster
538+
}
539+
512540
}
473 KB
Binary file not shown.
473 KB
Binary file not shown.

0 commit comments

Comments
 (0)