Skip to content

Commit 0193681

Browse files
committed
Fix raster getvaluesasstring method
1 parent 5cbfabc commit 0193681

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/main/groovy/geoscript/layer/Raster.groovy

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,20 +463,21 @@ class Raster implements Renderable {
463463
*/
464464
String getValuesAsString(Map options = [:], int x, int y, int w, int h, int band = 0) {
465465
boolean prettyPrint = options.get('prettyPrint', false)
466+
int numberOfDecimals = options.get('numberOfDecimals', 2)
466467
String NEW_LINE = System.getProperty("line.separator")
467468
StringBuilder builder = new StringBuilder()
468469
List values = getValues(x, y, w, h, band, false)
469470
int maxLength = 0
470-
int maxDecimal = 0
471471
values.eachWithIndex { List row, int r ->
472472
row.each { float n ->
473-
String s = String.valueOf(n)
474-
maxLength = Math.max(maxLength, s.length())
475-
maxDecimal = Math.max(maxDecimal, s.indexOf('.') ? s.substring(s.indexOf('.')).length() - 1 : 0)
473+
String s = String.valueOf(n.longValue())
474+
maxLength = Math.max(maxLength, s.length() + numberOfDecimals + 1)
476475
}
477476
}
478477
NumberFormat nf = NumberFormat.getNumberInstance()
479-
nf.setMinimumFractionDigits(maxDecimal)
478+
nf.setMinimumFractionDigits(numberOfDecimals)
479+
nf.setMaximumFractionDigits(numberOfDecimals)
480+
nf.setGroupingUsed(false)
480481

481482
String line = "-" * ((w * (maxLength + 3)) + 1)
482483
if (prettyPrint) {

src/test/groovy/geoscript/layer/RasterTestCase.groovy

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,15 +842,15 @@ class RasterTestCase {
842842
[0.10, 0.45, 0.193, 0.2, 12.456, 0.2, 0.0]
843843
]
844844
Raster raster = new Raster(data, bounds)
845-
String str = raster.getValuesAsString(0, 0, 7, 5).denormalize()
845+
String str = raster.getValuesAsString(0, 0, 7, 5, numberOfDecimals: 3).denormalize()
846846
assertEquals(""" 0.100 0.450 0.193 0.200 12.456 0.200 0.000
847847
0.100 1.450 1.193 1.200 112.456 1.200 0.000
848848
0.100 1.450 2.193 3.200 212.456 1.200 0.000
849849
0.100 1.450 1.193 1.200 112.456 1.200 0.000
850850
0.100 0.450 0.193 0.200 12.456 0.200 0.000
851851
""".denormalize(), str)
852852

853-
str = raster.getValuesAsString(0, 0, 7, 5, prettyPrint: true).denormalize()
853+
str = raster.getValuesAsString(0, 0, 7, 5, prettyPrint: true, numberOfDecimals: 3).denormalize()
854854
assertEquals("""-----------------------------------------------------------------------
855855
| 0.100 | 0.450 | 0.193 | 0.200 | 12.456 | 0.200 | 0.000 |
856856
-----------------------------------------------------------------------
@@ -866,6 +866,40 @@ class RasterTestCase {
866866

867867
}
868868

869+
@Test void getLargeValuesAsString() {
870+
Bounds bounds = new Bounds(7, 7, 17, 15, "EPSG:4326")
871+
List data = [
872+
[12120.10, 12120.45, 12120.193, 12120.2, 121212.456, 12120.2, 0.0],
873+
[12120.10, 11212.45, 12121.193, 11212.2, 1121212.456, 11212.2, 0.0],
874+
[1210.10, 12121.45, 21212.193, 31212.2, 2112122.456, 12121.2, 0.0],
875+
[12.10, 11212.45, 11212.193, 12121.2, 1212112.456, 11212.2, 0.0],
876+
[12120.10, 12120.45, 12120.193, 12120.2, 112122.456, 12120.2, 0.0]
877+
]
878+
Raster raster = new Raster(data, bounds)
879+
String str = raster.getValuesAsString(0, 0, 7, 5).denormalize()
880+
assertEquals(""" 12120.10 12120.45 12120.19 12120.20 121212.45 12120.20 0.00
881+
12120.10 11212.45 12121.19 11212.20 1121212.50 11212.20 0.00
882+
1210.10 12121.45 21212.19 31212.20 2112122.50 12121.20 0.00
883+
12.10 11212.45 11212.19 12121.20 1212112.50 11212.20 0.00
884+
12120.10 12120.45 12120.19 12120.20 112122.45 12120.20 0.00
885+
""".denormalize(), str)
886+
887+
str = raster.getValuesAsString(0, 0, 7, 5, prettyPrint: true).denormalize()
888+
assertEquals("""--------------------------------------------------------------------------------------------
889+
| 12120.10 | 12120.45 | 12120.19 | 12120.20 | 121212.45 | 12120.20 | 0.00 |
890+
--------------------------------------------------------------------------------------------
891+
| 12120.10 | 11212.45 | 12121.19 | 11212.20 | 1121212.50 | 11212.20 | 0.00 |
892+
--------------------------------------------------------------------------------------------
893+
| 1210.10 | 12121.45 | 21212.19 | 31212.20 | 2112122.50 | 12121.20 | 0.00 |
894+
--------------------------------------------------------------------------------------------
895+
| 12.10 | 11212.45 | 11212.19 | 12121.20 | 1212112.50 | 11212.20 | 0.00 |
896+
--------------------------------------------------------------------------------------------
897+
| 12120.10 | 12120.45 | 12120.19 | 12120.20 | 112122.45 | 12120.20 | 0.00 |
898+
--------------------------------------------------------------------------------------------
899+
""".denormalize(), str)
900+
901+
}
902+
869903
@Test void getData() {
870904
File file = new File(getClass().getClassLoader().getResource("alki.tif").toURI())
871905
GeoTIFF geoTIFF = new GeoTIFF(file)

0 commit comments

Comments
 (0)