diff --git a/src/main/java/haya4/tools/jpmesh/Jpmesh.java b/src/main/java/haya4/tools/jpmesh/Jpmesh.java index 4a37435..3f35e8b 100644 --- a/src/main/java/haya4/tools/jpmesh/Jpmesh.java +++ b/src/main/java/haya4/tools/jpmesh/Jpmesh.java @@ -8,49 +8,110 @@ /** * 地域メッシュコードの位置(南西角)を取得する - * @param str 地域メッシュコード - * @return 地域メッシュコードの南西角位置 + * @param str 地域メッシュコード(1~4次コード) + * @return 地域メッシュコードの南西角位置。変換できなかった場合にはNULL。 */ public static DirectPosition2D getPosition(String str) { if (str != null) { - if (str.length() == 8) { - double lat1 = Double.parseDouble(str.substring(0, 2)); - double lat2 = Double.parseDouble(str.substring(4, 5)); - double lat3 = Double.parseDouble(str.substring(6, 7)); - - double lon1 = Double.parseDouble(str.substring(2, 4)); - double lon2 = Double.parseDouble(str.substring(5, 6)); - double lon3 = Double.parseDouble(str.substring(7, 8)); + try { + Long.parseLong(str); + if (str.length() >= 4) { + double lat1 = Double.parseDouble(str.substring(0, 2)); + double lon1 = Double.parseDouble(str.substring(2, 4)); + double lat = lat1 / 1.5 * 3600; + double lon = (lon1 + 100) * 3600; - double lat = (((lat1 / 1.5 * 3600) + (lat2 * 300) + (lat3 * 30)) / 3600); - double lon = ((((lon1 + 100) * 3600) + (lon2 * 450) + (lon3 * 45)) / 3600); - return new DirectPosition2D(lon, lat); - } + if (str.length() >= 6) { + double lat2 = Double.parseDouble(str.substring(4, 5)); + double lon2 = Double.parseDouble(str.substring(5, 6)); + lat = lat + (lat2 * 300); + lon = lon + (lon2 * 450); + } + + if (str.length() >= 8) { + double lat3 = Double.parseDouble(str.substring(6, 7)); + double lon3 = Double.parseDouble(str.substring(7, 8)); + lat = lat + (lat3 * 30); + lon = lon + (lon3 * 45); + } + + if (str.length() >= 10) { + double lat4 = Double.parseDouble(str.substring(8, 9)); + double lon4 = Double.parseDouble(str.substring(9, 10)); + lat = lat + (lat4 * 3); + lon = lon + (lon4 * 4.5d); + } + + return new DirectPosition2D((lon / 3600), (lat / 3600)); + } + } + catch (NumberFormatException e) { + return null; + } } return null; } /** * 地域メッシュコードの位置(中央)を取得する - * @param str 地域メッシュコード - * @return 地域メッシュコードの中央位置 + * @param str 地域メッシュコード(1~4次コード) + * @return 地域メッシュコードの中央位置。変換できなかった場合にはNULL。 */ public static DirectPosition2D getCenterPosition(String str) { DirectPosition2D ret = getPosition(str); if (ret != null) { - double lat = (ret.getY() + (15.0d / 3600.0d)); - double lon = (ret.getX() + (22.5d / 3600.0d)); + double lat = (ret.getY()); + double lon = (ret.getX()); + if (str.length() >= 10) { + lat = (lat + (7.5d / 3600.0d)); + lon = (lon + (11.25d / 3600.0d)); + } + else if (str.length() >= 8) { + lat = (lat + (15.0d / 3600.0d)); + lon = (lon + (22.5d / 3600.0d)); + } + else if (str.length() >= 6) { + lat = (lat + (30.0d / 3600.0d)); + lon = (lon + (45.0d / 3600.0d)); + } + else if (str.length() >= 4) { + lat = (lat + (60.0d / 3600.0d)); + lon = (lon + (90.0d / 3600.0d)); + } return new DirectPosition2D(lon, lat); } return null; } + /** + * 地域メッシュコードの矩形領域を取得する + * @param str 地域メッシュコード(3次コード) + * @return 地域メッシュコードの矩形領域。変換できなかった場合にはNULL。 + */ public static Rectangle2D getRectangle(String str) { DirectPosition2D ret = getPosition(str); if (ret != null) { - double lat = (ret.getY()); - double lon = (ret.getX()); - return new Rectangle2D.Double(lon, lat, 0.0125d, (30d / 3600d)); + double lat = ret.getY(); + double lon = ret.getX(); + double lat2 = 0; + double lon2 = 0; + if (str.length() >= 10) { + lat2 = 15d / 3600.0d; + lon2 = 22.5d / 3600.0d; + } + else if (str.length() >= 8) { + lat2 = 30.0d / 3600.0d; + lon2 = 45.0d / 3600.0d; + } + else if (str.length() >= 6) { + lat2 = 60.0d / 3600.0d; + lon2 = 90.0d / 3600.0d; + } + else if (str.length() >= 4) { + lat2 = 120.0d / 3600.0d; + lon2 = 180.0d / 3600.0d; + } + return new Rectangle2D.Double(lon, lat, lon2, lat2); } return null; } diff --git a/src/test/java/haya4/tools/jpmesh/JpmeshTest.java b/src/test/java/haya4/tools/jpmesh/JpmeshTest.java index d0fc874..4c80f69 100755 --- a/src/test/java/haya4/tools/jpmesh/JpmeshTest.java +++ b/src/test/java/haya4/tools/jpmesh/JpmeshTest.java @@ -21,13 +21,28 @@ assertNotNull(ret); assertEquals(35.683333d, ret.getY(), 0.000001d); assertEquals(139.7d, ret.getX(), 0.000001d); + + ret = Jpmesh.getPosition("5339452600"); + assertNotNull(ret); + assertEquals(35.683333d, ret.getY(), 0.000001d); + assertEquals(139.7d, ret.getX(), 0.000001d); + + ret = Jpmesh.getPosition("533945"); + assertNotNull(ret); + assertEquals(35.666666d, ret.getY(), 0.000001d); + assertEquals(139.625000d, ret.getX(), 0.000001d); + + ret = Jpmesh.getPosition("5339"); + assertNotNull(ret); + assertEquals(35.333333d, ret.getY(), 0.000001d); + assertEquals(139.000000d, ret.getX(), 0.000001d); } @Test public void getCenterTest() { DirectPosition2D center = Jpmesh.getCenterPosition("53394526"); assertNotNull(center); - assertEquals(35.6875d, center.getY(), 0.000001d); + assertEquals(35.687500d, center.getY(), 0.000001d); assertEquals(139.70625d, center.getX(), 0.000001d); }