Changeset 14245 in osm for applications/editors/josm/plugins/editgpx/src/org/openstreetmap
- Timestamp:
- 2009-03-24T09:19:10+01:00 (16 years ago)
- Location:
- applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/EditGpxLayer.java
r14221 r14245 11 11 import java.awt.event.ActionEvent; 12 12 import java.util.ArrayList; 13 import java.util.Date; 13 14 import java.util.HashSet; 14 15 … … 40 41 public class EditGpxLayer extends Layer { 41 42 42 private static Icon icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(EditGpxPlugin.class.getResource("/images/editgpx_layer.png"))); 43 private DataSet dataSet; 44 private GPXLayerImportAction layerImport; 45 46 public EditGpxLayer(String str, DataSet ds) { 47 super(str); 48 dataSet = ds; 49 layerImport = new GPXLayerImportAction(dataSet); 50 } 51 52 /** 53 * check if dataSet is empty 54 * if so show import dialog to user 55 */ 56 public void initializeImport() { 57 try { 58 if(dataSet.nodes.isEmpty() ) { 59 layerImport.activateImport(); 60 } 61 } catch (Exception e) { 62 System.out.println(e.getMessage()); 63 e.printStackTrace(); 64 } 65 } 66 67 68 @Override 69 public Icon getIcon() { 70 return icon; 71 } 72 73 74 @Override 75 public Object getInfoComponent() { 76 return getToolTipText(); 77 } 78 79 80 @Override 81 public Component[] getMenuEntries() { 82 return new Component[] { 83 new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)), 84 new JMenuItem(new LayerListDialog.DeleteLayerAction(this)), 85 new JSeparator(), 86 new JMenuItem(layerImport), 87 new JMenuItem(new ConvertToGpxLayerAction()), 88 new JMenuItem(new ConvertToAnonTimeGpxLayerAction()), 89 new JSeparator(), 90 new JMenuItem(new LayerListPopup.InfoAction(this))}; 91 } 92 93 94 @Override 95 public String getToolTipText() { 96 return tr("Layer for editing GPX tracks"); 97 } 98 99 100 @Override 101 public boolean isMergable(Layer other) { 102 // TODO 103 return false; 104 } 105 106 107 @Override 108 public void mergeFrom(Layer from) { 109 // TODO 110 } 111 112 113 @Override 114 public void paint(Graphics g, MapView mv) { 115 g.setColor(Color.yellow); 116 117 //don't iterate through dataSet whiling making changes 118 synchronized(layerImport.importing) { 119 for(Node n: dataSet.nodes) { 120 if (!n.deleted) { 121 LatLon c = n.coor; 122 Point pnt = Main.map.mapView.getPoint(Main.proj.latlon2eastNorth(c)); 123 g.drawOval(pnt.x - 2, pnt.y - 2, 4, 4); 124 } 125 } 126 } 127 } 128 43 private static Icon icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(EditGpxPlugin.class.getResource("/images/editgpx_layer.png"))); 44 private DataSet dataSet; 45 private GPXLayerImportAction layerImport; 46 47 public EditGpxLayer(String str, DataSet ds) { 48 super(str); 49 dataSet = ds; 50 layerImport = new GPXLayerImportAction(dataSet); 51 } 52 53 /** 54 * check if dataSet is empty 55 * if so show import dialog to user 56 */ 57 public void initializeImport() { 58 try { 59 if(dataSet.nodes.isEmpty() ) { 60 layerImport.activateImport(); 61 } 62 } catch (Exception e) { 63 System.out.println(e.getMessage()); 64 e.printStackTrace(); 65 } 66 } 67 68 @Override 69 public Icon getIcon() { 70 return icon; 71 } 72 73 @Override 74 public Object getInfoComponent() { 75 return getToolTipText(); 76 } 77 78 @Override 79 public Component[] getMenuEntries() { 80 return new Component[] { 81 new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)), 82 new JMenuItem(new LayerListDialog.DeleteLayerAction(this)), 83 new JSeparator(), 84 new JMenuItem(layerImport), 85 new JMenuItem(new ConvertToGpxLayerAction()), 86 new JMenuItem(new ConvertToAnonTimeGpxLayerAction()), 87 new JSeparator(), 88 new JMenuItem(new LayerListPopup.InfoAction(this))}; 89 } 90 91 @Override 92 public String getToolTipText() { 93 return tr("Layer for editing GPX tracks"); 94 } 95 96 @Override 97 public boolean isMergable(Layer other) { 98 // TODO 99 return false; 100 } 101 102 @Override 103 public void mergeFrom(Layer from) { 104 // TODO 105 } 106 107 @Override 108 public void paint(Graphics g, MapView mv) { 109 g.setColor(Color.yellow); 110 111 //don't iterate through dataSet whiling making changes 112 synchronized(layerImport.importing) { 113 for(Node n: dataSet.nodes) { 114 if (!n.deleted) { 115 LatLon c = n.coor; 116 Point pnt = Main.map.mapView.getPoint(Main.proj.latlon2eastNorth(c)); 117 g.drawOval(pnt.x - 2, pnt.y - 2, 4, 4); 118 } 119 } 120 } 121 } 122 129 123 130 124 public void reset(){ 131 125 //TODO implement a reset 132 126 } 133 134 135 136 137 138 139 140 141 142 143 * 144 145 146 147 127 128 129 @Override 130 public void visitBoundingBox(BoundingXYVisitor v) { 131 // TODO Auto-generated method stub 132 } 133 134 135 /** 136 * convert a DataSet to GPX 137 * 138 * @param boolean anonTime If true set all time and date in GPX to 01/01/1970 00:00 ? 139 * @return GPXData 140 */ 141 private GpxData toGpxData(boolean anonTime) { 148 142 GpxData gpxData = new GpxData(); 149 143 HashSet<Node> doneNodes = new HashSet<Node>(); … … 163 157 continue; 164 158 } 159 160 Date tstamp = n.getTimestamp(); 161 165 162 if (trkseg == null) { 166 163 trkseg = new ArrayList<WayPoint>(); 167 164 trk.trackSegs.add(trkseg); 168 165 } 169 if (!n.isTagged()) { 170 doneNodes.add(n); 171 } 166 doneNodes.add(n); 167 172 168 WayPoint wpt = new WayPoint(n.coor); 173 if (!n.isTimestampEmpty()) 174 { 175 if (anonTime) { 176 wpt.attr.put("time", "1970-01-01T00:00:00"); 177 } else { 178 wpt.attr.put("time", n.getTimestamp()); 179 System.out.println("timestamp: "+n.getTimestamp()); 180 } 181 wpt.setTime(); 182 } 169 if (anonTime) { 170 wpt.attr.put("time", "1970-01-01T00:00:00"); 171 } else { 172 wpt.attr.put("time", tstamp); 173 System.out.println("timestamp: "+tstamp); 174 } 175 wpt.setTime(); 176 183 177 trkseg.add(wpt); 184 178 } … … 188 182 for (Node n : dataSet.nodes) { 189 183 if (n.incomplete || n.deleted || doneNodes.contains(n)) continue; 184 185 Date tstamp = n.getTimestamp(); 186 190 187 WayPoint wpt = new WayPoint(n.coor); 191 if (!n.isTimestampEmpty()) { 192 if (anonTime) { 193 wpt.attr.put("time", "1970-01-01T00:00:00"); 194 } else { 195 wpt.attr.put("time", n.getTimestamp()); 196 } 197 wpt.setTime(); 198 } 188 if (anonTime) { 189 wpt.attr.put("time", "1970-01-01T00:00:00"); 190 } else { 191 wpt.attr.put("time", tstamp); 192 } 193 wpt.setTime(); 194 199 195 if (n.keys != null && n.keys.containsKey("name")) { 200 196 wpt.attr.put("name", n.keys.get("name")); … … 204 200 return gpxData; 205 201 } 206 207 208 202 203 //context item "Convert to GPX layer" 204 public class ConvertToGpxLayerAction extends AbstractAction { 209 205 public ConvertToGpxLayerAction() { 210 206 super(tr("Convert to GPX layer"), ImageProvider.get("converttogpx")); … … 216 212 } 217 213 218 219 214 //context item "Convert to GPX layer with anonymised time" 215 public class ConvertToAnonTimeGpxLayerAction extends AbstractAction { 220 216 public ConvertToAnonTimeGpxLayerAction() { 221 217 super(tr("Convert to GPX layer with anonymised time"), ImageProvider.get("converttogpx")); -
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/EditGpxMode.java
r14221 r14245 21 21 public class EditGpxMode extends MapMode { 22 22 23 24 25 26 27 28 29 30 31 32 33 34 35 23 private static final long serialVersionUID = 7940589057093872411L; 24 Point pointPressed; 25 DataSet dataSet; 26 MapFrame mapFrame; 27 Rectangle oldRect; 28 MapFrame frame; 29 30 public EditGpxMode(MapFrame mapFrame, String name, String desc, DataSet ds) { 31 super(name, "editgpx_mode.png", desc, mapFrame, Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); 32 dataSet = ds; 33 } 34 35 @Override public void enterMode() { 36 36 super.enterMode(); 37 37 Main.map.mapView.addMouseListener(this); … … 44 44 Main.map.mapView.removeMouseMotionListener(this); 45 45 } 46 47 46 47 48 48 @Override public void mousePressed(MouseEvent e) { 49 49 pointPressed = new Point(e.getPoint()); 50 50 } 51 52 51 52 53 53 @Override public void mouseDragged(MouseEvent e) { 54 55 56 57 54 if ( (e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == MouseEvent.BUTTON1_DOWN_MASK) { 55 //if button1 is hold, draw the rectangle. 56 paintRect(pointPressed, e.getPoint()); 57 } 58 58 } 59 60 59 61 60 @Override public void mouseReleased(MouseEvent e) { 62 61 if (e.getButton() != MouseEvent.BUTTON1) { 63 62 return; 64 63 } else { 65 66 67 64 Point pointReleased = e.getPoint(); 65 66 //prepare vars 68 67 EastNorth en; 69 70 71 72 73 74 75 76 77 68 double scale = Main.map.mapView.getScale(); 69 EastNorth center = Main.map.mapView.getCenter(); //Center n/e coordinate of the desired screen center. 70 int width = Main.map.mapView.getWidth(); 71 int height = Main.map.mapView.getHeight(); 72 Rectangle r = createRect(pointReleased, pointPressed); 73 74 //go through nodes and mark the ones in the selection rect as deleted 75 for (Node n: dataSet.nodes) { 76 en = n.eastNorth; 78 77 //calculate point on screen from node n 79 78 double x = (en.east()-center.east())/scale + width/2; 80 79 double y = (center.north()-en.north())/scale + height/2; 81 80 Point p = new Point((int)x,(int)y); 82 81 if (r.contains(p)) { 83 82 n.deleted = true; //only set as deleted. this makes reset to beginning possible 84 83 } 85 84 } 86 oldRect = null; 87 Main.map.mapView.repaint(); 88 85 oldRect = null; 86 Main.map.mapView.repaint(); 89 87 } 90 91 88 } 92 89 … … 95 92 */ 96 93 public Rectangle createRect(Point p1, Point p2) { 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 94 int x,y,w,h; 95 if (p1.x == p2.x && p1.y == p2.y) { 96 //if p1 and p2 same points draw a small rectangle around them 97 x = p1.x -1; 98 y = p1.y -1; 99 w = 3; 100 h = 3; 101 } else { 102 if (p1.x < p2.x){ 103 x = p1.x; 104 w = p2.x-p1.x; 105 } else { 106 x = p2.x; 107 w = p1.x-p2.x; 108 } 109 if (p1.y < p2.y) { 110 y = p1.y; 111 h = p2.y-p1.y; 112 } else { 113 y = p2.y; 114 h = p1.y-p2.y; 115 } 116 } 117 return new Rectangle(x,y,w,h); 121 118 } 122 123 119 124 120 /** 125 121 * Draw a selection rectangle on screen. 126 122 */ 127 123 private void paintRect(Point p1, Point p2) { 128 129 130 131 132 133 134 135 136 137 124 Graphics g = frame.getGraphics();//Main.map.mapView.getGraphics(); 125 126 Rectangle r = oldRect; 127 if (r != null) { 128 //overwrite old rct 129 g.setXORMode(Color.BLACK); 130 g.setColor(Color.WHITE); 131 g.drawRect(r.x,r.y,r.width,r.height); 132 } 133 138 134 g.setXORMode(Color.BLACK); 139 g.setColor(Color.WHITE);135 g.setColor(Color.WHITE); 140 136 r = createRect(p1,p2); 141 137 g.drawRect(r.x,r.y,r.width,r.height); 142 138 oldRect = r; 143 144 139 } 145 140 146 141 147 142 public void setFrame(MapFrame mapFrame) { 148 143 frame = mapFrame; 149 144 } 150 151 152 145 } -
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/EditGpxPlugin.java
r14221 r14245 21 21 22 22 /** 23 * Provides an editable GPX layer. Editable layer here means the deletion of points is supported. 23 * Provides an editable GPX layer. Editable layer here means the deletion of points is supported. 24 24 * This plugin can be used to prepare tracks for upload to OSM eg. delete uninteresting parts 25 * of the track. 26 * Additionally while converting the track back to a normal GPX layer the time can be made 27 * anonymous. This feature sets all time stamps to 1970-01-01 00:00. 28 * 25 * of the track. 26 * Additionally while converting the track back to a normal GPX layer the time can be made 27 * anonymous. This feature sets all time stamps to 1970-01-01 00:00. 28 * 29 29 * TODO: 30 * - TODO s durchschauen31 * - write docu32 * - upload beta of plugin33 30 * - BUG: when importing eGpxLayer is shown as RawGpxLayer?? 34 31 * - BUG: after deletion of layer not all data is deleted (eg dataset) 35 32 * - implement reset if user made mistake while marking 36 * 37 * 33 * 34 * 38 35 */ 39 36 public class EditGpxPlugin extends Plugin { 40 37 41 42 43 44 45 46 47 48 49 50 38 private IconToggleButton btn; 39 private EditGpxMode mode; 40 protected static EditGpxLayer eGpxLayer; 41 protected static DataSet dataSet; 42 public static boolean active = false; 43 44 public EditGpxPlugin() { 45 dataSet = new DataSet(); 46 mode = new EditGpxMode(Main.map, "editgpx", tr("edit gpx tracks"), dataSet); 47 51 48 btn = new IconToggleButton(mode); 52 49 btn.setVisible(true); 53 } 54 55 56 /** 57 * initialize button. if button is pressed create new layer. 58 */ 59 @Override 50 } 51 52 /** 53 * initialize button. if button is pressed create new layer. 54 */ 55 @Override 60 56 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 61 57 if(oldFrame == null && newFrame != null) { 62 58 mode.setFrame(newFrame); 63 64 if(Main.map != null) 65 Main.map.addMapMode(btn); 66 67 active = btn.isSelected(); 68 69 btn.addActionListener(new ActionListener() { 70 public void actionPerformed(ActionEvent e) { 71 active = btn.isSelected(); 72 if (active) { 73 Main.worker.execute(new Runnable() { 74 public void run() { 75 updateLayer(); 76 } 77 }); 78 } 79 } 80 }); 81 } 82 } 83 84 /** 85 * create new layer, add listeners and try importing gpx data. 86 */ 87 private void updateLayer() { 88 if(eGpxLayer == null) { 89 eGpxLayer = new EditGpxLayer(tr("EditGpx"), dataSet); 90 Main.main.addLayer(eGpxLayer); 91 Layer.listeners.add(new LayerChangeListener(){ 92 93 public void activeLayerChange(final Layer oldLayer, final Layer newLayer) { 94 if(newLayer instanceof EditGpxLayer) 95 EditGpxPlugin.eGpxLayer = (EditGpxLayer)newLayer; 96 } 97 98 public void layerAdded(final Layer newLayer) { 99 } 100 101 public void layerRemoved(final Layer oldLayer) { 102 if(oldLayer == eGpxLayer) { 103 eGpxLayer = null; 104 //dataSet = new DataSet(); 105 59 60 if(Main.map != null) 61 Main.map.addMapMode(btn); 62 63 active = btn.isSelected(); 64 65 btn.addActionListener(new ActionListener() { 66 public void actionPerformed(ActionEvent e) { 67 active = btn.isSelected(); 68 if (active) { 69 Main.worker.execute(new Runnable() { 70 public void run() { 71 updateLayer(); 72 } 73 }); 106 74 } 107 75 } 108 76 }); 109 77 } 78 } 79 80 /** 81 * create new layer, add listeners and try importing gpx data. 82 */ 83 private void updateLayer() { 84 if(eGpxLayer == null) { 85 eGpxLayer = new EditGpxLayer(tr("EditGpx"), dataSet); 86 Main.main.addLayer(eGpxLayer); 87 Layer.listeners.add(new LayerChangeListener(){ 88 89 public void activeLayerChange(final Layer oldLayer, final Layer newLayer) { 90 if(newLayer instanceof EditGpxLayer) 91 EditGpxPlugin.eGpxLayer = (EditGpxLayer)newLayer; 92 } 93 94 public void layerAdded(final Layer newLayer) { 95 } 96 97 public void layerRemoved(final Layer oldLayer) { 98 if(oldLayer == eGpxLayer) { 99 eGpxLayer = null; 100 //dataSet = new DataSet(); 101 102 } 103 } 104 }); 105 110 106 eGpxLayer.initializeImport(); 111 } 107 } 112 108 Main.map.mapView.repaint(); 113 109 } 114 115 110 116 111 public static ImageIcon loadIcon(String name) { 117 112 URL url = EditGpxPlugin.class.getResource("/images/editgpx.png"); 118 113 return new ImageIcon(url); 119 114 } 120 121 115 } -
applications/editors/josm/plugins/editgpx/src/org/openstreetmap/josm/plugins/editgpx/GPXLayerImportAction.java
r14221 r14245 3 3 * 4 4 * other source from mesurement plugin written by Raphael Mack 5 * 5 * 6 6 */ 7 7 package org.openstreetmap.josm.plugins.editgpx; … … 37 37 /** 38 38 * Import GPX data from available layers 39 * 39 * 40 40 * 41 41 */ 42 42 class GPXLayerImportAction extends AbstractAction { 43 44 45 46 47 48 49 50 51 43 44 45 private static final long serialVersionUID = 5794897888911798168L; 46 private DataSet dataSet; 47 public Object importing = new Object(); //used for synchronization 48 49 public GPXLayerImportAction(DataSet ds) { 50 //TODO what is icon at the end? 51 super(tr("Import path from GPX layer"), ImageProvider.get("dialogs", "edit")); 52 52 this.dataSet = ds; 53 } 54 55 /** 56 * shows a list of GPX layers. if user selects one the data from this layer is 57 * imported. 58 */ 59 public void activateImport() { 60 Box panel = Box.createVerticalBox(); 61 DefaultListModel dModel= new DefaultListModel(); 53 } 62 54 63 final JList layerList = new JList(dModel); 55 /** 56 * shows a list of GPX layers. if user selects one the data from this layer is 57 * imported. 58 */ 59 public void activateImport() { 60 Box panel = Box.createVerticalBox(); 61 DefaultListModel dModel= new DefaultListModel(); 62 63 final JList layerList = new JList(dModel); 64 64 Collection<Layer> data = Main.map.mapView.getAllLayers(); 65 65 Layer lastLayer = null; … … 111 111 112 112 synchronized(importing) { 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 113 for (GpxTrack trk : gpx.data.tracks) { 114 for (Collection<WayPoint> segment : trk.trackSegs) { 115 Way w = new Way(); 116 for (WayPoint p : segment) { 117 Node n = new Node(p.latlon); 118 String timestr = p.getString("time"); 119 if(timestr != null) 120 { 121 timestr = timestr.replace("Z","+00:00"); 122 n.setTimestamp(DateUtils.fromString(timestr)); 123 } 124 dataSet.nodes.add(n); 125 w.nodes.add(n); //TODO what to do with these while deletion 126 } 127 dataSet.ways.add(w); 128 } 129 } 130 130 } 131 131 Main.map.mapView.repaint(); 132 132 133 } else { 133 } else { 134 134 // no gps layer 135 135 JOptionPane.showMessageDialog(Main.parent,tr("No GPX data layer found.")); 136 136 } 137 138 } 137 } 139 138 140 /** 141 * called when pressing "Import.." from context menu of EditGpx layer 142 * 143 */ 144 public void actionPerformed(ActionEvent arg0) { 145 activateImport(); 146 } 147 148 139 /** 140 * called when pressing "Import.." from context menu of EditGpx layer 141 * 142 */ 143 public void actionPerformed(ActionEvent arg0) { 144 activateImport(); 145 } 149 146 }
Note:
See TracChangeset
for help on using the changeset viewer.