diff --git a/importPicture/src/osm/jp/gpx/ImportPicture.java b/importPicture/src/osm/jp/gpx/ImportPicture.java index ef2b2fd..5586322 100644 --- a/importPicture/src/osm/jp/gpx/ImportPicture.java +++ b/importPicture/src/osm/jp/gpx/ImportPicture.java @@ -1,6 +1,8 @@ package osm.jp.gpx; import java.io.*; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.nio.channels.FileChannel; import java.text.DateFormat; import java.text.DecimalFormat; @@ -577,14 +579,15 @@ gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF); gpsDir.add(GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF, latitudeRef); { - double value = longitude; - final double longitudeDegrees = (long) value; - value %= 1; - value *= 60.0; - final double longitudeMinutes = (long) value; - value %= 1; - value *= 60.0; - final double longitudeSeconds = value; + BigDecimal value = BigDecimal.valueOf(longitude); + + final double longitudeDegrees = (value.setScale(0, RoundingMode.DOWN)).doubleValue(); + value = value.subtract(BigDecimal.valueOf(longitudeDegrees)); + value = value.multiply(BigDecimal.valueOf(60)); + final double longitudeMinutes = (value.setScale(0, RoundingMode.DOWN)).doubleValue(); + value = value.subtract(BigDecimal.valueOf(longitudeMinutes)); + value = value.multiply(BigDecimal.valueOf(60)); + final double longitudeSeconds = (value.setScale(0, RoundingMode.DOWN)).doubleValue(); gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_LONGITUDE); gpsDir.add(GpsTagConstants.GPS_TAG_GPS_LONGITUDE, RationalNumber.valueOf(longitudeDegrees), @@ -592,15 +595,14 @@ RationalNumber.valueOf(longitudeSeconds)); } { - double value = latitude; - final double latitudeDegrees = (long) value; - value %= 1; - value *= 60.0; - final double latitudeMinutes = (long) value; - value %= 1; - value *= 60.0; - final double latitudeSeconds = value; - gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_LATITUDE); + BigDecimal value = BigDecimal.valueOf(latitude); + final double latitudeDegrees = (value.setScale(0, RoundingMode.DOWN)).doubleValue(); + value = value.subtract(BigDecimal.valueOf(latitudeDegrees)); + value = value.multiply(BigDecimal.valueOf(60)); + final double latitudeMinutes = (value.setScale(0, RoundingMode.DOWN)).doubleValue(); + value = value.subtract(BigDecimal.valueOf(latitudeMinutes)); + value = value.multiply(BigDecimal.valueOf(60)); + final double latitudeSeconds = (value.setScale(0, RoundingMode.DOWN)).doubleValue(); gpsDir.removeField(GpsTagConstants.GPS_TAG_GPS_LATITUDE); gpsDir.add(GpsTagConstants.GPS_TAG_GPS_LATITUDE, RationalNumber.valueOf(latitudeDegrees), RationalNumber.valueOf(latitudeMinutes), diff --git a/importPicture/test/osm/jp/gpx/ImportPictureTest.java b/importPicture/test/osm/jp/gpx/ImportPictureTest.java index a462fe5..95d4b66 100644 --- a/importPicture/test/osm/jp/gpx/ImportPictureTest.java +++ b/importPicture/test/osm/jp/gpx/ImportPictureTest.java @@ -9,6 +9,8 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.nio.channels.FileChannel; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; @@ -227,7 +229,7 @@ // in TRKSEG(1) (2017-05-29T01:23:18 - 2017-05-29T01:24:05) new Expecter("separate/20170529_102318A.jpg", true, "2017:05:29 10:23:18", 35.8812697884D, 137.9952202085D, true), new Expecter("separate/20170529_102322A.jpg", true, "2017:05:29 10:23:22", 35.8810500987D, 137.9951669835D, true), - new Expecter("separate/20170529_102405A.jpg", true, "2017:05:29 10:24:05", 35.8808881603D, 137.9979396332D, true), + new Expecter("separate/20170529_102405A.jpg", true, "2017:05:29 10:24:05", 35.8808641881D, 137.9981065169D, true), // out of time (2017-05-29T01:24:05 - 2017-05-29T01:24:37) new Expecter("separate/20170529_102409A.jpg", false, null, 90.0D, 180.0D, false), @@ -263,7 +265,7 @@ // in TRKSEG(1) (2017-05-29T01:23:18 - 2017-05-29T01:24:05) new Expecter("separate/20170529_102318A.jpg", true, "2017:05:29 10:23:18", 35.8812697884D, 137.9952202085D, true), new Expecter("separate/20170529_102322A.jpg", true, "2017:05:29 10:23:22", 35.8810500987D, 137.9951669835D, true), - new Expecter("separate/20170529_102405A.jpg", true, "2017:05:29 10:24:05", 35.8808881603D, 137.9979396332D, true), + new Expecter("separate/20170529_102405A.jpg", true, "2017:05:29 10:24:05", 35.8808641881D, 137.9981065169D, true), // out of time (2017-05-29T01:24:05 - 2017-05-29T01:24:37) new Expecter("separate/20170529_102409A.jpg", true, "2017:05:29 10:24:09", 90.0D, 180.0D, true), @@ -608,15 +610,61 @@ // LAT,LON GPSInfo gpsInfo = exif.getGPS(); if (es[i].latD != 90.0D) { - assertThat(round7(gpsInfo.getLatitudeAsDegreesNorth()), is(round7(es[i].latD))); + //assertThat(comparePosition(gpsInfo.getLatitudeAsDegreesNorth(), es[i].latD), is(true)); + assertThat(String.format("%.7f", gpsInfo.getLatitudeAsDegreesNorth()), is(comparePosition(es[i].latD))); } if (es[i].lonD != 180.0D) { - assertThat(round7(gpsInfo.getLongitudeAsDegreesEast()), is(round7(es[i].lonD))); + //assertThat(comparePosition(gpsInfo.getLongitudeAsDegreesEast(), es[i].lonD), is(true)); + assertThat(String.format("%.7f", gpsInfo.getLongitudeAsDegreesEast()), is(comparePosition(es[i].lonD))); } } } } + static String comparePosition(double b) { + //System.out.println(String.format("a=%.10f : b=%.10f", a, b)); + + BigDecimal bB = BigDecimal.valueOf(b); + int degreesB = (bB.setScale(0, RoundingMode.DOWN)).intValue(); + bB = bB.subtract(BigDecimal.valueOf(degreesB)); + bB = bB.multiply(BigDecimal.valueOf(60)); + int minutesB = (bB.setScale(0, RoundingMode.DOWN)).intValue(); + bB = bB.subtract(BigDecimal.valueOf(minutesB)); + bB = bB.multiply(BigDecimal.valueOf(60)); + int secondsB = (bB.setScale(0, RoundingMode.DOWN)).intValue(); + + BigDecimal aB = BigDecimal.valueOf(secondsB); + aB = aB.divide(BigDecimal.valueOf(60), 7, RoundingMode.HALF_UP); + aB = aB.add(BigDecimal.valueOf(minutesB)); + aB = aB.divide(BigDecimal.valueOf(60), 7, RoundingMode.HALF_UP); + aB = aB.add(BigDecimal.valueOf(degreesB)); + + /* + BigDecimal aB = BigDecimal.valueOf(a); + int degreesA = (aB.setScale(0, RoundingMode.DOWN)).intValue(); + aB = aB.subtract(BigDecimal.valueOf(degreesA)); + aB = aB.multiply(BigDecimal.valueOf(60)); + int minutesA = (aB.setScale(0, RoundingMode.DOWN)).intValue(); + aB = aB.subtract(BigDecimal.valueOf(minutesA)); + aB = aB.multiply(BigDecimal.valueOf(60)); + int secondsA = (aB.setScale(0, RoundingMode.DOWN)).intValue(); + System.out.println(String.format("a=%2d : b=%2d", degreesA, degreesB)); + if (degreesA != degreesB) { + return false; + } + System.out.println(String.format("a=%2d : b=%2d", minutesA, minutesB)); + if (minutesA != minutesB) { + return false; + } + System.out.println(String.format("a=%2d : b=%2d", secondsA, secondsB)); + if (secondsA != secondsB) { + return false; + } + return true; + */ + return String.format("%.7f", aB.doubleValue()); + } + /** * 小数点第8位で四捨五入した文字列 * @param d