/
RasterReader.scala
233 lines (207 loc) · 8.9 KB
/
RasterReader.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
/*
* 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
import geotrellis.proj4._
import geotrellis.raster._
import geotrellis.raster.io.geotiff._
import geotrellis.raster.io.geotiff.reader.GeoTiffReader
import geotrellis.spark._
import geotrellis.util.{ByteReader, StreamingByteReader}
import geotrellis.vector.ProjectedExtent
import spire.syntax.cfor._
import java.time.format.DateTimeFormatter
import java.time.{ZoneOffset, ZonedDateTime}
/**
* Type class to read a raster either fully or partially from a ByteReader.
* This abstracts over the different ways to represent a GeoTiff values and different ways to key it.
*
* Option object is a type parameter such that novel ways of GeoTiff parsing can be provided by the user.
*
* @tparam O Options type that is used to configure the raster reading
* @tparam R Result of reading the raster bytes either fully or as a pixel window
*/
trait RasterReader[-O, R] extends Serializable {
def readFully(byteReader: ByteReader, options: O): R
def readWindow(byteReader: StreamingByteReader, pixelWindow: GridBounds, options: O): R
def readWindows(gbs: Array[GridBounds], info: GeoTiffReader.GeoTiffInfo, options: O): Iterator[R]
}
object RasterReader {
trait Options {
def crs: Option[CRS]
def timeTag: String
def timeFormat: String
lazy val timeFormatter = DateTimeFormatter.ofPattern(timeFormat).withZone(ZoneOffset.UTC)
def parseTime(tags: Tags): ZonedDateTime = {
val dateTimeString = tags.headTags.getOrElse(timeTag, sys.error(s"There is no tag $timeTag in the GeoTiff header"))
ZonedDateTime.from(timeFormatter.parse(dateTimeString))
}
}
private def best(m: Int, n: Int): Int = {
var i: Int = 1
var result: Int = m
// Search for the largest factor of n that is > 1 and <= m. If
// one cannot be found, give up and return m.
while (i < math.sqrt(n) && result == m) {
if ((n % i == 0) && ((n/i) <= m)) result = (n/i)
i += 1
}
result
}
def listWindows(
cols: Int, rows: Int, maxSize: Int,
segCols: Int, segRows: Int
): Array[GridBounds] = {
val colSize: Int = if (maxSize >= segCols) segCols; else best(maxSize, segCols)
val rowSize: Int = if (maxSize >= segRows) segRows; else best(maxSize, segRows)
val windows = listWindows(cols, rows, colSize, rowSize)
windows
}
/** List all pixel windows that cover a grid of given size */
def listWindows(cols: Int, rows: Int, colSize: Int, rowSize: Int): Array[GridBounds] = {
val result = scala.collection.mutable.ArrayBuffer[GridBounds]()
cfor(0)(_ < cols, _ + colSize) { col =>
cfor(0)(_ < rows, _ + rowSize) { row =>
result +=
GridBounds(
col,
row,
math.min(col + colSize - 1, cols - 1),
math.min(row + rowSize - 1, rows - 1)
)
}
}
result.toArray
}
/** List all pixel windows that cover a grid of given size */
def listWindows(cols: Int, rows: Int, maxTileSize: Option[Int]): Array[GridBounds] = {
val result = scala.collection.mutable.ArrayBuffer[GridBounds]()
maxTileSize match {
case Some(tileSize) =>
cfor(0)(_ < cols, _ + tileSize) { col =>
cfor(0)(_ < rows, _ + tileSize) { row =>
result +=
GridBounds(
col,
row,
math.min(col + tileSize - 1, cols - 1),
math.min(row + tileSize - 1, rows - 1)
)
}
}
case None =>
result += GridBounds(0, 0, cols - 1, rows - 1)
}
result.toArray
}
implicit def singlebandGeoTiffReader = new RasterReader[Options, (ProjectedExtent, Tile)] {
def readFully(byteReader: ByteReader, options: Options) = {
val geotiff = SinglebandGeoTiff(byteReader)
val raster: Raster[Tile] = geotiff.raster
(ProjectedExtent(raster.extent, options.crs.getOrElse(geotiff.crs)), raster.tile)
}
def readWindow(streamingByteReader: StreamingByteReader, pixelWindow: GridBounds, options: Options) = {
val geotiff = SinglebandGeoTiff.streaming(streamingByteReader)
val raster: Raster[Tile] = geotiff.raster.crop(pixelWindow)
(ProjectedExtent(raster.extent, options.crs.getOrElse(geotiff.crs)), raster.tile)
}
def readWindows(gbs: Array[GridBounds], info: GeoTiffReader.GeoTiffInfo, options: Options) = {
val geoTiff = GeoTiffReader.geoTiffSinglebandTile(info)
val gridBounds = geoTiff.gridBounds
gbs
.filter(gridBounds.contains)
.map { gb => (ProjectedExtent(info.mapTransform(gb), options.crs.getOrElse(info.crs)), geoTiff.crop(gb)) }
.toIterator
}
}
implicit def multibandGeoTiffReader = new RasterReader[Options, (ProjectedExtent, MultibandTile)] {
def readFully(byteReader: ByteReader, options: Options) = {
val geotiff = MultibandGeoTiff(byteReader)
val raster: Raster[MultibandTile] = geotiff.raster
(ProjectedExtent(raster.extent, options.crs.getOrElse(geotiff.crs)), raster.tile)
}
def readWindow(streamingByteReader: StreamingByteReader, pixelWindow: GridBounds, options: Options) = {
val geotiff = MultibandGeoTiff.streaming(streamingByteReader)
val raster: Raster[MultibandTile] = geotiff.raster.crop(pixelWindow)
(ProjectedExtent(raster.extent, options.crs.getOrElse(geotiff.crs)), raster.tile)
}
def readWindows(gbs: Array[GridBounds], info: GeoTiffReader.GeoTiffInfo, options: Options) = {
val geoTiff = GeoTiffReader.geoTiffMultibandTile(info)
val gridBounds = geoTiff.gridBounds
gbs
.filter(gridBounds.contains)
.map { gb => (ProjectedExtent(info.mapTransform(gb), options.crs.getOrElse(info.crs)), geoTiff.crop(gb)) }
.toIterator
}
}
implicit def temporalSinglebandGeoTiffReader = new RasterReader[Options, (TemporalProjectedExtent, Tile)] {
def readFully(byteReader: ByteReader, options: Options) = {
val geotiff = SinglebandGeoTiff(byteReader)
val raster: Raster[Tile] = geotiff.raster
val time = options.parseTime(geotiff.tags)
val crs = options.crs.getOrElse(geotiff.crs)
(TemporalProjectedExtent(raster.extent, crs, time), raster.tile)
}
def readWindow(streamingByteReader: StreamingByteReader, pixelWindow: GridBounds, options: Options) = {
val geotiff = SinglebandGeoTiff.streaming(streamingByteReader)
val raster: Raster[Tile] = geotiff.raster.crop(pixelWindow)
val time = options.parseTime(geotiff.tags)
val crs = options.crs.getOrElse(geotiff.crs)
(TemporalProjectedExtent(raster.extent, crs, time), raster.tile)
}
def readWindows(gbs: Array[GridBounds], info: GeoTiffReader.GeoTiffInfo, options: Options) = {
val geoTiff = GeoTiffReader.geoTiffSinglebandTile(info)
val gridBounds = geoTiff.gridBounds
gbs
.filter(gridBounds.contains)
.map { gb =>
(TemporalProjectedExtent(
info.mapTransform(gb),
crs = options.crs.getOrElse(info.crs),
options.parseTime(info.tags)), geoTiff.crop(gb))
}
.toIterator
}
}
implicit def temporalMultibandGeoTiffReader = new RasterReader[Options, (TemporalProjectedExtent, MultibandTile)] {
def readFully(byteReader: ByteReader, options: Options) = {
val geotiff = MultibandGeoTiff(byteReader)
val raster: Raster[MultibandTile] = geotiff.raster
val time = options.parseTime(geotiff.tags)
val crs = options.crs.getOrElse(geotiff.crs)
(TemporalProjectedExtent(raster.extent, crs, time), raster.tile)
}
def readWindow(streamingByteReader: StreamingByteReader, pixelWindow: GridBounds, options: Options) = {
val geotiff = MultibandGeoTiff.streaming(streamingByteReader)
val raster: Raster[MultibandTile] = geotiff.raster.crop(pixelWindow)
val time = options.parseTime(geotiff.tags)
val crs = options.crs.getOrElse(geotiff.crs)
(TemporalProjectedExtent(raster.extent, crs, time), raster.tile)
}
def readWindows(gbs: Array[GridBounds], info: GeoTiffReader.GeoTiffInfo, options: Options) = {
val geoTiff = GeoTiffReader.geoTiffMultibandTile(info)
val gridBounds = geoTiff.gridBounds
gbs
.filter(gridBounds.contains)
.map { gb =>
(TemporalProjectedExtent(
info.mapTransform(gb),
options.crs.getOrElse(info.crs),
options.parseTime(info.tags)), geoTiff.crop(gb))
}
.toIterator
}
}
}