Android Open Source - Geotrackin Kml22 File Logger






From Project

Back to project page Geotrackin.

License

The source code is released under:

GNU General Public License

If you think the Android project Geotrackin listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
*    This file is part of GPSLogger for Android.
*/*from  w w w  .  j  a  va2 s . c om*/
*    GPSLogger for Android is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 2 of the License, or
*    (at your option) any later version.
*
*    GPSLogger for Android is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with GPSLogger for Android.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.geotrackin.gpslogger.loggers;

import android.location.Location;
import com.geotrackin.gpslogger.common.RejectionHandler;
import com.geotrackin.gpslogger.common.Utilities;
import org.slf4j.LoggerFactory;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.RandomAccessFile;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Kml22FileLogger implements IFileLogger {
    protected final static Object lock = new Object();
    private final static ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(128), new RejectionHandler());
    private final boolean addNewTrackSegment;
    private final File kmlFile;
    protected final String name = "KML";


    public Kml22FileLogger(File kmlFile, boolean addNewTrackSegment) {
        this.kmlFile = kmlFile;
        this.addNewTrackSegment = addNewTrackSegment;
    }


    public void Write(Location loc) throws Exception {
        Kml22WriteHandler writeHandler = new Kml22WriteHandler(loc, kmlFile, addNewTrackSegment);
        EXECUTOR.execute(writeHandler);
    }

    public void Annotate(String description, Location loc) throws Exception {
        Kml22AnnotateHandler annotateHandler = new Kml22AnnotateHandler(kmlFile, description, loc);
        EXECUTOR.execute(annotateHandler);
    }

    @Override
    public String getName() {
        return name;
    }
}

class Kml22AnnotateHandler implements Runnable {
    private static final org.slf4j.Logger tracer = LoggerFactory.getLogger(Kml22AnnotateHandler.class.getSimpleName());
    File kmlFile;
    String description;
    Location loc;

    public Kml22AnnotateHandler(File kmlFile, String description, Location loc) {
        this.kmlFile = kmlFile;
        this.description = description;
        this.loc = loc;
    }


    @Override
    public void run() {
        if (!kmlFile.exists()) {
            return;
        }

        try {
            synchronized (Kml22FileLogger.lock) {

                String descriptionNode = GetPlacemarkXml(description, loc);


                BufferedReader bf = new BufferedReader(new FileReader(kmlFile));

                StringBuilder restOfFile = new StringBuilder();
                String currentLine;
                int lineNumber = 1;

                while ((currentLine = bf.readLine()) != null) {
                    if (lineNumber > 1) {
                        restOfFile.append(currentLine);
                        restOfFile.append("\n");
                    }

                    lineNumber++;
                }

                bf.close();

                RandomAccessFile raf = new RandomAccessFile(kmlFile, "rw");
                raf.seek(255);
                raf.write(descriptionNode.getBytes());
                raf.write(restOfFile.toString().getBytes());
                raf.close();

            }
        } catch (Exception e) {
            tracer.error("Kml22FileLogger.Annotate", e);
        }
    }

    String GetPlacemarkXml(String description, Location loc) {
        StringBuilder descriptionNode = new StringBuilder();
        descriptionNode.append("<Placemark><name>");
        descriptionNode.append(description);
        descriptionNode.append("</name><Point><coordinates>");
        descriptionNode.append(String.valueOf(loc.getLongitude()));
        descriptionNode.append(",");
        descriptionNode.append(String.valueOf(loc.getLatitude()));
        descriptionNode.append(",");
        descriptionNode.append(String.valueOf(loc.getAltitude()));
        descriptionNode.append("</coordinates></Point></Placemark>\n");

        return descriptionNode.toString();
    }
}

class Kml22WriteHandler implements Runnable {

    private static final org.slf4j.Logger tracer = LoggerFactory.getLogger(Kml22WriteHandler.class.getSimpleName());
    boolean addNewTrackSegment;
    File kmlFile;
    Location loc;


    public Kml22WriteHandler(Location loc, File kmlFile, boolean addNewTrackSegment) {

        this.loc = loc;
        this.kmlFile = kmlFile;
        this.addNewTrackSegment = addNewTrackSegment;
    }


