|
| 1 | +/* |
| 2 | + * This software is licensed under the Apache 2 license, quoted below. |
| 3 | + * |
| 4 | + * Copyright 2019 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 astraea.spark.rasterframes |
| 23 | + |
| 24 | +import com.vividsolutions.jts.geom._ |
| 25 | +import geotrellis.proj4.{CRS, LatLng, Sinusoidal, WebMercator} |
| 26 | +import org.apache.spark.sql.Encoders |
| 27 | +import org.scalatest.{FunSpec, Matchers} |
| 28 | + |
| 29 | +/** |
| 30 | + * Test for geometry reprojection. |
| 31 | + * |
| 32 | + * @since 11/29/18 |
| 33 | + */ |
| 34 | +class ReprojectGeometryTest extends FunSpec |
| 35 | + with TestEnvironment with Matchers { |
| 36 | + import spark.implicits._ |
| 37 | + |
| 38 | + describe("Geometry reprojection") { |
| 39 | + it("should allow reprojection geometry") { |
| 40 | + // Note: Test data copied from ReprojectSpec in GeoTrellis |
| 41 | + val fact = new GeometryFactory() |
| 42 | + val latLng: Geometry = fact.createLineString(Array( |
| 43 | + new Coordinate(-111.09374999999999, 34.784483415461345), |
| 44 | + new Coordinate(-111.09374999999999, 43.29919735147067), |
| 45 | + new Coordinate(-75.322265625, 43.29919735147067), |
| 46 | + new Coordinate(-75.322265625, 34.784483415461345), |
| 47 | + new Coordinate(-111.09374999999999, 34.784483415461345) |
| 48 | + )) |
| 49 | + |
| 50 | + val webMercator: Geometry = fact.createLineString(Array( |
| 51 | + new Coordinate(-12366899.680315234, 4134631.734001753), |
| 52 | + new Coordinate(-12366899.680315234, 5357624.186564572), |
| 53 | + new Coordinate(-8384836.254770693, 5357624.186564572), |
| 54 | + new Coordinate(-8384836.254770693, 4134631.734001753), |
| 55 | + new Coordinate(-12366899.680315234, 4134631.734001753) |
| 56 | + )) |
| 57 | + |
| 58 | + withClue("both literal crs") { |
| 59 | + |
| 60 | + val df = Seq((latLng, webMercator)).toDF("ll", "wm") |
| 61 | + |
| 62 | + val rp = df.select( |
| 63 | + reproject_geometry($"ll", LatLng, WebMercator) as "wm2", |
| 64 | + reproject_geometry($"wm", WebMercator, LatLng) as "ll2", |
| 65 | + reproject_geometry(reproject_geometry($"ll", LatLng, Sinusoidal), Sinusoidal, WebMercator) as "wm3" |
| 66 | + ).as[(Geometry, Geometry, Geometry)] |
| 67 | + |
| 68 | + |
| 69 | + val (wm2, ll2, wm3) = rp.first() |
| 70 | + |
| 71 | + wm2 should matchGeom(webMercator, 0.00001) |
| 72 | + ll2 should matchGeom(latLng, 0.00001) |
| 73 | + wm3 should matchGeom(webMercator, 0.00001) |
| 74 | + } |
| 75 | + |
| 76 | + withClue("one literal crs") { |
| 77 | + implicit val enc = Encoders.tuple(jtsGeometryEncoder, jtsGeometryEncoder, crsEncoder) |
| 78 | + |
| 79 | + val df = Seq((latLng, webMercator, LatLng: CRS)).toDF("ll", "wm", "llCRS") |
| 80 | + |
| 81 | + val rp = df.select( |
| 82 | + reproject_geometry($"ll", $"llCRS", WebMercator) as "wm2", |
| 83 | + reproject_geometry($"wm", WebMercator, $"llCRS") as "ll2", |
| 84 | + reproject_geometry(reproject_geometry($"ll", $"llCRS", Sinusoidal), Sinusoidal, WebMercator) as "wm3" |
| 85 | + ).as[(Geometry, Geometry, Geometry)] |
| 86 | + |
| 87 | + |
| 88 | + val (wm2, ll2, wm3) = rp.first() |
| 89 | + |
| 90 | + wm2 should matchGeom(webMercator, 0.00001) |
| 91 | + ll2 should matchGeom(latLng, 0.00001) |
| 92 | + wm3 should matchGeom(webMercator, 0.00001) |
| 93 | + |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments