Ignore:
Timestamp:
2006-04-01T13:01:41+02:00 (18 years ago)
Author:
imi
Message:
  • added more context menu items to layer list
  • added GPX export (raw gps and osm)
File:
1 edited

Legend:

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

    r71 r78  
    22
    33import java.io.IOException;
     4import java.io.PrintWriter;
    45import java.io.Writer;
    56import java.util.Collection;
     
    1516import org.jdom.output.Format;
    1617import org.jdom.output.XMLOutputter;
     18import org.openstreetmap.josm.data.Bounds;
     19import org.openstreetmap.josm.data.coor.LatLon;
    1720import org.openstreetmap.josm.data.osm.DataSet;
    1821import org.openstreetmap.josm.data.osm.LineSegment;
     
    2023import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2124import org.openstreetmap.josm.data.osm.Way;
     25import org.openstreetmap.josm.gui.layer.RawGpsDataLayer.GpsPoint;
     26import org.openstreetmap.josm.tools.XmlWriter;
    2227
    2328/**
     
    4954         * This is the output writer to store the resulting data in.
    5055         */
    51         private Writer out;
     56        private PrintWriter out;
    5257        /**
    5358         * The dataset beeing processed.
     
    7378         */
    7479        public GpxWriter(Writer out, DataSet ds) {
    75                 this.out = out;
     80                this.out = new PrintWriter(out);
    7681                this.ds = ds;
    7782        }
     
    318323                ext.getChildren().addAll(extensions);
    319324        }
     325
     326
     327        public GpxWriter(Writer writer, String name, String desc, String author, String email, String copyright, String year, String keywords) {
     328                out = writer instanceof PrintWriter ? (PrintWriter)writer : new PrintWriter(writer);
     329                out.println(XmlWriter.header());
     330                out.println("<gpx version='1.1' creator='JOSM' xmlns='http://www.topografix.com/GPX/1/1'>");
     331                out.println("  <metadata>");
     332                if (!name.equals(""))
     333                        out.println("    <name>"+XmlWriter.encode(name)+"</name>");
     334                if (!desc.equals(""))
     335                        out.println("    <desc>"+XmlWriter.encode(desc)+"</desc>");
     336                if (!author.equals("")) {
     337                        out.println("    <author>");
     338                        out.println("      <name>"+XmlWriter.encode(author)+"</name>");
     339                        if (!email.equals(""))
     340                                out.println("      <email>"+XmlWriter.encode(email)+"</email>");
     341                        out.println("    </author>");
     342                        if (!copyright.equals("")) {
     343                                out.println("    <copyright author='"+XmlWriter.encode(author)+"'>");
     344                                if (!year.equals(""))
     345                                        out.println("      <year>"+XmlWriter.encode(year)+"</year>");
     346                                out.println("      <license>"+XmlWriter.encode(copyright)+"</license>");
     347                                out.println("    </copyright>");
     348                        }
     349                }
     350                if (!keywords.equals("")) {
     351                        out.println("    <keywords>"+XmlWriter.encode(keywords)+"</keywords>");
     352                }
     353                // don't finish here, to give output functions the chance to add <bounds>
     354        }
     355
     356        /**
     357         * Export the dataset to gpx. Only the physical line segment structure is
     358         * exported. To do this, the list of ways is processed. If a way span a
     359         * sequence of line segments, this is added as one trkseg.
     360         * Then, all remaining line segments are added in one extra trk. Finally,
     361         * all remaining nodes are added as wpt.
     362         */
     363        public void output(DataSet data) {
     364                Collection<OsmPrimitive> all = data.allNonDeletedPrimitives();
     365                if (all.isEmpty()) {
     366                        out.println("  </metadata>");
     367                        out.println("</gpx>");
     368                        return;
     369                }
     370                // calculate bounds
     371                Bounds b = new Bounds(new LatLon(Double.MAX_VALUE, Double.MAX_VALUE), new LatLon(Double.MIN_VALUE, Double.MIN_VALUE));
     372                for (Node n : data.nodes)
     373                        if (!n.isDeleted())
     374                                b.extend(n.coor);
     375                out.println("    <bounds minlat='"+b.min.lat()+"' minlon='"+b.min.lon()+"' maxlat='"+b.max.lat()+"' maxlon='"+b.max.lon()+"' />");
     376                out.println("  </metadata>");
     377               
     378                // add ways
     379                for (Way w : data.ways) {
     380                        if (w.isDeleted())
     381                                continue;
     382                        out.println("  <trk>");
     383                        LineSegment oldLs = null;
     384                        for (LineSegment ls : w.segments) {
     385                                // end old segemnt, if no longer match a chain
     386                                if (oldLs != null && !oldLs.to.coor.equals(ls.from.coor)) {
     387                                        out.println("    </trkseg>");
     388                                        outputNode(oldLs.to, false);
     389                                        all.remove(oldLs.to);
     390                                        oldLs = null;
     391                                }
     392                                // start new segment if necessary
     393                                if (oldLs == null)
     394                                        out.println("    <trkseg>");
     395                                outputNode(ls.from, false);
     396                                all.remove(ls.from);
     397                                oldLs = ls;
     398                                all.remove(ls);
     399                        }
     400                        // write last node if there
     401                        if (oldLs != null) {
     402                                outputNode(oldLs.to, false);
     403                                all.remove(oldLs.to);
     404                                out.println("    </trkseg>");
     405                        }
     406                        out.println("  </trk>");
     407                        all.remove(w);
     408                }
     409               
     410                // add remaining line segments
     411                Collection<LineSegment> lineSegments = new LinkedList<LineSegment>();
     412                for (OsmPrimitive osm : all)
     413                        if (osm instanceof LineSegment)
     414                                lineSegments.add((LineSegment)osm);
     415                if (!lineSegments.isEmpty()) {
     416                        out.println("  <trk>");
     417                        for (LineSegment ls : lineSegments) {
     418                                out.println("    <trkseg>");
     419                                outputNode(ls.from, false);
     420                                all.remove(ls.from);
     421                                outputNode(ls.to, false);
     422                                all.remove(ls.to);
     423                                out.println("    </trkseg>");
     424                                all.remove(ls);
     425                        }
     426                        out.println("  </trk>");
     427                }
     428               
     429                // finally add the remaining nodes
     430                for (OsmPrimitive osm : all) {
     431                        outputNode((Node)osm, true);
     432                }
     433               
     434                out.println("</gpx>");
     435        }
     436
     437
     438        /**
     439         * Export the collection structure to gpx. The gpx will consists of only one
     440         * trk with as many trkseg as there are collections in the outer collection.
     441         */
     442        public void output(Collection<Collection<GpsPoint>> data) {
     443                if (data.size() == 0) {
     444                        out.println("  </metadata>");
     445                        out.println("</gpx>");
     446                        return;
     447                }
     448                // calculate bounds
     449                Bounds b = new Bounds(new LatLon(Double.MAX_VALUE, Double.MAX_VALUE), new LatLon(Double.MIN_VALUE, Double.MIN_VALUE));
     450                for (Collection<GpsPoint> c : data)
     451                        for (GpsPoint p : c)
     452                                b.extend(p.latlon);
     453                out.println("    <bounds minlat='"+b.min.lat()+"' minlon='"+b.min.lon()+"' maxlat='"+b.max.lat()+"' maxlon='"+b.max.lon()+"' />");
     454                out.println("  </metadata>");
     455               
     456                out.println("  <trk>");
     457                for (Collection<GpsPoint> c : data) {
     458                        out.println("    <trkseg>");
     459                        LatLon last = null;
     460                        for (GpsPoint p : c) {
     461                                // skip double entries
     462                                if (p.latlon.equals(last))
     463                                        continue;
     464                                last =  p.latlon;
     465                                LatLon ll = p.latlon;
     466                                out.print("      <trkpt lat='"+ll.lat()+"' lon='"+ll.lon()+"'");
     467                                if (p.time != null && !p.time.isEmpty()) {
     468                                        out.println(">");
     469                                        out.println("        <time>"+p.time+"</time>");
     470                                        out.println("      </trkpt>");
     471                                } else
     472                                        out.println(" />");
     473                        }
     474                        out.println("    </trkseg>");
     475                }
     476                out.println("  </trk>");
     477                out.println("</gpx>");
     478        }
     479
     480        private void outputNode(Node n, boolean wpt) {
     481                out.print((wpt?"  <wpt":"      <trkpt")+" lat='"+n.coor.lat()+"' lon='"+n.coor.lon()+"'");
     482                if (n.keys == null) {
     483                        out.println(" />");
     484                        return;
     485                }
     486                boolean found = false;
     487                String[] possibleKeys = {"ele", "time", "magvar", "geoidheight", "name",
     488                                "cmt", "desc", "src", "link", "sym", "type", "fix", "sat",
     489                                "hdop", "vdop", "pdop", "ageofgpsdata", "dgpsid"};
     490                Collection<String> keys = n.keySet();
     491                for (String k : possibleKeys) {
     492                        if (keys.contains(k)) {
     493                                if (!found) {
     494                                        found = true;
     495                                        out.println(">");
     496                                }
     497                                if (k.equals("link")) {
     498                                        out.println("        <link>");
     499                                        out.println("          <text>"+XmlWriter.encode(n.get(k))+"</text>");
     500                                        out.println("        </link>");
     501                                } else
     502                                        out.println("        <"+k+">"+XmlWriter.encode(n.get(k))+"</"+k+">");
     503                        }
     504                }
     505                if (found)
     506                        out.println(wpt?"  </wpt>":"      </trkpt>");
     507                else
     508                        out.println(" />");
     509        }
    320510}
Note: See TracChangeset for help on using the changeset viewer.