-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SEDONA-478] Make Sedona geometry functions and spatial join working …
…without GeoTools (#1398)
- Loading branch information
1 parent
c35efdb
commit 64570db
Showing
17 changed files
with
193 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
spark/common/src/main/scala/org/apache/sedona/sql/utils/GeoToolsCoverageAvailability.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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.sedona.sql.utils | ||
|
||
import org.apache.sedona.sql.RasterRegistrator.logger | ||
|
||
/** | ||
* A helper object to check if GeoTools GridCoverage2D is available on the classpath. | ||
*/ | ||
object GeoToolsCoverageAvailability { | ||
val gridClassName = "org.geotools.coverage.grid.GridCoverage2D" | ||
|
||
lazy val isGeoToolsAvailable: Boolean = { | ||
try { | ||
Class.forName(gridClassName, true, Thread.currentThread().getContextClassLoader) | ||
true | ||
} catch { | ||
case _: ClassNotFoundException => | ||
logger.warn("Geotools was not found on the classpath. Raster operations will not be available.") | ||
false | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...on/src/main/scala/org/apache/spark/sql/sedona_sql/expressions/InferrableRasterTypes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions.Expression | ||
import org.apache.spark.sql.catalyst.util.ArrayData | ||
import org.apache.spark.sql.sedona_sql.UDT.RasterUDT | ||
import org.apache.spark.sql.sedona_sql.expressions.raster.implicits.{RasterEnhancer, RasterInputExpressionEnhancer} | ||
import org.apache.spark.sql.types.{ArrayType, DataTypes, UserDefinedType} | ||
|
||
import scala.reflect.runtime.universe.{Type, typeOf} | ||
import org.geotools.coverage.grid.GridCoverage2D | ||
|
||
object InferrableRasterTypes { | ||
implicit val gridCoverage2DInstance: InferrableType[GridCoverage2D] = | ||
new InferrableType[GridCoverage2D] {} | ||
implicit val gridCoverage2DArrayInstance: InferrableType[Array[GridCoverage2D]] = | ||
new InferrableType[Array[GridCoverage2D]] {} | ||
|
||
def isRasterType(t: Type): Boolean = t =:= typeOf[GridCoverage2D] | ||
def isRasterArrayType(t: Type): Boolean = t =:= typeOf[Array[GridCoverage2D]] | ||
|
||
val rasterUDT: UserDefinedType[_] = RasterUDT | ||
val rasterUDTArray: ArrayType = DataTypes.createArrayType(RasterUDT) | ||
|
||
def rasterExtractor(expr: Expression)(input: InternalRow): Any = expr.toRaster(input) | ||
|
||
def rasterSerializer(output: Any): Any = | ||
if (output != null) { | ||
output.asInstanceOf[GridCoverage2D].serialize | ||
} else { | ||
null | ||
} | ||
|
||
def rasterArraySerializer(output: Any): Any = | ||
if (output != null) { | ||
val rasters = output.asInstanceOf[Array[GridCoverage2D]] | ||
val serialized = rasters.map { raster => | ||
val serialized = raster.serialize | ||
raster.dispose(true) | ||
serialized | ||
} | ||
ArrayData.toArrayData(serialized) | ||
} else { | ||
null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...src/main/scala/org/apache/spark/sql/sedona_sql/expressions/InferredRasterExpression.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.GeoToolsCoverageAvailability.isGeoToolsAvailable | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions.Expression | ||
import org.apache.spark.sql.types.{ArrayType, UserDefinedType} | ||
|
||
import scala.reflect.runtime.universe.{Type, typeOf} | ||
|
||
object InferredRasterExpression { | ||
def isRasterType(t: Type): Boolean = | ||
isGeoToolsAvailable && InferrableRasterTypes.isRasterType(t) | ||
|
||
def isRasterArrayType(t: Type): Boolean = | ||
isGeoToolsAvailable && InferrableRasterTypes.isRasterArrayType(t) | ||
|
||
def rasterUDT: UserDefinedType[_] = if (isGeoToolsAvailable) { | ||
InferrableRasterTypes.rasterUDT | ||
} else { | ||
null | ||
} | ||
|
||
def rasterUDTArray: ArrayType = if (isGeoToolsAvailable) { | ||
InferrableRasterTypes.rasterUDTArray | ||
} else { | ||
null | ||
} | ||
|
||
val rasterExtractor: Expression => InternalRow => Any = if (isGeoToolsAvailable) { | ||
InferrableRasterTypes.rasterExtractor | ||
} else { | ||
_ => _ => null | ||
} | ||
|
||
val rasterSerializer: Any => Any = if (isGeoToolsAvailable) { | ||
InferrableRasterTypes.rasterSerializer | ||
} else { | ||
(_: Any) => null | ||
} | ||
|
||
val rasterArraySerializer: Any => Any = if (isGeoToolsAvailable) { | ||
InferrableRasterTypes.rasterArraySerializer | ||
} else { | ||
(_: Any) => null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.