Ticket #11156: 5.patch

File 5.patch, 6.1 KB (added by simon04, 11 years ago)
  • measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementDialog.java

    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  
    6666     * The measurement label for area of the currently selected loop
    6767     */
    6868    protected JLabel selectAreaLabel;
     69   
     70    /**
     71     * The measurement label for radius if the currently selected loop is a circle.
     72     */
     73    protected JLabel selectRadiusLabel;
    6974
    7075    /**
    7176     * The measurement label for the segment angle, actually updated, if 2 nodes are selected
    public void actionPerformed(ActionEvent e)  
    116121
    117122        selectAreaLabel = new JLabel(getAreaText(0));
    118123        valuePanel.add(selectAreaLabel);
     124       
     125        valuePanel.add(new JLabel(tr("Selection Radius")));
     126
     127        selectRadiusLabel = new JLabel(getRadiusText(0));
     128        valuePanel.add(selectRadiusLabel);
    119129
    120130        JLabel angle = new JLabel(tr("Angle"));
    121131        angle.setToolTipText(tr("Angle between two selected Nodes"));
    protected String getDistText(double v) {  
    141151    protected String getAreaText(double v) {
    142152        return NavigatableComponent.getSystemOfMeasurement().getAreaText(v, new DecimalFormat("#0.000"), 1e-3);
    143153    }
     154   
     155    protected String getRadiusText(double v) {
     156        return NavigatableComponent.getSystemOfMeasurement().getDistText(v, new DecimalFormat("#0.000"), 1e-3);
     157    }
    144158
    145159    protected String getAngleText(double v) {
    146160        return new DecimalFormat("#0.0").format(v) + " \u00b0";
    public void resetValues(){  
    155169
    156170    @Override
    157171    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       
    161177        Node lastNode = null;
     178       
    162179        // Don't mix up way and nodes computation (fix #6872). Priority given to ways
    163180        ways = new SubclassFilteredCollection<OsmPrimitive, Way>(newSelection, OsmPrimitive.wayPredicate);
     181       
    164182        if (ways.isEmpty()) {
     183               
    165184            nodes = new SubclassFilteredCollection<OsmPrimitive, Node>(newSelection, OsmPrimitive.nodePredicate);
     185           
    166186            for (Node n : nodes) {
     187               
    167188                if (n.getCoor() != null) {
     189                       
    168190                    if (lastNode == null) {
     191                       
    169192                        lastNode = n;
    170193                    } else {
     194                       
    171195                        length += lastNode.getCoor().greatCircleDistance(n.getCoor());
    172196                        segAngle = MeasurementLayer.angleBetween(lastNode.getCoor(), n.getCoor());
    173197                        lastNode = n;
    public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {  
    175199                }
    176200            }
    177201        } else {
     202               
    178203            nodes = null;
     204           
    179205            for (Way w : ways) {
     206               
    180207                Node lastN = null;
    181                 double wayArea = 0.0;
     208               
     209                double wayArea    = 0d;
     210                Double firstSegLength  = null;
     211               
     212                boolean isCircle = true;
     213               
    182214                for (Node n: w.getNodes()) {
     215                       
    183216                    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                       
    185229                        //http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
    186230                        wayArea += (MeasurementLayer.calcX(n.getCoor()) * MeasurementLayer.calcY(lastN.getCoor()))
    187231                                 - (MeasurementLayer.calcY(n.getCoor()) * MeasurementLayer.calcX(lastN.getCoor()));
     232                       
    188233                        segAngle = MeasurementLayer.angleBetween(lastN.getCoor(), n.getCoor());
    189234                    }
     235                   
    190236                    lastN = n;
    191237                }
     238               
    192239                if (lastN != null && lastN == w.getNodes().iterator().next())
    193240                    wayArea = Math.abs(wayArea / 2);
     241               
     242                        // é uma area
    194243                else
    195244                    wayArea = 0;
     245               
    196246                area += wayArea;
    197247            }
     248           
     249            if (ways.size() == 1 && area > 0d) {
     250               
     251                radius = length / (2 * Math.PI);
     252            }
    198253        }
    199254       
    200255        final String lengthLabel = getDistText(length);
    201256        final String angleLabel = getAngleText(segAngle);
    202257        final String areaLabel = getAreaText(area);
     258        final String radiusLabel = getRadiusText(radius);
    203259       
    204260        GuiHelper.runInEDT(new Runnable() {
    205261            @Override
    public void run() {  
    207263                selectLengthLabel.setText(lengthLabel);
    208264                segAngleLabel.setText(angleLabel);
    209265                selectAreaLabel.setText(areaLabel);
     266                selectRadiusLabel.setText(radiusLabel);
    210267            }
    211268        });
    212269