Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* FILE: Functions.scala
* Copyright (c) 2015 - 2018 GeoSpark Development Team
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package org.apache.spark.sql.geosparksql.expressions
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.{ArrayData, GenericArrayData}
import org.apache.spark.sql.geosparksql.UDT.GeometryUDT
import org.apache.spark.sql.types.{DataType, DoubleType}
import org.apache.spark.unsafe.types.UTF8String
import org.datasyslab.geosparksql.utils.GeometrySerializer
import org.geotools.geometry.jts.JTS
import org.geotools.referencing.CRS
import org.opengis.referencing.operation.MathTransform
/**
* Return the distance between two geometries.
*
* @param inputExpressions This function takes two geometries and calculates the distance between two objects.
*/
case class ST_Distance(inputExpressions: Seq[Expression])
extends Expression with CodegenFallback {
// This is a binary expression
assert(inputExpressions.length == 2)
override def nullable: Boolean = false
override def toString: String = s" **${ST_Distance.getClass.getName}** "
override def children: Seq[Expression] = inputExpressions
override def eval(inputRow: InternalRow): Any = {
assert(inputExpressions.length == 2)
val leftArray = inputExpressions(0).eval(inputRow).asInstanceOf[ArrayData]
val rightArray = inputExpressions(1).eval(inputRow).asInstanceOf[ArrayData]
val leftGeometry = GeometrySerializer.deserialize(leftArray)
val rightGeometry = GeometrySerializer.deserialize(rightArray)
return leftGeometry.distance(rightGeometry)
}
override def dataType = DoubleType
}
/**
* Return the convex hull of a Geometry.
*
* @param inputExpressions
*/
case class ST_ConvexHull(inputExpressions: Seq[Expression])
extends Expression with CodegenFallback {
override def nullable: Boolean = false
override def eval(input: InternalRow): Any = {
assert(inputExpressions.length == 1)
val geometry = GeometrySerializer.deserialize(inputExpressions(0).eval(input).asInstanceOf[ArrayData])
new GenericArrayData(GeometrySerializer.serialize(geometry.convexHull()))
}
override def dataType: DataType = new GeometryUDT()
override def children: Seq[Expression] = inputExpressions
}
/**
* Return the bounding rectangle for a Geometry
*
* @param inputExpressions
*/
case class ST_Envelope(inputExpressions: Seq[Expression])
extends Expression with CodegenFallback {
override def nullable: Boolean = false
override def eval(input: InternalRow): Any = {
assert(inputExpressions.length == 1)
val geometry = GeometrySerializer.deserialize(inputExpressions(0).eval(input).asInstanceOf[ArrayData])
new GenericArrayData(GeometrySerializer.serialize(geometry.getEnvelope()))
}
override def dataType: DataType = new GeometryUDT()
override def children: Seq[Expression] = inputExpressions
}
/**
* Return the length measurement of a Geometry
*
* @param inputExpressions
*/
case class ST_Length(inputExpressions: Seq[Expression])
extends Expression with CodegenFallback {
override def nullable: Boolean = false
override def eval(input: InternalRow): Any = {
assert(inputExpressions.length == 1)
val geometry = GeometrySerializer.deserialize(inputExpressions(0).eval(input).asInstanceOf[ArrayData])
return geometry.getLength
}
override def dataType: DataType = DoubleType
override def children: Seq[Expression] = inputExpressions
}
/**
* Return the area measurement of a Geometry.
*
* @param inputExpressions
*/
case class ST_Area(inputExpressions: Seq[Expression])
extends Expression with CodegenFallback {
override def nullable: Boolean = false
override def eval(input: InternalRow): Any = {
assert(inputExpressions.length == 1)
val geometry = GeometrySerializer.deserialize(inputExpressions(0).eval(input).asInstanceOf[ArrayData])
return geometry.getArea
}
override def dataType: DataType = DoubleType
override def children: Seq[Expression] = inputExpressions
}
/**
* Return mathematical centroid of a geometry.
*
* @param inputExpressions
*/
case class ST_Centroid(inputExpressions: Seq[Expression])
extends Expression with CodegenFallback {
override def nullable: Boolean = false
override def eval(input: InternalRow): Any = {
assert(inputExpressions.length == 1)
val geometry = GeometrySerializer.deserialize(inputExpressions(0).eval(input).asInstanceOf[ArrayData])
new GenericArrayData(GeometrySerializer.serialize(geometry.getCentroid()))
}
override def dataType: DataType = new GeometryUDT()
override def children: Seq[Expression] = inputExpressions
}
/**
* Given a geometry, sourceEPSGcode, and targetEPSGcode, convert the geometry's Spatial Reference System / Coordinate Reference System.
*
* @param inputExpressions
*/
case class ST_Transform(inputExpressions: Seq[Expression])
extends Expression with CodegenFallback {
override def nullable: Boolean = false
override def eval(input: InternalRow): Any = {
assert(inputExpressions.length >= 3 && inputExpressions.length <= 5)
System.setProperty("org.geotools.referencing.forceXY", "true")
if (inputExpressions.length >= 4) {
System.setProperty("org.geotools.referencing.forceXY", inputExpressions(3).eval(input).asInstanceOf[Boolean].toString)
}
val originalGeometry = GeometrySerializer.deserialize(inputExpressions(0).eval(input).asInstanceOf[ArrayData])
val sourceCRScode = CRS.decode(inputExpressions(1).eval(input).asInstanceOf[UTF8String].toString)
val targetCRScode = CRS.decode(inputExpressions(2).eval(input).asInstanceOf[UTF8String].toString)
var transform: MathTransform = null
if (inputExpressions.length == 5) {
transform = CRS.findMathTransform(sourceCRScode, targetCRScode, inputExpressions(4).eval(input).asInstanceOf[Boolean])
}
else {
transform = CRS.findMathTransform(sourceCRScode, targetCRScode, false)
}
new GenericArrayData(GeometrySerializer.serialize(JTS.transform(originalGeometry, transform)))
}
override def dataType: DataType = new GeometryUDT()
override def children: Seq[Expression] = inputExpressions
}
/**
* Return the intersection shape of two geometries. The return type is a geometry
*
* @param inputExpressions
*/
case class ST_Intersection(inputExpressions: Seq[Expression])
extends Expression with CodegenFallback {
override def nullable: Boolean = false
override def eval(input: InternalRow): Any = {
assert(inputExpressions.length == 2)
val leftgeometry = GeometrySerializer.deserialize(inputExpressions(0).eval(input).asInstanceOf[ArrayData])
val rightgeometry = GeometrySerializer.deserialize(inputExpressions(1).eval(input).asInstanceOf[ArrayData])
new GenericArrayData(GeometrySerializer.serialize(leftgeometry.intersection(rightgeometry)))
}
override def dataType: DataType = new GeometryUDT()
override def children: Seq[Expression] = inputExpressions
}