Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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()
}
@metasim
Copy link
Author

metasim commented May 5, 2020

Result:

+---------------------+-------+
|location             |value  |
+---------------------+-------+
|[752340.0, 3895290.0]|8139.0 |
|[752370.0, 3895290.0]|8125.0 |
|[752400.0, 3895290.0]|8210.0 |
|[752430.0, 3895290.0]|8200.0 |
|[752460.0, 3895290.0]|8166.0 |
|[752490.0, 3895290.0]|8201.0 |
|[752520.0, 3895290.0]|8203.0 |
|[752550.0, 3895290.0]|8167.0 |
|[752580.0, 3895290.0]|8209.0 |
|[752610.0, 3895290.0]|8314.0 |
|[752640.0, 3895290.0]|8346.0 |
|[752670.0, 3895290.0]|8327.0 |
|[752700.0, 3895290.0]|8313.0 |
|[752730.0, 3895290.0]|8350.0 |
|[752760.0, 3895290.0]|8510.0 |
|[752790.0, 3895290.0]|10957.0|
|[752340.0, 3895260.0]|8280.0 |
|[752370.0, 3895260.0]|8340.0 |
|[752400.0, 3895260.0]|8241.0 |
|[752430.0, 3895260.0]|8247.0 |
+---------------------+-------+
only showing top 20 rows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment