-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Grigory Pomadchin <[email protected]>
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
raster/src/main/scala/geotrellis/raster/FeatureExtraction.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,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(_))) | ||
} | ||
} | ||
} |
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