jiayuasu commented on a change in pull request #523: URL: https://github.com/apache/incubator-sedona/pull/523#discussion_r632841395
########## File path: sql/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/raster.scala ########## @@ -0,0 +1,245 @@ +/* + * 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 +import scala.collection.convert.ImplicitConversions.`collection AsScalaIterable` + +// 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 geometry = readGeometry(geomString) + new GenericArrayData(GeometrySerializer.serialize(geometry)) + } + + private 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) Review comment: why do you implement CRS transformation here? This should be handled by Sedona ST_Transform outside this funciton. -- 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