shantanuaggarwal2695 commented on a change in pull request #523:
URL: https://github.com/apache/incubator-sedona/pull/523#discussion_r633123832



##########
File path: 
sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/raster.scala
##########
@@ -0,0 +1,223 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.spark.sql.sedona_sql.expressions
+
+import org.apache.sedona.sql.utils.GeometrySerializer
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.Expression
+import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
+import org.apache.spark.sql.catalyst.util.GenericArrayData
+import org.apache.spark.sql.sedona_sql.UDT.GeometryUDT
+import org.apache.spark.sql.sedona_sql.expressions.implicits.GeometryEnhancer
+import org.apache.spark.sql.types._
+import org.apache.spark.unsafe.types.UTF8String
+import org.geotools.coverage.grid.io.{AbstractGridFormat, 
GridCoverage2DReader, GridFormatFinder, OverviewPolicy}
+import org.geotools.coverage.grid.{GridCoordinates2D, GridCoverage2D}
+import org.geotools.gce.geotiff.GeoTiffReader
+import org.geotools.geometry.jts.JTS
+import org.geotools.referencing.CRS
+import org.geotools.util.factory.Hints
+import org.locationtech.jts.geom.{Coordinate, Geometry, GeometryFactory}
+import org.opengis.coverage.grid.{GridCoordinates, GridEnvelope}
+import org.opengis.parameter.{GeneralParameterValue, ParameterValue}
+
+import java.io.IOException
+import java.util
+
+class GeometryOperations {
+
+   def readGeometry(url: String): Geometry = {
+
+    val format = GridFormatFinder.findFormat(url)
+    val hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, true)
+    val reader = format.getReader(url, hints)
+    var coverage:GridCoverage2D = null
+
+    try coverage = reader.read(null)
+    catch {
+      case giveUp: IOException =>
+        throw new RuntimeException(giveUp)
+    }
+    reader.dispose()
+    val source = coverage.getCoordinateReferenceSystem
+    val target = CRS.decode("EPSG:4326", true)
+    val targetCRS = CRS.findMathTransform(source, target)
+    val gridRange2D = coverage.getGridGeometry.getGridRange
+    val cords = Array(Array(gridRange2D.getLow(0), gridRange2D.getLow(1)), 
Array(gridRange2D.getLow(0), gridRange2D.getHigh(1)), 
Array(gridRange2D.getHigh(0), gridRange2D.getHigh(1)), 
Array(gridRange2D.getHigh(0), gridRange2D.getLow(1)))
+    val polyCoordinates = new Array[Coordinate](5)
+    var index = 0
+
+    for (point <- cords) {
+      val coordinate2D = new GridCoordinates2D(point(0), point(1))
+      val result = coverage.getGridGeometry.gridToWorld(coordinate2D)
+      polyCoordinates({
+        index += 1; index - 1
+      }) = new Coordinate(result.getOrdinate(0), result.getOrdinate(1))
+    }
+
+    polyCoordinates(index) = polyCoordinates(0)
+    val factory = new GeometryFactory
+    val polygon = JTS.transform(factory.createPolygon(polyCoordinates), 
targetCRS)
+
+    polygon
+
+  }
+}
+
+
+// Fetches polygonal coordinates from a raster image
+case class ST_GeomFromRaster(inputExpressions: Seq[Expression])
+  extends Expression with CodegenFallback with UserDataGeneratator {
+  override def nullable: Boolean = false
+
+  override def eval(inputRow: InternalRow): Any = {
+    // This is an expression which takes one input expressions
+    assert(inputExpressions.length == 1)
+    val geomString = 
inputExpressions(0).eval(inputRow).asInstanceOf[UTF8String].toString
+    val geomConstruction = new GeometryOperations
+    val geometry = geomConstruction.readGeometry(geomString)
+    new GenericArrayData(GeometrySerializer.serialize(geometry))
+  }
+
+
+  override def dataType: DataType = GeometryUDT
+
+  override def children: Seq[Expression] = inputExpressions
+}
+
+
+// Constructs a raster dataframe from a raster image which contains multiple 
columns such as Geometry, Band values etc
+case class ST_GeomWithBandsFromGeoTiff(inputExpressions: Seq[Expression])
+  extends Expression with CodegenFallback with UserDataGeneratator {
+  override def nullable: Boolean = false
+
+  override def eval(inputRow: InternalRow): Any = {
+    // This is an expression which takes one input expressions
+    assert(inputExpressions.length == 2)
+    val geomString = 
inputExpressions(0).eval(inputRow).asInstanceOf[UTF8String].toString
+    val totalBands = inputExpressions(1).eval(inputRow).asInstanceOf[Int]
+    val geomConstruction = new GeometryOperations
+    val geometry = geomConstruction.readGeometry(geomString)
+    val bandvalues = getBands(geomString, totalBands)
+    returnValue(geometry.toGenericArrayData,bandvalues, 2)
+  }
+
+  private  def getBands(url: String, bands:Int): Array[Double] = {
+    val policy: ParameterValue[OverviewPolicy] = 
AbstractGridFormat.OVERVIEW_POLICY.createValue
+    policy.setValue(OverviewPolicy.IGNORE)
+
+    val gridsize: ParameterValue[String] = 
AbstractGridFormat.SUGGESTED_TILE_SIZE.createValue
+
+    val useJaiRead: ParameterValue[Boolean] = 
AbstractGridFormat.USE_JAI_IMAGEREAD.createValue.asInstanceOf[ParameterValue[Boolean]]
+    useJaiRead.setValue(true)
+
+
+    val reader: GridCoverage2DReader = new GeoTiffReader(url)
+    val coverage: GridCoverage2D = 
reader.read(Array[GeneralParameterValue](policy, gridsize, useJaiRead))
+
+    val dimensions: GridEnvelope = reader.getOriginalGridRange
+    val maxDimensions: GridCoordinates = dimensions.getHigh
+    val w: Int = maxDimensions.getCoordinateValue(0) + 1
+    val h: Int = maxDimensions.getCoordinateValue(1) + 1
+    val numBands: Int = bands
+
+    val bandValues: util.List[util.List[Double]] = new 
util.ArrayList[util.List[Double]](numBands)
+
+    for (i <- 0 until numBands) {
+      bandValues.add(new util.ArrayList[Double])
+    }
+
+    for (i <- 0 until w) {

Review comment:
       @jiayuasu  The current loop returns an array of size equal to the number 
of bands for every pixel and I use it to construct a 2D array of size number of 
bands * number of pixels while the second loop flattens this array. If I need 
to merge both the loops, I have to keep a separate starting index for every 
band which may vary. 
    




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to