/
S3GeoTiffRDD.scala
473 lines (426 loc) · 22.6 KB
/
S3GeoTiffRDD.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* 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.spark.io.s3
import geotrellis.proj4._
import geotrellis.raster._
import geotrellis.raster.io.geotiff.tags.TiffTags
import geotrellis.spark._
import geotrellis.raster.io.geotiff.reader.GeoTiffReader
import geotrellis.spark.io.{GeoTiffInfoReader, RasterReader}
import geotrellis.spark.io.s3.util.S3RangeReader
import geotrellis.util.{LazyLogging, StreamingByteReader}
import geotrellis.vector._
import org.apache.hadoop.conf.Configuration
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
import org.apache.spark.storage.StorageLevel
import com.amazonaws.services.s3.model._
import java.net.URI
import java.nio.ByteBuffer
import com.typesafe.config.ConfigFactory
/**
* The S3GeoTiffRDD object allows for the creation of whole or windowed RDD[(K, V)]s from files on S3.
*/
object S3GeoTiffRDD extends LazyLogging {
final val GEOTIFF_TIME_TAG_DEFAULT = "TIFFTAG_DATETIME"
final val GEOTIFF_TIME_FORMAT_DEFAULT = "yyyy:MM:dd HH:mm:ss"
lazy val windowSize: Option[Int] = try {
Some(ConfigFactory.load().getInt("geotrellis.s3.rdd.read.windowSize"))
} catch {
case _: Throwable =>
logger.warn("geotrellis.s3.rdd.read.windowSize is not set in .conf file.")
None
}
/**
* This case class contains the various parameters one can set when reading RDDs from S3 using Spark.
*
* TODO: Add persistLevel option
*
* @param tiffExtensions Read all file with an extension contained in the given list.
* @param crs Override CRS of the input files. If [[None]], the reader will use the file's original CRS.
* @param timeTag Name of tiff tag containing the timestamp for the tile.
* @param timeFormat Pattern for [[java.time.format.DateTimeFormatter]] to parse timeTag.
* @param maxTileSize Maximum allowed size of each tiles in output RDD.
* May result in a one input GeoTiff being split amongst multiple records if it exceeds this size.
* If no maximum tile size is specific, then each file file is read fully.
* 1024 by defaut.
* @param numPartitions How many partitions Spark should create when it repartitions the data.
* @param partitionBytes Desired partition size in bytes, at least one item per partition will be assigned.
This option is incompatible with the maxTileSize option.
* 128 Mb by default.
* @param chunkSize How many bytes should be read in at a time.
* @param delimiter Delimiter to use for S3 objet listings. See
* @param getS3Client A function to instantiate an S3Client. Must be serializable.
*/
case class Options(
tiffExtensions: Seq[String] = Seq(".tif", ".TIF", ".tiff", ".TIFF"),
crs: Option[CRS] = None,
timeTag: String = GEOTIFF_TIME_TAG_DEFAULT,
timeFormat: String = GEOTIFF_TIME_FORMAT_DEFAULT,
maxTileSize: Option[Int] = None,
numPartitions: Option[Int] = None,
partitionBytes: Option[Long] = Some(128l * 1024 * 1024),
chunkSize: Option[Int] = None,
delimiter: Option[String] = None,
getS3Client: () => S3Client = () => S3Client.DEFAULT
) extends RasterReader.Options
val contingencyTileSize = 512
object Options {
def DEFAULT = Options()
}
/**
* Create Configuration for [[S3InputFormat]] based on parameters and options.
* Important: won't pass partitionBytes into hadoop configuration if numPartition options is set.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param options An instance of [[Options]] that contains any user defined or default settings.
*/
private def configuration(bucket: String, prefix: String, options: S3GeoTiffRDD.Options)(implicit sc: SparkContext): Configuration = {
if(options.numPartitions.isDefined && options.partitionBytes.isDefined)
logger.warn("Both numPartitions and partitionBytes options are set. " +
"Only numPartitions would be passed into hadoop configuration.")
val conf = sc.hadoopConfiguration
S3InputFormat.setBucket(conf, bucket)
S3InputFormat.setPrefix(conf, prefix)
S3InputFormat.setExtensions(conf, options.tiffExtensions)
S3InputFormat.setCreateS3Client(conf, options.getS3Client)
options.numPartitions
.fold(S3InputFormat.removePartitionCount(conf)) { n =>
S3InputFormat.setPartitionCount(conf, n)
S3InputFormat.removePartitionBytes(conf)
}
if(options.numPartitions.isEmpty)
options.partitionBytes
.fold(S3InputFormat.removePartitionBytes(conf))(S3InputFormat.setPartitionBytes(conf, _))
options.delimiter.fold(S3InputFormat.removeDelimiter(conf))(S3InputFormat.setDelimiter(conf, _))
conf
}
/**
* A helper function to get the maximum (linear) tile size.
*/
private def getMaxSize(options: Options) = {
(options.maxTileSize, windowSize) match {
case (Some(maxTileSize), Some(windowSize)) => math.min(maxTileSize, windowSize)
case (Some(maxTileSize), None) => maxTileSize
case (None, Some(windowSize)) => windowSize
case _ => {
val size = Options.DEFAULT.maxTileSize match {
case Some(maxTileSize) => maxTileSize
case None => contingencyTileSize
}
logger.warn(s"Neither maxTileSize nor windowSize was given, defaulting to $size.")
size
}
}
}
/**
* Creates a RDD[(K, V)] whose K and V on the type of the GeoTiff that is going to be read in.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param uriToKey Function to transform input key basing on the URI information.
* @param options An instance of [[Options]] that contains any user defined or default settings.
* @param geometry An optional geometry to filter by. If this is provided, it is assumed that all GeoTiffs are in the same CRS, and that this geometry is in that CRS.
*/
def apply[I, K, V](
bucket: String, prefix: String,
uriToKey: (URI, I) => K,
options: Options,
geometry: Option[Geometry]
)(implicit sc: SparkContext, rr: RasterReader[Options, (I, V)]): RDD[(K, V)] = {
val conf = configuration(bucket, prefix, options)
lazy val sourceGeoTiffInfo = S3GeoTiffInfoReader(bucket, prefix, options)
(options.maxTileSize, options.partitionBytes) match {
case (_, Some(partitionBytes)) => {
val maxSize = getMaxSize(options)
val windows: RDD[(String, Array[GridBounds])] =
sourceGeoTiffInfo.windowsByBytes(
partitionBytes,
maxSize,
geometry
)
windows.persist()
val windowCount = windows.count.toInt
logger.info(s"Repartition into ${windowCount} partitions.")
val repartition =
if (windowCount > windows.partitions.length) windows.repartition(windowCount)
else windows
val result = repartition.flatMap { case (path, windowBounds) =>
rr.readWindows(windowBounds, sourceGeoTiffInfo.getGeoTiffInfo(path), options).map { case (k, v) =>
uriToKey(new URI(path), k) -> v
}
}
windows.unpersist()
result
}
case (Some(_), _) =>
val objectRequestsToDimensions: RDD[(GetObjectRequest, (Int, Int))] =
sc.newAPIHadoopRDD(
conf,
classOf[TiffTagsS3InputFormat],
classOf[GetObjectRequest],
classOf[TiffTags]
).mapValues { tiffTags => (tiffTags.cols, tiffTags.rows) }
apply[I, K, V](objectRequestsToDimensions, uriToKey, options, sourceGeoTiffInfo)
case _ =>
sc.newAPIHadoopRDD(
conf,
classOf[BytesS3InputFormat],
classOf[String],
classOf[Array[Byte]]
).mapPartitions(
_.map { case (key, bytes) =>
val (k, v) = rr.readFully(ByteBuffer.wrap(bytes), options)
uriToKey(new URI(key), k) -> v
},
preservesPartitioning = true
)
}
}
/**
* Creates a RDD[(K, V)] whose K and V on the type of the GeoTiff that is going to be read in.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param uriToKey Function to transform input key basing on the URI information.
* @param options An instance of [[Options]] that contains any user defined or default settings.
* @param geometry An optional geometry to filter by. If this is provided, it is assumed that all GeoTiffs are in the same CRS, and that this geometry is in that CRS.
*/
def apply[I, K, V](
bucket: String, prefix: String,
uriToKey: (URI, I) => K,
options: Options
)(implicit sc: SparkContext, rr: RasterReader[Options, (I, V)]): RDD[(K, V)] = {
apply(bucket, prefix, uriToKey, options, None)
}
/**
* Creates a RDD[(K, V)] whose K and V on the type of the GeoTiff that is going to be read in.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param options An instance of [[Options]] that contains any user defined or default settings.
*/
def apply[K, V](bucket: String, prefix: String, options: Options)
(implicit sc: SparkContext, rr: RasterReader[Options, (K, V)]): RDD[(K, V)] =
apply[K, K, V](bucket, prefix, (_: URI, key: K) => key, options)
/**
* Creates a RDD[(K, V)] whose K and V depends on the type of the GeoTiff that is going to be read in.
*
* @param objectRequestsToDimensions A RDD of GetObjectRequest of a given GeoTiff and its cols and rows as a (Int, Int).
* @param uriToKey function to transform input key basing on the URI information.
* @param options An instance of [[Options]] that contains any user defined or default settings.
*/
def apply[I, K, V](objectRequestsToDimensions: RDD[(GetObjectRequest, (Int, Int))], uriToKey: (URI, I) => K, options: Options, sourceGeoTiffInfo: => GeoTiffInfoReader)
(implicit rr: RasterReader[Options, (I, V)]): RDD[(K, V)] = {
val windows =
objectRequestsToDimensions
.flatMap { case (objectRequest, (cols, rows)) =>
val bucket = objectRequest.getBucketName
val key = objectRequest.getKey
val layout = sourceGeoTiffInfo.getGeoTiffInfo(s"s3://$bucket/$key").segmentLayout.tileLayout
val maxSize = getMaxSize(options)
RasterReader
.listWindows(cols, rows, maxSize, layout.tileCols, layout.tileRows)
.map((objectRequest, _))
}
// Windowed reading may have produced unbalanced partitions due to files of differing size
val repartitioned =
options.numPartitions match {
case Some(p) =>
logger.info(s"repartition into $p partitions.")
windows.repartition(p)
case None =>
options.partitionBytes match {
case Some(byteCount) =>
sourceGeoTiffInfo.estimatePartitionsNumber(byteCount, options.maxTileSize) match {
case Some(numPartitions) if numPartitions != windows.partitions.length =>
logger.info(s"repartition into $numPartitions partitions.")
windows.repartition(numPartitions)
case _ => windows
}
case _ =>
windows
}
}
repartitioned.map { case (objectRequest: GetObjectRequest, pixelWindow: GridBounds) =>
val reader = options.chunkSize match {
case Some(chunkSize) =>
StreamingByteReader(S3RangeReader(objectRequest, options.getS3Client()), chunkSize)
case None =>
StreamingByteReader(S3RangeReader(objectRequest, options.getS3Client()))
}
val (k, v) = rr.readWindow(reader, pixelWindow, options)
uriToKey(new URI(s"s3://${objectRequest.getBucketName}/${objectRequest.getKey}"), k) -> v
}
}
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as singleband GeoTiffs.
* If a GeoTiff contains multiple bands, only the first will be read.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param uriToKey function to transform input key basing on the URI information.
*/
def singleband[I, K](bucket: String, prefix: String, uriToKey: (URI, I) => K, options: Options)(implicit sc: SparkContext, rr: RasterReader[Options, (I, Tile)]): RDD[(K, Tile)] =
apply[I, K, Tile](bucket, prefix, uriToKey, options)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as singleband GeoTiffs.
* If a GeoTiff contains multiple bands, only the first will be read.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
*/
def singleband[K](bucket: String, prefix: String, options: Options)(implicit sc: SparkContext, rr: RasterReader[Options, (K, Tile)]): RDD[(K, Tile)] =
apply[K, Tile](bucket, prefix, options)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as multiband GeoTiffs.
* If a GeoTiff contains multiple bands, only the first will be read.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param uriToKey function to transform input key basing on the URI information.
*/
def multiband[I, K](bucket: String, prefix: String, uriToKey: (URI, I) => K, options: Options)(implicit sc: SparkContext, rr: RasterReader[Options, (I, MultibandTile)]): RDD[(K, MultibandTile)] =
apply[I, K, MultibandTile](bucket, prefix, uriToKey, options)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as multiband GeoTiffs.
* If a GeoTiff contains multiple bands, only the first will be read.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
*/
def multiband[K](bucket: String, prefix: String, options: Options)(implicit sc: SparkContext, rr: RasterReader[Options, (K, MultibandTile)]): RDD[(K, MultibandTile)] =
apply[K, MultibandTile](bucket, prefix, options)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as singleband GeoTiffs.
* If a GeoTiff contains multiple bands, only the first will be read.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
*/
def spatial(bucket: String, prefix: String)(implicit sc: SparkContext): RDD[(ProjectedExtent, Tile)] =
spatial(bucket, prefix, Options.DEFAULT)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as singleband tiles.
* If a GeoTiff contains multiple bands, only the first will be read.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param options An instance of [[Options]] that contains any user defined or default settings.
*/
def spatial(bucket: String, prefix: String, options: Options)(implicit sc: SparkContext): RDD[(ProjectedExtent, Tile)] =
singleband[ProjectedExtent](bucket, prefix, options)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as singleband tiles.
* If a GeoTiff contains multiple bands, only the first will be read.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param uriToKey function to transform input key basing on the URI information.
* @param options An instance of [[Options]] that contains any user defined or default settings.
*/
def spatial(bucket: String, prefix: String, uriToKey: (URI, ProjectedExtent) => ProjectedExtent, options: Options)(implicit sc: SparkContext): RDD[(ProjectedExtent, Tile)] =
singleband[ProjectedExtent, ProjectedExtent](bucket, prefix, uriToKey, options)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as multiband tiles.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
*/
def spatialMultiband(bucket: String, prefix: String)(implicit sc: SparkContext): RDD[(ProjectedExtent, MultibandTile)] =
spatialMultiband(bucket, prefix, Options.DEFAULT)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as multiband tiles.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param options An instance of [[Options]] that contains any user defined or default settings.
*/
def spatialMultiband(bucket: String, prefix: String, options: Options)(implicit sc: SparkContext): RDD[(ProjectedExtent, MultibandTile)] =
multiband[ProjectedExtent](bucket, prefix, options)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as multiband tiles.
* If a GeoTiff contains multiple bands, only the first will be read.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param uriToKey function to transform input key basing on the URI information.
* @param options An instance of [[Options]] that contains any user defined or default settings.
*/
def spatialMultiband(bucket: String, prefix: String, uriToKey: (URI, ProjectedExtent) => ProjectedExtent, options: Options)(implicit sc: SparkContext): RDD[(ProjectedExtent, MultibandTile)] =
multiband[ProjectedExtent, ProjectedExtent](bucket, prefix, uriToKey, options)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as singleband tiles.
* Will parse a timestamp from the default tiff tags to associate with each file.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
*/
def temporal(bucket: String, prefix: String)(implicit sc: SparkContext): RDD[(TemporalProjectedExtent, Tile)] =
temporal(bucket, prefix, Options.DEFAULT)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as singleband tiles.
* Will parse a timestamp from a tiff tags specified in options to associate with each tile.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param options Options for the reading process. Including the timestamp tiff tag and its pattern.
*/
def temporal(bucket: String, prefix: String, options: Options)(implicit sc: SparkContext): RDD[(TemporalProjectedExtent, Tile)] =
singleband[TemporalProjectedExtent](bucket, prefix, options)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as singleband tiles.
* Will parse a timestamp from a tiff tags specified in options to associate with each tile.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param uriToKey function to transform input key basing on the URI information.
* @param options Options for the reading process. Including the timestamp tiff tag and its pattern.
*/
def temporal(bucket: String, prefix: String, uriToKey: (URI, TemporalProjectedExtent) => TemporalProjectedExtent, options: Options)(implicit sc: SparkContext): RDD[(TemporalProjectedExtent, Tile)] =
singleband[TemporalProjectedExtent, TemporalProjectedExtent](bucket, prefix, uriToKey, options)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as multiband tiles.
* Will parse a timestamp from a tiff tags specified in options to associate with each tile.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
*/
def temporalMultiband(bucket: String, prefix: String)(implicit sc: SparkContext): RDD[(TemporalProjectedExtent, MultibandTile)] =
temporalMultiband(bucket, prefix, Options.DEFAULT)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as multiband tiles.
* Will parse a timestamp from a tiff tags specified in options to associate with each tile.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param options Options for the reading process. Including the timestamp tiff tag and its pattern.
*/
def temporalMultiband(bucket: String, prefix: String, options: Options)(implicit sc: SparkContext): RDD[(TemporalProjectedExtent, MultibandTile)] =
multiband[TemporalProjectedExtent](bucket, prefix, options)
/**
* Creates RDD that will read all GeoTiffs in the given bucket and prefix as multiband tiles.
* Will parse a timestamp from a tiff tags specified in options to associate with each tile.
*
* @param bucket Name of the bucket on S3 where the files are kept.
* @param prefix Prefix of all of the keys on S3 that are to be read in.
* @param uriToKey function to transform input key basing on the URI information.
* @param options Options for the reading process. Including the timestamp tiff tag and its pattern.
*/
def temporalMultiband(bucket: String, prefix: String, uriToKey: (URI, TemporalProjectedExtent) => TemporalProjectedExtent, options: Options)(implicit sc: SparkContext): RDD[(TemporalProjectedExtent, MultibandTile)] =
multiband[TemporalProjectedExtent, TemporalProjectedExtent](bucket, prefix, uriToKey, options)
}