Ticket #2064: josm-tangogps.patch

File josm-tangogps.patch, 5.8 KB (added by egore, 16 years ago)
  • src/org/openstreetmap/josm/actions/OpenFileAction.java

     
    2323import org.openstreetmap.josm.io.GpxReader;
    2424import org.openstreetmap.josm.io.NmeaReader;
    2525import org.openstreetmap.josm.io.OsmReader;
     26import org.openstreetmap.josm.io.TangoReader;
    2627import org.xml.sax.SAXException;
    2728import org.openstreetmap.josm.tools.Shortcut;
    2829
     
    6162                openFileAsGpx(file);
    6263            else if (asNmeaData(file.getName()))
    6364                openFileAsNmea(file);
     65            else if (asTangoData(file.getName()))
     66                openFileAsTango(file);
    6467            else
    6568                openAsData(file);
    6669        } catch (SAXException x) {
     
    169172        }
    170173    }
    171174
     175    private void openFileAsTango(File file) throws IOException, FileNotFoundException {
     176        String fn = file.getName();
     177        if (ExtensionFileFilter.filters[ExtensionFileFilter.TANGO].acceptName(fn)) {
     178            TangoReader r = null;
     179            InputStream is = new FileInputStream(file);
     180            r = new TangoReader(is,file.getAbsoluteFile().getParentFile());
     181            r.data.storageFile = file;
     182            GpxLayer gpxLayer = new GpxLayer(r.data, fn);
     183            Main.main.addLayer(gpxLayer);
     184            if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
     185                MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
     186                if (ml.data.size() > 0) {
     187                    Main.main.addLayer(ml);
     188                }
     189            }
     190
     191        } else {
     192            throw new IllegalStateException();
     193        }
     194    }
     195
    172196    private boolean asGpxData(String fn) {
    173197        return ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn);
    174198    }
     
    177201        return ExtensionFileFilter.filters[ExtensionFileFilter.NMEA].acceptName(fn);
    178202    }
    179203
     204    private boolean asTangoData(String fn) {
     205        return ExtensionFileFilter.filters[ExtensionFileFilter.TANGO].acceptName(fn);
     206    }
    180207
    181208}
  • src/org/openstreetmap/josm/actions/ExtensionFileFilter.java

     
    2121    public static final int OSM = 0;
    2222    public static final int GPX = 1;
    2323    public static final int NMEA = 2;
     24    public static final int TANGO = 3;
    2425
    2526    public static ExtensionFileFilter[] filters = {
    2627        new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files (*.osm *.xml)")),
    2728        new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files (*.gpx *.gpx.gz)")),
    2829        new ExtensionFileFilter("nmea,txt", "nmea", tr("NMEA-0183 Files (*.nmea *.txt)")),
     30        new ExtensionFileFilter("log", "log", tr("TangoGPS Files (*.log)")),
    2931    };
    3032
    3133    /**
  • src/org/openstreetmap/josm/io/TangoReader.java

     
     1//License: GPL. Copyright 2008 by Christoph Brill
     2
     3package org.openstreetmap.josm.io;
     4
     5import java.io.BufferedReader;
     6import java.io.File;
     7import java.io.FileReader;
     8import java.io.IOException;
     9import java.io.InputStream;
     10import java.io.InputStreamReader;
     11import java.util.ArrayList;
     12
     13import org.openstreetmap.josm.data.coor.LatLon;
     14import org.openstreetmap.josm.data.gpx.GpxData;
     15import org.openstreetmap.josm.data.gpx.GpxTrack;
     16import org.openstreetmap.josm.data.gpx.WayPoint;
     17
     18/**
     19 * Read a log file from TangoGPS. These are simple text files in the
     20 * form: <lat>,<lon>,<elevation>,<speed>,<course>,<hdop>,<datetime>
     21 *
     22 * @author cbrill
     23 */
     24public class TangoReader {
     25
     26    public GpxData data;
     27
     28    public TangoReader(InputStream source, File relativeMarkerPath) {
     29
     30        // create the data tree
     31        data = new GpxData();
     32        GpxTrack currentTrack = new GpxTrack();
     33        data.tracks.add(currentTrack);
     34        ArrayList<WayPoint> currentTrackSeg = new ArrayList<WayPoint>();
     35
     36        BufferedReader rd = null;
     37        try {
     38
     39            rd = new BufferedReader(new InputStreamReader(source));
     40
     41            String line;
     42            while ((line = rd.readLine()) != null) {
     43                String[] lineElements = line.split(",");
     44                if (lineElements.length == 7) {
     45                    WayPoint currentWayPoint = new WayPoint(parseLatLon(lineElements));
     46                    currentWayPoint.attr.put("ele", lineElements[2]);
     47                    currentWayPoint.attr.put("time", lineElements[6]);
     48                    currentWayPoint.setTime();
     49                    currentTrackSeg.add(currentWayPoint);
     50                }
     51            }
     52            currentTrack.trackSegs.add(currentTrackSeg);
     53            data.recalculateBounds();
     54        } catch (final IOException e) {
     55            // TODO tell user about the problem?
     56            e.printStackTrace();
     57        } finally {
     58            if (rd != null) {
     59                try {
     60                    rd.close();
     61                } catch (IOException e) {
     62                    // Don't care
     63                }
     64            }
     65        }
     66    }
     67
     68    private double parseCoord(String s) {
     69        try {
     70            return Double.parseDouble(s);
     71        } catch (NumberFormatException ex) {
     72            return Double.NaN;
     73        }
     74    }
     75
     76    private LatLon parseLatLon(String[] lineElements) {
     77        if (lineElements.length < 2) {
     78            return null;
     79        }
     80        return new LatLon(parseCoord(lineElements[0]), parseCoord(lineElements[1]));
     81    }
     82}