package net.sourceforge.gpstools.kml;
/* gpxconv
* Copyright (C) 2006 Moritz Ringler
* $Id: GpxTrackWriter.java 441 2010-12-13 20:04:20Z ringler $
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import net.sourceforge.gpstools.gpx.GpxType;
import net.sourceforge.gpstools.gpx.Trk;
import net.sourceforge.gpstools.gpx.Trkpt;
import net.sourceforge.gpstools.gpx.Trkseg;
import net.sourceforge.gpstools.utils.ColorFunction;
import net.sourceforge.gpstools.utils.AbstractColorProvider;
import net.sourceforge.gpstools.utils.GpsFormat;
import net.sourceforge.gpstools.utils.ValueFunction;
import de.mospace.xml.SaxXMLWriter;
import org.xml.sax.SAXException;
public class GpxTrackWriter {
protected final SaxXMLWriter xw;
protected final ColorFunction colorize;
protected final ValueFunction values;
private final GpsFormat format = GpsFormat.getInstance();
private final DecimalFormat numformat = new DecimalFormat("#0.###");
public GpxTrackWriter(OutputStream out, String name, ValueFunction vf, ColorFunction cf) throws SAXException, IOException{
final DecimalFormatSymbols sym = numformat.getDecimalFormatSymbols();
sym.setDecimalSeparator('.');
numformat.setDecimalFormatSymbols(sym);
colorize = cf;
values = vf;
try{
xw = new SaxXMLWriter(out, "UTF-8", "http://earth.google.com/kml/2.1");
} catch (UnsupportedEncodingException wonthappen){
throw new Error(wonthappen);
}
xw.startElement("Document");
xw.textElement("name", name);
xw.textElement("visibility", "1");
}
public void writeGpx(GpxType gpx) throws SAXException, IOException{
if(gpx.getTrkCount() > 0){
/* tracklogs */
xw.startElement("Folder");
xw.textElement("name", "Tracklogs");
xw.textElement("visibility", "1");
for(Trk trk : gpx.getTrk()){
Trkseg[] segs = trk.getTrkseg();
final int numseg = segs.length;
for(int i=0; i<numseg; i++){
writeTrackSegment(trk, segs[i], numseg, i+1);
}
}
xw.endElement("Folder");
}
}
private void writeTrackSegment(Trk trk, Trkseg seg, int numseg, int segidx) throws SAXException, IOException{
Trkpt[] pts = seg.getTrkpt();
for (int i = 0; i < pts.length; ){
i = writeNextLineString(pts, i);
}
}
private Color getColor(Trkpt t1, Trkpt t2){
return colorize.getColor(values.getValue(t1, t2));
}
private int writeNextLineString(Trkpt[] pts, int startindex) throws SAXException, IOException{
int i = startindex;
if(i > 0){
double d = values.getValue(pts[i - 1], pts[i]);
final Color c = colorize.getColor(d);
xw.startElement("Placemark");
writeLabel(d);
/* Style */
xw.startElement("Style");
xw.startElement("LineStyle");
xw.textElement("color", AbstractColorProvider.format(c,"SBGR"));
xw.textElement("width", "4");
xw.endElement("LineStyle");
xw.endElement("Style");
/* Data */
xw.startElement("LineString");
StringBuilder coordinates = new StringBuilder();
formatCoordinates(pts[i - 1], coordinates);
for(Color cc = c;
cc.equals(c);
cc = getColor(pts[i - 1], pts[i]))
{
formatCoordinates(pts[i], coordinates);
i++;
if(i == pts.length){
break;
}
}
coordinates.deleteCharAt(coordinates.length() - 1);
xw.textElement("coordinates", coordinates.toString());
xw.endElement("LineString");
xw.endElement("Placemark");
} else {
i++;
}
return i;
}
private StringBuilder formatCoordinates(Trkpt pt, StringBuilder sb){
sb.append(format.asLatLon(pt.getLon(), pt.getLat()));
Number ele = pt.getEle();
if(ele != null && ele.doubleValue() >= 0.01){
sb.append(',');
sb.append(format.asAltitude(ele));
}
sb.append(' ');
return sb;
}
void writeLabel(double d) throws SAXException, IOException{
xw.textElement("name", "<span style=\"color:" +
AbstractColorProvider.format(colorize.getColor(d),"#RGB") +
"\"><b>" + getValueFormat().format(d) + "</b></span>");
}
protected NumberFormat getValueFormat(){
return numformat;
}
public void endDocument() throws SAXException, IOException{
xw.endElement("Document");
xw.endDocument();
}
}