Skip to content

Commit

Permalink
More standratized crop functions behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
pomadchin authored and echeipesh committed May 30, 2018
1 parent c203aa2 commit a3083f7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,23 @@ trait MultibandTileCropMethods extends TileCropMethods[MultibandTile] {
* [[MultibandTile]] and return a new MultibandTile.
*/
def crop(gb: GridBounds, options: Options): MultibandTile = {
if (!gb.intersects(self.gridBounds)) throw GeoAttrsError(s"Grid bounds do not intersect: ${self.gridBounds} crop $gb")
self match {
case geotiffTile: GeoTiffMultibandTile =>
val cropBounds =
if(options.clamp)
gb.intersection(self)
.getOrElse(throw new GeoAttrsError(s"Grid bounds do not intersect: $self crop $gb"))
else
gb
if (options.clamp) gb.intersection(self).get
else gb
geotiffTile.crop(cropBounds)
case _ =>
val croppedBands = Array.ofDim[Tile](self.bandCount)
for(b <- 0 until self.bandCount) {
for (b <- 0 until self.bandCount) {
croppedBands(b) = self.band(b).crop(gb, options)
}
ArrayMultibandTile(croppedBands)
}
}


/**
* Given a source Extent (the extent of the present
* [[MultibandTile]]), a destination Extent, and a set of Options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,15 @@ trait SinglebandTileCropMethods extends TileCropMethods[Tile] {
* [[Tile]].
*/
def crop(gb: GridBounds, options: Options): Tile = {
if (!gb.intersects(self.gridBounds)) throw GeoAttrsError(s"Grid bounds do not intersect: ${self.gridBounds} crop $gb")
val cropBounds =
if(options.clamp)
gb.intersection(self) match {
case Some(intersection) => intersection
case None =>
throw new GeoAttrsError(s"Grid bounds do not intersect: $self crop $gb")
}
else
gb
if(options.clamp) gb.intersection(self).get
else gb

val res =
self match {
case gtTile: GeoTiffTile =>
gtTile.crop(gb)
case _ =>
CroppedTile(self, cropBounds)
case gtTile: GeoTiffTile => gtTile.crop(gb)
case _ => CroppedTile(self, cropBounds)
}

if(options.force) res.toArrayTile else res
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,13 @@ abstract class GeoTiffMultibandTile(
/**
* Crop this tile to given pixel region.
*
* @param gridBounds Pixel bounds specifying the crop area.
* @param bounds Pixel bounds specifying the crop area.
*/
def crop(gridBounds: GridBounds): ArrayMultibandTile =
crop(List(gridBounds)).next._2
def crop(bounds: GridBounds): ArrayMultibandTile = {
val iter = crop(List(bounds))
if (iter.isEmpty) throw new reader.MalformedGeoTiffException(s"No intersections of ${bounds} vs ${gridBounds}")
else iter.next._2
}

/**
* Performs a crop operaiton.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ case class MultibandGeoTiff(
case Some(ext) =>
val raster: Raster[MultibandTile] = this.raster.crop(ext, options)
MultibandGeoTiff(raster, ext, this.crs, this.tags, this.options, this.overviews)
case _ => throw new IllegalArgumentException(s"Extent to crop by ($subExtent) should intersect the imagery extent ($extent).")
case _ => throw GeoAttrsError(s"Extent to crop by ($subExtent) should intersect the imagery extent ($extent).")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ case class SinglebandGeoTiff(
case Some(ext) =>
val raster: Raster[Tile] = this.raster.crop(ext, options)
SinglebandGeoTiff(raster, ext, this.crs, this.tags, this.options, this.overviews)
case _ => throw new IllegalArgumentException(s"Extent to crop by ($subExtent) should intersect the imagery extent ($extent).")
case _ => throw GeoAttrsError(s"Extent to crop by ($subExtent) should intersect the imagery extent ($extent).")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class MultibandGeoTiffSpec extends FunSpec with Matchers with RasterMatchers wit
ymax = extent.ymax + 2 * extent.height
)

intercept[IllegalArgumentException] {
intercept[GeoAttrsError] {
tiff.crop(subExtent)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class SinglebandGeoTiffSpec extends FunSpec with Matchers with RasterMatchers wi
ymax = extent.ymax + 2 * extent.height
)

intercept[IllegalArgumentException] {
intercept[GeoAttrsError] {
tiff.crop(subExtent)
}
}
Expand Down

0 comments on commit a3083f7

Please sign in to comment.