package com.ximple.eofms.util; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Envelope; public abstract class TPCLIDConverter { public final static int SX1200 = 800; public final static int SY1200 = 500; public final static int SX600 = 400; public final static int SY600 = 250; public static String CoordinateToTpclId(Coordinate dp) { long orgX, orgY; int mapX, mapY; int shiftX, shiftY; int dx1, dy1; int dx2, dy2; if (dp == null) { return null; } double X = dp.x; double Y = dp.y; String coordToTPCLID = ""; char mapID = coordinateToSingleMapID(dp); if (mapID == 'w') { return ""; } /* get the origin point of mapID */ Coordinate Point = getOriginPoint("" + mapID, Integer.MAX_VALUE); if (Point == null) { return null; } orgX = (long) Point.x; orgY = (long) Point.y; mapX = intDivision((X - orgX), SX1200); if (mapID == 'Z' && mapX >= 100) { mapX = mapX - 100; } mapY = intDivision((Y - orgY), SY1200); shiftX = (int) (X - orgX) % SX1200; shiftY = (int) (Y - orgY) % SY1200; dx1 = intDivision((shiftX % 100), 10); dy1 = intDivision((shiftY % 100), 10); dx2 = (shiftX % 100) % 10; dy2 = (shiftY % 100) % 10; coordToTPCLID = "" + mapID; coordToTPCLID = coordToTPCLID + dataFormat(mapX); coordToTPCLID = coordToTPCLID + dataFormat(mapY); coordToTPCLID = coordToTPCLID + intToAscii(shiftX / 100 + asciiToInt("A")); coordToTPCLID = coordToTPCLID + intToAscii(shiftY / 100 + asciiToInt("A")); coordToTPCLID = coordToTPCLID + dx1 + dy1 + dx2 + dy2; return coordToTPCLID; } public static char coordinateToSingleMapID(Coordinate dp) { char mapID = 'w'; String[] strY = StringUtils.splitToArray(Double.toString(dp.y), "."); String[] strX = StringUtils.splitToArray(Double.toString(dp.x), "."); int intY = Integer.parseInt(strY[0]); int intX = Integer.parseInt(strX[0]); if (intY > 2944000) { return mapID; } if (intY >= 2894000 && intY <= 2944000 && intX >= 10000 && intX <= 90000) { mapID = 'S'; return mapID; } if (intY >= 2614000 && intY <= 2664000 && intX >= 10000 && intX <= 66000) { mapID = 'X'; return mapID; } if (intY >= 2564000 && intY <= 2614000 && intX >= 10000 && intX <= 66000) { mapID = 'Y'; return mapID; } if (intY >= 2675800 && intY <= 2725800 && intX >= 10000 && intX <= 170000) { mapID = 'Z'; return mapID; } if (intY > 2800000) { return mapID; } if (intY >= 2750000) { if (intX < 170000) { return mapID; } if (intX < 250000) { mapID = 'A'; return mapID; } if (intX < 330000) { mapID = 'B'; return mapID; } if (intX < 410000) { mapID = 'C'; return mapID; } return mapID; } if (intY >= 2700000) { if (intX < 170000) { return mapID; } if (intX < 250000) { mapID = 'D'; return mapID; } if (intX < 330000) { mapID = 'E'; return mapID; } if (intX < 410000) { mapID = 'F'; return mapID; } return mapID; } if (intY >= 2650000) { if (intX < 170000) { return mapID; } if (intX < 250000) { mapID = 'G'; return mapID; } if (intX < 330000) { mapID = 'H'; return mapID; } if (intX < 410000) { mapID = 'I'; return mapID; } return mapID; } if (intY >= 2600000) { if (intX < 90000) { return mapID; } if (intX < 170000) { mapID = 'J'; return mapID; } if (intX < 250000) { mapID = 'K'; return mapID; } if (intX < 330000) { mapID = 'L'; return mapID; } return mapID; } if (intY >= 2550000) { if (intX < 90000) { return mapID; } if (intX < 170000) { mapID = 'M'; return mapID; } if (intX < 250000) { mapID = 'N'; return mapID; } if (intX < 330000) { mapID = 'O'; return mapID; } return mapID; } if (intY >= 2500000) { if (intX < 90000) { return mapID; } if (intX < 170000) { mapID = 'P'; return mapID; } if (intX < 250000) { mapID = 'Q'; return mapID; } if (intX < 330000) { mapID = 'R'; return mapID; } return mapID; } if (intY >= 2450000) { if (intX < 90000) { return mapID; } if (intX < 170000) { mapID = 'S'; return mapID; } if (intX < 250000) { mapID = 'T'; return mapID; } if (intX < 330000) { mapID = 'U'; return mapID; } return mapID; } if (intY >= 2400000) { if (intX < 170000) { return mapID; } if (intX < 250000) { mapID = 'V'; return mapID; } if (intX < 330000) { mapID = 'W'; return mapID; } return mapID; } return mapID; } public static Envelope convertTpclIdToEnvelope(String tpclid) { String tempString = ""; Coordinate point = null; Coordinate tempPoint = null; double width, height; if (tpclid.length() < 5) { return null; } tempString = tpclid.substring(0, 1); int xmapid = Integer.parseInt(tpclid.substring(1, 3)); // Get the origin point point = getOriginPoint(tempString, xmapid); if (point == null) { return null; } tempString = tpclid.substring(1, 5); width = SX1200; height = SY1200; tempPoint = twoNumberScale(tempString, 800, 500); if (tempPoint != null) { point.x = (point.x + tempPoint.x); point.y = (point.y + tempPoint.y); } if (tpclid.length() >= 7) { tempString = (asciiToInt(tpclid.substring(5, 6)) - 65) + "" + (asciiToInt(tpclid.substring(6, 7)) - 65); tempPoint = twoNumberScale(tempString, 100); if (tempPoint != null) { point.x = point.x + tempPoint.x; point.y = point.y + tempPoint.y; } width = 100.0; height = 100.0; } if (tpclid.length() >= 9) { tempString = tpclid.substring(7, 8); tempPoint = twoNumberScale(tempString, 10); if (tempPoint != null) { point.x = point.x + tempPoint.x; point.y = point.y + tempPoint.y; } width = 10.0; height = 10.0; } if (tpclid.length() >= 11) { tempString = tpclid.substring(10); tempPoint = twoNumberScale(tempString, 1); if (tempPoint != null) { point.x = point.x + tempPoint.x; point.y = point.y + tempPoint.y; } width = 1.0; height = 1.0; } Coordinate pt2 = new Coordinate(point); pt2.x += width; pt2.y += height; return new Envelope(point, pt2); } public static Coordinate convertTpclIdToCoordinate(String tpclid) { String tempString = ""; Coordinate point = null; Coordinate tempPoint = null; if (tpclid.length() < 5) { return null; } tempString = tpclid.substring(0, 1); int xmapid = Integer.parseInt(tpclid.substring(1, 3)); // Get the origin point point = getOriginPoint(tempString, xmapid); if (point == null) { return null; } tempString = tpclid.substring(1, 5); tempPoint = twoNumberScale(tempString, 800, 500); if (tempPoint != null) { point.x = (point.x + tempPoint.x); point.y = (point.y + tempPoint.y); } if (tpclid.length() >= 7) { tempString = (asciiToInt(tpclid.substring(5, 6)) - 65) + "" + (asciiToInt(tpclid.substring(6, 7)) - 65); tempPoint = twoNumberScale(tempString, 100); if (tempPoint != null) { point.x = point.x + tempPoint.x; point.y = point.y + tempPoint.y; } } if (tpclid.length() >= 9) { tempString = tpclid.substring(7, 9); tempPoint = twoNumberScale(tempString, 10); if (tempPoint != null) { point.x = point.x + tempPoint.x; point.y = point.y + tempPoint.y; } } if (tpclid.length() >= 11) { tempString = tpclid.substring(10); tempPoint = twoNumberScale(tempString, 1); if (tempPoint != null) { point.x = point.x + tempPoint.x; point.y = point.y + tempPoint.y; } } return point; } private static int intDivision(double p1, int p2) { double resultValue = 0.0; String result; resultValue = p1 / p2; result = Double.toString(resultValue); String[] temp = StringUtils.splitToArray(result, "."); result = temp[0]; return Integer.parseInt(result); } private static Coordinate twoNumberScale(String number, int scaleX, int scaleY) { Coordinate tempPoint = new Coordinate(); if (number.length() == 2) { tempPoint.x = Double.parseDouble(number.substring(0, 1)) * scaleX; tempPoint.y = Double.parseDouble(number.substring(1, 2)) * scaleY; } else if (number.length() == 1) { tempPoint.x = Double.parseDouble(number.substring(0, 1)) * scaleX; tempPoint.y = 0; } else if (number.length() == 4) { tempPoint.x = Double.parseDouble(number.substring(0, 2)) * scaleX; tempPoint.y = Double.parseDouble(number.substring(2, 4)) * scaleY; } else { tempPoint.x = 0; tempPoint.y = 0; } return tempPoint; } public static int asciiToInt(String p1) { if (p1.endsWith("A")) return 65; if (p1.endsWith("B")) return 66; if (p1.endsWith("C")) return 67; if (p1.endsWith("D")) return 68; if (p1.endsWith("E")) return 69; if (p1.endsWith("F")) return 70; if (p1.endsWith("G")) return 71; if (p1.endsWith("H")) return 72; if (p1.endsWith("I")) return 73; if (p1.endsWith("J")) return 74; if (p1.endsWith("K")) return 75; if (p1.endsWith("L")) return 76; if (p1.endsWith("M")) return 77; if (p1.endsWith("N")) return 78; if (p1.endsWith("O")) return 79; if (p1.endsWith("P")) return 80; if (p1.endsWith("Q")) return 81; if (p1.endsWith("R")) return 82; if (p1.endsWith("S")) return 83; if (p1.endsWith("T")) return 84; if (p1.endsWith("U")) return 85; if (p1.endsWith("V")) return 86; if (p1.endsWith("W")) return 87; if (p1.endsWith("X")) return 88; if (p1.endsWith("Y")) return 89; if (p1.endsWith("Z")) return 90; return 0; } public static char intToAscii(int p1) { switch (p1) { case 65: return 'A'; case 66: return 'B'; case 67: return 'C'; case 68: return 'D'; case 69: return 'E'; case 70: return 'F'; case 71: return 'G'; case 72: return 'H'; case 73: return 'I'; case 74: return 'J'; case 75: return 'K'; case 76: return 'L'; case 77: return 'M'; case 78: return 'N'; case 79: return 'O'; case 80: return 'P'; case 81: return 'Q'; case 82: return 'R'; case 83: return 'S'; case 84: return 'T'; case 85: return 'U'; case 86: return 'V'; case 87: return 'W'; case 88: return 'X'; case 89: return 'Y'; case 90: return 'Z'; default: return '1'; } } private static Coordinate getOriginPoint(String letter, int xMapId) { int aSwitch = asciiToInt(letter); Coordinate Point = new Coordinate(); switch (aSwitch) { case 65: //A { Point.x = 170000; Point.y = 2750000; break; } case 66: //B { Point.x = 250000; Point.y = 2750000; break; } case 67: //C { Point.x = 330000; Point.y = 2750000; break; } case 68: //D { Point.x = 170000; Point.y = 2700000; break; } case 69: //E { Point.x = 250000; Point.y = 2700000; break; } case 70: //F { Point.x = 330000; Point.y = 2700000; break; } case 71: //G { Point.x = 170000; Point.y = 2650000; break; } case 72: //H { Point.x = 250000; Point.y = 2650000; break; } case 73: //I { Point.x = 330000; Point.y = 2650000; break; } case 74: //J { Point.x = 90000; Point.y = 2600000; break; } case 75: //K { Point.x = 170000; Point.y = 2600000; break; } case 76: //L { Point.x = 250000; Point.y = 2600000; break; } case 77: //M { Point.x = 90000; Point.y = 2550000; break; } case 78: //N { Point.x = 170000; Point.y = 2550000; break; } case 79: //O { Point.x = 250000; Point.y = 2550000; break; } case 80: //P { Point.x = 90000; Point.y = 2500000; break; } case 81: //Q { Point.x = 170000; Point.y = 2500000; break; } case 82: //R { Point.x = 250000; Point.y = 2500000; break; } case 83: // S { Point.x = 10000; Point.y = 2894000; break; } case 84: //T { Point.x = 170000; Point.y = 2450000; break; } case 85: //U { Point.x = 250000; Point.y = 2450000; break; } case 86: //V { Point.x = 170000; Point.y = 2400000; break; } case 87: // W { Point.x = 250000; Point.y = 2400000; break; } case 88: //X { Point.x = 10000; Point.y = 2614000; break; } case 89: //Y { Point.x = 10000; Point.y = 2564000; break; } case 90: //Z { Point.x = (xMapId < 51) ? 90000 : 10000; Point.y = 2675800; break; } default: { return null; } } return Point; } private static Coordinate twoNumberScale(String number, int scale) { return twoNumberScale(number, scale, scale); } private static String dataFormat(int p1) { String s1 = Integer.toString(p1); if (s1.length() < 2) s1 = "0" + s1; return s1; } }