/
Stitcher.scala
120 lines (108 loc) · 4.26 KB
/
Stitcher.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright 2016 Azavea
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package geotrellis.raster.stitch
import geotrellis.raster._
import scalaz.Semigroup
/**
* The Stitcher base trait.
*/
trait Stitcher[T <: CellGrid] extends Serializable {
/**
* Stitch an Iterable of tile, corner pairs into a new tile with
* the given number of columns and rows.
*
* The parameter 'pieces' is an iterable of (T, (Int, Int)) tuples,
* were the first item in the tuple is a tile and the second one is
* the position that the tile should occupy (given by smallest
* column and smallest row).
*
* @param pieces An iterable of tile, corner pairs
* @param cols The number of columns in the result tile
* @param rows The number of rows in the result tile
* @return The result tile
*/
def stitch(pieces: Iterable[(T, (Int, Int))], cols: Int, rows: Int): T
}
/**
* Stitcher object.
*/
object Stitcher {
/**
* Implicit object holding [[Tile]] stitcher function.
*/
implicit object TileStitcher extends Stitcher[Tile] {
/**
* Stitch an Iterable of [[Tile]], corner pairs into a new Tile
* with the given number of columns and rows.
*
* The parameter 'pieces' is an iterable of (T, (Int, Int)) tuples,
* were the first item in the tuple is a Tile and the second one is
* the position that the Tile should occupy (given by smallest
* column and smallest row).
*
* @param pieces An iterable of Tile, corner pairs
* @param cols The number of columns in the result Tile
* @param rows The number of rows in the result Tile
* @return The result Tile
*/
def stitch(pieces: Iterable[(Tile, (Int, Int))], cols: Int, rows: Int): Tile = {
val result = ArrayTile.empty(pieces.head._1.cellType, cols, rows)
for((tile, (updateColMin, updateRowMin)) <- pieces) {
result.update(updateColMin, updateRowMin, tile)
}
result
}
}
/**
* Implicit object holding [[MultibandTile]] stitcher function.
*/
implicit object MultibandTileStitcher extends Stitcher[MultibandTile] {
/**
* Stitch an Iterable of [[MultibandTile]], corner pairs into a new
* MultibandTile with the given number of columns and rows.
*
* The parameter 'pieces' is an iterable of (T, (Int, Int)) tuples,
* were the first item in the tuple is a MultibandTile and the
* second one is the position that the MultibandTile should occupy
* (given by smallest column and smallest row).
*
* @param pieces An iterable of MultibandTile, corner pairs
* @param cols The number of columns in the result MultibandTile
* @param rows The number of rows in the result MultibandTile
* @return The result MultibandTile
*/
def stitch(pieces: Iterable[(MultibandTile, (Int, Int))], cols: Int, rows: Int): MultibandTile = {
val headTile = pieces.head._1
val bands = Array.fill[MutableArrayTile](headTile.bandCount)(ArrayTile.empty(headTile.cellType, cols, rows))
for ((tile, (updateColMin, updateRowMin)) <- pieces) {
for(b <- 0 until headTile.bandCount) {
bands(b).update(updateColMin, updateRowMin, tile.band(b))
}
}
ArrayMultibandTile(bands)
}
}
implicit class TileFeatureStitcher[
T <: CellGrid: Stitcher,
D : Semigroup
](val self: TileFeature[T, D]) extends Stitcher[TileFeature[T, D]] {
def stitch(pieces: Iterable[(TileFeature[T, D], (Int, Int))], cols: Int, rows: Int): TileFeature[T, D] = {
val newPieces = pieces.map{ piece ⇒ (piece._1.tile, piece._2) }
val newData = pieces.map(_._1.data).reduce(Semigroup[D].append(_, _))
TileFeature(implicitly[Stitcher[T]].stitch(newPieces, cols, rows), newData)
}
}
}