Changeset 29 in josm for src


Ignore:
Timestamp:
2005-12-02T07:40:16+01:00 (18 years ago)
Author:
imi
Message:
  • fixed GPX export / import. Now featuring pending line segments
  • added XML export
  • merged open actions to autodetect format by extension
  • added undo (not working yet)
Location:
src/org/openstreetmap/josm
Files:
1 added
10 edited
1 moved

Legend:

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

    r24 r29  
    1616import org.openstreetmap.josm.actions.AboutAction;
    1717import org.openstreetmap.josm.actions.ExitAction;
    18 import org.openstreetmap.josm.actions.OpenGpxAction;
     18import org.openstreetmap.josm.actions.OpenAction;
    1919import org.openstreetmap.josm.actions.OpenOsmServerAction;
    2020import org.openstreetmap.josm.actions.PreferencesAction;
    2121import org.openstreetmap.josm.actions.SaveAction;
    2222import org.openstreetmap.josm.actions.SaveGpxAction;
     23import org.openstreetmap.josm.actions.UndoAction;
    2324import org.openstreetmap.josm.data.Preferences;
    2425import org.openstreetmap.josm.data.Preferences.PreferencesException;
     
    7677                // creating actions
    7778                OpenOsmServerAction openServerAction = new OpenOsmServerAction();
    78                 OpenGpxAction openGpxAction = new OpenGpxAction();
     79                OpenAction openAction = new OpenAction();
    7980                SaveAction saveAction = new SaveAction();
    8081                SaveGpxAction saveGpxAction = new SaveGpxAction();
    8182                ExitAction exitAction = new ExitAction();
     83                UndoAction undoAction = new UndoAction();
    8284                PreferencesAction preferencesAction = new PreferencesAction();
    8385                AboutAction aboutAction = new AboutAction();
     
    8991                JMenu fileMenu = new JMenu("Files");
    9092                fileMenu.setMnemonic('F');
    91                 fileMenu.add(openGpxAction);
     93                fileMenu.add(openAction);
    9294                fileMenu.add(saveAction);
    9395                fileMenu.add(saveGpxAction);
     
    9597                fileMenu.add(exitAction);
    9698                mainMenu.add(fileMenu);
     99
    97100               
    98101                JMenu connectionMenu = new JMenu("Connection");
     
    103106                JMenu editMenu = new JMenu("Edit");
    104107                editMenu.setMnemonic('E');
     108                //editMenu.add(undoAction);
     109                editMenu.addSeparator();
    105110                editMenu.add(preferencesAction);
    106111                mainMenu.add(editMenu);
     
    116121                toolBar.setFloatable(false);
    117122                toolBar.add(openServerAction);
    118                 toolBar.add(openGpxAction);
     123                toolBar.add(openAction);
    119124                toolBar.add(saveAction);
    120125                toolBar.add(saveGpxAction);
     126                toolBar.addSeparator();
     127                //toolBar.add(undoAction);
    121128                toolBar.addSeparator();
    122129                toolBar.add(preferencesAction);
  • src/org/openstreetmap/josm/actions/OpenAction.java

    r27 r29  
    3232import org.openstreetmap.josm.gui.layer.RawGpsDataLayer;
    3333import org.openstreetmap.josm.io.GpxReader;
     34import org.openstreetmap.josm.io.OsmReader;
    3435import org.openstreetmap.josm.io.RawGpsReader;
    3536
     
    4041 * @author imi
    4142 */
    42 public class OpenGpxAction extends AbstractAction {
     43public class OpenAction extends AbstractAction {
    4344
    4445        /**
    4546         * Create an open action. The name is "Open GPX".
    4647         */
    47         public OpenGpxAction() {
    48                 super("Open GPX", ImageProvider.get("opengpx"));
    49                 putValue(ACCELERATOR_KEY, KeyStroke.getAWTKeyStroke(KeyEvent.VK_O,
    50                                 InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
     48        public OpenAction() {
     49                super("Open", ImageProvider.get("open"));
     50                putValue(ACCELERATOR_KEY, KeyStroke.getAWTKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
    5151                putValue(MNEMONIC_KEY, KeyEvent.VK_O);
    52                 putValue(SHORT_DESCRIPTION, "Open a file in GPX format.");
     52                putValue(SHORT_DESCRIPTION, "Open a file.");
    5353        }
    5454
     
    6868                // additional options
    6969                JCheckBox rawGps = new JCheckBox("Raw GPS data", true);
    70                 rawGps.setToolTipText("Check this, if the data are obtained from a gps device.");
     70                rawGps.setToolTipText("Check this, if the data were obtained from a gps device.");
    7171                JCheckBox newLayer = new JCheckBox("As Layer", true);
    7272                newLayer.setToolTipText("Open as a new layer or replace all current layers.");
     
    8686                        return;
    8787               
    88                 File gpxFile = fc.getSelectedFile();
    89                 if (gpxFile == null)
     88                File filename = fc.getSelectedFile();
     89                if (filename == null)
    9090                        return;
    91                
     91
    9292                try {
    9393                        Layer layer;
    9494                        if (rawGps.isSelected()) {
    95                                 Collection<Collection<GeoPoint>> data = new RawGpsReader(new FileReader(gpxFile)).parse();
    96                                 layer = new RawGpsDataLayer(data, gpxFile.getName());
     95                                Collection<Collection<GeoPoint>> data = new RawGpsReader(new FileReader(filename)).parse();
     96                                layer = new RawGpsDataLayer(data, filename.getName());
    9797                        } else {
    98                                 DataSet dataSet = new GpxReader(new FileReader(gpxFile)).parse();
     98                                DataSet dataSet = filename.getName().toLowerCase().endsWith("gpx") ?
     99                                                new GpxReader(new FileReader(filename)).parse() :
     100                                                new OsmReader(new FileReader(filename)).parse();
    99101                                Collection<OsmPrimitive> l = Main.main.ds.mergeFrom(dataSet);
    100                                 layer = new OsmDataLayer(l, gpxFile.getName());
     102                                layer = new OsmDataLayer(l, filename.getName());
    101103                        }
    102104                       
    103105                        if (Main.main.getMapFrame() == null || !newLayer.isSelected())
    104                                 Main.main.setMapFrame(gpxFile.getName(), new MapFrame(layer));
     106                                Main.main.setMapFrame(filename.getName(), new MapFrame(layer));
    105107                        else
    106108                                Main.main.getMapFrame().mapView.addLayer(layer);
    107                        
     109
    108110                } catch (JDOMException x) {
    109111                        x.printStackTrace();
     
    111113                } catch (IOException x) {
    112114                        x.printStackTrace();
    113                         JOptionPane.showMessageDialog(Main.main, "Could not read '"+gpxFile.getName()+"'\n"+x.getMessage());
     115                        JOptionPane.showMessageDialog(Main.main, "Could not read '"+filename.getName()+"'\n"+x.getMessage());
    114116                }
    115117        }
  • src/org/openstreetmap/josm/gui/MapView.java

    r24 r29  
    141141                        for (LayerChangeListener l : listeners)
    142142                                l.layerRemoved(layer);
     143                if (layer == editLayer)
     144                        editLayer = null;
    143145        }
    144146
  • src/org/openstreetmap/josm/gui/dialogs/LayerList.java

    r23 r29  
    2424
    2525import org.openstreetmap.josm.Main;
     26import org.openstreetmap.josm.data.osm.DataSet;
    2627import org.openstreetmap.josm.gui.ImageProvider;
    2728import org.openstreetmap.josm.gui.MapFrame;
     
    154155                                if (model.size() == 1) {
    155156                                        Main.main.setMapFrame(null, null);
     157                                        Main.main.ds = new DataSet();
    156158                                } else {
    157159                                        int sel = layers.getSelectedIndex();
  • src/org/openstreetmap/josm/gui/dialogs/PropertiesDialog.java

    r28 r29  
    1313import java.awt.event.WindowFocusListener;
    1414import java.util.Collection;
     15import java.util.HashMap;
    1516import java.util.Iterator;
    1617import java.util.TreeMap;
     
    292293                data.setRowCount(0);
    293294                TreeMap<String, Collection<String>> props = new TreeMap<String, Collection<String>>();
     295                HashMap<String, Integer> valueCounts = new HashMap<String, Integer>();
    294296                for (OsmPrimitive osm : newSelection) {
    295297                        if (osm.keys != null) {
     
    301303                                        }
    302304                                        value.add(e.getValue());
     305                                       
     306                                        Integer count = valueCounts.get(e.getValue());
     307                                        if (count == null)
     308                                                count = 0;
     309                                        valueCounts.put(e.getValue(), count+1);
    303310                                }
    304311                        }
    305312                }
     313                int selCount = newSelection.size();
    306314                for (Entry<String, Collection<String>> e : props.entrySet()) {
    307315                        JComboBox value = new JComboBox(e.getValue().toArray());
    308316                        value.setEditable(true);
    309                         if (e.getValue().size() > 1)
     317                        if (e.getValue().size() > 1 || valueCounts.get(e.getValue().iterator().next()) != selCount)
    310318                                value.getEditor().setItem("<different>");
    311319                        data.addRow(new Object[]{e.getKey(), value});
  • src/org/openstreetmap/josm/gui/layer/EditLayer.java

    r23 r29  
    4242         * All commands that were made on the dataset.
    4343         */
    44         public Collection<Command> commands = new LinkedList<Command>();
     44        public LinkedList<Command> commands = new LinkedList<Command>();
    4545
    4646        /**
  • src/org/openstreetmap/josm/io/GpxReader.java

    r24 r29  
    3434         * The OSM namespace used (for extensions).
    3535         */
    36         private static final Namespace OSM = Namespace.getNamespace("osm");
     36        private static final Namespace OSM = Namespace.getNamespace("osm", "http://www.openstreetmap.org");
    3737
    3838        /**
     
    115115        private void parseTrack(Element e, DataSet ds) {
    116116                Track track = new Track();
     117                boolean pendingLS = false; // is this track just a fake?
     118
    117119                for (Object o : e.getChildren()) {
    118120                        Element child = (Element)o;
     
    127129                                        else {
    128130                                                LineSegment lineSegment = new LineSegment(start, node);
    129                                                 parseKeyValueExtensions(lineSegment, ((Element)w).getChild("extensions", GPX));
     131                                                parseKeyValueExtensions(lineSegment, child.getChild("extensions", GPX));
    130132                                                track.add(lineSegment);
    131133                                                start = null;
    132134                                        }
    133135                                }
    134                         } else if (child.getName().equals("extensions"))
     136                        } else if (child.getName().equals("extensions")) {
    135137                                parseKeyValueExtensions(track, child);
    136                         else if (child.getName().equals("link"))
     138                                if (child.getChild("segment", OSM) != null)
     139                                        pendingLS = true;
     140                        } else if (child.getName().equals("link"))
    137141                                parseKeyValueLink(track, child);
    138142                        else
    139143                                parseKeyValueTag(track, child);
    140144                }
    141                 ds.tracks.add(track);
     145                if (pendingLS && track.segments.size() == 1)
     146                        ds.pendingLineSegments.add(track.segments.get(0));
     147                else
     148                        ds.tracks.add(track);
    142149        }
    143150       
     
    175182        private void parseKeyValueExtensions(OsmPrimitive osm, Element e) {
    176183                if (e != null) {
    177                         if (osm.keys == null)
    178                                 osm.keys = new HashMap<Key, String>();
    179184                        for (Object o : e.getChildren("property", OSM)) {
     185                                if (osm.keys == null)
     186                                        osm.keys = new HashMap<Key, String>();
    180187                                Element child = (Element)o;
    181                                 Key key = Key.get(child.getAttributeValue("name"));
    182                                 osm.keys.put(key, child.getAttributeValue("value"));
     188                                String keyname = child.getAttributeValue("key");
     189                                if (keyname != null) {
     190                                        Key key = Key.get(keyname);
     191                                        String value = child.getAttributeValue("value");
     192                                        if (value == null)
     193                                                value = "";
     194                                        osm.keys.put(key, value);
     195                                }
    183196                        }
    184197                }
  • src/org/openstreetmap/josm/io/GpxWriter.java

    r25 r29  
    100100                        // line segments
    101101                        for (LineSegment ls : t.segments) {
    102                                 Element lsElem = new Element("trkseg", GPX);
    103                                 if (ls.keys != null)
    104                                 addPropertyExtensions(lsElem, ls.keys);
    105                                 lsElem.getChildren().add(parseWaypoint(ls.start, "trkpt"));
    106                                 lsElem.getChildren().add(parseWaypoint(ls.end, "trkpt"));
     102                                tElem.getChildren().add(parseLineSegment(ls));
    107103                                nodes.remove(ls.start);
    108104                                nodes.remove(ls.end);
    109                                 tElem.getChildren().add(lsElem);
    110105                        }
     106
    111107                        e.getChildren().add(tElem);
    112108                }
    113109               
     110                // encode pending line segments as tracks
     111                for (LineSegment ls : Main.main.ds.pendingLineSegments) {
     112                        Element t = new Element("trk", GPX);
     113                        t.getChildren().add(parseLineSegment(ls));
     114                        nodes.remove(ls.start);
     115                        nodes.remove(ls.end);
     116                        Element ext = new Element("extensions", GPX);
     117                        ext.getChildren().add(new Element("segment", OSM));
     118                        t.getChildren().add(ext);
     119                        e.getChildren().add(t);
     120                }
     121
    114122                // waypoints (missing nodes)
    115123                for (Node n : nodes)
     
    117125
    118126                return e;
     127        }
     128
     129
     130        /**
     131         * Parse a line segment and store it into a JDOM-Element. Return that element.
     132         */
     133        @SuppressWarnings("unchecked")
     134        private Element parseLineSegment(LineSegment ls) {
     135                Element lsElem = new Element("trkseg", GPX);
     136                if (ls.keys != null)
     137                addPropertyExtensions(lsElem, ls.keys);
     138                lsElem.getChildren().add(parseWaypoint(ls.start, "trkpt"));
     139                lsElem.getChildren().add(parseWaypoint(ls.end, "trkpt"));
     140                return lsElem;
    119141        }
    120142
     
    213235                if (keys.isEmpty())
    214236                        return;
    215                 Element extensions = e.getChild("extensions");
     237                Element extensions = e.getChild("extensions", GPX);
    216238                if (extensions == null)
    217239                        e.getChildren().add(extensions = new Element("extensions", GPX));
  • src/org/openstreetmap/josm/io/OsmReader.java

    r28 r29  
    4747                        Element root = builder.build(source).getRootElement();
    4848                        return parseDataSet(root);
     49                } catch (NumberFormatException nfe) {
     50                        throw new JDOMException("NumberFormatException. Probably a tag is missing.", nfe);
    4951                } catch (NullPointerException npe) {
    5052                        throw new JDOMException("NullPointerException. Probably a tag name mismatch.", npe);
     
    9698                                        String token = t.nextToken();
    9799                                        if (!" ".equals(token))
    98                                                 data.keys.put(Key.get(token), "yes");
     100                                                data.keys.put(Key.get(token), "");
    99101                                }
    100102                        }
  • src/org/openstreetmap/josm/io/OsmServerReader.java

    r27 r29  
    4949         */
    5050        public Collection<Collection<GeoPoint>> parseRawGps() throws IOException, JDOMException {
    51                 String url = urlStr+"trackpoints?bbox="+lat1+","+lon1+","+lat2+","+lon2+"&page=";
     51                String url = urlStr+"trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page=";
    5252                Collection<Collection<GeoPoint>> data = new LinkedList<Collection<GeoPoint>>();
    5353                Collection<GeoPoint> list = new LinkedList<GeoPoint>();
  • src/org/openstreetmap/josm/io/RawGpsReader.java

    r23 r29  
    99import org.jdom.Element;
    1010import org.jdom.JDOMException;
     11import org.jdom.Namespace;
    1112import org.jdom.input.SAXBuilder;
    1213import org.openstreetmap.josm.data.GeoPoint;
     
    3435
    3536        /**
     37         * The gpx namespace.
     38         */
     39        private Namespace GPX = Namespace.getNamespace("http://www.topografix.com/GPX/1/0");
     40       
     41        /**
    3642         * Parse and return the read data
    3743         */
     
    5258                Collection<Collection<GeoPoint>> data = new LinkedList<Collection<GeoPoint>>();
    5359
    54                 for (Object o : root.getChildren("wpt", GpxReader.GPX)) {
     60                // workaround for bug where the server adds /gpx.asd to the namespace
     61                GPX = Namespace.getNamespace(root.getNamespaceURI());
     62               
     63                for (Object o : root.getChildren("wpt", GPX)) {
    5564                        Collection<GeoPoint> line = new LinkedList<GeoPoint>();
    5665                        line.add(new GeoPoint(
     
    5968                        data.add(line);
    6069                }
    61                 for (Object o : root.getChildren("rte", GpxReader.GPX)) {
    62                         Collection<GeoPoint> line = parseLine(((Element)o).getChildren("rtept", GpxReader.GPX));
     70                for (Object o : root.getChildren("rte", GPX)) {
     71                        Collection<GeoPoint> line = parseLine(((Element)o).getChildren("rtept", GPX));
    6372                        if (!line.isEmpty())
    6473                                data.add(line);
    6574                }
    66                 for (Object o : root.getChildren("trk", GpxReader.GPX)) {
    67                         for (Object seg : ((Element)o).getChildren("trkseg", GpxReader.GPX)) {
    68                                 Collection<GeoPoint> line = parseLine(((Element)seg).getChildren("trkpt", GpxReader.GPX));
     75                for (Object o : root.getChildren("trk", GPX)) {
     76                        for (Object seg : ((Element)o).getChildren("trkseg", GPX)) {
     77                                Collection<GeoPoint> line = parseLine(((Element)seg).getChildren("trkpt", GPX));
    6978                                if (!line.isEmpty())
    7079                                        data.add(line);
Note: See TracChangeset for help on using the changeset viewer.