Skip to content

Commit

Permalink
Resolved #2366 by having the ascii draw methods be method extensions …
Browse files Browse the repository at this point in the history
…of Tile

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

Updated the CHANGELOG

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

Fixed bug

Signed-off-by: Jacob Bouffard <jbouffard@azavea.com>
  • Loading branch information
Jacob Bouffard committed Apr 26, 2018
1 parent 777588e commit 03a4d4f
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 81 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ API Changes
- **New:** ``ZoomResample`` can now be used on ``MultibandTileLayerRDD``\s.
- **New:** A ``Partitioner`` can be specified in the ``reproject`` methods of ``TileLayerRDD``.
- **New:** Compression ``level`` of GeoTiffs can be specified in the ``DeflateCompression`` constructor.
- **Change:**: The Ascii draw methods are now method extensions of ``Tile``.

Fixes
^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ case class CompositeTile(tiles: Seq[Tile],
*
* @return A string containing the ascii art
*/
override def asciiDraw(): String = {
def asciiDraw(): String = {
val sb = new StringBuilder
for(layoutRow <- 0 until tileLayout.layoutRows) {
for(row <- 0 until tileLayout.tileRows) {
Expand Down
78 changes: 0 additions & 78 deletions raster/src/main/scala/geotrellis/raster/Tile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -379,82 +379,4 @@ trait Tile extends CellGrid with IterableTile with MappableTile[Tile] with LazyL

(zmin, zmax)
}

/**
* Return ascii art of this raster.
*/
def asciiDraw(): String = {
val buff = ArrayBuffer[String]()
var max = 0
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
val v = get(col, row)
val s = if (isNoData(v)) "ND" else s"$v"
max = math.max(max, s.size)
buff += s
}
}

createAsciiTileString(buff.toArray, max)
}

/**
* Return ascii art of this raster. The single int parameter
* indicates the number of significant digits to be printed.
*/
def asciiDrawDouble(significantDigits: Int = Int.MaxValue): String = {
val buff = ArrayBuffer[String]()
val mc = new java.math.MathContext(significantDigits)
var max = 0
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
val v = getDouble(col, row)
val s = if (isNoData(v)) "ND" else {
val s = s"$v"
if (s.size > significantDigits) BigDecimal(s).round(mc).toString
else s
}

max = math.max(s.size, max)
buff += s
}
}

createAsciiTileString(buff.toArray, max)
}

private def createAsciiTileString(buff: Array[String], maxSize: Int) = {
val sb = new StringBuilder
val limit = math.max(6, maxSize)
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
val s = buff(row * cols + col)
val pad = " " * math.max(limit - s.length, 1)
sb.append(s"$pad$s")
}

sb += '\n'
}
sb += '\n'
sb.toString
}

/**
* Return ascii art of a range from this raster.
*/
def asciiDrawRange(colMin: Int, colMax: Int, rowMin: Int, rowMax: Int): String = {
var s = ""
for (row <- rowMin to rowMax) {
for (col <- colMin to colMax) {
val z = this.get(row, col)
if (isNoData(z)) {
s += ".."
} else {
s += "%02X".formatLocal(Locale.ENGLISH, z)
}
}
s += "\n"
}
s
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package geotrellis.raster.render

import geotrellis.raster.Tile
import geotrellis.raster.render.ascii.AsciiArtEncoder
import geotrellis.raster.render.ascii.{AsciiArtEncoder, NumericEncoder}
import geotrellis.util.MethodExtensions

/**
Expand All @@ -30,4 +30,21 @@ trait AsciiRenderMethods extends MethodExtensions[Tile] {
def renderAscii(palette: AsciiArtEncoder.Palette = AsciiArtEncoder.Palette.WIDE): String =
AsciiArtEncoder.encode(self, palette)

/**
* Return ascii art of this raster.
*/
def asciiDraw(): String = NumericEncoder.encodeIntegrals(self)

/**
* Return ascii art of this raster. The single int parameter
* indicates the number of significant digits to be printed.
*/
def asciiDrawDouble(significantDigits: Int = Int.MaxValue): String =
NumericEncoder.encodeDecimals(self, significantDigits)

/**
* Return ascii art of a range from this raster.
*/
def asciiDrawRange(colMin: Int, rowMin: Int, colMax: Int, rowMax: Int): String =
NumericEncoder.encodeRange(self, colMin, rowMin, colMax, rowMax)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package geotrellis.raster.render.ascii


import geotrellis.raster.{Tile, isNoData}

import spire.syntax.cfor._

import scala.collection.mutable.ArrayBuffer
import scala.math.BigDecimal

import java.util.Locale


case object NumericEncoder {
def encodeIntegrals(tile: Tile): String = {
val cols = tile.cols
val rows = tile.rows
val buff = ArrayBuffer[String]()
var max = 0
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
val v = tile.get(col, row)
val s = if (isNoData(v)) "ND" else s"$v"
max = math.max(max, s.size)
buff += s
}
}

createAsciiTileString(buff.toArray, max, cols, rows)
}

def encodeDecimals(tile: Tile, significantDigits: Int = Int.MaxValue): String = {
val cols = tile.cols
val rows = tile.rows
val buff = ArrayBuffer[String]()
val mc = new java.math.MathContext(significantDigits)
var max = 0
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
val v = tile.getDouble(col, row)
val s = if (isNoData(v)) "ND" else {
val s = s"$v"
if (s.size > significantDigits) BigDecimal(s).round(mc).toString
else s
}

max = math.max(s.size, max)
buff += s
}
}

createAsciiTileString(buff.toArray, max, cols, rows)
}

def encodeRange(tile: Tile, colMin: Int, colMax: Int, rowMin: Int, rowMax: Int): String = {
var s = ""
for (row <- rowMin to rowMax) {
for (col <- colMin to colMax) {
val z = tile.get(row, col)
if (isNoData(z)) {
s += ".."
} else {
s += "%02X".formatLocal(Locale.ENGLISH, z)
}
}
s += "\n"
}
s
}

private def createAsciiTileString(
buff: Array[String],
maxSize: Int,
cols: Int,
rows: Int
) = {
val sb = new StringBuilder
val limit = math.max(6, maxSize)
cfor(0)(_ < rows, _ + 1) { row =>
cfor(0)(_ < cols, _ + 1) { col =>
val s = buff(row * cols + col)
val pad = " " * math.max(limit - s.length, 1)
sb.append(s"$pad$s")
}

sb += '\n'
}
sb += '\n'
sb.toString
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class RDDStitchMethodsSpec extends FunSpec

assert(restitched.toArray() === tile.toArray(),
s"""Expected:
|${raster.asciiDraw()}
|${raster.tile.asciiDraw()}
|
|Stitched:
|${restitched.asciiDraw()}
Expand Down

0 comments on commit 03a4d4f

Please sign in to comment.