Changeset 12778 in osm for applications/editors/josm/plugins/measurement/src
- Timestamp:
- 2009-01-01T18:28:53+01:00 (16 years ago)
- Location:
- applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementDialog.java
r12471 r12778 34 34 public class MeasurementDialog extends ToggleDialog implements ActionListener 35 35 { 36 36 private static final long serialVersionUID = 4708541586297950021L; 37 37 38 /** 38 /** 39 39 * The reset button 40 40 */ … … 64 64 * Constructor 65 65 */ 66 public MeasurementDialog() 66 public MeasurementDialog() 67 67 { 68 68 super(tr("Measured values"), "measure", tr("Open the measurement window."), … … 78 78 79 79 JPanel valuePanel = new JPanel(new GridLayout(0,2)); 80 80 81 81 valuePanel.add(new JLabel(tr("Path Length"))); 82 82 83 83 pathLengthLabel = new JLabel("0 m"); 84 84 valuePanel.add(pathLengthLabel); 85 85 86 86 valuePanel.add(new JLabel(tr("Selection Length"))); 87 87 88 88 selectLengthLabel = new JLabel("0 m"); 89 89 valuePanel.add(selectLengthLabel); 90 90 91 91 valuePanel.add(new JLabel(tr("Selection Area"))); 92 92 93 93 selectAreaLabel = new JLabel("0 m\u00b2"); 94 94 valuePanel.add(selectAreaLabel); 95 95 96 96 JLabel angle = new JLabel(tr("Angle")); 97 97 angle.setToolTipText(tr("Angle between two selected Nodes")); 98 98 valuePanel.add(angle); 99 99 100 100 segAngleLabel = new JLabel("- \u00b0"); 101 101 valuePanel.add(segAngleLabel); 102 102 103 103 add(valuePanel, BorderLayout.CENTER); 104 104 105 105 this.setPreferredSize(new Dimension(0, 92)); 106 106 final MeasurementDialog dlg = this; 107 //TODO: is this enough? 107 //TODO: is this enough? 108 108 109 109 Main.ds.selListeners.add(new SelectionChangedListener(){ 110 110 111 112 113 111 public void selectionChanged(Collection<? extends OsmPrimitive> arg0) { 112 double length = 0.0; 113 double segAngle = 0.0; 114 114 double area = 0.0; 115 115 Node lastNode = null; 116 116 for(OsmPrimitive p:arg0){ 117 117 if(p instanceof Node){ 118 118 Node n =(Node)p; … … 132 132 //http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/ 133 133 area += (MeasurementLayer.calcX(n.coor) * MeasurementLayer.calcY(lastN.coor)) 134 134 - (MeasurementLayer.calcY(n.coor) * MeasurementLayer.calcX(lastN.coor)); 135 135 } 136 136 lastN = n; … … 142 142 } 143 143 } 144 145 144 } 145 dlg.selectLengthLabel.setText(new DecimalFormat("#0.00").format(length) + " m"); 146 146 147 dlg.segAngleLabel.setText(new DecimalFormat("#0.0").format(segAngle) + " \u00b0"); 148 149 150 151 147 dlg.segAngleLabel.setText(new DecimalFormat("#0.0").format(segAngle) + " \u00b0"); 148 dlg.selectAreaLabel.setText(new DecimalFormat("#0.00").format(area) + " m\u00b2"); 149 } 150 }); 151 } 152 152 153 public void actionPerformed(ActionEvent e) 154 155 156 157 158 159 160 161 162 163 164 165 166 167 153 public void actionPerformed(ActionEvent e) 154 { 155 String actionCommand = e.getActionCommand(); 156 if( actionCommand.equals("Reset")){ 157 resetValues(); 158 } 159 } 160 161 /** 162 * Cleans the active Meausurement Layer 163 */ 164 public void resetValues(){ 165 MeasurementPlugin.getCurrentLayer().reset(); 166 } 167 168 168 } -
applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementLayer.java
r12473 r12778 1 1 package org.openstreetmap.josm.plugins.measurement; 2 /// @author Raphael Mack <ramack@raphael-mack.de> 2 /// @author Raphael Mack <ramack@raphael-mack.de> 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 … … 53 53 */ 54 54 public class MeasurementLayer extends Layer { 55 55 56 56 public MeasurementLayer(String arg0) { 57 57 super(arg0); 58 58 } 59 59 60 private static Icon icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(MeasurementPlugin.class.getResource("/images/measurement.png"))); 60 private static Icon icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(MeasurementPlugin.class.getResource("/images/measurement.png"))); 61 61 private Collection<WayPoint> points = new ArrayList<WayPoint>(32); 62 62 63 63 @Override public Icon getIcon() { 64 64 return icon; 65 65 } 66 66 67 67 @Override public String getToolTipText() { 68 68 return tr("Layer to make measurements"); … … 76 76 @Override public void mergeFrom(Layer from) { 77 77 // TODO: nyi - doubts about how this should be done are around. Ideas? 78 78 79 79 } 80 80 … … 110 110 new JMenuItem(new LayerListPopup.InfoAction(this))}; 111 111 } 112 112 113 113 public void removeLastPoint(){ 114 114 WayPoint l = null; … … 116 116 if(l != null) points.remove(l); 117 117 recalculate(); 118 Main.map.repaint(); 119 } 120 121 public void mouseClicked(MouseEvent e){ 118 Main.map.repaint(); 119 } 120 121 public void mouseClicked(MouseEvent e){ 122 122 if (e.getButton() != MouseEvent.BUTTON1) return; 123 123 124 LatLon coor = Main.map.mapView.getLatLon(e.getX(), e.getY()); 124 LatLon coor = Main.map.mapView.getLatLon(e.getX(), e.getY()); 125 125 points.add(new WayPoint(coor)); 126 126 … … 132 132 points.clear(); 133 133 recalculate(); 134 Main.map.repaint(); 135 } 136 134 Main.map.repaint(); 135 } 136 137 137 private void recalculate(){ 138 138 double pathLength = 0.0, segLength = 0.0; // in meters 139 139 WayPoint last = null; 140 140 141 141 pathLength = 0.0; 142 142 for(WayPoint p : points){ … … 151 151 MeasurementPlugin.measurementDialog.pathLengthLabel.setText(pathLength < 800?nf2.format(pathLength) + " m":nf.format(pathLength/1000) + " km"); 152 152 } 153 153 154 154 public static double calcDistance(LatLon p1, LatLon p2){ 155 155 double lat1, lon1, lat2, lon2; 156 156 double dlon, dlat; 157 157 158 158 lat1 = p1.lat() * Math.PI / 180.0; 159 159 lon1 = p1.lon() * Math.PI / 180.0; … … 172 172 double lat1, lon1, lat2, lon2; 173 173 double dlon, dlat; 174 174 175 175 lat1 = p1.lat() * Math.PI / 180.0; 176 176 lon1 = p1.lon() * Math.PI / 180.0; … … 185 185 return 6367000 * c; 186 186 } 187 187 188 188 public static double calcY(LatLon p1){ 189 189 double lat1, lon1, lat2, lon2; 190 190 double dlon, dlat; 191 191 192 192 lat1 = p1.lat() * Math.PI / 180.0; 193 193 lon1 = p1.lon() * Math.PI / 180.0; … … 202 202 return 6367000 * c; 203 203 } 204 204 205 205 public static double calcDistance(WayPoint p1, WayPoint p2){ 206 206 return calcDistance(p1.latlon, p2.latlon); 207 207 } 208 208 209 209 public static double angleBetween(WayPoint p1, WayPoint p2){ 210 210 return angleBetween(p1.latlon, p2.latlon); 211 211 } 212 212 213 213 public static double angleBetween(LatLon p1, LatLon p2){ 214 214 double lat1, lon1, lat2, lon2; 215 215 double dlon; 216 216 double heading; 217 217 218 218 lat1 = p1.lat() * Math.PI / 180.0; 219 219 lon1 = p1.lon() * Math.PI / 180.0; … … 223 223 dlon = lon2 - lon1; 224 224 double coslat2 = Math.cos(lat2); 225 225 226 226 return (180 * Math.atan2(coslat2 * Math.sin(dlon), 227 227 (Math.cos(lat1) * Math.sin(lat2) 228 - 228 - 229 229 Math.sin(lat1) * coslat2 * Math.cos(dlon)))) / Math.PI; 230 230 } … … 234 234 double dlon, dlat; 235 235 double heading; 236 236 237 237 lat1 = p1.lat() * Math.PI / 180.0; 238 238 lon1 = p1.lon() * Math.PI / 180.0; … … 250 250 heading = 2 * Math.PI - heading; 251 251 } 252 252 253 253 return heading * 180 / Math.PI; 254 254 } … … 257 257 private class GPXLayerImportAction extends AbstractAction { 258 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 if(l instanceof GpxLayer){ 259 private MeasurementLayer layer; 260 261 /** 262 * The data model for the list component. 263 */ 264 private DefaultListModel model = new DefaultListModel(); 265 266 /** 267 * @param layer the targeting measurement layer 268 */ 269 public GPXLayerImportAction(MeasurementLayer layer) { 270 super(tr("Import path from GPX layer"), ImageProvider.get("dialogs", "edit")); // TODO: find better image 271 this.layer = layer; 272 } 273 274 public void actionPerformed(ActionEvent e) { 275 Box panel = Box.createVerticalBox(); 276 final JList layerList = new JList(model); 277 Collection<Layer> data = Main.map.mapView.getAllLayers(); 278 Layer lastLayer = null; 279 int layerCnt = 0; 280 281 for (Layer l : data){ 282 if(l instanceof GpxLayer){ 283 283 model.addElement(l); 284 284 lastLayer = l; 285 285 layerCnt++; 286 286 } 287 288 287 } 288 if(layerCnt == 1){ 289 289 layerList.setSelectedValue(lastLayer, true); 290 290 } … … 304 304 305 305 JCheckBox dropFirst = new JCheckBox(tr("Drop existing path")); 306 306 307 307 panel.add(layerList); 308 308 panel.add(dropFirst); 309 309 310 310 final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){ 311 311 @Override public void selectInitialValue() { … … 315 315 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Import path from GPX layer")); 316 316 dlg.setVisible(true); 317 317 318 318 Object answer = optionPane.getValue(); 319 319 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE || … … 326 326 points = new ArrayList<WayPoint>(32); 327 327 } 328 328 329 329 for (GpxTrack trk : gpx.data.tracks) { 330 330 for (Collection<WayPoint> trkseg : trk.trackSegs) { … … 333 333 } 334 334 } 335 335 } 336 336 recalculate(); 337 337 Main.parent.repaint(); -
applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementMode.java
r12471 r12778 15 15 public class MeasurementMode extends MapMode { 16 16 17 private static final long serialVersionUID = 3853830673475744263L; 18 19 public MeasurementMode(MapFrame mapFrame, String name, String desc) { 20 super(name, "measurement.png", desc, mapFrame, Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); 21 } 17 private static final long serialVersionUID = 3853830673475744263L; 22 18 23 @Override public void enterMode() { 24 super.enterMode(); 25 Main.map.mapView.addMouseListener(this); 26 } 19 public MeasurementMode(MapFrame mapFrame, String name, String desc) { 20 super(name, "measurement.png", desc, mapFrame, Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); 21 } 27 22 28 @Override public void exitMode() {29 super.exitMode();30 Main.map.mapView.removeMouseListener(this);31 23 @Override public void enterMode() { 24 super.enterMode(); 25 Main.map.mapView.addMouseListener(this); 26 } 32 27 33 /** 34 * If user clicked with the left button, add a node at the current mouse 35 * position. 36 * 37 * If in nodesegment mode, add the node to the line segment by splitting the 38 * segment. The new created segment will be inserted in every way the segment 39 * was part of. 40 */ 41 @Override public void mouseClicked(MouseEvent e) { 42 if (e.getButton() == MouseEvent.BUTTON3){ 43 MeasurementPlugin.getCurrentLayer().removeLastPoint(); 44 }else if (e.getButton() == MouseEvent.BUTTON1){ 45 LatLon coor = Main.map.mapView.getLatLon(e.getX(), e.getY()); 46 if (coor.isOutSideWorld()) { 47 JOptionPane.showMessageDialog(Main.parent,tr("Can not draw outside of the world.")); 48 return; 49 } 50 MeasurementPlugin.getCurrentLayer().mouseClicked(e); 51 } 52 } 28 @Override public void exitMode() { 29 super.exitMode(); 30 Main.map.mapView.removeMouseListener(this); 31 } 32 33 /** 34 * If user clicked with the left button, add a node at the current mouse 35 * position. 36 * 37 * If in nodesegment mode, add the node to the line segment by splitting the 38 * segment. The new created segment will be inserted in every way the segment 39 * was part of. 40 */ 41 @Override public void mouseClicked(MouseEvent e) { 42 if (e.getButton() == MouseEvent.BUTTON3){ 43 MeasurementPlugin.getCurrentLayer().removeLastPoint(); 44 }else if (e.getButton() == MouseEvent.BUTTON1){ 45 LatLon coor = Main.map.mapView.getLatLon(e.getX(), e.getY()); 46 if (coor.isOutSideWorld()) { 47 JOptionPane.showMessageDialog(Main.parent,tr("Can not draw outside of the world.")); 48 return; 49 } 50 MeasurementPlugin.getCurrentLayer().mouseClicked(e); 51 } 52 } 53 53 54 54 } -
applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementPlugin.java
r12473 r12778 12 12 public class MeasurementPlugin extends Plugin { 13 13 14 private IconToggleButton btn; 15 private MeasurementMode mode; 16 protected static MeasurementDialog measurementDialog; 17 protected static MeasurementLayer currentLayer; 18 19 public MeasurementPlugin() { 20 mode = new MeasurementMode(Main.map, "measurement", tr("measurement mode")); 21 btn = new IconToggleButton(mode); 22 btn.setVisible(true); 23 measurementDialog = new MeasurementDialog(); 24 } 25 26 @Override 27 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 28 if(newFrame != null) 29 newFrame.addToggleDialog(measurementDialog); 30 if(Main.map != null) 31 Main.map.addMapMode(btn); 32 } 14 private IconToggleButton btn; 15 private MeasurementMode mode; 16 protected static MeasurementDialog measurementDialog; 17 protected static MeasurementLayer currentLayer; 33 18 34 public static MeasurementLayer getCurrentLayer(){ 35 if(currentLayer == null){ 36 currentLayer = new MeasurementLayer(tr("Measurements")); 37 Main.main.addLayer(currentLayer); 38 currentLayer.listeners.add(new LayerChangeListener(){ 39 public void activeLayerChange(final Layer oldLayer, final Layer newLayer) { 40 if(newLayer instanceof MeasurementLayer) 41 MeasurementPlugin.currentLayer = (MeasurementLayer)newLayer; 42 } 43 public void layerAdded(final Layer newLayer) { 44 } 45 public void layerRemoved(final Layer oldLayer) { 46 } 47 }); 48 } 49 return currentLayer; 50 } 19 public MeasurementPlugin() { 20 mode = new MeasurementMode(Main.map, "measurement", tr("measurement mode")); 21 btn = new IconToggleButton(mode); 22 btn.setVisible(true); 23 measurementDialog = new MeasurementDialog(); 24 } 25 26 @Override 27 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 28 if(newFrame != null) 29 newFrame.addToggleDialog(measurementDialog); 30 if(Main.map != null) 31 Main.map.addMapMode(btn); 32 } 33 34 public static MeasurementLayer getCurrentLayer(){ 35 if(currentLayer == null){ 36 currentLayer = new MeasurementLayer(tr("Measurements")); 37 Main.main.addLayer(currentLayer); 38 currentLayer.listeners.add(new LayerChangeListener(){ 39 public void activeLayerChange(final Layer oldLayer, final Layer newLayer) { 40 if(newLayer instanceof MeasurementLayer) 41 MeasurementPlugin.currentLayer = (MeasurementLayer)newLayer; 42 } 43 public void layerAdded(final Layer newLayer) { 44 } 45 public void layerRemoved(final Layer oldLayer) { 46 } 47 }); 48 } 49 return currentLayer; 50 } 51 51 }
Note:
See TracChangeset
for help on using the changeset viewer.