Skip to content

Commit

Permalink
make extractBufferTile function total, FocalNeighborhood.fromString f…
Browse files Browse the repository at this point in the history
…unction signature change
  • Loading branch information
pomadchin committed Sep 30, 2021
1 parent c406536 commit 5c48898
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ trait TypedEncoders {
implicit val uriInjection: Injection[URI, String] = Injection(_.toString, new URI(_))
implicit val uriTypedEncoder: TypedEncoder[URI] = TypedEncoder.usingInjection

implicit val neighborhoodInjection: Injection[Neighborhood, String] = Injection(FocalNeighborhood(_), FocalNeighborhood.unapply(_).get)
implicit val neighborhoodInjection: Injection[Neighborhood, String] = Injection(FocalNeighborhood(_), FocalNeighborhood.fromString(_).get)
implicit val neighborhoodTypedEncoder: TypedEncoder[Neighborhood] = TypedEncoder.usingInjection

implicit val envelopeTypedEncoder: TypedEncoder[Envelope] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ object DynamicExtractors {
}

lazy val neighborhoodExtractor: PartialFunction[DataType, Any => Neighborhood] = {
case _: StringType => (v: Any) => FocalNeighborhood.unapply(v.asInstanceOf[UTF8String].toString).get
case _: StringType => (v: Any) => FocalNeighborhood.fromString(v.asInstanceOf[UTF8String].toString).get
case n if n.conformsToSchema(neighborhoodEncoder.schema) => { case ir: InternalRow => ir.as[Neighborhood] }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ package object focalops extends Serializable {
// otherwise it is some tile
case _ => prt.tile
}
case _ => tile
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ package object util extends DataFrameRenderers {
import geotrellis.raster.mapalgebra.focal._

// pattern matching and string interpolation work only since Scala 2.13
def unapply(name: String): Option[Neighborhood] =
def fromString(name: String): Try[Neighborhood] = Try {
name.toLowerCase().trim() match {
case s if s.startsWith("square-") => Try(Square(Integer.parseInt(s.split("square-").last))).toOption
case s if s.startsWith("circle-") => Try(Circle(java.lang.Double.parseDouble(s.split("circle-").last))).toOption
case s if s.startsWith("nesw-") => Try(Nesw(Integer.parseInt(s.split("nesw-").last))).toOption
case s if s.startsWith("wedge-") => Try {
case s if s.startsWith("square-") => Square(Integer.parseInt(s.split("square-").last))
case s if s.startsWith("circle-") => Circle(java.lang.Double.parseDouble(s.split("circle-").last))
case s if s.startsWith("nesw-") => Nesw(Integer.parseInt(s.split("nesw-").last))
case s if s.startsWith("wedge-") => {
val List(radius: Double, startAngle: Double, endAngle: Double) =
s
.split("wedge-")
Expand All @@ -206,9 +206,9 @@ package object util extends DataFrameRenderers {
.map(java.lang.Double.parseDouble)

Wedge(radius, startAngle, endAngle)
}.toOption
}

case s if s.startsWith("annulus-") => Try {
case s if s.startsWith("annulus-") => {
val List(innerRadius: Double, outerRadius: Double) =
s
.split("annulus-")
Expand All @@ -218,9 +218,10 @@ package object util extends DataFrameRenderers {
.map(java.lang.Double.parseDouble)

Annulus(innerRadius, outerRadius)
}.toOption
case _ => None
}
case _ => throw new IllegalArgumentException(s"Unrecognized Neighborhood $name")
}
}

def apply(neighborhood: Neighborhood): String = {
neighborhood match {
Expand Down

0 comments on commit 5c48898

Please sign in to comment.