Last active
May 27, 2020 03:02
-
-
Save metasim/5236f81a119fcc73833f6f93ee55608f to your computer and use it in GitHub Desktop.
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
import geotrellis.raster._ | |
import geotrellis.vector.Extent | |
import org.apache.spark.sql._ | |
import org.apache.spark.sql.functions._ | |
import org.locationtech.jts.geom.Point | |
import org.locationtech.rasterframes._ | |
import org.locationtech.rasterframes.datasource.raster._ | |
import org.locationtech.rasterframes.encoders.CatalystSerializer._ | |
object ExplodeWithLocation extends App { | |
implicit val spark = SparkSession.builder() | |
.master("local[*]").appName("RasterFrames") | |
.withKryoSerialization.getOrCreate().withRasterFrames | |
spark.sparkContext.setLogLevel("ERROR") | |
import spark.implicits._ | |
val example = "https://raw.githubusercontent.com/locationtech/rasterframes/develop/core/src/test/resources/LC08_B7_Memphis_COG.tiff" | |
val rf = spark.read.raster.from(example).withTileDimensions(16, 16).load() | |
val grid2map = udf((encExtent: Row, encDims: Row, colIdx: Int, rowIdx: Int) => { | |
val extent = encExtent.to[Extent] | |
val dims = encDims.to[Dimensions[Int]] | |
GridExtent(extent, dims.cols, dims.rows).gridToMap(colIdx, rowIdx) | |
}) | |
val exploded = rf | |
.withColumn("dims", rf_dimensions($"proj_raster")) | |
.withColumn("extent", rf_extent($"proj_raster")) | |
.select(rf_explode_tiles($"proj_raster"), $"dims", $"extent") | |
.select(grid2map($"extent", $"dims", $"column_index", $"row_index") as "location", $"proj_raster" as "value") | |
exploded.show(false) | |
spark.stop() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result: