// Updates: 2006.02.14 import java.io.*; /** * Converts the WGS84 coordinates of a track file (track.txt) provided by the * GeoVTag application in CH1903. * * @author Michel Deriaz */ public class Track2CH1903 { /** * An abreviation equals to Double.NEGATIVE_INFINITY, used as an error code. */ public static final double NI = Double.NEGATIVE_INFINITY; /** * Main method. Lines with errors are ignored. */ public static void main(String[] args) { String result = ""; String data = readFile("track.txt"); String[] lines = data.split("\\r\\n|\\r|\\n"); for (int i = 0; i < lines.length; i++) { String[] items = lines[i].split(","); if (items.length < 2) continue; // ignore lines with errors double latSec = NI; double lonSec = NI; try { latSec = Double.parseDouble(items[1]) * 3600; lonSec = Double.parseDouble(items[2]) * 3600; } catch (Exception e) { continue; // ignore lines with errors } double[] inCH = WGS84ToCH1903(latSec, lonSec); result += items[0] + "," + inCH[0] + "," + inCH[1]; // for (int j = 3; j < items.length; j++) { // result += "," + items[j]; // } result += "\n"; } writeFile("track.txt", result); } /** * Transforms WGS84 coordinates in CH1903. * @param latSec the latitude, in seconds * @param lonSec the longitude, in seconds * @return a (x,y) couple of CH coordinates */ public static double[] WGS84ToCH1903(double latSec, double lonSec) { double[] result = new double[2]; double lat2 = (latSec - 169028.66) / 10000; double lon2 = (lonSec - 26782.5) / 10000; result[0] = 200147.07 + 308807.95 * lat2 + 3745.25 * lon2 * lon2 + 76.63 * lat2 * lat2 - 194.56 * lon2 * lon2 * lat2 + 119.79 * lat2 * lat2 * lat2; result[1] = 600072.37 + 211455.93 * lon2 - 10938.51 * lon2 * lat2 - 0.36 * lon2 * lat2 * lat2 - 44.54 * lon2 * lon2 * lon2; return result; } private static String readFile(String name) { File f = new File(name); int size = (int)f.length (); char[] cbuf = new char[size]; try { FileReader fr = new FileReader(name); fr.read(cbuf); fr.close(); } catch(IOException ioe) { return null; } return String.valueOf(cbuf); } private static void writeFile(String name, String text) { try { FileWriter fw = new FileWriter(name); fw.write(text); fw.close(); } catch(IOException ioe) { System.out.println(ioe); } } }