Skip to content

Commit

Permalink
Improved the reprojected logic so that it now checks to see if a repr…
Browse files Browse the repository at this point in the history
…ojection can be skipped

Signed-off-by: Jacob Bouffard <jbouffard@azavea.com>

Updated the CHANGELOG

Signed-off-by: Jacob Bouffard <jbouffard@azavea.com>

Updated remaining reproject method

Signed-off-by: Jacob Bouffard <jbouffard@azavea.com>

Fixed failing test

Signed-off-by: Jacob Bouffard <jbouffard@azavea.com>

Renamed _reproject to reprojectInternal

Signed-off-by: Jacob Bouffard <jbouffard@azavea.com>

Fixed spelling

Signed-off-by: Jacob Bouffard <jbouffard@azavea.com>

Renamed the reprojectInternal method to reprojection

Signed-off-by: Jacob Bouffard <jbouffard@azavea.com>
  • Loading branch information
Jacob Bouffard committed Jul 16, 2019
1 parent c7008db commit dc58f04
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- VLM: `GeoTiffRasterSource` reads are now thread safe
- VLM: `GeoTiffRasterSource`s will now reuse a tiff instead of rereading
it when possible.
- VLM: `RasterSource` will now short circut reprojection if the given
source is already in the target CRS.

### Removed
- Summary: Subproject removed. The polygonal summary prototype was moved to GeoTrellis core for the 3.0 release. See: https://github.com/locationtech/geotrellis/blob/master/docs/guide/rasters.rst#polygonal-summary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ case class GDALRasterSource(
}
}

def reproject(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): RasterSource =
def reprojection(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): RasterSource =
GDALRasterSource(dataPath, options.reproject(gridExtent, crs, targetCRS, reprojectOptions))

