-
Notifications
You must be signed in to change notification settings - Fork 47
Adds rf_render_png and rf_rgb_composite. #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3ccc98b
Skeleton for creating composites.
metasim 8b77afd
Initial test of rf_render_png in Python.
metasim 96e1b23
Added test uncovering another edge case in using Extents for tiling.
metasim 20258c1
Merge branch 'fix/295' into feature/rgb-composites
metasim 1f07291
Merge branch 'develop' into feature/rgb-composites
metasim 8d0fcd8
Fixed merge fail.
metasim cd0f9d2
Documentation update.
metasim d1946eb
Merge branch 'develop' into feature/rgb-composites
metasim 9b5da3c
Merge branch 'develop' into feature/rgb-composites
metasim 9dc6a63
Updated rf_assemble_tile to accept literal or columnar tile size
metasim 20cf328
Added tile sample rendering to DataFrame renderers.
metasim abb8f4e
Misc documentation tweaks.
metasim 66e135a
Add condition to check truncation in markdown render test
vpipkt 4e5698a
Additional tests for DataFrame rendering truncation.
metasim 2a4a8b2
Incorporated PR feedback.
metasim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.