Skip to content

Commit 05877c7

Browse files
committed
Fix st_reproject throws on encountering null value in column
Author: Netanel Malka <[email protected]> st_reproject throws on encountering null value in column - #501 Added null check on ReprojectGeometry Signed-off-by: Netanel Malka <[email protected]>
1 parent 0da9aa9 commit 05877c7

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

core/src/main/scala/org/locationtech/rasterframes/expressions/transformers/ReprojectGeometry.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,13 @@ case class ReprojectGeometry(geometry: Expression, srcCRS: Expression, dstCRS: E
8282
// Optimized pass-through case.
8383
case (s: LazyCRS, r: LazyCRS) if s.encoded == r.encoded => geometry.eval(input)
8484
case _ =>
85-
val geom = JTSTypes.GeometryTypeInstance.deserialize(geometry.eval(input))
86-
JTSTypes.GeometryTypeInstance.serialize(reproject(geom, src, dst))
85+
if (geometry.eval(input) != null) {
86+
val geom = JTSTypes.GeometryTypeInstance.deserialize(geometry.eval(input))
87+
JTSTypes.GeometryTypeInstance.serialize(reproject(geom, src, dst))
88+
}
89+
else {
90+
null
91+
}
8792
}
8893
}
8994
}

core/src/test/scala/org/locationtech/rasterframes/ReprojectGeometrySpec.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
package org.locationtech.rasterframes
2323

2424
import geotrellis.proj4.{CRS, LatLng, Sinusoidal, WebMercator}
25+
import org.apache.spark.sql.functions.lit
2526
import org.apache.spark.sql.Encoders
2627
import org.locationtech.jts.geom._
2728

@@ -118,5 +119,15 @@ class ReprojectGeometrySpec extends TestEnvironment {
118119

119120
checkDocs("st_reproject")
120121
}
122+
123+
it("should work on null columns") {
124+
val df = Seq(1, 2, 3).toDF("id")
125+
126+
noException shouldBe thrownBy {
127+
df.withColumn("nullId", lit(null))
128+
.select(st_reproject(st_makePoint($"nullId", $"nullId"), WebMercator, Sinusoidal))
129+
.count()
130+
}
131+
}
121132
}
122133
}

0 commit comments

Comments
 (0)