-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #301 from s22s/feature/rgb-composites
Adds rf_render_png and rf_rgb_composite.
- Loading branch information
Showing
31 changed files
with
794 additions
and
425 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
.../src/main/scala/org/locationtech/rasterframes/expressions/transformers/RGBComposite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* This software is licensed under the Apache 2 license, quoted below. | ||
* | ||
* Copyright 2019 Astraea, Inc. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.locationtech.rasterframes.expressions.transformers | ||
|
||
import geotrellis.raster.ArrayMultibandTile | ||
import org.apache.spark.sql.Column | ||
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult | ||
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.{TypeCheckFailure, TypeCheckSuccess} | ||
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback | ||
import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionDescription, TernaryExpression} | ||
import org.apache.spark.sql.rf.TileUDT | ||
import org.apache.spark.sql.types.DataType | ||
import org.locationtech.rasterframes._ | ||
import org.locationtech.rasterframes.encoders.CatalystSerializer._ | ||
import org.locationtech.rasterframes.expressions.DynamicExtractors.tileExtractor | ||
import org.locationtech.rasterframes.expressions.row | ||
import org.locationtech.rasterframes.tiles.ProjectedRasterTile | ||
|
||
/** | ||
* Expression to combine the given tile columns into an 32-bit RGB composite. | ||
* Tiles in each row will first be and-ed with 0xFF, bit shifted, and or-ed into a single 32-bit word. | ||
* @param red tile column to represent red channel | ||
* @param green tile column to represent green channel | ||
* @param blue tile column to represent blue channel | ||
*/ | ||
@ExpressionDescription( | ||
usage = "_FUNC_(red, green, blue) - Combines the given tile columns into an 32-bit RGB composite.", | ||
arguments = """ | ||
Arguments: | ||
* red - tile column representing the red channel | ||
* green - tile column representing the green channel | ||
* blue - tile column representing the blue channel""" | ||
) | ||
case class RGBComposite(red: Expression, green: Expression, blue: Expression) extends TernaryExpression | ||
with CodegenFallback { | ||
|
||
override def nodeName: String = "rf_rgb_composite" | ||
|
||
override def dataType: DataType = if( | ||
red.dataType.conformsTo[ProjectedRasterTile] || | ||
blue.dataType.conformsTo[ProjectedRasterTile] || | ||
green.dataType.conformsTo[ProjectedRasterTile] | ||
) red.dataType | ||
else TileType | ||
|
||
override def children: Seq[Expression] = Seq(red, green, blue) | ||
|
||
override def checkInputDataTypes(): TypeCheckResult = { | ||
if (!tileExtractor.isDefinedAt(red.dataType)) { | ||
TypeCheckFailure(s"Red channel input type '${red.dataType}' does not conform to a raster type.") | ||
} | ||
else if (!tileExtractor.isDefinedAt(green.dataType)) { | ||
TypeCheckFailure(s"Green channel input type '${green.dataType}' does not conform to a raster type.") | ||
} | ||
else if (!tileExtractor.isDefinedAt(blue.dataType)) { | ||
TypeCheckFailure(s"Blue channel input type '${blue.dataType}' does not conform to a raster type.") | ||
} | ||
else TypeCheckSuccess | ||
} | ||
|
||
override protected def nullSafeEval(input1: Any, input2: Any, input3: Any): Any = { | ||
val (r, rc) = tileExtractor(red.dataType)(row(input1)) | ||
val (g, gc) = tileExtractor(green.dataType)(row(input2)) | ||
val (b, bc) = tileExtractor(blue.dataType)(row(input3)) | ||
|
||
// Pick the first available TileContext, if any, and reassociate with the result | ||
val ctx = Seq(rc, gc, bc).flatten.headOption | ||
val composite = ArrayMultibandTile( | ||
r.rescale(0, 255), g.rescale(0, 255), b.rescale(0, 255) | ||
).color() | ||
ctx match { | ||
case Some(c) => c.toProjectRasterTile(composite).toInternalRow | ||
case None => | ||
implicit val tileSer = TileUDT.tileSerializer | ||
composite.toInternalRow | ||
} | ||
} | ||
} | ||
|
||
object RGBComposite { | ||
def apply(red: Column, green: Column, blue: Column): Column = | ||
new Column(RGBComposite(red.expr, green.expr, blue.expr)) | ||
} |
79 changes: 79 additions & 0 deletions
79
core/src/main/scala/org/locationtech/rasterframes/expressions/transformers/RenderPNG.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* This software is licensed under the Apache 2 license, quoted below. | ||
* | ||
* Copyright 2019 Astraea, Inc. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.locationtech.rasterframes.expressions.transformers | ||
|
||
import geotrellis.raster.Tile | ||
import geotrellis.raster.render.ColorRamp | ||
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback | ||
import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionDescription} | ||
import org.apache.spark.sql.types.{BinaryType, DataType} | ||
import org.apache.spark.sql.{Column, TypedColumn} | ||
import org.locationtech.rasterframes.expressions.UnaryRasterOp | ||
import org.locationtech.rasterframes.model.TileContext | ||
|
||
/** | ||
* Converts a tile into a PNG encoded byte array. | ||
* @param child tile column | ||
* @param ramp color ramp to use for non-composite tiles. | ||
*/ | ||
abstract class RenderPNG(child: Expression, ramp: Option[ColorRamp]) extends UnaryRasterOp with CodegenFallback with Serializable { | ||
override def dataType: DataType = BinaryType | ||
override protected def eval(tile: Tile, ctx: Option[TileContext]): Any = { | ||
val png = ramp.map(tile.renderPng).getOrElse(tile.renderPng()) | ||
png.bytes | ||
} | ||
} | ||
|
||
object RenderPNG { | ||
import org.locationtech.rasterframes.encoders.SparkBasicEncoders._ | ||
|
||
@ExpressionDescription( | ||
usage = "_FUNC_(tile) - Encode the given tile into a RGB composite PNG. Assumes the red, green, and " + | ||
"blue channels are encoded as 8-bit channels within the 32-bit word.", | ||
arguments = """ | ||
Arguments: | ||
* tile - tile to render""" | ||
) | ||
case class RenderCompositePNG(child: Expression) extends RenderPNG(child, None) { | ||
override def nodeName: String = "rf_render_png" | ||
} | ||
|
||
object RenderCompositePNG { | ||
def apply(red: Column, green: Column, blue: Column): TypedColumn[Any, Array[Byte]] = | ||
new Column(RenderCompositePNG(RGBComposite(red.expr, green.expr, blue.expr))).as[Array[Byte]] | ||
} | ||
|
||
@ExpressionDescription( | ||
usage = "_FUNC_(tile) - Encode the given tile as a PNG using a color ramp with assignemnts from quantile computation", | ||
arguments = """ | ||
Arguments: | ||
* tile - tile to render""" | ||
) | ||
case class RenderColorRampPNG(child: Expression, colors: ColorRamp) extends RenderPNG(child, Some(colors)) { | ||
override def nodeName: String = "rf_render_png" | ||
} | ||
|
||
object RenderColorRampPNG { | ||
def apply(tile: Column, colors: ColorRamp): TypedColumn[Any, Array[Byte]] = | ||
new Column(RenderColorRampPNG(tile.expr, colors)).as[Array[Byte]] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.