Changeset 40 in josm


Ignore:
Timestamp:
2006-01-11T23:39:36+01:00 (18 years ago)
Author:
imi
Message:
  • added world boundaries
  • added bug report exception handler
  • raw gps/real data when open depends now on extension
Location:
src/org/openstreetmap/josm
Files:
2 added
19 edited

Legend:

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

    r33 r40  
    1515
    1616import org.openstreetmap.josm.actions.AboutAction;
     17import org.openstreetmap.josm.actions.DownloadAction;
    1718import org.openstreetmap.josm.actions.ExitAction;
    1819import org.openstreetmap.josm.actions.OpenAction;
    19 import org.openstreetmap.josm.actions.DownloadAction;
    2020import org.openstreetmap.josm.actions.PreferencesAction;
    2121import org.openstreetmap.josm.actions.RedoAction;
     
    2626import org.openstreetmap.josm.data.Preferences.PreferencesException;
    2727import org.openstreetmap.josm.data.osm.DataSet;
     28import org.openstreetmap.josm.gui.BugReportExceptionHandler;
    2829import org.openstreetmap.josm.gui.ImageProvider;
    2930import org.openstreetmap.josm.gui.MapFrame;
     
    144145         */
    145146        public static void main(String[] args) {
     147                setupExceptionHandler();
    146148                setupUiDefaults();
    147149               
     
    217219                UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
    218220        }
     221
     222        /**
     223         * Setup an exception handler that displays a sorry message and the possibility
     224         * to do a bug report.
     225         */
     226        private static void setupExceptionHandler() {
     227                Thread.setDefaultUncaughtExceptionHandler(new BugReportExceptionHandler());
     228        }
    219229}
  • src/org/openstreetmap/josm/actions/DownloadAction.java

    r37 r40  
    7777                        GeoPoint bottomLeft = mv.getPoint(0, h, true);
    7878                        GeoPoint topRight = mv.getPoint(w, 0, true);
     79                        if (bottomLeft.isOutSideWorld())
     80                                bottomLeft = new GeoPoint(-89.999, -179.999); // do not use the Projection constants, since this look better.
     81                        if (topRight.isOutSideWorld())
     82                                topRight = new GeoPoint(89.999, 179.999);
    7983                        latlon[0].setText(""+bottomLeft.lat);
    8084                        latlon[1].setText(""+bottomLeft.lon);
  • src/org/openstreetmap/josm/actions/OpenAction.java

    r38 r40  
    11package org.openstreetmap.josm.actions;
    22
    3 import java.awt.GridBagLayout;
    43import java.awt.event.ActionEvent;
    54import java.awt.event.InputEvent;
     
    87import java.io.FileReader;
    98import java.io.IOException;
    10 import java.io.Reader;
    119import java.util.Collection;
    1210import java.util.LinkedList;
    1311
    14 import javax.swing.Box;
    15 import javax.swing.JCheckBox;
    1612import javax.swing.JFileChooser;
    17 import javax.swing.JLabel;
    1813import javax.swing.JOptionPane;
    19 import javax.swing.JPanel;
    2014import javax.swing.KeyStroke;
    2115
     
    2418import org.openstreetmap.josm.data.GeoPoint;
    2519import org.openstreetmap.josm.data.osm.DataSet;
    26 import org.openstreetmap.josm.gui.GBC;
    2720import org.openstreetmap.josm.gui.MapFrame;
    2821import org.openstreetmap.josm.gui.layer.Layer;
     
    5548                fc.setAcceptAllFileFilterUsed(true);
    5649               
    57                 // additional options
    58                 JCheckBox rawGps = new JCheckBox("Raw GPS data", true);
    59                 rawGps.setToolTipText("Check this, if the data were obtained from a gps device.");
    60                
    61                 JPanel p = new JPanel(new GridBagLayout());
    62                 p.add(new JLabel("Options"), GBC.eop());
    63                 p.add(rawGps, GBC.eol());
    64                 p.add(Box.createVerticalGlue(), GBC.eol().fill());
    65                 fc.setAccessory(p);
    66 
    6750                if (fc.showOpenDialog(Main.main) != JFileChooser.APPROVE_OPTION)
    6851                        return;
     
    7457                try {
    7558                        Layer layer;
    76                         Reader in = new FileReader(filename);
    7759                        String extension = filename.getName().toLowerCase().substring(filename.getName().lastIndexOf('.')+1);
    78                         if (rawGps.isSelected()) {
     60
     61                        if (asRawData(extension)) {
    7962                                Collection<Collection<GeoPoint>> data;
    80                                 if (extension.equals("gpx"))
    81                                         data = new RawGpsReader(in).parse();
    82                                 else if (extension.equals("xml") || extension.equals("osm")) {
    83                                         JOptionPane.showMessageDialog(Main.main, "Osm server data import for GPS data is not supported.");
    84                                         return;
     63                                if (extension.equals("gpx")) {
     64                                        data = new RawGpsReader(new FileReader(filename)).parse();
    8565                                } else if (extension.equals("csv") || extension.equals("txt")) {
    8666                                        data = new LinkedList<Collection<GeoPoint>>();
    87                                         data.add(new RawCsvReader(in).parse());
    88                                 } else {
    89                                         JOptionPane.showMessageDialog(Main.main, "Unknown file extension: "+extension);
    90                                         return;
    91                                 }
     67                                        data.add(new RawCsvReader(new FileReader(filename)).parse());
     68                                } else
     69                                        throw new IllegalStateException();
    9270                                layer = new RawGpsDataLayer(data, filename.getName());
    9371                        } else {
    9472                                DataSet dataSet;
    9573                                if (extension.equals("gpx"))
    96                                         dataSet = new GpxReader(in).parse();
     74                                        dataSet = new GpxReader(new FileReader(filename)).parse();
    9775                                else if (extension.equals("xml") || extension.equals("osm"))
    98                                         dataSet = new OsmReader(in).parse();
     76                                        dataSet = new OsmReader(new FileReader(filename)).parse();
    9977                                else if (extension.equals("csv") || extension.equals("txt")) {
    10078                                        JOptionPane.showMessageDialog(Main.main, "CSV Data import for non-GPS data is not implemented yet.");
     
    12098                }
    12199        }
     100
     101        /**
     102         * @return Return whether the file should be opened as raw gps data. May ask the
     103         * user, if unsure.
     104         */
     105        private boolean asRawData(String extension) {
     106                if (extension.equals("csv") || extension.equals("txt"))
     107                        return true;
     108                if (!extension.equals("gpx"))
     109                        return false;
     110                return JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(
     111                                Main.main, "Do you want to open the file as raw gps data?",
     112                                "Open as raw data?", JOptionPane.YES_NO_OPTION);
     113        }
    122114}
  • src/org/openstreetmap/josm/actions/SaveAction.java

    r39 r40  
    1313
    1414import org.openstreetmap.josm.Main;
     15import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1516import org.openstreetmap.josm.io.GpxWriter;
    1617import org.openstreetmap.josm.io.OsmWriter;
     
    3738                        return;
    3839                }
     40                if (isDataSetEmpty() && JOptionPane.NO_OPTION == JOptionPane.showConfirmDialog(Main.main, "The document contains no data. Save anyway?", "Empty document", JOptionPane.YES_NO_OPTION))
     41                        return;
     42
    3943                JFileChooser fc = new JFileChooser("data");
    4044                for (int i = 0; i < ExtensionFileFilter.filters.length; ++i)
     
    4549                if (file == null)
    4650                        return;
    47                
     51
    4852                try {
    49                         FileWriter fileWriter = new FileWriter(file);
    5053                        String fn = file.getName();
     54                        FileWriter fileWriter;
    5155                        if (fn.endsWith(".gpx"))
    52                                 new GpxWriter(fileWriter).output();
     56                                new GpxWriter(fileWriter = new FileWriter(file)).output();
    5357                        else if (fn.endsWith(".xml") || fn.endsWith(".osm"))
    54                                 new OsmWriter(fileWriter, Main.main.ds).output();
     58                                new OsmWriter(fileWriter = new FileWriter(file), Main.main.ds).output();
    5559                        else if (fn.endsWith(".txt") || fn.endsWith(".csv")) {
    5660                                JOptionPane.showMessageDialog(Main.main, "CSV output not supported yet.");
     
    6064                                return;
    6165                        }
    62                                
    6366                        fileWriter.close();
    6467                } catch (IOException e) {
     
    6871        }
    6972
     73        /**
     74         * Check the data set if it would be empty on save. It is empty, if it contains
     75         * no objects (after all objects that are created and deleted without beeing
     76         * transfered to the server have been removed).
     77         * 
     78         * @return <code>true</code>, if a save result in an empty data set.
     79         */
     80        private boolean isDataSetEmpty() {
     81                for (OsmPrimitive osm : Main.main.ds.allPrimitives())
     82                        if (!osm.isDeleted() || osm.id > 0)
     83                                return false;
     84                return true;
     85        }
     86
    7087}
  • src/org/openstreetmap/josm/actions/mapmode/AddNodeAction.java

    r30 r40  
    33import java.awt.event.KeyEvent;
    44import java.awt.event.MouseEvent;
     5
     6import javax.swing.JOptionPane;
    57
    68import org.openstreetmap.josm.Main;
     
    5052                        Node node = new Node();
    5153                        node.coor = mv.getPoint(e.getX(), e.getY(), true);
     54                        if (node.coor.isOutSideWorld()) {
     55                                JOptionPane.showMessageDialog(Main.main, "Can not add a node outside of the world.");
     56                                return;
     57                        }
    5258                        mv.editLayer().add(new AddCommand(Main.main.ds, node));
    5359                        mv.repaint();
  • src/org/openstreetmap/josm/actions/mapmode/MoveAction.java

    r31 r40  
    77import java.util.Collection;
    88
     9import javax.swing.JOptionPane;
     10
    911import org.openstreetmap.josm.Main;
    1012import org.openstreetmap.josm.command.Command;
    1113import org.openstreetmap.josm.command.MoveCommand;
    1214import org.openstreetmap.josm.data.GeoPoint;
     15import org.openstreetmap.josm.data.osm.Node;
    1316import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1417import org.openstreetmap.josm.gui.MapFrame;
     
    8487
    8588                Collection<OsmPrimitive> selection = Main.main.ds.getSelected();
     89                Collection<Node> affectedNodes = MoveCommand.getAffectedNodes(selection);
     90               
     91                // check if any coordinate would be outside the world
     92                for (OsmPrimitive osm : affectedNodes) {
     93                        if (osm instanceof Node && ((Node)osm).coor.isOutSideWorld()) {
     94                                JOptionPane.showMessageDialog(Main.main, "Cannot move objects outside of the world.");
     95                                return;
     96                        }
     97                }
     98               
    8699                Command c = mv.editLayer().lastCommand();
    87                 if (c instanceof MoveCommand && MoveCommand.getAffectedNodes(selection).equals(((MoveCommand)c).objects))
     100                if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand)c).objects))
    88101                        ((MoveCommand)c).moveAgain(dx,dy);
    89102                else
  • src/org/openstreetmap/josm/command/AddCommand.java

    r35 r40  
    66import org.openstreetmap.josm.data.osm.OsmPrimitive;
    77import org.openstreetmap.josm.data.osm.visitor.AddVisitor;
     8import org.openstreetmap.josm.data.osm.visitor.DeleteVisitor;
    89
    910/**
     
    3839
    3940        public void undoCommand() {
    40                 osm.setDeleted(true);
     41                osm.visit(new DeleteVisitor(ds));
    4142        }
    4243
  • src/org/openstreetmap/josm/command/DeleteCommand.java

    r35 r40  
    66import org.openstreetmap.josm.data.osm.DataSet;
    77import org.openstreetmap.josm.data.osm.OsmPrimitive;
    8 import org.openstreetmap.josm.data.osm.visitor.AddVisitor;
    98import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
    10 import org.openstreetmap.josm.data.osm.visitor.Visitor;
    119
    1210/**
     
    3937
    4038        public void undoCommand() {
    41                 Visitor v = new AddVisitor(ds);
    4239                for (OsmPrimitive osm : data)
    43                         osm.visit(v);
     40                        osm.setDeleted(false);
    4441        }
    4542
  • src/org/openstreetmap/josm/data/Bounds.java

    r23 r40  
    11package org.openstreetmap.josm.data;
     2
     3import org.openstreetmap.josm.Main;
     4import org.openstreetmap.josm.data.projection.Projection;
    25
    36/**
     
    4548                this.max = max;
    4649        }
    47        
     50
     51        /**
     52         * Construct bounds that span the whole world.
     53         */
     54        public Bounds() {
     55                min = new GeoPoint(-Projection.MAX_LAT, -Projection.MAX_LON);
     56                Main.pref.getProjection().latlon2xy(min);
     57                max = new GeoPoint(Projection.MAX_LAT, Projection.MAX_LON);
     58                Main.pref.getProjection().latlon2xy(max);
     59        }
     60
    4861        /**
    4962         * @return The bounding rectangle that covers <code>this</code> and
     
    5972                return new Bounds(nmin, nmax);
    6073        }
    61        
     74
    6275        /**
    6376         * @return The bounding rectangle that covers <code>this</code> and
  • src/org/openstreetmap/josm/data/GeoPoint.java

    r18 r40  
    11package org.openstreetmap.josm.data;
     2
     3import org.openstreetmap.josm.data.projection.Projection;
    24
    35
     
    6668                                !Double.isNaN(lat) && !Double.isNaN(lon);
    6769        }
     70
     71        /**
     72         * @return <code>true</code>, if the coordinate is outside the world, compared
     73         * by using lat/lon.
     74         */
     75        public boolean isOutSideWorld() {
     76                return lat < -Projection.MAX_LAT || lat > Projection.MAX_LAT ||
     77                        lon < -Projection.MAX_LON || lon > Projection.MAX_LON;
     78        }
    6879}
  • src/org/openstreetmap/josm/data/osm/DataSet.java

    r35 r40  
    5050                o.addAll(lineSegments);
    5151                o.addAll(tracks);
     52                return o;
     53        }
     54
     55        /**
     56         * @return A collection containing all not-deleted primitives (except keys).
     57         */
     58        public Collection<OsmPrimitive> allNonDeletedPrimitives() {
     59                Collection<OsmPrimitive> o = new LinkedList<OsmPrimitive>();
     60                for (OsmPrimitive osm : allPrimitives())
     61                        if (!osm.isDeleted())
     62                                o.add(osm);
    5263                return o;
    5364        }
  • src/org/openstreetmap/josm/data/osm/visitor/AddVisitor.java

    r35 r40  
    2626        public void visit(Node n) {
    2727                ds.nodes.add(n);
    28                 n.setDeleted(false);
    2928        }
    3029        public void visit(LineSegment ls) {
    3130                ds.lineSegments.add(ls);
    32                 ls.setDeleted(false);
    3331        }
    3432        public void visit(Track t) {
    3533                ds.tracks.add(t);
    36                 t.setDeleted(false);
    3734        }
    3835        public void visit(Key k) {}
  • src/org/openstreetmap/josm/data/osm/visitor/CollectBackReferencesVisitor.java

    r31 r40  
    1414 * Helper that collect all line segments a node is part of, all tracks
    1515 * a node or line segment is part of and all areas a node is part of.
     16 *
     17 * Deleted objects are not collected.
     18 *
    1619 * @author imi
    1720 */
     
    2528        public final Collection<OsmPrimitive> data = new HashSet<OsmPrimitive>();
    2629
     30
     31        /**
     32         * Construct a back reference counter.
     33         * @param ds The dataset to operate on.
     34         */
    2735        public CollectBackReferencesVisitor(DataSet ds) {
    2836                this.ds = ds;
     
    3139        public void visit(Node n) {
    3240                for (Track t : ds.tracks) {
     41                        if (t.isDeleted())
     42                                continue;
    3343                        for (LineSegment ls : t.segments) {
    3444                                if (ls.start == n || ls.end == n) {
     
    3848                        }
    3949                }
    40                 for (LineSegment ls : ds.lineSegments)
     50                for (LineSegment ls : ds.lineSegments) {
     51                        if (ls.isDeleted())
     52                                continue;
    4153                        if (ls.start == n || ls.end == n)
    4254                                data.add(ls);
     55                }
    4356        }
    4457        public void visit(LineSegment ls) {
    45                 for (Track t : ds.tracks)
     58                for (Track t : ds.tracks) {
     59                        if (t.isDeleted())
     60                                continue;
    4661                        if (t.segments.contains(ls))
    4762                                data.add(t);
     63                }
    4864        }
    4965        public void visit(Track t) {}
  • src/org/openstreetmap/josm/data/projection/Projection.java

    r23 r40  
    1818 */
    1919abstract public class Projection implements Cloneable {
     20
     21        public static double MAX_LAT = 85; // yep - JOSM cannot cartograph the poles.
     22        public static double MAX_LON = 179.99999;
    2023
    2124        /**
  • src/org/openstreetmap/josm/data/projection/UTM.java

    r35 r40  
    195195        ZoneData autoDetect(Bounds b) {
    196196                ZoneData zd = new ZoneData();
    197                 if (b == null)
    198                         return zd;
    199197                GeoPoint center = b.centerLatLon();
    200198                double lat = center.lat;
  • src/org/openstreetmap/josm/gui/MapView.java

    r39 r40  
    394394                                l.paint(g, this);
    395395                }
     396               
     397                // draw world borders
     398                g.setColor(Color.DARK_GRAY);
     399                Bounds b = new Bounds();
     400                Point min = getScreenPoint(b.min);
     401                Point max = getScreenPoint(b.max);
     402                int x1 = Math.min(min.x, max.x);
     403                int y1 = Math.min(min.y, max.y);
     404                int x2 = Math.max(min.x, max.x);
     405                int y2 = Math.max(min.y, max.y);
     406                g.drawRect(x1, y1, x2-x1+1, y2-y1+1);
    396407        }
    397408
  • src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r39 r40  
    22
    33import java.awt.Graphics;
     4import java.util.Collection;
    45import java.util.Iterator;
    56import java.util.LinkedList;
     
    9091        @Override
    9192        public String getToolTipText() {
    92                 return data.nodes.size()+" nodes, "+data.lineSegments.size()+" segments, "+data.tracks.size()+" streets.";
     93                return undeletedSize(data.nodes)+" nodes, "+
     94                        undeletedSize(data.lineSegments)+" segments, "+
     95                        undeletedSize(data.tracks)+" streets.";
    9396        }
    9497
     
    110113                for (Node n : data.nodes)
    111114                        b.visit(n);
    112                 return b.bounds;
     115                return b.bounds != null ? b.bounds : new Bounds();
    113116        }
    114117
     
    118121                for (Node n : data.nodes)
    119122                        b.visit(n);
    120                 return b.bounds;
     123                return b.bounds != null ? b.bounds : new Bounds();
    121124        }
    122125
     
    197200                        it.remove();
    198201        }
     202
     203        /**
     204         * @return The number of not-deleted primitives in the list.
     205         */
     206        private int undeletedSize(Collection<? extends OsmPrimitive> list) {
     207                int size = 0;
     208                for (OsmPrimitive osm : list)
     209                        if (!osm.isDeleted())
     210                                size++;
     211                return size;
     212        }
    199213}
  • src/org/openstreetmap/josm/io/OsmWriter.java

    r35 r40  
    6363                List<Element> list = root.getChildren();
    6464                properties = new LinkedList<Element>();
    65                 for (OsmPrimitive osm : ds.allPrimitives()) {
    66                         if (!osm.isDeleted()) {
    67                                 osm.visit(this);
    68                                 list.add(element);
    69                         }
     65                for (OsmPrimitive osm : ds.allNonDeletedPrimitives()) {
     66                        osm.visit(this);
     67                        list.add(element);
    7068                }
    7169                list.addAll(properties);
  • src/org/openstreetmap/josm/io/RawGpsReader.java

    r29 r40  
    5555         */
    5656        @SuppressWarnings("unchecked")
    57         private Collection<Collection<GeoPoint>> parseData(Element root) {
     57        private Collection<Collection<GeoPoint>> parseData(Element root) throws JDOMException {
    5858                Collection<Collection<GeoPoint>> data = new LinkedList<Collection<GeoPoint>>();
    5959
     
    6363                for (Object o : root.getChildren("wpt", GPX)) {
    6464                        Collection<GeoPoint> line = new LinkedList<GeoPoint>();
    65                         line.add(new GeoPoint(
    66                                         Float.parseFloat(((Element)o).getAttributeValue("lat")),
    67                                         Float.parseFloat(((Element)o).getAttributeValue("lon"))));
     65                        line.add(new GeoPoint(parseFloat((Element)o, LatLon.lat), parseFloat((Element)o, LatLon.lon)));
    6866                        data.add(line);
    6967                }
     
    8381        }
    8482
     83        enum LatLon {lat, lon}
     84        /**
     85         * Return a parsed float value from the element behind the object o.
     86         * @param o An object of dynamic type org.jdom.Element (will be casted).
     87         * @param attr The name of the attribute.
     88         * @throws JDOMException If the absolute of the value is out of bound.
     89         */
     90        private float parseFloat(Element e, LatLon attr) throws JDOMException {
     91                float f = Float.parseFloat(e.getAttributeValue(attr.toString()));
     92                if (Math.abs(f) > (attr == LatLon.lat ? 90 : 180))
     93                        throw new JDOMException("Data error: "+attr+" value '"+f+"' is out of bound.");
     94                return f;
     95        }
     96
    8597        /**
    8698         * Parse the list of trackpoint - elements and return a collection with the
    8799         * points read.
    88100         */
    89         private Collection<GeoPoint> parseLine(List<Element> wpt) {
     101        private Collection<GeoPoint> parseLine(List<Element> wpt) throws JDOMException {
    90102                Collection<GeoPoint> data = new LinkedList<GeoPoint>();
    91103                for (Element e : wpt)
    92                         data.add(new GeoPoint(
    93                                         Float.parseFloat(e.getAttributeValue("lat")),
    94                                         Float.parseFloat(e.getAttributeValue("lon"))));
     104                        data.add(new GeoPoint(parseFloat(e, LatLon.lat), parseFloat(e, LatLon.lon)));
    95105                return data;
    96106        }
Note: See TracChangeset for help on using the changeset viewer.