/
SinglebandGeoTiff.scala
212 lines (173 loc) · 8.12 KB
/
SinglebandGeoTiff.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
/*
* 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.io.geotiff
import geotrellis.util.ByteReader
import geotrellis.raster._
import geotrellis.raster.io.geotiff.reader.GeoTiffReader
import geotrellis.vector.Extent
import geotrellis.proj4.CRS
import geotrellis.raster.crop.Crop
import geotrellis.raster.resample.ResampleMethod
import spire.syntax.cfor._
case class SinglebandGeoTiff(
tile: Tile,
extent: Extent,
crs: CRS,
tags: Tags,
options: GeoTiffOptions,
overviews: List[GeoTiff[Tile]] = Nil
) extends GeoTiff[Tile] {
val cellType = tile.cellType
def mapTile(f: Tile => Tile): SinglebandGeoTiff =
SinglebandGeoTiff(f(tile), extent, crs, tags, options, overviews)
def withStorageMethod(storageMethod: StorageMethod): SinglebandGeoTiff =
SinglebandGeoTiff(tile, extent, crs, tags, options.copy(storageMethod = storageMethod), overviews)
def imageData: GeoTiffImageData =
tile match {
case gtt: GeoTiffTile => gtt
case _ => tile.toGeoTiffTile(options)
}
def crop(subExtent: Extent, options: Crop.Options): SinglebandGeoTiff = {
val raster: Raster[Tile] =
this.raster.crop(subExtent, options)
SinglebandGeoTiff(raster, subExtent, this.crs, this.tags, this.options, this.overviews)
}
def crop(colMax: Int, rowMax: Int): SinglebandGeoTiff =
crop(0, 0, colMax, rowMax)
def crop(colMin: Int, rowMin: Int, colMax: Int, rowMax: Int): SinglebandGeoTiff = {
val raster: Raster[Tile] =
this.raster.crop(colMin, rowMin, colMax, rowMax)
SinglebandGeoTiff(raster, raster._2, this.crs, this.tags, this.options, this.overviews)
}
def crop(gridBounds: GridBounds): SinglebandGeoTiff =
crop(gridBounds.colMin, gridBounds.rowMin, gridBounds.colMax, gridBounds.rowMax)
def crop(subExtent: Extent): SinglebandGeoTiff = crop(subExtent, Crop.Options.DEFAULT)
def crop(subExtent: Extent, cellSize: CellSize, resampleMethod: ResampleMethod, strategy: OverviewStrategy): SinglebandRaster =
getClosestOverview(cellSize, strategy)
.crop(subExtent, Crop.Options(clamp = false))
.resample(RasterExtent(subExtent, cellSize), resampleMethod, strategy)
def crop(windows: Seq[GridBounds]): Iterator[(GridBounds, Tile)] = tile match {
case geotiffTile: GeoTiffTile => geotiffTile.crop(windows)
case arrayTile: Tile => arrayTile.crop(windows)
}
def resample(rasterExtent: RasterExtent, resampleMethod: ResampleMethod, strategy: OverviewStrategy): SinglebandRaster =
getClosestOverview(cellSize, strategy)
.raster
.resample(rasterExtent, resampleMethod)
def buildOverview(resampleMethod: ResampleMethod, decimationFactor: Int, blockSize: Int): SinglebandGeoTiff = {
val overviewRasterExtent = RasterExtent(
extent,
cols = math.ceil(tile.cols.toDouble / decimationFactor).toInt,
rows = math.ceil(tile.rows.toDouble / decimationFactor).toInt
)
val segmentLayout: GeoTiffSegmentLayout = GeoTiffSegmentLayout(
totalCols = overviewRasterExtent.cols,
totalRows = overviewRasterExtent.rows,
storageMethod = Tiled(blockSize, blockSize),
interleaveMethod = PixelInterleave,
bandType = BandType.forCellType(tile.cellType))
// force ArrayTile to avoid costly compressor thrashing in GeoTiff segments when resample will stride segments
val segments: Seq[((Int, Int), Tile)] = Raster(tile.toArrayTile(), extent)
.resample(overviewRasterExtent, resampleMethod)
.tile
.split(segmentLayout.tileLayout)
.zipWithIndex
.map { case (tile, index) =>
val col = index % segmentLayout.tileLayout.layoutCols
val row = index / segmentLayout.tileLayout.layoutCols
((col, row), tile)
}
val storageMethod = Tiled(blockSize, blockSize)
val overviewOptions = options.copy(subfileType = Some(ReducedImage), storageMethod = storageMethod)
val overviewTile = GeoTiffBuilder[Tile].makeTile(
segments.toIterator, segmentLayout, cellType, options.compression
)
SinglebandGeoTiff(overviewTile, extent, crs, Tags.empty, overviewOptions)
}
def withOverviews(resampleMethod: ResampleMethod, decimations: List[Int] = Nil, blockSize: Int = GeoTiff.DefaultBlockSize): SinglebandGeoTiff = {
val overviewDecimations: List[Int] =
if (decimations.isEmpty) {
GeoTiff.defaultOverviewDecimations(tile.cols, tile.rows, blockSize)
} else {
decimations
}
if (overviewDecimations.isEmpty) {
this
} else {
// force ArrayTile to avoid costly compressor thrashing in GeoTiff segments when resample will stride segments
val arrayTile = tile.toArrayTile()
val staged = SinglebandGeoTiff(arrayTile, extent, crs, tags, options, Nil)
val overviews = overviewDecimations.map { (decimationFactor: Int) =>
staged.buildOverview(resampleMethod, decimationFactor, blockSize)
}
SinglebandGeoTiff(tile, extent, crs, tags, options, overviews)
}
}
def copy(tile: Tile, extent: Extent, crs: CRS, tags: Tags, options: GeoTiffOptions, overviews: List[GeoTiff[Tile]]): SinglebandGeoTiff =
SinglebandGeoTiff(tile, extent, crs, tags, options, overviews)
}
object SinglebandGeoTiff {
def apply(
tile: Tile,
extent: Extent,
crs: CRS
): SinglebandGeoTiff =
SinglebandGeoTiff(tile, extent, crs, Tags.empty, GeoTiffOptions.DEFAULT)
/** Read a single-band GeoTIFF file from a byte array.
* The GeoTIFF will be fully decompressed and held in memory.
*/
def apply(bytes: Array[Byte]): SinglebandGeoTiff =
GeoTiffReader.readSingleband(bytes)
/** Read a single-band GeoTIFF file from a byte array.
* If decompress = true, the GeoTIFF will be fully decompressed and held in memory.
*/
def apply(bytes: Array[Byte], decompress: Boolean, streaming: Boolean): SinglebandGeoTiff =
GeoTiffReader.readSingleband(bytes, decompress, streaming)
/** Read a single-band GeoTIFF file from the file at the given path.
* The GeoTIFF will be fully decompressed and held in memory.
*/
def apply(path: String): SinglebandGeoTiff =
GeoTiffReader.readSingleband(path)
def apply(path: String, e: Extent): SinglebandGeoTiff =
GeoTiffReader.readSingleband(path, e)
def apply(path: String, e: Option[Extent]): SinglebandGeoTiff =
GeoTiffReader.readSingleband(path, e)
/** Read a single-band GeoTIFF file from the file at the given path.
* If decompress = true, the GeoTIFF will be fully decompressed and held in memory.
*/
def apply(path: String, decompress: Boolean, streaming: Boolean): SinglebandGeoTiff =
GeoTiffReader.readSingleband(path, decompress, streaming)
def apply(byteReader: ByteReader): SinglebandGeoTiff =
GeoTiffReader.readSingleband(byteReader)
def apply(byteReader: ByteReader, e: Extent): SinglebandGeoTiff =
GeoTiffReader.readSingleband(byteReader, e)
def apply(byteReader: ByteReader, e: Option[Extent]): SinglebandGeoTiff =
GeoTiffReader.readSingleband(byteReader, e)
/** Read a single-band GeoTIFF file from the file at the given path.
* The tile data will remain tiled/striped and compressed in the TIFF format.
*/
def compressed(path: String): SinglebandGeoTiff =
GeoTiffReader.readSingleband(path, false, false)
/** Read a single-band GeoTIFF file from a byte array.
* The tile data will remain tiled/striped and compressed in the TIFF format.
*/
def compressed(bytes: Array[Byte]): SinglebandGeoTiff =
GeoTiffReader.readSingleband(bytes, false, false)
def streaming(path: String): SinglebandGeoTiff =
GeoTiffReader.readSingleband(path, false, true)
def streaming(byteReader: ByteReader): SinglebandGeoTiff =
GeoTiffReader.readSingleband(byteReader, false, true)
}