/
Regrid.scala
251 lines (218 loc) · 9.69 KB
/
Regrid.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package geotrellis.spark.regrid
import org.apache.spark.rdd.RDD
import geotrellis.raster._
import geotrellis.raster.crop._
import geotrellis.raster.prototype._
import geotrellis.raster.stitch._
import geotrellis.spark._
import geotrellis.spark.tiling._
import geotrellis.util._
import geotrellis.vector._
import scala.reflect._
import scala.collection.mutable
object Regrid {
private case class Interval[N : Ordering](start: N, end: N) {
val ord = implicitly[Ordering[N]]
// let the interval be start to end, inclusive
def intersect(that: Interval[N]) = {
Interval(if (ord.compare(this.start, that.start) > 0) this.start else that.start,
if (ord.compare(this.end, that.end) < 0) this.end else that.end)
}
}
def regrid[
K: SpatialComponent: ClassTag,
V <: CellGrid: ClassTag: Stitcher: (? => CropMethods[V]): (? => TilePrototypeMethods[V]),
M: Component[?, LayoutDefinition]: Component[?, Bounds[K]]
](layer: RDD[(K, V)] with Metadata[M], tileCols: Int, tileRows: Int): RDD[(K, V)] with Metadata[M] = {
// println(layer.metadata.toJson.prettyPrint)
val md = layer.metadata
val ld = md.getComponent[LayoutDefinition]
if(ld.tileLayout.tileCols == tileCols && ld.tileLayout.tileRows == tileRows) {
layer
} else {
val ceil = { x: Double => math.ceil(x).toInt }
val tl = ld.tileLayout
val oldEx = ld.extent
val CellSize(cellW, cellH) = ld.cellSize
val (oldW, oldH) = (tl.tileCols, tl.tileRows)
val (newW, newH) = (tileCols, tileRows)
val ntl = TileLayout(ceil(tl.layoutCols * oldW / newW.toDouble), ceil(tl.layoutRows * oldH / newH.toDouble), tileCols, tileRows)
// Set up a new layout where the upper-left corner of the (0,0) spatial key
// is preserved, and the right and bottom edges are are adjusted to cover
// the new extent of the resized tiles
val nld = LayoutDefinition(
Extent(
oldEx.xmin,
oldEx.ymax - cellH * tl.layoutRows * oldH,
oldEx.xmin + cellW * tl.layoutCols * oldW,
oldEx.ymax),
ntl)
val bounds =
md.getComponent[Bounds[K]] match {
case KeyBounds(minKey, maxKey) =>
val newMinKey = {
val sk = nld.mapTransform(ld.mapTransform(minKey.getComponent[SpatialKey]).northWest)
minKey.setComponent[SpatialKey](sk)
}
val newMaxKey = {
val sk = nld.mapTransform(ld.mapTransform(maxKey.getComponent[SpatialKey]).southEast)
maxKey.setComponent[SpatialKey](sk)
}
KeyBounds(newMinKey, newMaxKey)
case EmptyBounds =>
EmptyBounds
}
val newMd =
md.setComponent[LayoutDefinition](nld)
.setComponent[Bounds[K]](bounds)
val tiled: RDD[(K, V)] =
layer
.flatMap{ case (key, oldTile) => {
val SpatialKey(oldX, oldY) = key
val oldXstart = oldX.toLong * oldW.toLong
val oldYstart = oldY.toLong * oldH.toLong
val oldXrange = Interval(oldXstart, oldXstart + oldW - 1)
val oldYrange = Interval(oldYstart, oldYstart + oldH - 1)
val tileEx = ld.mapTransform(key)
val newBounds = nld.mapTransform(tileEx)
for (
x <- newBounds.colMin to newBounds.colMax ;
newXrange = {
val newXstart = x * tileCols.toLong
Interval(newXstart, newXstart + tileCols - 1)
} ;
y <- newBounds.rowMin to newBounds.rowMax ;
newYrange = {
val newYstart = y * tileRows.toLong
Interval(newYstart, newYstart + tileRows - 1)
}
) yield {
val newKey = key.setComponent[SpatialKey](SpatialKey(x, y))
val xSpan: Interval[Long] = oldXrange intersect newXrange
val ySpan: Interval[Long] = oldYrange intersect newYrange
newKey ->
(oldTile.crop((xSpan.start - oldXstart).toInt,
(ySpan.start - oldYstart).toInt,
(xSpan.end - oldXstart).toInt,
(ySpan.end - oldYstart).toInt),
((xSpan.start - newXrange.start).toInt, (ySpan.start - newYrange.start).toInt)
)
}
}}
.groupByKey
.mapValues { tiles => implicitly[Stitcher[V]].stitch(tiles, tileCols, tileRows) }
ContextRDD(tiled, newMd)
// val ceil = { x: Double => math.ceil(x).toInt }
// val tl = ld.tileLayout
// val oldTileCols = tl.tileCols
// val oldTileRows = tl.tileRows
// if(tileSize > oldTileSize) {
// val r = tileSize / oldTileSize
// val ntl = TileLayout(ceil(tl.layoutCols / r.toDouble), ceil(tl.layoutRows / r.toDouble), tileSize, tileSize)
// val layoutDefinition =
// LayoutDefinition(
// ld.extent,
// ntl
// )
// val bounds =
// md.getComponent[Bounds[K]] match {
// case KeyBounds(minKey, maxKey) =>
// val newMinKey = {
// val SpatialKey(minKeyCol, minKeyRow) = minKey.getComponent[SpatialKey]
// val sk = SpatialKey(minKeyCol / r, minKeyRow / r)
// minKey.setComponent[SpatialKey](sk)
// }
// val newMaxKey = {
// val SpatialKey(maxKeyCol, maxKeyRow) = maxKey.getComponent[SpatialKey]
// val sk = SpatialKey(ceil(maxKeyCol / r.toDouble), ceil(maxKeyRow / r.toDouble))
// maxKey.setComponent[SpatialKey](sk)
// }
// KeyBounds(newMinKey, newMaxKey)
// case EmptyBounds =>
// EmptyBounds
// }
// val newMd =
// md.setComponent[LayoutDefinition](layoutDefinition)
// .setComponent[Bounds[K]](bounds)
// val tiled =
// layer
// .map { case (key, tile) =>
// val sk = key.getComponent[SpatialKey]
// val newKey = key.setComponent[SpatialKey](SpatialKey(sk.col / r, sk.row / r))
// (newKey, (key, tile))
// }
// .groupByKey
// .map { case (key, tiles) =>
// val pieces =
// tiles.map { case (oldKey, oldTile) =>
// val SpatialKey(oldCol, oldRow) = oldKey.getComponent[SpatialKey]
// val updateCol = (oldCol % r) * oldTileSize
// val updateRow = (oldRow % r) * oldTileSize
// (oldTile, (updateCol, updateRow))
// }
// val newTile =
// implicitly[Stitcher[V]].stitch(pieces, tileSize, tileSize)
// (key, newTile)
// }
// ContextRDD(tiled, newMd)
// } else {
// val r = oldTileSize / tileSize
// val ntl = TileLayout(tl.layoutCols * r, tl.layoutRows * r, tileSize, tileSize)
// val layoutDefinition =
// LayoutDefinition(
// ld.extent,
// ntl
// )
// val bounds =
// md.getComponent[Bounds[K]] match {
// case KeyBounds(minKey, maxKey) =>
// val newMinKey = {
// val SpatialKey(minKeyCol, minKeyRow) = minKey.getComponent[SpatialKey]
// val sk = SpatialKey(minKeyCol * r, minKeyRow * r)
// minKey.setComponent[SpatialKey](sk)
// }
// val newMaxKey = {
// val SpatialKey(maxKeyCol, maxKeyRow) = maxKey.getComponent[SpatialKey]
// val sk = SpatialKey((maxKeyCol + 1) * r - 1, (maxKeyRow + 1) * r - 1)
// maxKey.setComponent[SpatialKey](sk)
// }
// KeyBounds(newMinKey, newMaxKey)
// case EmptyBounds =>
// EmptyBounds
// }
// val newMd =
// md.setComponent[LayoutDefinition](layoutDefinition)
// .setComponent[Bounds[K]](bounds)
// val layoutCols = r
// val layoutRows = r
// val tiled =
// layer.flatMap { case (key, tile) =>
// val result = mutable.ListBuffer[(K, V)]()
// val thisKey = key.getComponent[SpatialKey]
// for(layoutRow <- 0 until layoutRows) {
// for(layoutCol <- 0 until layoutCols) {
// val sk = SpatialKey(thisKey.col * layoutCols + layoutCol, thisKey.row * layoutRows + layoutRow)
// val newTile =
// tile.crop(
// GridBounds(
// layoutCol * tileSize,
// layoutRow * tileSize,
// (layoutCol + 1) * tileSize - 1,
// (layoutRow + 1) * tileSize - 1
// )
// )
// result += ((key.setComponent[SpatialKey](sk), newTile))
// }
// }
// result
// }
// ContextRDD(tiled, newMd)
// }
}
}
def regrid[
K: SpatialComponent: ClassTag,
V <: CellGrid: ClassTag: Stitcher: (? => CropMethods[V]): (? => TilePrototypeMethods[V]),
M: Component[?, LayoutDefinition]: Component[?, Bounds[K]]
](layer: RDD[(K, V)] with Metadata[M], tileSize: Int): RDD[(K, V)] with Metadata[M] = regrid(layer, tileSize, tileSize)
}