/
FilteringCOGLayerReader.scala
239 lines (204 loc) · 9.02 KB
/
FilteringCOGLayerReader.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
/*
* Copyright 2017 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.spark.io.cog
import geotrellis.raster.{CellGrid, RasterExtent}
import geotrellis.raster.io.geotiff.reader.GeoTiffReader
import geotrellis.spark._
import geotrellis.spark.io._
import geotrellis.spark.io.index.{Index, IndexRanges, MergeQueue}
import geotrellis.util._
import geotrellis.spark.util.KryoWrapper
import org.apache.spark.rdd._
import spray.json._
import org.apache.spark.SparkContext
import java.net.URI
import geotrellis.raster.io.geotiff.reader.TiffTagsReader
import scala.reflect._
abstract class FilteringCOGLayerReader[ID] extends COGLayerReader[ID] {
val attributeStore: AttributeStore
/** read
*
* This function will read an RDD layer based on a query.
*
* @param id The ID of the layer to be read
* @param rasterQuery The query that will specify the filter for this read.
* @param numPartitions The desired number of partitions in the resulting RDD.
*
* @tparam K Type of RDD Key (ex: SpatialKey)
* @tparam V Type of RDD Value (ex: Tile or MultibandTile )
*/
def read[
K: SpatialComponent: Boundable: JsonFormat: ClassTag,
V <: CellGrid: GeoTiffReader: ClassTag
](id: ID, rasterQuery: LayerQuery[K, TileLayerMetadata[K]], numPartitions: Int): RDD[(K, V)] with Metadata[TileLayerMetadata[K]]
def baseRead[
K: SpatialComponent: Boundable: JsonFormat: ClassTag,
V <: CellGrid: GeoTiffReader: ClassTag
](
id: LayerId,
tileQuery: LayerQuery[K, TileLayerMetadata[K]],
numPartitions: Int,
getKeyPath: (ZoomRange, Int) => BigInt => String,
pathExists: String => Boolean, // check the path above exists
fullPath: String => URI, // add an fs prefix
defaultThreads: Int
)(implicit sc: SparkContext,
getByteReader: URI => ByteReader,
idToLayerId: ID => LayerId
): RDD[(K, V)] with Metadata[TileLayerMetadata[K]] = {
val COGLayerStorageMetadata(cogLayerMetadata, keyIndexes) =
try {
attributeStore.read[COGLayerStorageMetadata[K]](LayerId(id.name, 0), "cog_metadata")
} catch {
// to follow GeoTrellis Layer Readers logic
case e: AttributeNotFoundError => throw new LayerNotFoundError(id).initCause(e)
}
val metadata = cogLayerMetadata.tileLayerMetadata(id.zoom)
val queryKeyBounds: Seq[KeyBounds[K]] = tileQuery(metadata)
val readDefinitions: Seq[(ZoomRange, Seq[(SpatialKey, Int, TileBounds, Seq[(TileBounds, SpatialKey)])])] =
cogLayerMetadata.getReadDefinitions(queryKeyBounds, id.zoom)
readDefinitions.headOption.map(_._1) match {
case Some(zoomRange) => {
val baseKeyIndex = keyIndexes(zoomRange)
val maxWidth = Index.digits(baseKeyIndex.toIndex(baseKeyIndex.keyBounds.maxKey))
val keyPath: BigInt => String = getKeyPath(zoomRange, maxWidth)
val decompose = (bounds: KeyBounds[K]) => baseKeyIndex.indexRanges(bounds)
val baseLayout = cogLayerMetadata.layoutForZoom(zoomRange.minZoom)
val layout = cogLayerMetadata.layoutForZoom(id.zoom)
val baseKeyBounds = cogLayerMetadata.zoomRangeInfoFor(zoomRange.minZoom)._2
def transformKeyBounds(keyBounds: KeyBounds[K]): KeyBounds[K] = {
val KeyBounds(minKey, maxKey) = keyBounds
val extent = layout.extent
val sourceRe = RasterExtent(extent, layout.layoutCols, layout.layoutRows)
val targetRe = RasterExtent(extent, baseLayout.layoutCols, baseLayout.layoutRows)
val minSpatialKey = minKey.getComponent[SpatialKey]
val (minCol, minRow) = {
val (x, y) = sourceRe.gridToMap(minSpatialKey.col, minSpatialKey.row)
targetRe.mapToGrid(x, y)
}
val maxSpatialKey = maxKey.getComponent[SpatialKey]
val (maxCol, maxRow) = {
val (x, y) = sourceRe.gridToMap(maxSpatialKey.col, maxSpatialKey.row)
targetRe.mapToGrid(x, y)
}
KeyBounds(
minKey.setComponent(SpatialKey(minCol, minRow)),
maxKey.setComponent(SpatialKey(maxCol, maxRow))
)
}
val baseQueryKeyBounds: Seq[KeyBounds[K]] =
queryKeyBounds
.flatMap { qkb =>
transformKeyBounds(qkb).intersect(baseKeyBounds) match {
case EmptyBounds => None
case kb: KeyBounds[K] => Some(kb)
}
}
.distinct
val rdd =
FilteringCOGLayerReader
.read[K, V](
keyPath = keyPath,
pathExists = pathExists,
fullPath = fullPath,
baseQueryKeyBounds = baseQueryKeyBounds,
decomposeBounds = decompose,
readDefinitions = readDefinitions.flatMap(_._2).groupBy(_._1),
threads = defaultThreads,
numPartitions = Some(numPartitions)
)
new ContextRDD(rdd, metadata)
}
case None =>
new ContextRDD(sc.parallelize(Seq()), metadata.setComponent[Bounds[K]](EmptyBounds))
}
}
def read[
K: SpatialComponent: Boundable: JsonFormat: ClassTag,
V <: CellGrid: GeoTiffReader: ClassTag
](id: ID, rasterQuery: LayerQuery[K, TileLayerMetadata[K]]): RDD[(K, V)] with Metadata[TileLayerMetadata[K]] =
read(id, rasterQuery, defaultNumPartitions)
def read[
K: SpatialComponent: Boundable: JsonFormat: ClassTag,
V <: CellGrid: GeoTiffReader: ClassTag
](id: ID, numPartitions: Int): RDD[(K, V)] with Metadata[TileLayerMetadata[K]] =
read(id, new LayerQuery[K, TileLayerMetadata[K]], numPartitions)
def query[
K: SpatialComponent: Boundable: JsonFormat: ClassTag,
V <: CellGrid: GeoTiffReader: ClassTag
](layerId: ID): BoundLayerQuery[K, TileLayerMetadata[K], RDD[(K, V)] with Metadata[TileLayerMetadata[K]]] =
new BoundLayerQuery(new LayerQuery, read(layerId, _))
def query[
K: SpatialComponent: Boundable: JsonFormat: ClassTag,
V <: CellGrid: GeoTiffReader: ClassTag
](layerId: ID, numPartitions: Int): BoundLayerQuery[K, TileLayerMetadata[K], RDD[(K, V)] with Metadata[TileLayerMetadata[K]]] =
new BoundLayerQuery(new LayerQuery, read(layerId, _, numPartitions))
}
object FilteringCOGLayerReader {
def read[
K: SpatialComponent: Boundable: JsonFormat: ClassTag,
V <: CellGrid: GeoTiffReader
](
keyPath: BigInt => String, // keyPath
pathExists: String => Boolean, // check the path above exists
fullPath: String => URI, // add an fs prefix
baseQueryKeyBounds: Seq[KeyBounds[K]], // each key here represents a COG filename
decomposeBounds: KeyBounds[K] => Seq[(BigInt, BigInt)],
readDefinitions: Map[SpatialKey, Seq[(SpatialKey, Int, TileBounds, Seq[(TileBounds, SpatialKey)])]],
threads: Int,
numPartitions: Option[Int] = None
)(implicit sc: SparkContext, getByteReader: URI => ByteReader): RDD[(K, V)] = {
if (baseQueryKeyBounds.isEmpty) return sc.emptyRDD[(K, V)]
val kwFormat = KryoWrapper(implicitly[JsonFormat[K]])
val ranges = if (baseQueryKeyBounds.length > 1)
MergeQueue(baseQueryKeyBounds.flatMap(decomposeBounds))
else
baseQueryKeyBounds.flatMap(decomposeBounds)
val bins = IndexRanges.bin(ranges, numPartitions.getOrElse(sc.defaultParallelism))
sc.parallelize(bins, bins.size)
.mapPartitions { partition: Iterator[Seq[(BigInt, BigInt)]] =>
val keyFormat = kwFormat.value
partition flatMap { seq =>
LayerReader.njoin[K, V](seq.toIterator, threads) { index: BigInt =>
if (!pathExists(keyPath(index))) Vector()
else {
val uri = fullPath(keyPath(index))
val byteReader: ByteReader = uri
val baseKey = TiffTagsReader
.read(byteReader)
.tags
.headTags(GTKey)
.parseJson
.convertTo[K](keyFormat)
readDefinitions
.get(baseKey.getComponent[SpatialKey])
.toVector
.flatten
.flatMap { case (spatialKey, overviewIndex, _, seq) =>
val key = baseKey.setComponent(spatialKey)
val tiff = GeoTiffReader[V].read(uri, decompress = false, streaming = true).getOverview(overviewIndex)
val map = seq.map { case (gb, sk) => gb -> key.setComponent(sk) }.toMap
tiff
.crop(map.keys.toSeq)
.flatMap { case (k, v) => map.get(k).map(i => i -> v) }
.toVector
}
}
}
}
}
}
}