Skip to content

Commit

Permalink
Also ensure proper formatting of UTMRef when fetching the encompassin…
Browse files Browse the repository at this point in the history
…g tile-square
  • Loading branch information
centic9 committed Oct 27, 2024
1 parent 8760f63 commit b208bb9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/main/java/org/dstadler/ctw/utils/UTMRefWithHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,8 @@ public static String getSquareString(LatLng latLng) {

private static String getSquareStringInternal(LatLng latLng) {
UTMRef utmRef = latLng.toUTMRef();
return "" +
utmRef.getLngZone() +
utmRef.getLatZone() + " " +
normalize(utmRef.getEasting()) + " " +
normalize(utmRef.getNorthing());
return formatUTMRef(utmRef.getLngZone(), utmRef.getLatZone(),
normalize(utmRef.getEasting()), normalize(utmRef.getNorthing()));
}

/**
Expand Down Expand Up @@ -197,13 +194,17 @@ public static Set<UTMRefWithHash> readSquares(File file) throws IOException {
}

public String toString() {
return formatUTMRef(this.getLngZone(), this.getLatZone(), this.getEasting(), this.getNorthing());
}

private static String formatUTMRef(int lngZone, char latZone, double easting, double northing) {
// work around a bug in underlying UTMRef when Double.toString()
// starts to use exponential format, e.g. for "33T 100000.0 10000000.0"
return String.format(Locale.ROOT, "%d%s %f %f",
this.getLngZone(), this.getLatZone(), this.getEasting(), this.getNorthing()).
lngZone, latZone, easting, northing).
// cut away trailing zeros in the decimal part, couuld not find how to do this
// with String.format() itself
replaceAll("\\.(\\d)0+", ".$1");
replaceAll("\\.(\\d)0+", ".$1");
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/org/dstadler/ctw/utils/UTMRefWithHashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ void testSquareStringNormalize() {
String ref1 = UTMRefWithHash.getSquareString(new LatLng(47.999573, 15.187657));
String ref2 = UTMRefWithHash.getSquareString(new LatLng(48.002663, 15.187625));

// first ensure we get the expected string
assertEquals("33T 513000.0 5316000.0", ref1);

assertEquals(ref1, ref2,
"Expecting to get the same square for the two coordinates, but had " + ref1 + " and " + ref2);
}
Expand Down

0 comments on commit b208bb9

Please sign in to comment.