Ignore:
Timestamp:
2007-11-01T01:50:51+01:00 (16 years ago)
Author:
framm
Message:
  • patch for better GPX file support by Raphael Mack <ramack@…>
  • dropped support for CSV files
Location:
trunk/src/org/openstreetmap/josm/io
Files:
1 added
2 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/BoundingBoxDownloader.java

    r343 r444  
    66import java.io.IOException;
    77import java.io.InputStream;
    8 import java.util.Collection;
    9 import java.util.LinkedList;
    108
    119import org.openstreetmap.josm.Main;
     
    1412import org.openstreetmap.josm.data.osm.DataSet;
    1513import org.openstreetmap.josm.data.osm.DataSource;
    16 import org.openstreetmap.josm.gui.layer.RawGpsLayer.GpsPoint;
     14import org.openstreetmap.josm.data.gpx.GpxData;
    1715import org.xml.sax.SAXException;
    1816
     
    4139     *          ways.
    4240     */
    43     public Collection<Collection<GpsPoint>> parseRawGps() throws IOException, SAXException {
     41        public GpxData parseRawGps() throws IOException, SAXException {
    4442                Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server..."));
    4543        try {
    4644                String url = "trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page=";
    47                 Collection<Collection<GpsPoint>> data = new LinkedList<Collection<GpsPoint>>();
    48                 Collection<GpsPoint> list = new LinkedList<GpsPoint>();
    4945
    50                 for (int i = 0;;++i) {
     46                        boolean done = false;
     47                        GpxData result = null;
     48                        for (int i = 0;!done;++i) {
    5149                        Main.pleaseWaitDlg.currentAction.setText(tr("Downloading points {0} to {1}...", i * 5000, ((i + 1) * 5000)));
    5250                        InputStream in = getInputStream(url+i, Main.pleaseWaitDlg);
    5351                        if (in == null)
    5452                                break;
    55                         // Use only track points, since the server mix everything together
    56                         Collection<Collection<GpsPoint>> allWays = new RawGpsReader(in, null).trackData;
    57 
    58                         boolean foundSomething = false;
    59                         for (Collection<GpsPoint> t : allWays) {
    60                                 if (!t.isEmpty()) {
    61                                         foundSomething = true;
    62                                         list.addAll(t);
    63                                 }
     53                                GpxData currentGpx = new GpxReader(in, null).data;
     54                                if (result == null) {
     55                                        result = currentGpx;
     56                                } else if (currentGpx.hasTrackPoints()) {
     57                                        result.mergeFrom(currentGpx);
     58                                } else{
     59                                        done = true;
    6460                        }
    65                         if (!foundSomething)
    66                                 break;
    6761                        in.close();
    6862                        activeConnection = null;
    6963                }
    70                 if (!list.isEmpty())
    71                         data.add(list);
    72                 return data;
     64                        result.fromServer = true;
     65                        return result;
    7366        } catch (IllegalArgumentException e) {
    7467                // caused by HttpUrlConnection in case of illegal stuff in the response
  • trunk/src/org/openstreetmap/josm/io/GpxWriter.java

    r343 r444  
    33
    44import java.io.PrintWriter;
     5import java.io.OutputStream;
    56import java.util.Collection;
    6 import java.util.LinkedList;
     7import java.util.Map;
    78
    89import org.openstreetmap.josm.data.Bounds;
    9 import org.openstreetmap.josm.data.coor.LatLon;
    10 import org.openstreetmap.josm.data.osm.DataSet;
    11 import org.openstreetmap.josm.data.osm.Node;
    12 import org.openstreetmap.josm.data.osm.OsmPrimitive;
    13 import org.openstreetmap.josm.data.osm.Way;
    14 import org.openstreetmap.josm.gui.layer.RawGpsLayer.GpsPoint;
     10
     11import org.openstreetmap.josm.data.gpx.GpxData;
     12import org.openstreetmap.josm.data.gpx.GpxTrack;
     13import org.openstreetmap.josm.data.gpx.GpxRoute;
     14import org.openstreetmap.josm.data.gpx.GpxLink;
     15import org.openstreetmap.josm.data.gpx.WayPoint;
    1516
    1617/**
     
    2223 * or less than 2 &lt;trkpt&gt; are exported.
    2324 *
     25 * TODO: to export OSM data as gpx do a transformation into a GpxData instance first
     26 *
    2427 * @author imi
    2528 */
     
    3033        }
    3134
    32         /**
    33          * Export the dataset to gpx.  The ways are converted to trksegs, each in
    34          * a seperate trk.  Finally, all remaining nodes are added as wpt.
     35        public GpxWriter(OutputStream out) {
     36                super(new PrintWriter(out));
     37        }
     38
     39        public GpxWriter() {
     40                super(null);
     41                //sorry for this one here, this will be cleaned up once the new scheme works
     42        }
     43
     44        private GpxData data;
     45        private String indent = "";
     46
     47        private final static int WAY_POINT = 0;
     48        private final static int ROUTE_POINT = 1;
     49        private final static int TRACK_POINT = 2;
     50
     51        public void write(GpxData data) {
     52                this.data = data;
     53                out.println("<?xml version='1.0' encoding='UTF-8'?>");
     54                out.println("<gpx version=\"1.1\" creator=\"JOSM GPX export\" xmlns=\"http://www.topografix.com/GPX/1/1\">");
     55                indent = "  ";
     56                writeMetaData();
     57                writeWayPoints();
     58                writeRoutes();
     59                writeTracks();
     60                out.print("</gpx>");
     61                out.flush();
     62                out.close();
     63        }
     64
     65        private void writeAttr(Map<String, Object> attr) {
     66                boolean hasAuthor = false;
     67                for (Map.Entry<String, Object> ent : attr.entrySet()) {
     68                        String k = ent.getKey();
     69                        if (k.indexOf("author") == 0) {
     70                                hasAuthor = true;
     71                        } else if (k.equals("link")) {
     72                                for (GpxLink link : (Collection<GpxLink>) ent.getValue()) {
     73                                        gpxLink(link);
     74                                }
     75                        } else {
     76                                simpleTag(k, (String) ent.getValue());
     77                        }
     78                }
     79
     80                if (hasAuthor) {
     81                        open("author");
     82                        simpleTag("name", (String) attr.get("authorname"));
     83                        simpleTag("email", (String) attr.get("authoremail"));
     84                        gpxLink((GpxLink) attr.get("authorlink"));
     85                        closeln("author");
     86                }
     87
     88                // TODO: copyright
     89        }
     90
     91        private void writeMetaData() {
     92                openln("metadata");
     93                writeAttr(data.attr);
     94
     95                data.recalculateBounds();
     96                Bounds bounds = data.bounds;
     97                String b = "minlat=\"" + bounds.min.lat() + "\" minlon=\"" + bounds.min.lon() +
     98                        "\" maxlat=\"" + bounds.max.lat() + "\" maxlon=\"" + bounds.max.lon() + "\"" ;
     99                inline("bounds", b);
     100
     101                closeln("metadata");
     102        }
     103
     104        private void writeWayPoints() {
     105                for (WayPoint pnt : data.waypoints) {
     106                        wayPoint(pnt, WAY_POINT);
     107                }       
     108        }
     109       
     110        private void writeRoutes() {
     111                for (GpxRoute rte : data.routes) {
     112                        openln("rte");
     113                        writeAttr(rte.attr);
     114                        for (WayPoint pnt : rte.routePoints) {
     115                                wayPoint(pnt, ROUTE_POINT);
     116                        }
     117                        closeln("rte");
     118                }
     119        }
     120       
     121        private void writeTracks() {
     122                for (GpxTrack trk : data.tracks) {
     123                        open("trk");
     124                        writeAttr(trk.attr);
     125                        for (Collection<WayPoint> seg : trk.trackSegs) {
     126                                openln("trkseg");
     127                                for (WayPoint pnt : seg) {
     128                                        wayPoint(pnt, TRACK_POINT);
     129                                }
     130                                closeln("trkseg");
     131                        }
     132                        closeln("trk");
     133                }
     134        }
     135
     136        private void openln(String tag) {
     137                open(tag);
     138                out.print("\n");
     139        }
     140
     141        private void open(String tag) {
     142                out.print(indent + "<" + tag + ">");
     143                indent += "  ";
     144        }
     145
     146        private void openAtt(String tag, String attributes) {
     147                out.println(indent + "<" + tag + " " + attributes + ">");
     148                indent += "  ";
     149        }
     150       
     151        private void inline(String tag, String attributes) {
     152                out.println(indent + "<" + tag + " " + attributes + " />");
     153        }
     154
     155        private void close(String tag) {
     156                indent = indent.substring(2);
     157                out.print(indent + "</" + tag + ">");
     158        }
     159
     160        private void closeln(String tag) {
     161                close(tag);
     162                out.print("\n");
     163        }
     164
     165        /**       
     166         * if content not null, open tag, write encoded content, and close tag
     167         * else do nothing.
    35168         */
    36         public static final class All implements XmlWriter.OsmWriterInterface {
    37                 private final DataSet data;
    38                 private final String name;
    39                 private final String desc;
    40                 private final String author;
    41                 private final String email;
    42                 private final String copyright;
    43                 private final String year;
    44                 private final String keywords;
    45                 private boolean metadataClosed = false;
    46 
    47                 public All(DataSet data, String name, String desc, String author, String email, String copyright, String year, String keywords) {
    48                         this.data = data;
    49                         this.name = name;
    50                         this.desc = desc;
    51                         this.author = author;
    52                         this.email = email;
    53                         this.copyright = copyright;
    54                         this.year = year;
    55                         this.keywords = keywords;
    56                 }
    57 
    58                 public void header(PrintWriter out) {
    59                         out.println("<gpx version='1.1' creator='JOSM' xmlns='http://www.topografix.com/GPX/1/1'>");
    60                         out.println("  <metadata>");
    61                         if (!name.equals(""))
    62                                 out.println("    <name>"+XmlWriter.encode(name)+"</name>");
    63                         if (!desc.equals(""))
    64                                 out.println("    <desc>"+XmlWriter.encode(desc)+"</desc>");
    65                         if (!author.equals("")) {
    66                                 out.println("    <author>");
    67                                 out.println("      <name>"+XmlWriter.encode(author)+"</name>");
    68                                 if (!email.equals(""))
    69                                         out.println("      <email>"+XmlWriter.encode(email)+"</email>");
    70                                 out.println("    </author>");
    71                                 if (!copyright.equals("")) {
    72                                         out.println("    <copyright author='"+XmlWriter.encode(author)+"'>");
    73                                         if (!year.equals(""))
    74                                                 out.println("      <year>"+XmlWriter.encode(year)+"</year>");
    75                                         out.println("      <license>"+XmlWriter.encode(copyright)+"</license>");
    76                                         out.println("    </copyright>");
    77                                 }
    78                         }
    79                         if (!keywords.equals("")) {
    80                                 out.println("    <keywords>"+XmlWriter.encode(keywords)+"</keywords>");
    81                         }
    82                         // don't finish here, to give output functions the chance to add <bounds>
    83                 }
    84 
    85                 public void write(PrintWriter out) {
    86                         Collection<OsmPrimitive> all = data.allNonDeletedPrimitives();
    87                         if (all.isEmpty())
    88                                 return;
    89                         GpxWriter writer = new GpxWriter(out);
    90                         // calculate bounds
    91                         Bounds b = new Bounds(new LatLon(Double.MAX_VALUE, Double.MAX_VALUE), new LatLon(-Double.MAX_VALUE, -Double.MAX_VALUE));
    92                         for (Node n : data.nodes)
    93                                 if (!n.deleted)
    94                                         b.extend(n.coor);
    95                         out.println("    <bounds minlat='"+b.min.lat()+"' minlon='"+b.min.lon()+"' maxlat='"+b.max.lat()+"' maxlon='"+b.max.lon()+"' />");
    96                         out.println("  </metadata>");
    97                         metadataClosed = true;
    98 
    99                         // add ways
    100                         for (Way w : data.ways) {
    101                                 if (w.deleted)
    102                                         continue;
    103                                 out.println("  <trk>");
    104                                                 out.println("    <trkseg>");
    105                                 for (Node n : w.nodes) {
    106                                         writer.outputNode(n, false);
    107                                         all.remove(n);
    108                         }
    109                                         out.println("    </trkseg>");
    110                                 out.println("  </trk>");
    111                                 all.remove(w);
    112                         }
    113 
    114                         // finally add the remaining nodes
    115                         for (OsmPrimitive osm : all)
    116                                 if (osm instanceof Node)
    117                                         writer.outputNode((Node)osm, true);
    118                 }
    119 
    120                 public void footer(PrintWriter out) {
    121                         if (!metadataClosed)
    122                                 out.println("  </metadata>");
    123                         out.println("</gpx>");
    124                 }
    125         }
    126 
    127 
    128         /**
    129          * Export the collection structure to gpx. The gpx will consists of only one
    130          * trk with as many trkseg as there are collections in the outer collection.
     169        private void simpleTag(String tag, String content) {
     170                if (content != null && content.length() > 0) {
     171                        open(tag);
     172                        out.print(encode(content));
     173                        out.println("</" + tag + ">");
     174                        indent = indent.substring(2);
     175                }
     176        }
     177
     178        /**       
     179         * output link
    131180         */
    132         public static final class Trk implements XmlWriter.OsmWriterInterface {
    133                 private final Collection<Collection<GpsPoint>> data;
    134                 public Trk(Collection<Collection<GpsPoint>> data) {
    135                         this.data = data;
    136                 }
    137 
    138                 public void header(PrintWriter out) {
    139                         out.println("<gpx version='1.1' creator='JOSM' xmlns='http://www.topografix.com/GPX/1/1'>");
    140                 }
    141 
    142                 public void write(PrintWriter out) {
    143                         if (data.size() == 0)
    144                                 return;
    145                         // calculate bounds
    146                         Bounds b = new Bounds(new LatLon(Double.MAX_VALUE, Double.MAX_VALUE), new LatLon(Double.MIN_VALUE, Double.MIN_VALUE));
    147                         for (Collection<GpsPoint> c : data)
    148                                 for (GpsPoint p : c)
    149                                         b.extend(p.latlon);
    150                         out.println("  <metadata>");
    151                         out.println("    <bounds minlat='"+b.min.lat()+"' minlon='"+b.min.lon()+"' maxlat='"+b.max.lat()+"' maxlon='"+b.max.lon()+"' />");
    152                         out.println("  </metadata>");
    153 
    154                         out.println("  <trk>");
    155                         for (Collection<GpsPoint> c : data) {
    156                                 out.println("    <trkseg>");
    157                                 LatLon last = null;
    158                                 for (GpsPoint p : c) {
    159                                         // skip double entries
    160                                         if (p.latlon.equals(last))
    161                                                 continue;
    162                                         last =  p.latlon;
    163                                         LatLon ll = p.latlon;
    164                                         out.print("      <trkpt lat='"+ll.lat()+"' lon='"+ll.lon()+"'");
    165                                         if (p.time != null && p.time.length()!=0) {
    166                                                 out.println(">");
    167                                                 out.println("        <time>"+p.time+"</time>");
    168                                                 out.println("      </trkpt>");
    169                                         } else
    170                                                 out.println(" />");
    171                                 }
    172                                 out.println("    </trkseg>");
    173                         }
    174                         out.println("  </trk>");
    175                 }
    176 
    177                 public void footer(PrintWriter out) {
    178                         out.println("</gpx>");
    179         }
    180         }
    181 
    182         private void outputNode(Node n, boolean wpt) {
    183                 out.print((wpt?"  <wpt":"      <trkpt")+" lat='"+n.coor.lat()+"' lon='"+n.coor.lon()+"'");
    184                 if (n.keys == null) {
    185                         out.println(" />");
    186                         return;
    187                 }
    188                 boolean found = false;
    189                 String[] possibleKeys = {"ele", "time", "magvar", "geoidheight", "name",
    190                                 "cmt", "desc", "src", "link", "sym", "type", "fix", "sat",
    191                                 "hdop", "vdop", "pdop", "ageofgpsdata", "dgpsid"};
    192                 Collection<String> keys = n.keySet();
    193                 for (String k : possibleKeys) {
    194                         if (keys.contains(k)) {
    195                                 if (!found) {
    196                                         found = true;
    197                                         out.println(">");
    198                                 }
    199                                 if (k.equals("link")) {
    200                                         out.println("        <link>");
    201                                         out.println("          <text>"+XmlWriter.encode(n.get(k))+"</text>");
    202                                         out.println("        </link>");
    203                                 } else
    204                                         out.println("        <"+k+">"+XmlWriter.encode(n.get(k))+"</"+k+">");
    205                         }
    206                 }
    207                 if (found)
    208                         out.println(wpt?"  </wpt>":"      </trkpt>");
    209                 else
    210                         out.println(" />");
     181        private void gpxLink(GpxLink link) {
     182                if (link != null) {
     183                        openAtt("link", "href=\"" + link.uri + "\"");
     184                        simpleTag("text", link.text);
     185                        simpleTag("type", link.type);
     186                        closeln("link");
     187                }
     188        }
     189
     190        /**       
     191         * output a point
     192         */
     193        private void wayPoint(WayPoint pnt, int mode) {
     194                String type;
     195                switch(mode) {
     196                case WAY_POINT:
     197                        type = "wpt";
     198                        break;
     199                case ROUTE_POINT:
     200                        type = "rtept";
     201                        break;
     202                case TRACK_POINT:
     203                        type = "trkpt";
     204                        break;
     205                default:
     206                        throw new RuntimeException("Bug detected. Please report this!");
     207                }
     208                if (pnt != null) {
     209                        openAtt(type, "lat=\"" + pnt.latlon.lat() + "\" lon=\"" + pnt.latlon.lon() + "\"");
     210                        writeAttr(pnt.attr);
     211                        closeln(type);
     212                }
    211213        }
    212214}
Note: See TracChangeset for help on using the changeset viewer.