|
| 1 | +/* |
| 2 | + * This software is licensed under the Apache 2 license, quoted below. |
| 3 | + * |
| 4 | + * Copyright 2020 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 | + * SPDX-License-Identifier: Apache-2.0 |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +package examples |
| 23 | + |
| 24 | +import geotrellis.raster._ |
| 25 | +import geotrellis.vector.Extent |
| 26 | +import org.apache.spark.sql._ |
| 27 | +import org.apache.spark.sql.functions._ |
| 28 | +import org.locationtech.rasterframes._ |
| 29 | +import org.locationtech.rasterframes.datasource.raster._ |
| 30 | +import org.locationtech.rasterframes.encoders.CatalystSerializer._ |
| 31 | + |
| 32 | +object ExplodeWithLocation extends App { |
| 33 | + |
| 34 | + implicit val spark = SparkSession.builder() |
| 35 | + .master("local[*]").appName("RasterFrames") |
| 36 | + .withKryoSerialization.getOrCreate().withRasterFrames |
| 37 | + spark.sparkContext.setLogLevel("ERROR") |
| 38 | + |
| 39 | + import spark.implicits._ |
| 40 | + |
| 41 | + val example = "https://raw.githubusercontent.com/locationtech/rasterframes/develop/core/src/test/resources/LC08_B7_Memphis_COG.tiff" |
| 42 | + val rf = spark.read.raster.from(example).withTileDimensions(16, 16).load() |
| 43 | + |
| 44 | + val grid2map = udf((encExtent: Row, encDims: Row, colIdx: Int, rowIdx: Int) => { |
| 45 | + val extent = encExtent.to[Extent] |
| 46 | + val dims = encDims.to[Dimensions[Int]] |
| 47 | + GridExtent(extent, dims.cols, dims.rows).gridToMap(colIdx, rowIdx) |
| 48 | + }) |
| 49 | + |
| 50 | + val exploded = rf |
| 51 | + .withColumn("dims", rf_dimensions($"proj_raster")) |
| 52 | + .withColumn("extent", rf_extent($"proj_raster")) |
| 53 | + .select(rf_explode_tiles($"proj_raster"), $"dims", $"extent") |
| 54 | + .select(grid2map($"extent", $"dims", $"column_index", $"row_index") as "location", $"proj_raster" as "value") |
| 55 | + |
| 56 | + exploded.show(false) |
| 57 | + |
| 58 | + spark.stop() |
| 59 | +} |
0 commit comments