Skip to content

Commit

Permalink
FeatureExtraction TC prototype
Browse files Browse the repository at this point in the history
Signed-off-by: Grigory Pomadchin <[email protected]>
  • Loading branch information
pomadchin committed Oct 5, 2019
1 parent 82878cb commit cb8a443
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
58 changes: 58 additions & 0 deletions raster/src/main/scala/geotrellis/raster/FeatureExtraction.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package geotrellis.raster

import geotrellis.raster.rasterize.Rasterizer
import geotrellis.vector.{Feature, Geometry, Point, PointFeature}
import spire.syntax.cfor._

import scala.collection.mutable.ListBuffer

trait FeatureExtraction[G <: Geometry, T <: CellGrid[Int], D] {
def features(geom: G, raster: Raster[T]): Array[Array[PointFeature[D]]]
}

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

implicit def multibandTile[G <: Geometry] = new FeatureExtraction[G, MultibandTile, Int] {
def features(geom: G, raster: Raster[MultibandTile]): Array[Array[PointFeature[Int]]] = {
val arr = Array.ofDim[Array[PointFeature[Int]]](raster.tile.bandCount)

cfor(0)(_ < raster.tile.bandCount, _ + 1) { i =>
val buffer = ListBuffer[PointFeature[Int]]()
Rasterizer.foreachCellByGeometry(geom, raster.rasterExtent) { case (col, row) =>
buffer += Feature(Point(raster.rasterExtent.gridToMap(col, row)), raster.tile.band(i).get(col, row))
}
arr(i) = buffer.toArray
}

arr
}
}

implicit def multibandTileDouble[G <: Geometry] = new FeatureExtraction[G, MultibandTile, Double] {
def features(geom: G, raster: Raster[MultibandTile]): Array[Array[PointFeature[Double]]] = {
val arr = Array.ofDim[Array[PointFeature[Double]]](raster.tile.bandCount)

cfor(0)(_ < raster.tile.bandCount, _ + 1) { i =>
val buffer = ListBuffer[PointFeature[Double]]()
Rasterizer.foreachCellByGeometry(geom, raster.rasterExtent) { case (col, row) =>
buffer += Feature(Point(raster.rasterExtent.gridToMap(col, row)), raster.tile.band(i).getDouble(col, row))
}
arr(i) = buffer.toArray
}

arr
}
}

implicit def tile[G <: Geometry] = new FeatureExtraction[G, Tile, Int] {
def features(geom: G, raster: Raster[Tile]): Array[Array[PointFeature[Int]]] =
multibandTile[G].features(geom, raster.mapTile(MultibandTile(_)))
}

implicit def tileDouble[G <: Geometry] = new FeatureExtraction[G, Tile, Double] {
def features(geom: G, raster: Raster[Tile]): Array[Array[PointFeature[Double]]] = {
multibandTileDouble[G].features(geom, raster.mapTile(MultibandTile(_)))
}
}
}
22 changes: 22 additions & 0 deletions raster/src/main/scala/geotrellis/raster/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,26 @@ trait Implicits
np.percentile(tile.toArrayDouble.filter(isData(_)), pctBreak)
}
}

implicit class MultibandRasterFeatureExtractionMethods(val self: Raster[MultibandTile]) extends MethodExtensions[Raster[MultibandTile]] {
def features[G <: Geometry: FeatureExtraction[*, MultibandTile, D], D](geom: G, raster: Raster[MultibandTile]): Array[Array[PointFeature[D]]] =
FeatureExtraction[G, MultibandTile, D].features(geom, raster)

def featuresInt[G <: Geometry: FeatureExtraction[*, MultibandTile, Int]](geom: G, raster: Raster[MultibandTile]): Array[Array[PointFeature[Int]]] =
features[G, Int](geom, raster)

def featuresDouble[G <: Geometry: FeatureExtraction[*, MultibandTile, Double]](geom: G, raster: Raster[MultibandTile]): Array[Array[PointFeature[Double]]] =
features[G, Double](geom, raster)
}

implicit class rasterFeatureExtractionMethods(val self: Raster[Tile]) extends MethodExtensions[Raster[Tile]] {
def features[G <: Geometry: FeatureExtraction[*, Tile, D], D](geom: G, raster: Raster[Tile]): Array[PointFeature[D]] =
FeatureExtraction[G, Tile, D].features(geom, raster).head

def featuresInt[G <: Geometry: FeatureExtraction[*, Tile, Int]](geom: G, raster: Raster[Tile]): Array[PointFeature[Int]] =
features[G, Int](geom, raster)

def featuresDouble[G <: Geometry: FeatureExtraction[*, Tile, Double]](geom: G, raster: Raster[Tile]): Array[PointFeature[Double]] =
features[G, Double](geom, raster)
}
}

0 comments on commit cb8a443

Please sign in to comment.