From 0903b9fecf2ff2d51da2eb008387a2e15561e055 Mon Sep 17 00:00:00 2001
From: willy <willy.tiengo@gmail.com>
Date: Sat, 21 Feb 2015 11:49:05 -0300
Subject: [PATCH] if the selection is a circle, it's measured its radius.
---
.../plugins/measurement/MeasurementDialog.java | 67 ++++++++++++++++++++--
1 file changed, 62 insertions(+), 5 deletions(-)
diff --git a/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementDialog.java b/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementDialog.java
index 053c015..819cd78 100644
|
a
|
b
|
|
| 66 | 66 | * The measurement label for area of the currently selected loop |
| 67 | 67 | */ |
| 68 | 68 | protected JLabel selectAreaLabel; |
| | 69 | |
| | 70 | /** |
| | 71 | * The measurement label for radius if the currently selected loop is a circle. |
| | 72 | */ |
| | 73 | protected JLabel selectRadiusLabel; |
| 69 | 74 | |
| 70 | 75 | /** |
| 71 | 76 | * The measurement label for the segment angle, actually updated, if 2 nodes are selected |
| … |
… |
public void actionPerformed(ActionEvent e)
|
| 116 | 121 | |
| 117 | 122 | selectAreaLabel = new JLabel(getAreaText(0)); |
| 118 | 123 | valuePanel.add(selectAreaLabel); |
| | 124 | |
| | 125 | valuePanel.add(new JLabel(tr("Selection Radius"))); |
| | 126 | |
| | 127 | selectRadiusLabel = new JLabel(getRadiusText(0)); |
| | 128 | valuePanel.add(selectRadiusLabel); |
| 119 | 129 | |
| 120 | 130 | JLabel angle = new JLabel(tr("Angle")); |
| 121 | 131 | angle.setToolTipText(tr("Angle between two selected Nodes")); |
| … |
… |
protected String getDistText(double v) {
|
| 141 | 151 | protected String getAreaText(double v) { |
| 142 | 152 | return NavigatableComponent.getSystemOfMeasurement().getAreaText(v, new DecimalFormat("#0.000"), 1e-3); |
| 143 | 153 | } |
| | 154 | |
| | 155 | protected String getRadiusText(double v) { |
| | 156 | return NavigatableComponent.getSystemOfMeasurement().getDistText(v, new DecimalFormat("#0.000"), 1e-3); |
| | 157 | } |
| 144 | 158 | |
| 145 | 159 | protected String getAngleText(double v) { |
| 146 | 160 | return new DecimalFormat("#0.0").format(v) + " \u00b0"; |
| … |
… |
public void resetValues(){
|
| 155 | 169 | |
| 156 | 170 | @Override |
| 157 | 171 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) { |
| 158 | | double length = 0.0; |
| 159 | | double segAngle = 0.0; |
| 160 | | double area = 0.0; |
| | 172 | double length = 0d; |
| | 173 | double segAngle = 0d; |
| | 174 | double area = 0d; |
| | 175 | double radius = 0d; |
| | 176 | |
| 161 | 177 | Node lastNode = null; |
| | 178 | |
| 162 | 179 | // Don't mix up way and nodes computation (fix #6872). Priority given to ways |
| 163 | 180 | ways = new SubclassFilteredCollection<OsmPrimitive, Way>(newSelection, OsmPrimitive.wayPredicate); |
| | 181 | |
| 164 | 182 | if (ways.isEmpty()) { |
| | 183 | |
| 165 | 184 | nodes = new SubclassFilteredCollection<OsmPrimitive, Node>(newSelection, OsmPrimitive.nodePredicate); |
| | 185 | |
| 166 | 186 | for (Node n : nodes) { |
| | 187 | |
| 167 | 188 | if (n.getCoor() != null) { |
| | 189 | |
| 168 | 190 | if (lastNode == null) { |
| | 191 | |
| 169 | 192 | lastNode = n; |
| 170 | 193 | } else { |
| | 194 | |
| 171 | 195 | length += lastNode.getCoor().greatCircleDistance(n.getCoor()); |
| 172 | 196 | segAngle = MeasurementLayer.angleBetween(lastNode.getCoor(), n.getCoor()); |
| 173 | 197 | lastNode = n; |
| … |
… |
public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
| 175 | 199 | } |
| 176 | 200 | } |
| 177 | 201 | } else { |
| | 202 | |
| 178 | 203 | nodes = null; |
| | 204 | |
| 179 | 205 | for (Way w : ways) { |
| | 206 | |
| 180 | 207 | Node lastN = null; |
| 181 | | double wayArea = 0.0; |
| | 208 | |
| | 209 | double wayArea = 0d; |
| | 210 | Double firstSegLength = null; |
| | 211 | |
| | 212 | boolean isCircle = true; |
| | 213 | |
| 182 | 214 | for (Node n: w.getNodes()) { |
| | 215 | |
| 183 | 216 | if (lastN != null && lastN.getCoor() != null && n.getCoor() != null) { |
| 184 | | length += lastN.getCoor().greatCircleDistance(n.getCoor()); |
| | 217 | |
| | 218 | double segLength = lastN.getCoor().greatCircleDistance(n.getCoor()); |
| | 219 | |
| | 220 | if (firstSegLength == null) firstSegLength = segLength; |
| | 221 | |
| | 222 | if (isCircle && Math.abs(firstSegLength - segLength) > 0.000001) { |
| | 223 | |
| | 224 | isCircle = false; |
| | 225 | } |
| | 226 | |
| | 227 | length += segLength; |
| | 228 | |
| 185 | 229 | //http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/ |
| 186 | 230 | wayArea += (MeasurementLayer.calcX(n.getCoor()) * MeasurementLayer.calcY(lastN.getCoor())) |
| 187 | 231 | - (MeasurementLayer.calcY(n.getCoor()) * MeasurementLayer.calcX(lastN.getCoor())); |
| | 232 | |
| 188 | 233 | segAngle = MeasurementLayer.angleBetween(lastN.getCoor(), n.getCoor()); |
| 189 | 234 | } |
| | 235 | |
| 190 | 236 | lastN = n; |
| 191 | 237 | } |
| | 238 | |
| 192 | 239 | if (lastN != null && lastN == w.getNodes().iterator().next()) |
| 193 | 240 | wayArea = Math.abs(wayArea / 2); |
| | 241 | |
| | 242 | // é uma area |
| 194 | 243 | else |
| 195 | 244 | wayArea = 0; |
| | 245 | |
| 196 | 246 | area += wayArea; |
| 197 | 247 | } |
| | 248 | |
| | 249 | if (ways.size() == 1 && area > 0d) { |
| | 250 | |
| | 251 | radius = length / (2 * Math.PI); |
| | 252 | } |
| 198 | 253 | } |
| 199 | 254 | |
| 200 | 255 | final String lengthLabel = getDistText(length); |
| 201 | 256 | final String angleLabel = getAngleText(segAngle); |
| 202 | 257 | final String areaLabel = getAreaText(area); |
| | 258 | final String radiusLabel = getRadiusText(radius); |
| 203 | 259 | |
| 204 | 260 | GuiHelper.runInEDT(new Runnable() { |
| 205 | 261 | @Override |
| … |
… |
public void run() {
|
| 207 | 263 | selectLengthLabel.setText(lengthLabel); |
| 208 | 264 | segAngleLabel.setText(angleLabel); |
| 209 | 265 | selectAreaLabel.setText(areaLabel); |
| | 266 | selectRadiusLabel.setText(radiusLabel); |
| 210 | 267 | } |
| 211 | 268 | }); |
| 212 | 269 | |