def resample(resampleGrid: ResampleGrid[Long], method: ResampleMethod, strategy: OverviewStrategy): RasterSource =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class GDALWarpOptionsSpec extends FunSpec with RasterMatchers with BetterRasterM
val rs =
GDALRasterSource(filePath)
.reproject(
targetCRS = WebMercator,
crs = WebMercator,
reprojectOptions = ReprojectOptions.DEFAULT.copy(targetCellSize = CellSize(10, 10).some),
strategy = AutoHigherResolution
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ trait MosaicRasterSource extends RasterSource {
*
* @see [[geotrellis.contrib.vlm.RasterSource.reproject]]
*/
def reproject(crs: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy)
: RasterSource = MosaicRasterSource(
sources map { _.reproject(crs, reprojectOptions, strategy) },
crs,
gridExtent.reproject(this.crs, crs, reprojectOptions)
)
def reprojection(crs: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): RasterSource =
MosaicRasterSource(
sources map { _.reproject(crs, reprojectOptions, strategy) },
crs,
gridExtent.reproject(this.crs, crs, reprojectOptions)
)

def read(extent: Extent, bands: Seq[Int]): Option[Raster[MultibandTile]] = {
val rasters = sources map { _.read(extent, bands) }
Expand Down
17 changes: 12 additions & 5 deletions vlm/src/main/scala/geotrellis/contrib/vlm/RasterSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@ trait RasterSource extends CellGrid[Long] with Serializable {
/** Raster pixel row count */
def rows: Long = gridExtent.rows

protected def reprojection(crs: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): RasterSource

/** Reproject to different CRS with explicit sampling reprojectOptions.
* @see [[geotrellis.raster.reproject.Reproject]]
* @group reproject
*/
def reproject(crs: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): RasterSource
def reproject(crs: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): RasterSource =
if (crs == this.crs) this
else reprojection(crs, reprojectOptions, strategy)

/** Sampling grid is defined over the footprint of the data at default resolution
*
Expand All @@ -94,7 +98,8 @@ trait RasterSource extends CellGrid[Long] with Serializable {
* @group reproject
*/
def reproject(crs: CRS, method: ResampleMethod = NearestNeighbor, strategy: OverviewStrategy = AutoHigherResolution): RasterSource =
reproject(crs, Reproject.Options(method = method), strategy)
if (crs == this.crs) this
else reprojection(crs, Reproject.Options(method = method), strategy)

/** Sampling grid and resolution is defined by given [[GridExtent]].
* Resulting extent is the extent of the minimum enclosing pixel region
Expand All @@ -103,16 +108,18 @@ trait RasterSource extends CellGrid[Long] with Serializable {
*/
def reprojectToGrid(crs: CRS, grid: GridExtent[Long], method: ResampleMethod = NearestNeighbor, strategy: OverviewStrategy = AutoHigherResolution): RasterSource =
if (crs == this.crs && grid == this.gridExtent) this
else if (crs == this.crs) resampleToGrid(grid)
else reproject(crs, Reproject.Options(method = method, parentGridExtent = Some(grid)), strategy)
else if (crs == this.crs) resampleToGrid(grid, method)
else reprojection(crs, Reproject.Options(method = method, parentGridExtent = Some(grid)), strategy)

/** Sampling grid and resolution is defined by given [[RasterExtent]] region.
* The extent of the result is also taken from given [[RasterExtent]],
* this region may be larger or smaller than the footprint of the data
* @group reproject
*/
def reprojectToRegion(crs: CRS, region: RasterExtent, method: ResampleMethod = NearestNeighbor, strategy: OverviewStrategy = AutoHigherResolution): RasterSource =
reproject(crs, Reproject.Options(method = method, targetRasterExtent = Some(region)), strategy)
if (crs == this.crs && region == this.gridExtent) this
else if (crs == this.crs) resampleToRegion(region.asInstanceOf[GridExtent[Long]], method)
else reprojection(crs, Reproject.Options(method = method, targetRasterExtent = Some(region)), strategy)


def resample(resampleGrid: ResampleGrid[Long], method: ResampleMethod, strategy: OverviewStrategy): RasterSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class GeotrellisRasterSource(
override def readBounds(bounds: Traversable[GridBounds[Long]], bands: Seq[Int]): Iterator[Raster[MultibandTile]] =
bounds.toIterator.flatMap(_.intersection(this.gridBounds).flatMap(read(_, bands)))

def reproject(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): RasterSource = {
def reprojection(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): RasterSource = {
if (targetCRS != this.crs) {
val (closestLayerId, targetGridExtent) = GeotrellisReprojectRasterSource.getClosestSourceLayer(targetCRS, sourceLayers, reprojectOptions, strategy)
new GeotrellisReprojectRasterSource(attributeStore, dataPath, closestLayerId, sourceLayers, targetGridExtent, targetCRS, reprojectOptions, targetCellType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class GeotrellisReprojectRasterSource(
override def readBounds(bounds: Traversable[GridBounds[Long]], bands: Seq[Int]): Iterator[Raster[MultibandTile]] =
bounds.toIterator.flatMap(_.intersection(this.gridBounds).flatMap(read(_, bands)))

def reproject(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): RasterSource = {
def reprojection(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): RasterSource = {
if (targetCRS == sourceLayer.metadata.crs) {
val resampleGrid = ResampleGrid.fromReprojectOptions(reprojectOptions).get
val resampledGridExtent = resampleGrid(this.sourceLayer.gridExtent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class GeotrellisResampleRasterSource(
.flatMap(read(_, bands))
}

def reproject(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): GeotrellisReprojectRasterSource = {
def reprojection(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): GeotrellisReprojectRasterSource = {
val (closestLayerId, gridExtent) = GeotrellisReprojectRasterSource.getClosestSourceLayer(targetCRS, sourceLayers, reprojectOptions, strategy)
new GeotrellisReprojectRasterSource(attributeStore, dataPath, layerId, sourceLayers, gridExtent, targetCRS, reprojectOptions, targetCellType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ case class GeoTiffRasterSource(
def bandCount: Int = tiff.bandCount
def cellType: CellType = dstCellType.getOrElse(tiff.cellType)

def reproject(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): GeoTiffReprojectRasterSource =
def reprojection(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): GeoTiffReprojectRasterSource =
GeoTiffReprojectRasterSource(dataPath, targetCRS, reprojectOptions, strategy, targetCellType, Some(tiff))

def resample(resampleGrid: ResampleGrid[Long], method: ResampleMethod, strategy: OverviewStrategy): GeoTiffResampleRasterSource =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ case class GeoTiffReprojectRasterSource(
}.map { convertRaster }
}

def reproject(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): RasterSource =
def reprojection(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): RasterSource =
GeoTiffReprojectRasterSource(dataPath, targetCRS, reprojectOptions, strategy, targetCellType, Some(tiff))

def resample(resampleGrid: ResampleGrid[Long], method: ResampleMethod, strategy: OverviewStrategy): RasterSource =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ case class GeoTiffResampleRasterSource(
@transient protected lazy val closestTiffOverview: GeoTiff[MultibandTile] =
tiff.getClosestOverview(gridExtent.cellSize, strategy)

def reproject(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): GeoTiffReprojectRasterSource =
def reprojection(targetCRS: CRS, reprojectOptions: Reproject.Options, strategy: OverviewStrategy): GeoTiffReprojectRasterSource =
new GeoTiffReprojectRasterSource(dataPath, targetCRS, reprojectOptions, strategy, targetCellType) {
override lazy val gridExtent: GridExtent[Long] = reprojectOptions.targetRasterExtent match {
case Some(targetRasterExtent) => targetRasterExtent.toGridType[Long]
Expand Down

0 comments on commit dc58f04

Please sign in to comment.