Skip to content

Commit

Permalink
Make PointFeatureExctractor more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
pomadchin committed Oct 7, 2019
1 parent 763a10b commit 44bbfb1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ API Changes & Project structure changes

``import geotrellis.raster._``

- **New:** Rasters FeatureExtraction (`#3117 <https://github.com/locationtech/geotrellis/pull/3117>`_):

- ``geotrellis.vectortile``

- **Add:** ``geotrellis.vectortile.MVTFeature`` which properly conforms to the MVT 2.0 spec. Specifically, MVTFeature adds support for an ``Option[Long]`` id property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import spire.syntax.cfor._

import scala.collection.mutable.ListBuffer

trait FeatureExtraction[I <: Geometry, T <: CellGrid[Int], O <: Geometry, D] {
def features(geom: I, raster: Raster[T]): Array[Feature[O, Array[D]]]
trait FeatureExtractor[I <: Geometry, T <: CellGrid[Int], O <: Geometry, D] {
def features(geom: I, raster: Raster[T]): Array[Feature[O, D]]
}

object FeatureExtraction {
def apply[I <: Geometry: FeatureExtraction[*, T, O, D], T <: CellGrid[Int], O <: Geometry, D] = implicitly[FeatureExtraction[I, T, O, D]]
object FeatureExtractor {
def apply[I <: Geometry: FeatureExtractor[*, T, O, D], T <: CellGrid[Int], O <: Geometry, D] = implicitly[FeatureExtractor[I, T, O, D]]

implicit def multibandTile[I <: Geometry] = new PointFeatureExtraction[I, MultibandTile, Int] {
implicit def multibandTile[I <: Geometry] = new PointFeatureExtractor[I, MultibandTile, Array[Int]] {
def features(geom: I, raster: Raster[MultibandTile]): Array[PointFeature[Array[Int]]] = {
val buffer = ListBuffer[PointFeature[Array[Int]]]()

Expand All @@ -44,7 +44,7 @@ object FeatureExtraction {
}
}

implicit def multibandTileDouble[I <: Geometry] = new PointFeatureExtraction[I, MultibandTile, Double] {
implicit def multibandTileDouble[I <: Geometry] = new PointFeatureExtractor[I, MultibandTile, Array[Double]] {
def features(geom: I, raster: Raster[MultibandTile]): Array[PointFeature[Array[Double]]] = {
val buffer = ListBuffer[PointFeature[Array[Double]]]()

Expand All @@ -58,14 +58,14 @@ object FeatureExtraction {
}
}

implicit def tile[I <: Geometry] = new PointFeatureExtraction[I, Tile, Int] {
def features(geom: I, raster: Raster[Tile]): Array[PointFeature[Array[Int]]] =
multibandTile[I].features(geom, raster.mapTile(MultibandTile(_)))
implicit def tile[I <: Geometry] = new PointFeatureExtractor[I, Tile, Int] {
def features(geom: I, raster: Raster[Tile]): Array[PointFeature[Int]] =
multibandTile[I].features(geom, raster.mapTile(MultibandTile(_))).map(_.mapData(_.head))
}

implicit def tileDouble[I <: Geometry] = new PointFeatureExtraction[I, Tile, Double] {
def features(geom: I, raster: Raster[Tile]): Array[PointFeature[Array[Double]]] = {
multibandTileDouble[I].features(geom, raster.mapTile(MultibandTile(_)))
implicit def tileDouble[I <: Geometry] = new PointFeatureExtractor[I, Tile, Double] {
def features(geom: I, raster: Raster[Tile]): Array[PointFeature[Double]] = {
multibandTileDouble[I].features(geom, raster.mapTile(MultibandTile(_))).map(_.mapData(_.head))
}
}
}
12 changes: 6 additions & 6 deletions raster/src/main/scala/geotrellis/raster/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,17 @@ trait Implicits
}
}

implicit class MultibandRasterPointFeatureExtractionMethods(val self: Raster[MultibandTile]) extends MethodExtensions[Raster[MultibandTile]] {
implicit class MultibandRasterPointFeatureExtractorMethods(val self: Raster[MultibandTile]) extends MethodExtensions[Raster[MultibandTile]] {
def pointFeatures[D] = new {
def apply[I <: Geometry: PointFeatureExtraction[*, MultibandTile, D]](geom: I): Array[PointFeature[Array[D]]] =
FeatureExtraction[I, MultibandTile, Point, D].features(geom, self)
def apply[I <: Geometry: PointFeatureExtractor[*, MultibandTile, Array[D]]](geom: I): Array[PointFeature[Array[D]]] =
FeatureExtractor[I, MultibandTile, Point, Array[D]].features(geom, self)
}
}

implicit class rasterPointFeatureExtractionMethods(val self: Raster[Tile]) extends MethodExtensions[Raster[Tile]] {
implicit class RasterPointFeatureExtractorMethods(val self: Raster[Tile]) extends MethodExtensions[Raster[Tile]] {
def pointFeatures[D] = new {
def apply[I <: Geometry: PointFeatureExtraction[*, Tile, D]](geom: I): Array[PointFeature[D]] =
FeatureExtraction[I, Tile, Point, D].features(geom, self).map(_.mapData(_.head))
def apply[I <: Geometry: PointFeatureExtractor[*, Tile, D]](geom: I): Array[PointFeature[D]] =
FeatureExtractor[I, Tile, Point, D].features(geom, self)
}
}
}
2 changes: 1 addition & 1 deletion raster/src/main/scala/geotrellis/raster/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ package object raster extends Implicits {
def fill(v: Double) = { java.util.Arrays.fill(arr, v) ; arr }
}

type PointFeatureExtraction[I <: Geometry, T <: CellGrid[Int], D] = FeatureExtraction[I, T, Point, D]
type PointFeatureExtractor[I <: Geometry, T <: CellGrid[Int], D] = FeatureExtractor[I, T, Point, D]

/* http://stackoverflow.com/questions/3508077/how-to-define-type-disjunction-union-types */
sealed class TileOrMultibandTile[T]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import geotrellis.raster.testkit._

import org.scalatest._

class FeatureExtractionSpec extends FunSpec with Matchers {
class FeatureExtractorSpec extends FunSpec with Matchers {
describe("Tile") {
it("should extract all int point features") {
val ext = Extent(0.0, 0.0, 3.0, 3.0)
Expand Down

0 comments on commit 44bbfb1

Please sign in to comment.