Skip to content

Commit d54b7d4

Browse files
authored
Merge pull request #498 from s22s/feature/2-examples
Added a couple example inspired by Gitter discussion
2 parents 52356cc + 2b41739 commit d54b7d4

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

core/src/test/scala/org/locationtech/rasterframes/ref/RasterSourceSpec.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ class RasterSourceSpec extends TestEnvironment with TestData {
166166
gdal.bandCount should be (3)
167167
}
168168

169+
it("should support nested vsi file paths") {
170+
val path = URI.create("gdal://vsihdfs/hdfs://dp-01.tap-psnc.net:9000/user/dpuser/images/landsat/LC081900242018092001T1-SC20200409091832/LC08_L1TP_190024_20180920_20180928_01_T1_sr_band1.tif")
171+
assert(RFRasterSource(path).isInstanceOf[GDALRasterSource])
172+
}
173+
169174
it("should interpret no scheme as file://") {
170175
val localSrc = geotiffDir.resolve("LC08_B7_Memphis_COG.tiff").toString
171176
val schemelessUri = new URI(localSrc)

datasource/src/main/scala/org/locationtech/rasterframes/datasource/geotiff/package.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ package object geotiff {
5353
tag[GeoTiffRasterFrameWriterTag][DataFrameWriter[T]](
5454
writer.format(GeoTiffDataSource.SHORT_NAME)
5555
)
56-
56+
}
57+
implicit class GeoTiffFormatHasOptions[T](val writer: GeoTiffRasterFrameWriter[T]) {
5758
def withDimensions(cols: Int, rows: Int): GeoTiffRasterFrameWriter[T] =
5859
tag[GeoTiffRasterFrameWriterTag][DataFrameWriter[T]](
5960
writer
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 org.apache.spark.sql._
25+
import org.apache.spark.sql.functions._
26+
import org.locationtech.rasterframes._
27+
import org.locationtech.rasterframes.datasource.raster._
28+
import org.locationtech.rasterframes.encoders.CatalystSerializer._
29+
import geotrellis.raster._
30+
import geotrellis.vector.Extent
31+
import org.locationtech.jts.geom.Point
32+
33+
object ValueAtPoint extends App {
34+
35+
implicit val spark = SparkSession.builder()
36+
.master("local[*]").appName("RasterFrames")
37+
.withKryoSerialization.getOrCreate().withRasterFrames
38+
spark.sparkContext.setLogLevel("ERROR")
39+
40+
import spark.implicits._
41+
42+
val example = "https://raw.githubusercontent.com/locationtech/rasterframes/develop/core/src/test/resources/LC08_B7_Memphis_COG.tiff"
43+
val rf = spark.read.raster.from(example).withTileDimensions(16, 16).load()
44+
val point = st_makePoint(766770.000, 3883995.000)
45+
46+
val rf_value_at_point = udf((extentEnc: Row, tile: Tile, point: Point) => {
47+
val extent = extentEnc.to[Extent]
48+
Raster(tile, extent).getDoubleValueAtPoint(point)
49+
})
50+
51+
rf.where(st_intersects(rf_geometry($"proj_raster"), point))
52+
.select(rf_value_at_point(rf_extent($"proj_raster"), rf_tile($"proj_raster"), point) as "value")
53+
.show(false)
54+
55+
//rf.show()
56+
57+
spark.stop()
58+
}

0 commit comments

Comments
 (0)