Changeset 21472 in osm for applications
- Timestamp:
- 2010-05-27T20:18:25+02:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/editgpx
- Files:
-
- 7 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/EditGpxLayer.java
r19683 r21472 12 12 import java.awt.Toolkit; 13 13 import java.awt.event.ActionEvent; 14 import java.util.ArrayList;15 import java.util.Collection;16 import java.util.Date;17 import java.util.HashMap;18 import java.util.HashSet;19 import java.util.Iterator;20 import java.util.List;21 import java.util.Map;22 14 23 15 import javax.swing.AbstractAction; … … 30 22 import org.openstreetmap.josm.data.Bounds; 31 23 import org.openstreetmap.josm.data.gpx.GpxData; 32 import org.openstreetmap.josm.data.gpx.ImmutableGpxTrack;33 import org.openstreetmap.josm.data.gpx.WayPoint;34 import org.openstreetmap.josm.data.osm.DataSet;35 import org.openstreetmap.josm.data.osm.Node;36 import org.openstreetmap.josm.data.osm.Way;37 24 import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 38 25 import org.openstreetmap.josm.gui.MapView; … … 41 28 import org.openstreetmap.josm.gui.layer.GpxLayer; 42 29 import org.openstreetmap.josm.gui.layer.Layer; 43 import org.openstreetmap.josm.tools.DateUtils; 30 import org.openstreetmap.josm.plugins.editgpx.data.EditGpxData; 31 import org.openstreetmap.josm.plugins.editgpx.data.EditGpxTrack; 32 import org.openstreetmap.josm.plugins.editgpx.data.EditGpxTrackSegment; 33 import org.openstreetmap.josm.plugins.editgpx.data.EditGpxWayPoint; 44 34 import org.openstreetmap.josm.tools.ImageProvider; 45 35 … … 48 38 49 39 private static Icon icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(EditGpxPlugin.class.getResource("/images/editgpx_layer.png"))); 50 private DataSet dataSet;40 private final EditGpxData data; 51 41 private GPXLayerImportAction layerImport; 52 42 53 public EditGpxLayer(String str, DataSet ds) {43 public EditGpxLayer(String str, EditGpxData gpxData) { 54 44 super(str); 55 data Set = ds;56 layerImport = new GPXLayerImportAction(data Set);45 data = gpxData; 46 layerImport = new GPXLayerImportAction(data); 57 47 } 58 48 … … 63 53 public void initializeImport() { 64 54 try { 65 if(data Set.getNodes().isEmpty()) {55 if(data.isEmpty()) { 66 56 layerImport.activateImport(); 67 57 } … … 117 107 //don't iterate through dataSet whiling making changes 118 108 synchronized(layerImport.importing) { 119 for(Node n: dataSet.getNodes()) { 120 if (!n.isDeleted()) { 121 Point pnt = Main.map.mapView.getPoint(n.getEastNorth()); 122 g.drawOval(pnt.x - 2, pnt.y - 2, 4, 4); 109 for (EditGpxTrack track: data.getTracks()) { 110 for (EditGpxTrackSegment segment: track.getSegments()) { 111 for (EditGpxWayPoint wayPoint: segment.getWayPoints()) { 112 if (!wayPoint.isDeleted()) { 113 Point pnt = Main.map.mapView.getPoint(wayPoint.getCoor().getEastNorth()); 114 g.drawOval(pnt.x - 2, pnt.y - 2, 4, 4); 115 } 116 } 123 117 } 124 118 } … … 145 139 */ 146 140 private GpxData toGpxData(boolean anonTime) { 147 GpxData gpxData = new GpxData(); 148 HashSet<Node> doneNodes = new HashSet<Node>(); 149 //add all ways 150 for (Way w : dataSet.getWays()) { 151 if (w.isIncomplete() || w.isDeleted()) continue; 152 List<Collection<WayPoint>> segments = new ArrayList<Collection<WayPoint>>(); 153 154 List<WayPoint> trkseg = null; 155 for (Node n : w.getNodes()) { 156 if (n.isIncomplete() || n.isDeleted()) { 157 trkseg = null; 158 continue; 159 } 160 161 Date tstamp = n.getTimestamp(); 162 163 if (trkseg == null) { 164 trkseg = new ArrayList<WayPoint>(); 165 segments.add(trkseg); 166 } 167 doneNodes.add(n); 168 169 WayPoint wpt = new WayPoint(n.getCoor()); 170 if (anonTime) { 171 wpt.attr.put("time", "1970-01-01T00:00:00Z"); 172 } else { 173 wpt.attr.put("time", DateUtils.fromDate(tstamp)); 174 } 175 wpt.setTime(); 176 177 trkseg.add(wpt); 178 } 179 180 // Do not create empty segments 181 for (Iterator<Collection<WayPoint>> segIt = segments.iterator(); segIt.hasNext(); ) { 182 if (segIt.next().isEmpty()) { 183 segIt.remove(); 184 } 185 } 186 187 Map<String, Object> trkAttributes = new HashMap<String, Object>(); 188 if (w.get("name") != null) { 189 trkAttributes.put("name", w.get("name")); 190 } 191 if (!segments.isEmpty()) { 192 gpxData.tracks.add(new ImmutableGpxTrack(segments, trkAttributes)); 193 } 194 195 } 196 197 // add nodes as waypoints 198 for (Node n : dataSet.getNodes()) { 199 if (n.isIncomplete() || n.isDeleted() || doneNodes.contains(n)) continue; 200 201 Date tstamp = n.getTimestamp(); 202 203 WayPoint wpt = new WayPoint(n.getCoor()); 204 if (anonTime) { 205 wpt.attr.put("time", "1970-01-01T00:00:00Z"); 206 } else { 207 wpt.attr.put("time", DateUtils.fromDate(tstamp)); 208 } 209 wpt.setTime(); 210 211 if (n.getKeys() != null && n.keySet().contains("name")) { 212 wpt.attr.put("name", n.get("name")); 213 } 214 gpxData.waypoints.add(wpt); 215 } 216 return gpxData; 141 return data.createGpxData(); 217 142 } 218 143 -
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/EditGpxMode.java
r19683 r21472 14 14 import org.openstreetmap.josm.Main; 15 15 import org.openstreetmap.josm.actions.mapmode.MapMode; 16 import org.openstreetmap.josm.data.osm.DataSet;17 import org.openstreetmap.josm.data.osm.Node;18 16 import org.openstreetmap.josm.gui.MapFrame; 17 import org.openstreetmap.josm.plugins.editgpx.data.EditGpxData; 18 import org.openstreetmap.josm.plugins.editgpx.data.EditGpxTrack; 19 import org.openstreetmap.josm.plugins.editgpx.data.EditGpxTrackSegment; 20 import org.openstreetmap.josm.plugins.editgpx.data.EditGpxWayPoint; 19 21 20 22 21 23 public class EditGpxMode extends MapMode { 22 24 23 24 25 DataSet dataSet;26 27 28 25 private static final long serialVersionUID = 7940589057093872411L; 26 Point pointPressed; 27 EditGpxData data; 28 MapFrame mapFrame; 29 Rectangle oldRect; 30 MapFrame frame; 29 31 30 public EditGpxMode(MapFrame mapFrame, String name, String desc, DataSet ds) {31 32 dataSet = ds;33 32 public EditGpxMode(MapFrame mapFrame, String name, String desc, EditGpxData gpxData) { 33 super(name, "editgpx_mode.png", desc, mapFrame, Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); 34 this.data = gpxData; 35 } 34 36 35 36 37 38 39 37 @Override public void enterMode() { 38 super.enterMode(); 39 Main.map.mapView.addMouseListener(this); 40 Main.map.mapView.addMouseMotionListener(this); 41 } 40 42 41 42 43 44 45 43 @Override public void exitMode() { 44 super.exitMode(); 45 Main.map.mapView.removeMouseListener(this); 46 Main.map.mapView.removeMouseMotionListener(this); 47 } 46 48 47 49 48 49 50 50 @Override public void mousePressed(MouseEvent e) { 51 pointPressed = new Point(e.getPoint()); 52 } 51 53 52 54 53 54 55 56 57 58 55 @Override public void mouseDragged(MouseEvent e) { 56 if ( (e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) == InputEvent.BUTTON1_DOWN_MASK) { 57 //if button1 is hold, draw the rectangle. 58 paintRect(pointPressed, e.getPoint()); 59 } 60 } 59 61 60 61 62 63 64 62 @Override public void mouseReleased(MouseEvent e) { 63 if (e.getButton() != MouseEvent.BUTTON1) { 64 return; 65 } else { 66 Point pointReleased = e.getPoint(); 65 67 66 68 Rectangle r = createRect(pointReleased, pointPressed); 67 69 68 //go through nodes and mark the ones in the selection rect as deleted 69 for (Node n: dataSet.getNodes()) { 70 Point p = Main.map.mapView.getPoint(n); 71 if (r.contains(p)) { 72 n.setDeleted(true); 73 } 74 } 75 oldRect = null; 76 Main.map.mapView.repaint(); 77 } 78 } 70 //go through nodes and mark the ones in the selection rect as deleted 71 for (EditGpxTrack track: data.getTracks()) { 72 for (EditGpxTrackSegment segment: track.getSegments()) { 73 for (EditGpxWayPoint wayPoint: segment.getWayPoints()) { 74 Point p = Main.map.mapView.getPoint(wayPoint.getCoor().getEastNorth()); 75 if (r.contains(p)) { 76 wayPoint.setDeleted(true); 77 } 78 } 79 } 80 } 81 oldRect = null; 82 Main.map.mapView.repaint(); 83 } 84 } 79 85 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 86 /** 87 * create rectangle out of two given corners 88 */ 89 public Rectangle createRect(Point p1, Point p2) { 90 int x,y,w,h; 91 if (p1.x == p2.x && p1.y == p2.y) { 92 //if p1 and p2 same points draw a small rectangle around them 93 x = p1.x -1; 94 y = p1.y -1; 95 w = 3; 96 h = 3; 97 } else { 98 if (p1.x < p2.x){ 99 x = p1.x; 100 w = p2.x-p1.x; 101 } else { 102 x = p2.x; 103 w = p1.x-p2.x; 104 } 105 if (p1.y < p2.y) { 106 y = p1.y; 107 h = p2.y-p1.y; 108 } else { 109 y = p2.y; 110 h = p1.y-p2.y; 111 } 112 } 113 return new Rectangle(x,y,w,h); 114 } 109 115 110 111 112 113 114 116 /** 117 * Draw a selection rectangle on screen. 118 */ 119 private void paintRect(Point p1, Point p2) { 120 Graphics g = frame.getGraphics();//Main.map.mapView.getGraphics(); 115 121 116 117 118 119 120 121 122 122 Rectangle r = oldRect; 123 if (r != null) { 124 //overwrite old rct 125 g.setXORMode(Color.BLACK); 126 g.setColor(Color.WHITE); 127 g.drawRect(r.x,r.y,r.width,r.height); 128 } 123 129 124 125 126 127 128 129 130 g.setXORMode(Color.BLACK); 131 g.setColor(Color.WHITE); 132 r = createRect(p1,p2); 133 g.drawRect(r.x,r.y,r.width,r.height); 134 oldRect = r; 135 } 130 136 131 137 132 133 134 138 public void setFrame(MapFrame mapFrame) { 139 frame = mapFrame; 140 } 135 141 } -
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/EditGpxPlugin.java
r19434 r21472 3 3 */ 4 4 package org.openstreetmap.josm.plugins.editgpx; 5 6 import static org.openstreetmap.josm.tools.I18n.tr; 5 7 6 8 import java.awt.event.ActionEvent; … … 11 13 12 14 import org.openstreetmap.josm.Main; 13 import org.openstreetmap.josm.data.osm.DataSet;14 15 import org.openstreetmap.josm.gui.IconToggleButton; 15 16 import org.openstreetmap.josm.gui.MapFrame; … … 19 20 import org.openstreetmap.josm.plugins.Plugin; 20 21 import org.openstreetmap.josm.plugins.PluginInformation; 21 22 import static org.openstreetmap.josm.tools.I18n.tr; 22 import org.openstreetmap.josm.plugins.editgpx.data.EditGpxData; 23 23 24 24 /** … … 38 38 public class EditGpxPlugin extends Plugin { 39 39 40 41 42 43 protected static DataSet dataSet;44 40 private IconToggleButton btn; 41 private EditGpxMode mode; 42 protected static EditGpxLayer eGpxLayer; 43 protected static EditGpxData gpxData; 44 public static boolean active = false; 45 45 46 47 48 dataSet = new DataSet();49 mode = new EditGpxMode(Main.map, "editgpx", tr("edit gpx tracks"), dataSet);46 public EditGpxPlugin(PluginInformation info) { 47 super(info); 48 gpxData = new EditGpxData(); 49 mode = new EditGpxMode(Main.map, "editgpx", tr("edit gpx tracks"), gpxData); 50 50 51 52 53 51 btn = new IconToggleButton(mode); 52 btn.setVisible(true); 53 } 54 54 55 56 57 58 59 60 61 55 /** 56 * initialize button. if button is pressed create new layer. 57 */ 58 @Override 59 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 60 if(oldFrame == null && newFrame != null) { 61 mode.setFrame(newFrame); 62 62 63 64 63 if(Main.map != null) 64 Main.map.addMapMode(btn); 65 65 66 66 active = btn.isSelected(); 67 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 68 btn.addActionListener(new ActionListener() { 69 public void actionPerformed(ActionEvent e) { 70 active = btn.isSelected(); 71 if (active) { 72 Main.worker.execute(new Runnable() { 73 public void run() { 74 updateLayer(); 75 } 76 }); 77 } 78 } 79 }); 80 } 81 } 82 82 83 84 85 86 87 88 eGpxLayer = new EditGpxLayer(tr("EditGpx"), dataSet);89 90 83 /** 84 * create new layer, add listeners and try importing gpx data. 85 */ 86 private void updateLayer() { 87 if(eGpxLayer == null) { 88 eGpxLayer = new EditGpxLayer(tr("EditGpx"), gpxData); 89 Main.main.addLayer(eGpxLayer); 90 MapView.addLayerChangeListener(new LayerChangeListener(){ 91 91 92 93 94 95 92 public void activeLayerChange(final Layer oldLayer, final Layer newLayer) { 93 if(newLayer instanceof EditGpxLayer) 94 EditGpxPlugin.eGpxLayer = (EditGpxLayer)newLayer; 95 } 96 96 97 98 97 public void layerAdded(final Layer newLayer) { 98 } 99 99 100 101 102 103 104 105 106 107 100 public void layerRemoved(final Layer oldLayer) { 101 if(oldLayer == eGpxLayer) { 102 eGpxLayer = null; 103 //dataSet = new DataSet(); 104 MapView.removeLayerChangeListener(this); 105 } 106 } 107 }); 108 108 109 110 111 112 109 eGpxLayer.initializeImport(); 110 } 111 Main.map.mapView.repaint(); 112 } 113 113 114 115 116 117 114 public static ImageIcon loadIcon(String name) { 115 URL url = EditGpxPlugin.class.getResource("/images/editgpx.png"); 116 return new ImageIcon(url); 117 } 118 118 } -
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/GPXLayerImportAction.java
r19683 r21472 25 25 26 26 import org.openstreetmap.josm.Main; 27 import org.openstreetmap.josm.data.gpx.GpxTrack;28 import org.openstreetmap.josm.data.gpx.GpxTrackSegment;29 import org.openstreetmap.josm.data.gpx.WayPoint;30 import org.openstreetmap.josm.data.osm.DataSet;31 import org.openstreetmap.josm.data.osm.Node;32 import org.openstreetmap.josm.data.osm.Way;33 27 import org.openstreetmap.josm.gui.layer.GpxLayer; 34 28 import org.openstreetmap.josm.gui.layer.Layer; 35 import org.openstreetmap.josm. tools.DateUtils;29 import org.openstreetmap.josm.plugins.editgpx.data.EditGpxData; 36 30 import org.openstreetmap.josm.tools.ImageProvider; 37 31 … … 44 38 45 39 46 47 private DataSet dataSet;48 40 private static final long serialVersionUID = 5794897888911798168L; 41 private EditGpxData data; 42 public Object importing = new Object(); //used for synchronization 49 43 50 public GPXLayerImportAction(DataSet ds) {51 52 53 this.dataSet = ds;54 44 public GPXLayerImportAction(EditGpxData data) { 45 //TODO what is icon at the end? 46 super(tr("Import path from GPX layer"), ImageProvider.get("dialogs", "edit")); 47 this.data = data; 48 } 55 49 56 57 58 59 60 61 62 50 /** 51 * shows a list of GPX layers. if user selects one the data from this layer is 52 * imported. 53 */ 54 public void activateImport() { 55 Box panel = Box.createVerticalBox(); 56 DefaultListModel dModel= new DefaultListModel(); 63 57 64 65 66 67 58 final JList layerList = new JList(dModel); 59 Collection<Layer> data = Main.map.mapView.getAllLayers(); 60 Layer lastLayer = null; 61 int layerCnt = 0; 68 62 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 63 for (Layer l : data){ 64 if(l instanceof GpxLayer){ 65 dModel.addElement(l); 66 lastLayer = l; 67 layerCnt++; 68 } 69 } 70 if(layerCnt == 1){ 71 layerList.setSelectedValue(lastLayer, true); 72 } 73 if(layerCnt > 0){ 74 layerList.setCellRenderer(new DefaultListCellRenderer(){ 75 @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 76 Layer layer = (Layer)value; 77 JLabel label = (JLabel)super.getListCellRendererComponent(list, 78 layer.getName(), index, isSelected, cellHasFocus); 79 Icon icon = layer.getIcon(); 80 label.setIcon(icon); 81 label.setToolTipText(layer.getToolTipText()); 82 return label; 83 } 84 }); 91 85 92 86 JCheckBox dropFirst = new JCheckBox(tr("Drop existing path")); 93 87 94 95 88 panel.add(layerList); 89 panel.add(dropFirst); 96 90 97 98 99 100 101 102 103 91 final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){ 92 @Override public void selectInitialValue() { 93 layerList.requestFocusInWindow(); 94 } 95 }; 96 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Import path from GPX layer")); 97 dlg.setVisible(true); 104 98 105 106 107 108 109 99 Object answer = optionPane.getValue(); 100 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE || 101 (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION)) { 102 return; 103 } 110 104 111 105 GpxLayer gpx = (GpxLayer)layerList.getSelectedValue(); 112 106 113 synchronized(importing) { 114 for (GpxTrack trk : gpx.data.tracks) { 115 for (GpxTrackSegment segment : trk.getSegments()) { 116 Way w = new Way(); 117 for (WayPoint p : segment.getWayPoints()) { 118 Node n = new Node(p.getCoor()); 119 String timestr = p.getString("time"); 120 if(timestr != null) 121 n.setTimestamp(DateUtils.fromString(timestr)); 122 dataSet.addPrimitive(n); 123 w.addNode(n); //TODO what to do with these while deletion 124 } 125 dataSet.addPrimitive(w); 126 } 127 } 128 } 129 Main.map.mapView.repaint(); 107 synchronized(importing) { 108 this.data.load(gpx.data); 109 } 110 Main.map.mapView.repaint(); 130 111 131 132 133 134 135 112 } else { 113 // no gps layer 114 JOptionPane.showMessageDialog(Main.parent,tr("No GPX data layer found.")); 115 } 116 } 136 117 137 138 139 140 141 142 143 118 /** 119 * called when pressing "Import.." from context menu of EditGpx layer 120 * 121 */ 122 public void actionPerformed(ActionEvent arg0) { 123 activateImport(); 124 } 144 125 }
Note:
See TracChangeset
for help on using the changeset viewer.