    @Override
    public void run() {
        try {

            RandomAccessFile raf;

            String dateTimeString = Utilities.GetIsoDateTime(new Date(loc.getTime()));
            String placemarkHead = "<Placemark>\n<gx:Track>\n";
            String placemarkTail = "</gx:Track>\n</Placemark></Document></kml>\n";

            synchronized (Kml22FileLogger.lock) {

                if (!kmlFile.exists()) {
                    kmlFile.createNewFile();

                    FileOutputStream initialWriter = new FileOutputStream(kmlFile, true);
                    BufferedOutputStream initialOutput = new BufferedOutputStream(initialWriter);

                    StringBuilder initialXml = new StringBuilder();
                    initialXml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                    initialXml.append("<kml xmlns=\"http://www.opengis.net/kml/2.2\" ");
                    initialXml.append("xmlns:gx=\"http://www.google.com/kml/ext/2.2\" ");
                    initialXml.append("xmlns:kml=\"http://www.opengis.net/kml/2.2\" ");
                    initialXml.append("xmlns:atom=\"http://www.w3.org/2005/Atom\">");
                    initialXml.append("<Document>");
                    initialXml.append("<name>").append(dateTimeString).append("</name>\n");

                    initialXml.append("</Document></kml>\n");
                    initialOutput.write(initialXml.toString().getBytes());
                    initialOutput.flush();
                    initialOutput.close();

                    //New file, so new track segment
                    addNewTrackSegment = true;
                }


                if (addNewTrackSegment) {
                    raf = new RandomAccessFile(kmlFile, "rw");
                    raf.seek(kmlFile.length() - 18);
                    raf.write((placemarkHead + placemarkTail).getBytes());
                    raf.close();

                }

                StringBuilder coords = new StringBuilder();
                coords.append("\n<when>");
                coords.append(dateTimeString);
                coords.append("</when>\n<gx:coord>");
                coords.append(String.valueOf(loc.getLongitude()));
                coords.append(" ");
                coords.append(String.valueOf(loc.getLatitude()));
                coords.append(" ");
                coords.append(String.valueOf(loc.getAltitude()));
                coords.append("</gx:coord>\n");
                coords.append(placemarkTail);

                raf = new RandomAccessFile(kmlFile, "rw");
                raf.seek(kmlFile.length() - 42);
                raf.write(coords.toString().getBytes());
                raf.close();
                tracer.debug("Finished writing to KML22 File");
            }

        } catch (Exception e) {
            tracer.error("Kml22FileLogger.Write", e);
        }
    }
}




Java Source Code List

com.geotrackin.gpslogger.Faqtivity.java
com.geotrackin.gpslogger.GeneralLocationListener.java
com.geotrackin.gpslogger.GpsLoggingService.java
com.geotrackin.gpslogger.GpsMainActivity.java
com.geotrackin.gpslogger.IGpsLoggerServiceClient.java
com.geotrackin.gpslogger.NavigationDrawerFragment.java
com.geotrackin.gpslogger.StartupReceiver.java
com.geotrackin.gpslogger.common.AppSettings.java
com.geotrackin.gpslogger.common.GpsRollingFileAppender.java
com.geotrackin.gpslogger.common.IActionListener.java
com.geotrackin.gpslogger.common.IMessageBoxCallback.java
com.geotrackin.gpslogger.common.OpenGTSClient.java
com.geotrackin.gpslogger.common.RejectionHandler.java
com.geotrackin.gpslogger.common.Session.java
com.geotrackin.gpslogger.common.Utilities.java
com.geotrackin.gpslogger.common.FileDialog.FileDialog.java
com.geotrackin.gpslogger.common.FileDialog.SelectionMode.java
com.geotrackin.gpslogger.loggers.FileLoggerFactory.java
com.geotrackin.gpslogger.loggers.Gpx10FileLogger.java
com.geotrackin.gpslogger.loggers.IFileLogger.java
com.geotrackin.gpslogger.loggers.Kml22FileLogger.java
com.geotrackin.gpslogger.loggers.OpenGTSLogger.java
com.geotrackin.gpslogger.loggers.PlainTextFileLogger.java
com.geotrackin.gpslogger.loggers.customurl.CustomUrlLoggerActivity.java
com.geotrackin.gpslogger.loggers.customurl.HttpUrlLogger.java
com.geotrackin.gpslogger.loggers.nmea.NmeaFileLogger.java
com.geotrackin.gpslogger.senders.AlarmReceiver.java
com.geotrackin.gpslogger.senders.FileSenderFactory.java
com.geotrackin.gpslogger.senders.IFileSender.java
com.geotrackin.gpslogger.senders.ZipHelper.java
com.geotrackin.gpslogger.senders.dropbox.DropBoxAuthorizationActivity.java
com.geotrackin.gpslogger.senders.dropbox.DropBoxHelper.java
com.geotrackin.gpslogger.senders.email.AutoEmailActivity.java
com.geotrackin.gpslogger.senders.email.AutoEmailHelper.java
com.geotrackin.gpslogger.senders.email.Mail.java
com.geotrackin.gpslogger.senders.ftp.AutoFtpActivity.java
com.geotrackin.gpslogger.senders.ftp.FtpHelper.java
com.geotrackin.gpslogger.senders.ftp.Ftp.java
com.geotrackin.gpslogger.senders.gdocs.GDocsHelper.java
com.geotrackin.gpslogger.senders.gdocs.GDocsSettingsActivity.java
com.geotrackin.gpslogger.senders.opengts.GpxReader.java
com.geotrackin.gpslogger.senders.opengts.OpenGTSActivity.java
com.geotrackin.gpslogger.senders.opengts.OpenGTSHelper.java
com.geotrackin.gpslogger.senders.osm.OSMAuthorizationActivity.java
com.geotrackin.gpslogger.senders.osm.OSMHelper.java
com.geotrackin.gpslogger.settings.GeneralSettingsActivity.java
com.geotrackin.gpslogger.settings.LoggingSettingsActivity.java
com.geotrackin.gpslogger.settings.UploadSettingsActivity.java
com.geotrackin.gpslogger.shortcuts.ShortcutCreate.java
com.geotrackin.gpslogger.shortcuts.ShortcutStart.java
com.geotrackin.gpslogger.shortcuts.ShortcutStop.java
com.geotrackin.gpslogger.views.GenericViewFragment.java
com.geotrackin.gpslogger.views.GpsBigViewFragment.java
com.geotrackin.gpslogger.views.GpsDetailedViewFragment.java
com.geotrackin.gpslogger.views.GpsSimpleViewFragment.java
com.geotrackin.gpslogger.views.component.ToggleComponent.java