|
| 1 | +/* |
| 2 | + * This software is licensed under the Apache 2 license, quoted below. |
| 3 | + * |
| 4 | + * Copyright 2018 Astraea. Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 7 | + * use this file except in compliance with the License. You may obtain a copy of |
| 8 | + * the License at |
| 9 | + * |
| 10 | + * [http://www.apache.org/licenses/LICENSE-2.0] |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | + * License for the specific language governing permissions and limitations under |
| 16 | + * the License. |
| 17 | + * |
| 18 | + * |
| 19 | + */ |
| 20 | + |
1 | 21 | package astraea.spark.rasterframes.experimental |
2 | 22 |
|
| 23 | +import geotrellis.util.MethodExtensions |
| 24 | +import java.net.URI |
| 25 | + |
| 26 | +import astraea.spark.rasterframes._ |
| 27 | +import astraea.spark.rasterframes.util._ |
| 28 | +import geotrellis.proj4.WebMercator |
| 29 | +import geotrellis.raster.io.geotiff.tags.codes.ColorSpace |
| 30 | +import geotrellis.raster.io.geotiff._ |
| 31 | +import geotrellis.raster.resample.Bilinear |
| 32 | +import geotrellis.raster._ |
| 33 | +import geotrellis.raster.render.{ColorMap, ColorMaps, ColorRamps} |
| 34 | +import geotrellis.spark._ |
| 35 | +import geotrellis.spark.io.slippy.HadoopSlippyTileWriter |
| 36 | +import geotrellis.spark.tiling.ZoomedLayoutScheme |
| 37 | +import org.apache.spark.annotation.Experimental |
| 38 | + |
| 39 | +import scala.util.Try |
| 40 | + |
3 | 41 | /** |
4 | | - * |
| 42 | + * Experimental support for exporting a RasterFrame into Slippy map format. |
5 | 43 | * |
6 | 44 | * @since 4/10/18 |
7 | 45 | */ |
8 | | -class SlippyExport { |
| 46 | +@Experimental |
| 47 | +trait SlippyExport extends MethodExtensions[RasterFrame]{ |
| 48 | + /** |
| 49 | + * Export GeoTiff tiles in a slippy map directory structure; for debugging purposes only. |
| 50 | + * NB: Temporal components are ignored blindly. |
| 51 | + */ |
| 52 | + @Experimental |
| 53 | + def exportGeoTiffTiles(dest: URI): Unit = { |
| 54 | + val spark = self.sparkSession |
| 55 | + implicit val sc = spark.sparkContext |
9 | 56 |
|
| 57 | + val tlm = self.tileLayerMetadata.widen |
| 58 | + val crs = tlm.crs |
| 59 | + val mapTransform = tlm.mapTransform |
| 60 | + |
| 61 | + val writer = new HadoopSlippyTileWriter[MultibandTile](dest.toASCIIString, "tiff")({ (key, tile) => |
| 62 | + val extent = mapTransform(key) |
| 63 | + // If we have exactly 3 columns, we assume RGB color space. |
| 64 | + val opts = GeoTiffOptions.DEFAULT |
| 65 | + .mapWhen(_ ⇒ tile.bands.lengthCompare(3) == 0, |
| 66 | + _.copy(colorSpace = ColorSpace.RGB) |
| 67 | + ) |
| 68 | + MultibandGeoTiff(tile, extent, crs, opts).toByteArray |
| 69 | + }) |
| 70 | + |
| 71 | + val tlrdd: MultibandTileLayerRDD[SpatialKey] = self.toMultibandTileLayerRDD(self.tileColumns: _*) match { |
| 72 | + case Left(spatial) ⇒ spatial |
| 73 | + case Right(origRDD) ⇒ |
| 74 | + val newMD = origRDD.metadata.map(_.spatialKey) |
| 75 | + val rdd = origRDD.map { case (k, v) ⇒ (k.spatialKey, v)} |
| 76 | + ContextRDD(rdd, newMD) |
| 77 | + } |
| 78 | + |
| 79 | + writer.write(0, tlrdd) |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Export tiles as a slippy map. For debugging purposes only. |
| 84 | + * NB: Temporal components are ignored blindly. |
| 85 | + */ |
| 86 | + @Experimental |
| 87 | + def exportSlippyMap(dest: URI, colorMap: Option[ColorMap] = None): Unit = { |
| 88 | + val spark = self.sparkSession |
| 89 | + implicit val sc = spark.sparkContext |
| 90 | + |
| 91 | + val inputRDD: MultibandTileLayerRDD[SpatialKey] = self.toMultibandTileLayerRDD(self.tileColumns: _*) match { |
| 92 | + case Left(spatial) ⇒ spatial |
| 93 | + case Right(origRDD) ⇒ |
| 94 | + val newMD = origRDD.metadata.map(_.spatialKey) |
| 95 | + val rdd = origRDD.map { case (k, v) ⇒ (k.spatialKey, v)} |
| 96 | + ContextRDD(rdd, newMD) |
| 97 | + } |
| 98 | + |
| 99 | + val layoutScheme = ZoomedLayoutScheme(WebMercator, tileSize = 256) |
| 100 | + |
| 101 | + val (zoom, reprojected) = inputRDD.reproject(WebMercator, layoutScheme, Bilinear) |
| 102 | + |
| 103 | + val writer = new HadoopSlippyTileWriter[MultibandTile](dest.toASCIIString, "png")({ (_, tile) => |
| 104 | + val png = if(colorMap.isEmpty && tile.bands.lengthCompare(3) == 0) { |
| 105 | + // `Try` below is due to https://github.com/locationtech/geotrellis/issues/2621 |
| 106 | + tile.mapBands((_, t) ⇒ Try(t.rescale(0, 255)).getOrElse(t)).renderPng() |
| 107 | + } |
| 108 | + else { |
| 109 | + // Are there other ways to use the other bands? |
| 110 | + val selected = tile.bands.head |
| 111 | + colorMap.map(m ⇒ selected.renderPng(m)).getOrElse(selected.renderPng(ColorRamps.greyscale(256))) |
| 112 | + } |
| 113 | + png.bytes |
| 114 | + }) |
| 115 | + |
| 116 | + writer.write(zoom, reprojected) |
| 117 | + } |
10 | 118 | } |
| 119 | + |
| 120 | +object SlippyExport { |
| 121 | + implicit class RasterFrameHasSlippy(val self: RasterFrame) extends SlippyExport |
| 122 | +} |
| 123 | + |
0 commit comments