package net.catchpole.B9.tools;

import net.catchpole.B9.devices.gps.GpsParser;
import net.catchpole.B9.devices.gps.listener.LocationListener;
import net.catchpole.B9.spacial.Location;

import java.io.*;

/**
 * Parses raw GPS data and creates a KML file which can be loaded into Google Earth.
 */
public class KMLGenerator {
    private GpsParser gpsParser = new GpsParser();

    public KMLGenerator(String inFile) throws IOException {
        DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(inFile)));
        try {
            final PrintWriter writer = new PrintWriter(inFile + ".kml", "UTF-8");
            writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?><kml xmlns=\"http://www.opengis.net/kml/2.2\"><Document><Placemark><name>" +
                    inFile + "</name><description></description><LineString><extrude>1</extrude><tessellate>1</tessellate><coordinates>");
            gpsParser.addListener(new LocationListener() {
                public void listen(Location location) {
                    writer.println(String.format( "%.6f", location.getLongitude() ) + "," + String.format( "%.6f", location.getLatitude() )+ ",0");
                }
            });
            try {
                String line;
                while ((line = dis.readLine()) != null) {
                    gpsParser.parse(line);
                }
                writer.println("</coordinates></LineString></Placemark></Document></kml>");
            } finally {
                writer.close();
            }
        } finally {
            dis.close();
        }
    }

    public static void main(String[] args) throws IOException {
        new KMLGenerator(args[0]);
    }
}

@@ -369,16 +369,71 @@
 arser();
+%0A    private PrintWriter writer;%0A    private int count;
 %0A%0A    pu
@@ -621,26 +621,59 @@
     
-final PrintWriter 
+String outFile = inFile + %22.kml%22;%0A            this.
 writ
@@ -693,31 +693,23 @@
 tWriter(
-in
+out
 File
- + %22.kml%22
 , %22UTF-8
@@ -1257,24 +1257,53 @@
 ) )+ %22,0%22);%0A
+                    count++;%0A
             
@@ -1630,32 +1630,32 @@
 writer.close();%0A
-
             %7D%0A  
@@ -1644,32 +1644,110 @@
 ;%0A            %7D%0A
+            System.out.println(count + %22 coordinates written to %22 + outFile);%0A
         %7D finall

add coordinate count to KMLGenerator