-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolved #2366 by having the ascii draw methods be method extensions …
…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
Showing
6 changed files
with
112 additions
and
81 deletions.
There are no files selected for viewing
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
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
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
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
91 changes: 91 additions & 0 deletions
91
raster/src/main/scala/geotrellis/raster/render/ascii/NumericEncoder.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,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 | ||
} | ||
} |
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