/
HadoopSinglebandGeoTiffCollectionLayerReader2.scala
147 lines (124 loc) · 4.32 KB
/
HadoopSinglebandGeoTiffCollectionLayerReader2.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
package geotrellis.spark.io.hadoop.geotiff
import geotrellis.proj4.{CRS, WebMercator}
import geotrellis.raster.io.geotiff.reader.{GeoTiffReader, TiffTagsReader}
import geotrellis.raster.{CellType, GridBounds, Raster, RasterExtent, SinglebandRaster, Tile}
import geotrellis.spark.tiling.{LayoutDefinition, ZoomedLayoutScheme}
import geotrellis.spark.{Bounds, ContextCollection, EmptyBounds, KeyBounds, LayerId, SpatialKey, TileLayerMetadata}
import geotrellis.util.StreamingByteReader
import geotrellis.vector.{Extent, ProjectedExtent}
import geotrellis.raster.io.geotiff.tags.TiffTags
import java.net.URI
import geotrellis.spark.io.hadoop.{HdfsRangeReader, HdfsUtils}
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.Path
/** Approach with TiffTags stored in a DB */
/* case class HadoopSinglebandGeoTiffCollectionLayerReader2(
// seq can be stored in some backend
/** This should be done in a separate interface */
attributeStore: HadoopGeoTiffAttributeStore,
layoutScheme: ZoomedLayoutScheme,
discriminator: (String, String) => Boolean,
conf: Configuration
) {
def read(layerId: LayerId)(x: Int, y: Int): Raster[Tile] = {
val layout =
layoutScheme
.levelForZoom(layerId.zoom)
.layout
val mapTransform = layout.mapTransform
val keyExtent: Extent = mapTransform(SpatialKey(x, y))
attributeStore
.get(
layerName = Some(layerId.name),
extent = Some(ProjectedExtent(keyExtent, layoutScheme.crs)),
discriminator = Some(discriminator)
).map { md =>
val tiff =
GeoTiffReader
.readSingleband(
StreamingByteReader(HdfsRangeReader(new Path(md.uri), conf)),
false,
true
)
val reprojectedKeyExtent = keyExtent.reproject(layoutScheme.crs, tiff.crs)
val ext =
tiff
.extent
.intersection(reprojectedKeyExtent)
.getOrElse(reprojectedKeyExtent)
tiff
.crop(ext, layout.cellSize)
.reproject(tiff.crs, layoutScheme.crs)
}
.reduce(_ merge _)
.resample(RasterExtent(keyExtent, layoutScheme.tileSize, layoutScheme.tileSize))
}
def readAll(layerId: LayerId): Seq[Raster[Tile]] = {
val layout =
layoutScheme
.levelForZoom(layerId.zoom)
.layout
attributeStore
.get(
layerName = Some(layerId.name),
discriminator = Some(discriminator)
).map { md =>
val tiff =
GeoTiffReader
.readSingleband(
StreamingByteReader(HdfsRangeReader(new Path(md.uri), conf)),
false,
true
)
tiff
.crop(tiff.extent, layout.cellSize)
.reproject(tiff.crs, layoutScheme.crs)
}
}
/*def keyedReadAll(layerId: LayerId): Seq[Raster[Tile]] = {
val layout =
layoutScheme
.levelForZoom(layerId.zoom)
.layout
val mt = layout.mapTransform
val result: Seq[(GridBounds, SinglebandRaster)] =
seq
.filter { case (_, p) => layerId.name == discriminator(p) }
.map { case (tiffTags, uri) =>
val tiff =
GeoTiffReader
.readSingleband(
StreamingByteReader(HdfsRangeReader(new Path(uri), conf)),
tiffTags,
false,
true
)
val res =
tiff
.crop(tiff.extent, layout.cellSize)
.reproject(tiff.crs, layoutScheme.crs)
null
}
ContextCollection(result, collectMetadata(layerId.zoom))
null
}*/
}
object HadoopSinglebandGeoTiffCollectionLayerReader2 {
def fetchSingleband(
path: URI,
layoutScheme: ZoomedLayoutScheme = ZoomedLayoutScheme(WebMercator),
discriminator: URI => String = uri => uri.toString.split("/").last.split("\\.").head,
filterPaths: String => Boolean = _ => true,
conf: Configuration = new Configuration
): HadoopSinglebandGeoTiffCollectionLayerReader2 = {
val seq =
HdfsUtils
.listFiles(new Path(path), conf)
.map { p =>
val tiffTags = TiffTagsReader.read(StreamingByteReader(HdfsRangeReader(p, conf)))
ProjectedExtent(tiffTags.extent, tiffTags.crs) -> p.toUri
}
//HadoopSinglebandGeoTiffCollectionLayerReader2(seq, layoutScheme, discriminator, conf)
null
}
} */