Changeset 78 in josm for src/org


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)
Location:
src/org/openstreetmap/josm
Files:
3 added
20 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/Main.java

    r77 r78  
    1515import java.util.StringTokenizer;
    1616
     17import javax.swing.Action;
    1718import javax.swing.JFrame;
    1819import javax.swing.JMenu;
     
    2728import org.openstreetmap.josm.actions.DownloadAction;
    2829import org.openstreetmap.josm.actions.ExitAction;
     30import org.openstreetmap.josm.actions.GpxExportAction;
    2931import org.openstreetmap.josm.actions.OpenAction;
    3032import org.openstreetmap.josm.actions.PreferencesAction;
     
    8082        public final RedoAction redoAction;
    8183
    82         /**
    83          * This directory is used for all disc IO access as starting directory. Should
    84          * be set accordingly after the IO action.
    85          */
    86         public File currentDirectory = new File(".");
    87 
    8884        private OpenAction openAction;
    8985
     
    104100               
    105101                downloadAction = new DownloadAction();
    106                 UploadAction uploadAction = new UploadAction();
     102                Action uploadAction = new UploadAction();
    107103                openAction = new OpenAction();
    108                 SaveAction saveAction = new SaveAction();
    109                 ExitAction exitAction = new ExitAction();
     104                Action saveAction = new SaveAction();
     105                Action gpxExportAction = new GpxExportAction(null);
     106                Action exitAction = new ExitAction();
    110107                undoAction = new UndoAction();
    111108                redoAction = new RedoAction();
    112                 PreferencesAction preferencesAction = new PreferencesAction();
    113                 AboutAction aboutAction = new AboutAction();
     109                Action preferencesAction = new PreferencesAction();
     110                Action aboutAction = new AboutAction();
    114111
    115112                // creating menu
     
    121118                fileMenu.add(openAction);
    122119                fileMenu.add(saveAction);
     120                fileMenu.add(gpxExportAction);
    123121                fileMenu.addSeparator();
    124122                fileMenu.add(exitAction);
     
    151149                toolBar.add(downloadAction);
    152150                toolBar.add(uploadAction);
     151                toolBar.addSeparator();
    153152                toolBar.add(openAction);
    154153                toolBar.add(saveAction);
     154                toolBar.add(gpxExportAction);
    155155                toolBar.addSeparator();
    156156                toolBar.add(undoAction);
  • src/org/openstreetmap/josm/actions/OpenAction.java

    r71 r78  
    1616import org.jdom.JDOMException;
    1717import org.openstreetmap.josm.Main;
    18 import org.openstreetmap.josm.data.coor.LatLon;
    1918import org.openstreetmap.josm.data.osm.DataSet;
    2019import org.openstreetmap.josm.gui.MapFrame;
     
    2221import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    2322import org.openstreetmap.josm.gui.layer.RawGpsDataLayer;
     23import org.openstreetmap.josm.gui.layer.RawGpsDataLayer.GpsPoint;
    2424import org.openstreetmap.josm.io.GpxReader;
    2525import org.openstreetmap.josm.io.OsmReader;
     
    3535 * @author imi
    3636 */
    37 public class OpenAction extends JosmAction {
     37public class OpenAction extends DiskAccessAction {
    3838
    3939        /**
     
    4545
    4646        public void actionPerformed(ActionEvent e) {
    47                 JFileChooser fc = new JFileChooser(Main.main.currentDirectory);
    48                 for (int i = 0; i < ExtensionFileFilter.filters.length; ++i)
    49                         fc.addChoosableFileFilter(ExtensionFileFilter.filters[i]);
    50                 fc.setAcceptAllFileFilterUsed(true);
    51 
    52                 if (fc.showOpenDialog(Main.main) != JFileChooser.APPROVE_OPTION)
     47                JFileChooser fc = createAndOpenFileChooser(true, true);
     48                if (fc == null)
    5349                        return;
    54                
    55                 Main.main.currentDirectory = fc.getCurrentDirectory();
    56 
    57                 File filename = fc.getSelectedFile();
    58                 if (filename == null)
    59                         return;
    60 
    61                 openFile(filename);
     50                File[] files = fc.getSelectedFiles();
     51                for (int i = files.length; i > 0; --i)
     52                        openFile(files[i-1]);
    6253        }
    6354
     
    7162
    7263                        if (asRawData(fn)) {
    73                                 Collection<Collection<LatLon>> data;
     64                                Collection<Collection<GpsPoint>> data;
    7465                                if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn)) {
    7566                                        data = new RawGpsReader(new FileReader(filename)).parse();
    7667                                } else if (ExtensionFileFilter.filters[ExtensionFileFilter.CSV].acceptName(fn)) {
    77                                         data = new LinkedList<Collection<LatLon>>();
     68                                        data = new LinkedList<Collection<GpsPoint>>();
    7869                                        data.add(new RawCsvReader(new FileReader(filename)).parse());
    7970                                } else
     
    8273                        } else {
    8374                                DataSet dataSet;
    84                                 if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn))
     75                                if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn)) {
     76                                        JOptionPane.showMessageDialog(Main.main, "Warning: Soon, it will be no longer possible to open GPX files as osm data. Please convert your files to .osm format.");
    8577                                        dataSet = new GpxReader(new FileReader(filename)).parse();
    86                                 else if (ExtensionFileFilter.filters[ExtensionFileFilter.OSM].acceptName(fn)) {
     78                                } else if (ExtensionFileFilter.filters[ExtensionFileFilter.OSM].acceptName(fn)) {
    8779                                        try {
    8880                                                // temporary allow loading of old xml format.
     
    9183                                                if (x.getMessage().equals("Unknown version: null")) {
    9284                                                        int answer = JOptionPane.showConfirmDialog(Main.main,
    93                                                                         "This seems to be an old 0.2 API XML file.\n" +
     85                                                                        fn+" seems to be an old 0.2 API XML file.\n" +
    9486                                                                        "JOSM can try to open it with the old parser. This option\n" +
    9587                                                                        "will not be available in future JOSM version. You should\n" +
     
    10496                                        }                                       
    10597                                } else if (ExtensionFileFilter.filters[ExtensionFileFilter.CSV].acceptName(fn)) {
    106                                         JOptionPane.showMessageDialog(Main.main, "CSV Data import for non-GPS data is not implemented yet.");
     98                                        JOptionPane.showMessageDialog(Main.main, fn+": CSV Data import for non-GPS data is not implemented yet.");
    10799                                        return;
    108100                                } else {
    109                                         JOptionPane.showMessageDialog(Main.main, "Unknown file extension: "+fn.substring(filename.getName().lastIndexOf('.')+1));
     101                                        JOptionPane.showMessageDialog(Main.main, fn+": Unknown file extension: "+fn.substring(filename.getName().lastIndexOf('.')+1));
    110102                                        return;
    111103                                }
     
    140132                        return false;
    141133                return JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(
    142                                 Main.main, "Do you want to open the file as raw gps data?",
     134                                Main.main, "Do you want to open "+fn+" as raw gps data?",
    143135                                "Open as raw data?", JOptionPane.YES_NO_OPTION);
    144136        }
  • src/org/openstreetmap/josm/actions/SaveAction.java

    r71 r78  
    1515import org.openstreetmap.josm.Main;
    1616import org.openstreetmap.josm.data.osm.LineSegment;
    17 import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1817import org.openstreetmap.josm.io.GpxWriter;
    1918import org.openstreetmap.josm.io.OsmWriter;
     
    2423 * @author imi
    2524 */
    26 public class SaveAction extends JosmAction {
     25public class SaveAction extends DiskAccessAction {
    2726
    2827        /**
    2928         * Construct the action with "Save" as label.
     29         * @param layer Save only this layer. If <code>null</code>, save the whole Main
     30         *              data set.
    3031         */
    3132        public SaveAction() {
     
    4142                        return;
    4243
    43                 JFileChooser fc = new JFileChooser(Main.main.currentDirectory);
    44                 for (int i = 0; i < ExtensionFileFilter.filters.length; ++i)
    45                         fc.addChoosableFileFilter(ExtensionFileFilter.filters[i]);
    46                 fc.setAcceptAllFileFilterUsed(true);
    47                 fc.showSaveDialog(Main.main);
     44                JFileChooser fc = createAndOpenFileChooser(false, false);
     45                if (fc == null)
     46                        return;
     47
    4848                File file = fc.getSelectedFile();
    49                 if (file == null)
    50                         return;
    51                 Main.main.currentDirectory = fc.getCurrentDirectory();
    52                 if (file.exists() && JOptionPane.YES_OPTION !=
    53                                 JOptionPane.showConfirmDialog(Main.main, "File exists. Overwrite?", "Overwrite", JOptionPane.YES_NO_OPTION))
    54                         return;
    5549
    5650                try {
     
    5852                        if (fn.indexOf('.') == -1) {
    5953                                FileFilter ff = fc.getFileFilter();
    60                                 if (ff instanceof ExtensionFileFilter) {
    61                                         fn = fn + "." + ((ExtensionFileFilter)ff).defaultExtension;
    62                                         file = new File(fn);
    63                                 }
     54                                if (ff instanceof ExtensionFileFilter)
     55                                        fn = "." + ((ExtensionFileFilter)ff).defaultExtension;
     56                                else
     57                                        fn += ".osm";
     58                                file = new File(fn);
    6459                        }
    6560                        FileWriter fileWriter;
     
    8883                }
    8984        }
    90 
    91         /**
    92          * Check the data set if it would be empty on save. It is empty, if it contains
    93          * no objects (after all objects that are created and deleted without beeing
    94          * transfered to the server have been removed).
    95          * 
    96          * @return <code>true</code>, if a save result in an empty data set.
    97          */
    98         private boolean isDataSetEmpty() {
    99                 for (OsmPrimitive osm : Main.main.ds.allPrimitives())
    100                         if (!osm.isDeleted() || osm.id > 0)
    101                                 return false;
    102                 return true;
    103         }
    104 
    10585}
  • src/org/openstreetmap/josm/data/Bounds.java

    r73 r78  
    3939                return "Bounds["+min.lat()+","+min.lon()+","+max.lat()+","+max.lon()+"]";
    4040        }
     41
     42        /**
     43         * Extend the bounds if necessary to include the given point.
     44         */
     45        public void extend(LatLon ll) {
     46                if (ll.lat() < min.lat() || ll.lon() < min.lon())
     47                        min = new LatLon(Math.min(ll.lat(), min.lat()), Math.min(ll.lon(), min.lon()));
     48                if (ll.lat() > max.lat() || ll.lon() > max.lon())
     49                        max = new LatLon(Math.max(ll.lat(), max.lat()), Math.max(ll.lon(), max.lon()));
     50        }
    4151}
  • src/org/openstreetmap/josm/data/Preferences.java

    r77 r78  
    7373        }
    7474        synchronized public boolean getBoolean(String key) {
    75                 return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : false;
     75                return getBoolean(key, false);
     76        }
     77        synchronized public boolean getBoolean(String key, boolean def) {
     78                return properties.containsKey(key) ? Boolean.parseBoolean(properties.get(key)) : def;
    7679        }
    7780
  • src/org/openstreetmap/josm/data/coor/Coordinate.java

    r71 r78  
    5252        @Override
    5353        public boolean equals(Object obj) {
    54                 return obj instanceof EastNorth ? x == ((EastNorth)obj).x && ((EastNorth)obj).y == y : false;
     54                return obj instanceof Coordinate ? x == ((Coordinate)obj).x && ((Coordinate)obj).y == y : false;
    5555        }
    5656
  • src/org/openstreetmap/josm/data/osm/DataSet.java

    r71 r78  
    88
    99/**
    10  * DataSet is the data behind the application. It can consist of only a few
     10 * DataSet is the data behind the application. It can consists of only a few
    1111 * points up to the whole osm database. DataSet's can be merged together,
    1212 * saved, (up/down/disk)loaded etc.
  • src/org/openstreetmap/josm/data/osm/visitor/SelectionComponentVisitor.java

    r71 r78  
    4242                                name = ls.id+" ("+ls.from.coor.lat()+","+ls.from.coor.lon()+") -> ("+ls.to.coor.lat()+","+ls.to.coor.lon()+")";
    4343                }
    44                 icon = ImageProvider.get("data", "linesegment");
     44                icon = ImageProvider.get("data", "segment");
    4545        }
    4646
  • src/org/openstreetmap/josm/data/projection/Projection.java

    r74 r78  
    1212public interface Projection {
    1313
    14         public static double MAX_LAT = 85;
     14        public static double MAX_LAT = 85.05112877980659; // Mercator squares the world
    1515        public static double MAX_LON = 180;
    1616        public static final double MAX_SERVER_PRECISION = 1e12;
  • src/org/openstreetmap/josm/gui/MapView.java

    r77 r78  
    1717import org.openstreetmap.josm.data.osm.DataSet;
    1818import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
     19import org.openstreetmap.josm.data.projection.Projection;
    1920import org.openstreetmap.josm.gui.layer.Layer;
    2021import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     
    214215                       
    215216                        if (v.min == null || v.max == null) {
    216                                 // no bounds means standard scale and center
    217                                 center = Main.proj.latlon2eastNorth(new LatLon(51.526447, -0.14746371));
    218                                 scale = 10;
     217                                // no bounds means whole world
     218                                center = getProjection().latlon2eastNorth(new LatLon(0,0));
     219                                EastNorth world = getProjection().latlon2eastNorth(new LatLon(Projection.MAX_LAT,Projection.MAX_LON));
     220                                double scaleX = world.east()*2/w;
     221                                double scaleY = world.north()*2/h;
     222                                scale = Math.max(scaleX, scaleY); // minimum scale to see all of the screen
     223                               
    219224                        } else {
    220225                                center = new EastNorth(v.min.east()/2+v.max.east()/2, v.min.north()/2+v.max.north()/2);
  • src/org/openstreetmap/josm/gui/PreferenceDialog.java

    r77 r78  
    176176                // projection combo box
    177177                for (int i = 0; i < projectionCombo.getItemCount(); ++i) {
    178                         if (projectionCombo.getItemAt(i).toString().equals(Main.pref.get("projection"))) {
     178                        if (projectionCombo.getItemAt(i).getClass().getName().equals(Main.pref.get("projection"))) {
    179179                                projectionCombo.setSelectedIndex(i);
    180180                                break;
     
    258258                osmDataUsername.setToolTipText("Login name (email) to the OSM account.");
    259259                osmDataPassword.setToolTipText("Login password to the OSM account. Leave blank to not store any password.");
    260                 csvImportString.setToolTipText("<html>Import string specification. Currently, only lat/lon pairs are imported.<br>" +
     260                csvImportString.setToolTipText("<html>Import string specification. lat/lon and time are imported.<br>" +
    261261                                "<b>lat</b>: The latitude coordinate<br>" +
    262262                                "<b>lon</b>: The longitude coordinate<br>" +
     263                                "<b>time</b>: The measured time as string<br>" +
    263264                                "<b>ignore</b>: Skip this field<br>" +
    264265                                "An example: \"ignore ignore lat lon\" will use ' ' as delimiter, skip the first two values and read then lat/lon.<br>" +
  • src/org/openstreetmap/josm/gui/dialogs/LayerList.java

    r77 r78  
    22
    33import java.awt.BorderLayout;
    4 import java.awt.Color;
    54import java.awt.Component;
    65import java.awt.Dimension;
     
    1716import javax.swing.Icon;
    1817import javax.swing.JButton;
    19 import javax.swing.JColorChooser;
    2018import javax.swing.JLabel;
    2119import javax.swing.JList;
    22 import javax.swing.JMenuItem;
    23 import javax.swing.JOptionPane;
    2420import javax.swing.JPanel;
    25 import javax.swing.JPopupMenu;
    2621import javax.swing.JScrollPane;
    2722import javax.swing.ListSelectionModel;
     
    3631import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
    3732import org.openstreetmap.josm.gui.layer.Layer;
    38 import org.openstreetmap.josm.gui.layer.RawGpsDataLayer;
    39 import org.openstreetmap.josm.tools.ColorHelper;
    4033import org.openstreetmap.josm.tools.ImageProvider;
    4134import org.openstreetmap.josm.tools.ImageProvider.OverlayPosition;
     
    122115                layers.addMouseListener(new MouseAdapter(){
    123116                        private void openPopup(MouseEvent e) {
    124                                 final int index = layers.locationToIndex(e.getPoint());
    125                                 final Layer layer = (Layer)layers.getModel().getElementAt(index);
    126                                 if (!(layer instanceof RawGpsDataLayer))
    127                                         return; // currently only options for raw layers.
    128                                 JPopupMenu menu = new JPopupMenu();
    129                                 JMenuItem color = new JMenuItem("Customize Color");
    130                                 color.addActionListener(new ActionListener(){
    131                                         public void actionPerformed(ActionEvent e) {
    132                                                 String col = Main.pref.get("color.layer "+layer.name, Main.pref.get("color.gps point", ColorHelper.color2html(Color.gray)));
    133                                                 JColorChooser c = new JColorChooser(ColorHelper.html2color(col));
    134                                                 Object[] options = new Object[]{"OK", "Cancel", "Default"};
    135                                                 int answer = JOptionPane.showOptionDialog(Main.main, c, "Choose a color", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
    136                                                 switch (answer) {
    137                                                 case 0:
    138                                                         Main.pref.put("color.layer "+layer.name, ColorHelper.color2html(c.getColor()));
    139                                                         break;
    140                                                 case 1:
    141                                                         return;
    142                                                 case 2:
    143                                                         Main.pref.put("color.layer "+layer.name, null);
    144                                                         break;
    145                                                 }
    146                                                 Main.main.repaint();
    147                                         }
    148                                 });
    149                                 menu.add(color);
     117                                int index = layers.locationToIndex(e.getPoint());
     118                                Layer layer = (Layer)layers.getModel().getElementAt(index);
     119                                LayerListPopup menu = new LayerListPopup(layer);
    150120                                menu.show(LayerList.this, e.getX(), e.getY());
    151121                        }
  • src/org/openstreetmap/josm/gui/layer/Layer.java

    r77 r78  
    7878         */
    7979        abstract public void visitBoundingBox(BoundingXYVisitor v);
     80
     81        abstract public Object getInfoComponent();
    8082}
  • src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r77 r78  
    22
    33import java.awt.Graphics;
     4import java.awt.GridBagLayout;
    45import java.util.Collection;
    56import java.util.HashSet;
     
    1011
    1112import javax.swing.Icon;
     13import javax.swing.JLabel;
     14import javax.swing.JPanel;
    1215
    1316import org.openstreetmap.josm.Main;
     
    2124import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
    2225import org.openstreetmap.josm.data.osm.visitor.SimplePaintVisitor;
     26import org.openstreetmap.josm.data.osm.visitor.Visitor;
    2327import org.openstreetmap.josm.gui.MapView;
     28import org.openstreetmap.josm.tools.GBC;
    2429import org.openstreetmap.josm.tools.ImageProvider;
    2530
     
    279284                return size;
    280285        }
     286
     287        @Override
     288        public Object getInfoComponent() {
     289                final int[] normal = new int[3];               
     290                final int[] deleted = new int[3];
     291                final String[] names = {"node", "segment", "way"};
     292                Visitor counter = new Visitor(){
     293                        private void inc(OsmPrimitive osm, int i) {
     294                                normal[i]++;
     295                                if (osm.isDeleted())
     296                                        deleted[i]++;
     297                        }
     298                        public void visit(Node n) {
     299                                inc(n, 0);
     300                        }
     301                        public void visit(LineSegment ls) {
     302                                inc(ls, 1);
     303                        }
     304                        public void visit(Way w) {
     305                                inc(w, 2);
     306                        }
     307                };
     308                for (OsmPrimitive osm : data.allPrimitives())
     309                        osm.visit(counter);
     310
     311                JPanel p = new JPanel(new GridBagLayout());
     312                p.add(new JLabel(name+" consists of:"), GBC.eol());
     313                for (int i = 0; i < normal.length; ++i) {
     314                        String s = normal[i]+" "+names[i]+(normal[i] != 1 ?"s":"");
     315                        if (deleted[i] > 0)
     316                                s += " ("+deleted[i]+" deleted)";
     317                        p.add(new JLabel(s, ImageProvider.get("data", names[i]), JLabel.HORIZONTAL), GBC.eol().insets(15,0,0,0));
     318                }
     319                return p;
     320        }
    281321}
  • src/org/openstreetmap/josm/gui/layer/RawGpsDataLayer.java

    r77 r78  
    55import java.awt.Point;
    66import java.util.Collection;
    7 import java.util.LinkedList;
    87
    98import javax.swing.Icon;
     
    2827        private static Icon icon;
    2928       
     29        public static class GpsPoint {
     30                public final LatLon latlon;
     31                public final EastNorth eastNorth;
     32                public final String time;
     33                public GpsPoint(LatLon ll, String t) {
     34                        latlon = ll;
     35                        eastNorth = Main.proj.latlon2eastNorth(ll);
     36                        time = t;
     37                }
     38        }
     39       
    3040        /**
    3141         * A list of ways which containing a list of points.
    3242         */
    33         private final Collection<Collection<LatLon>> data;
    34         private Collection<Collection<EastNorth>> eastNorth;
     43        public final Collection<Collection<GpsPoint>> data;
    3544
    36         public RawGpsDataLayer(Collection<Collection<LatLon>> data, String name) {
     45        public RawGpsDataLayer(Collection<Collection<GpsPoint>> data, String name) {
    3746                super(name);
    3847                this.data = data;
    39 
    40                 eastNorth = new LinkedList<Collection<EastNorth>>();
    41                 for (Collection<LatLon> c : data) {
    42                         Collection<EastNorth> eastNorthList = new LinkedList<EastNorth>();
    43                         for (LatLon ll : c)
    44                                 eastNorthList.add(Main.proj.latlon2eastNorth(ll));
    45                         this.eastNorth.add(eastNorthList);
    46                 }
    47 
    4848                Main.pref.addPreferenceChangedListener(new PreferenceChangedListener(){
    4949                        public void preferenceChanged(String key, String newValue) {
     
    7575                        g.setColor(Color.GRAY);
    7676                Point old = null;
    77                 for (Collection<EastNorth> c : eastNorth) {
     77                for (Collection<GpsPoint> c : data) {
    7878                        if (!Main.pref.getBoolean("forceRawGpsLines"))
    7979                                old = null;
    80                         for (EastNorth eastNorth : c) {
    81                                 Point screen = mv.getPoint(eastNorth);
     80                        for (GpsPoint p : c) {
     81                                Point screen = mv.getPoint(p.eastNorth);
    8282                                if (Main.pref.getBoolean("drawRawGpsLines") && old != null)
    8383                                        g.drawLine(old.x, old.y, screen.x, screen.y);
     
    9292        public String getToolTipText() {
    9393                int points = 0;
    94                 for (Collection<LatLon> c : data)
     94                for (Collection<GpsPoint> c : data)
    9595                        points += c.size();
    96                 return data.size()+" ways, "+points+" points.";
     96                return data.size()+" tracks, "+points+" points.";
    9797        }
    9898
     
    110110        @Override
    111111        public void visitBoundingBox(BoundingXYVisitor v) {
    112                 for (Collection<EastNorth> c : eastNorth)
    113                         for (EastNorth eastNorth : c)
    114                                 v.visit(eastNorth);
     112                for (Collection<GpsPoint> c : data)
     113                        for (GpsPoint p : c)
     114                                v.visit(p.eastNorth);
     115        }
     116
     117        @Override
     118        public Object getInfoComponent() {
     119                StringBuilder b = new StringBuilder();
     120                int points = 0;
     121                for (Collection<GpsPoint> c : data) {
     122                        b.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a track with "+c.size()+" points<br>");
     123                        points += c.size();
     124                }
     125                b.append("</html>");
     126                return "<html>"+name+" consists of "+data.size()+" tracks ("+points+" points)<br>"+b.toString();
    115127        }
    116128}
  • 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}
  • src/org/openstreetmap/josm/io/OsmServerReader.java

    r75 r78  
    1111import org.jdom.JDOMException;
    1212import org.openstreetmap.josm.Main;
    13 import org.openstreetmap.josm.data.coor.LatLon;
    1413import org.openstreetmap.josm.data.osm.DataSet;
     14import org.openstreetmap.josm.gui.layer.RawGpsDataLayer.GpsPoint;
    1515import org.xml.sax.SAXException;
    1616
     
    4747         *              ways.
    4848         */
    49         public Collection<Collection<LatLon>> parseRawGps() throws IOException, JDOMException {
     49        public Collection<Collection<GpsPoint>> parseRawGps() throws IOException, JDOMException {
    5050                String url = Main.pref.get("osm-server.url")+"/0.3/trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page=";
    51                 Collection<Collection<LatLon>> data = new LinkedList<Collection<LatLon>>();
    52                 Collection<LatLon> list = new LinkedList<LatLon>();
     51                Collection<Collection<GpsPoint>> data = new LinkedList<Collection<GpsPoint>>();
     52                Collection<GpsPoint> list = new LinkedList<GpsPoint>();
    5353               
    5454                for (int i = 0;;++i) {
     
    5757                                break;
    5858                        RawGpsReader gpsReader = new RawGpsReader(r);
    59                         Collection<Collection<LatLon>> allWays = gpsReader.parse();
     59                        Collection<Collection<GpsPoint>> allWays = gpsReader.parse();
    6060                        boolean foundSomething = false;
    61                         for (Collection<LatLon> t : allWays) {
     61                        for (Collection<GpsPoint> t : allWays) {
    6262                                if (!t.isEmpty()) {
    6363                                        foundSomething = true;
  • src/org/openstreetmap/josm/io/OsmServerWriter.java

    r75 r78  
    6666        public void visit(Node n) {
    6767                if (n.id == 0 && !n.isDeleted()) {
    68                         n.put("created_by", "JOSM");
    6968                        sendRequest("PUT", "node", n, true);
    7069                } else if (n.isDeleted()) {
     
    8180        public void visit(LineSegment ls) {
    8281                if (ls.id == 0 && !ls.isDeleted()) {
    83                         ls.put("created_by", "JOSM");
    8482                        sendRequest("PUT", "segment", ls, true);
    8583                } else if (ls.isDeleted()) {
     
    9694        public void visit(Way w) {
    9795                if (w.id == 0 && !w.isDeleted()) {
    98                         w.put("created_by", "JOSM");
    9996                        sendRequest("PUT", "way", w, true);
    10097                } else if (w.isDeleted()) {
  • src/org/openstreetmap/josm/io/RawCsvReader.java

    r74 r78  
    1212import org.openstreetmap.josm.Main;
    1313import org.openstreetmap.josm.data.coor.LatLon;
     14import org.openstreetmap.josm.gui.layer.RawGpsDataLayer.GpsPoint;
    1415
    1516/**
     
    2829        }
    2930       
    30         public Collection<LatLon> parse() throws JDOMException, IOException {
    31                 Collection<LatLon> data = new LinkedList<LatLon>();
     31        public Collection<GpsPoint> parse() throws JDOMException, IOException {
     32                Collection<GpsPoint> data = new LinkedList<GpsPoint>();
    3233                String formatStr = Main.pref.get("csvImportString");
    3334                if (formatStr == null)
     
    6364                                StringTokenizer st = new StringTokenizer(line, delim);
    6465                                double lat = 0, lon = 0;
     66                                String time = null;
    6567                                for (String token : format) {
    6668                                        if (token.equals("lat"))
     
    6870                                        else if (token.equals("lon"))
    6971                                                lon = Double.parseDouble(st.nextToken());
     72                                        else if (token.equals("time"))
     73                                                time = (time == null?"":(time+" ")) + st.nextToken();
    7074                                        else if (token.equals("ignore"))
    7175                                                st.nextToken();
     
    7377                                                throw new JDOMException("Unknown data type: '"+token+"'."+(Main.pref.get("csvImportString").equals("") ? " Maybe add an format string in preferences." : ""));
    7478                                }
    75                                 data.add(new LatLon(lat, lon));
     79                                data.add(new GpsPoint(new LatLon(lat, lon), time));
    7680                        }
    7781                } catch (RuntimeException e) {
  • src/org/openstreetmap/josm/io/RawGpsReader.java

    r71 r78  
    55import java.util.Collection;
    66import java.util.LinkedList;
    7 import java.util.List;
    87
    98import org.jdom.Element;
     
    1211import org.jdom.input.SAXBuilder;
    1312import org.openstreetmap.josm.data.coor.LatLon;
     13import org.openstreetmap.josm.gui.layer.RawGpsDataLayer.GpsPoint;
    1414
    1515/**
     
    4242         * Parse and return the read data
    4343         */
    44         public Collection<Collection<LatLon>> parse() throws JDOMException, IOException {
     44        public Collection<Collection<GpsPoint>> parse() throws JDOMException, IOException {
    4545                final SAXBuilder builder = new SAXBuilder();
    4646                builder.setValidation(false);
     
    5555         */
    5656        @SuppressWarnings("unchecked")
    57         private Collection<Collection<LatLon>> parseData(Element root) throws JDOMException {
    58                 Collection<Collection<LatLon>> data = new LinkedList<Collection<LatLon>>();
     57        private Collection<Collection<GpsPoint>> parseData(Element root) throws JDOMException {
     58                Collection<Collection<GpsPoint>> data = new LinkedList<Collection<GpsPoint>>();
    5959
    6060                // workaround for bug where the server adds /gpx.asd to the namespace
     
    6262               
    6363                for (Object o : root.getChildren("wpt", GPX)) {
    64                         Collection<LatLon> line = new LinkedList<LatLon>();
    65                         line.add(new LatLon(parseDouble((Element)o, LatLonAttr.lat), parseDouble((Element)o, LatLonAttr.lon)));
     64                        Collection<GpsPoint> line = new LinkedList<GpsPoint>();
     65                        line.add(new GpsPoint(
     66                                        new LatLon(parseDouble((Element)o, LatLonAttr.lat), parseDouble((Element)o, LatLonAttr.lon)),
     67                                        ((Element)o).getChildText("time", GPX)));
    6668                        data.add(line);
    67                 }
    68                 for (Object o : root.getChildren("rte", GPX)) {
    69                         Collection<LatLon> line = parseLine(((Element)o).getChildren("rtept", GPX));
    70                         if (!line.isEmpty())
    71                                 data.add(line);
    7269                }
    7370                for (Object o : root.getChildren("trk", GPX)) {
    7471                        for (Object seg : ((Element)o).getChildren("trkseg", GPX)) {
    75                                 Collection<LatLon> line = parseLine(((Element)seg).getChildren("trkpt", GPX));
     72                                Collection<GpsPoint> data1 = new LinkedList<GpsPoint>();
     73                                for (Object trkObj : ((Element)seg).getChildren("trkpt", GPX)) {
     74                                        data1.add(new GpsPoint(
     75                                                        new LatLon(parseDouble((Element)trkObj, LatLonAttr.lat), parseDouble((Element)trkObj, LatLonAttr.lon)),
     76                                                        ((Element)trkObj).getChildText("time", GPX)));
     77                                }
     78                                Collection<GpsPoint> line = data1;
    7679                                if (!line.isEmpty())
    7780                                        data.add(line);
     
    9497                return d;
    9598        }
    96 
    97         /**
    98          * Parse the list of waypoint - elements and return a collection with the
    99          * points read.
    100          */
    101         private Collection<LatLon> parseLine(List<Element> wpt) throws JDOMException {
    102                 Collection<LatLon> data = new LinkedList<LatLon>();
    103                 for (Element e : wpt)
    104                         data.add(new LatLon(parseDouble(e, LatLonAttr.lat), parseDouble(e, LatLonAttr.lon)));
    105                 return data;
    106         }
    10799}
Note: See TracChangeset for help on using the changeset viewer.