Statistics
| Revision:

root / org.gvsig.expressionfield / trunk / org.gvsig.expressionfield / src / main / java / org / gvsig / expressionfield / project / documents / table / operators / Perimeter.java @ 30

History | View | Annotate | Download (5.26 KB)

1
/**
2
 * *****************************************************************************
3
 *
4
 * gvSIG. Desktop Geographic Information System.
5
 *
6
 * Copyright (C) 2007-2013 gvSIG Association.
7
 *
8
 * This program is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License as published by the Free Software
10
 * Foundation; either version 3 of the License, or (at your option) any later
11
 * version.
12
 *
13
 * This program is distributed in the hope that it will be useful, but WITHOUT
14
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
 * details.
17
 *
18
 * You should have received a copy of the GNU General Public License along with
19
 * this program; if not, write to the Free Software Foundation, Inc., 51
20
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
 *
22
 * For any additional information, do not hesitate to contact us at info AT
23
 * gvsig.com, or visit our website www.gvsig.com.
24
 *
25
 ******************************************************************************
26
 */
27
package org.gvsig.expressionfield.project.documents.table.operators;
28

    
29
import java.awt.geom.Point2D;
30
import java.util.ArrayList;
31

    
32
import org.apache.bsf.BSFException;
33
import org.apache.bsf.BSFManager;
34
import org.gvsig.andami.PluginServices;
35
import org.gvsig.andami.messages.NotificationManager;
36
import org.gvsig.expressionfield.ExpressionFieldExtension;
37
import org.gvsig.expressionfield.project.documents.table.GraphicOperator;
38
import org.gvsig.expressionfield.project.documents.table.IOperator;
39
import org.gvsig.fmap.dal.exception.DataException;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.mapcontext.MapContext;
43
import org.gvsig.fmap.mapcontext.ViewPort;
44

    
45
/**
46
 * @author Vicente Caballero Navarro
47
 */
48
public class Perimeter extends GraphicOperator {
49

    
50
    public String addText(String s) {
51
        return s.concat(toString() + "()");
52
    }
53

    
54
    public double process(Feature feature) {
55
//                ReadableVectorial adapter = getLayer().getSource();
56
        org.gvsig.fmap.geom.Geometry geom = null;
57
        geom = feature.getDefaultGeometry();//adapter.getShape(index.get());
58
        ArrayList parts = getXY(geom);
59
        double perimeter = 0;
60
        for (int j = 0; j < parts.size(); j++) {
61
            Double[][] xsys = (Double[][]) parts.get(j);//getXY(geom);
62
            double dist = 0;
63
            double distAll = 0;
64

    
65
            ViewPort vp = getLayer().getMapContext().getViewPort();
66
            for (int i = 0; i < (xsys[0].length - 1); i++) {
67
                dist = 0;
68

    
69
                Point2D p = new Point2D.Double(xsys[0][i].doubleValue(), xsys[1][i].doubleValue());//vp.toMapPoint(new Point(event.getXs()[i].intValue(), event.getYs()[i].intValue()));
70
                Point2D p2 = new Point2D.Double(xsys[0][i + 1].doubleValue(), xsys[1][i + 1].doubleValue());//vp.toMapPoint(new Point(event.getXs()[i + 1].intValue(), event.getYs()[i + 1].intValue()));
71
                dist = vp.distanceWorld(p, p2);
72
                //System.out.println("distancia parcial = "+dist);
73
                distAll += dist;
74
            }
75
            int distanceUnits = vp.getDistanceUnits();
76
            perimeter += distAll / MapContext.getDistanceTrans2Meter()[distanceUnits];
77
        }
78
        return perimeter;
79
    }
80

    
81
    public void eval(BSFManager interpreter) throws BSFException {
82
        interpreter.declareBean("jperimeter", this, Perimeter.class);
83
//                interpreter.eval(ExpressionFieldExtension.BEANSHELL,null,-1,-1,"double perimeter(){return jperimeter.process(indexRow);};");
84
        interpreter.exec(ExpressionFieldExtension.JYTHON, null, -1, -1, "def perimeter():\n"
85
                + "  return jperimeter.process(featureContainer.getFeature())");
86
    }
87

    
88
    public String toString() {
89
        return "perimeter";
90
    }
91

    
92
    public boolean isEnable() {
93
        if (getLayer() == null) {
94
            return false;
95
        }
96
        int geomType = org.gvsig.fmap.geom.Geometry.TYPES.POINT;
97
        try {
98
            FeatureStore store = getLayer().getFeatureStore();
99
            geomType = store.getDefaultFeatureType().getAttributeDescriptor(store.getDefaultFeatureType().getDefaultGeometryAttributeIndex()).getGeometryType();
100
        } catch (DataException e) {
101
            NotificationManager.addError(e);
102
        }
103
        return (getType() == IOperator.NUMBER
104
                && (geomType == org.gvsig.fmap.geom.Geometry.TYPES.SURFACE
105
                || geomType == org.gvsig.fmap.geom.Geometry.TYPES.MULTISURFACE
106
                || geomType == org.gvsig.fmap.geom.Geometry.TYPES.CURVE
107
                || geomType == org.gvsig.fmap.geom.Geometry.TYPES.MULTICURVE));
108
    }
109

    
110
    public String getTooltip() {
111
        return PluginServices.getText(this, "operator") + ":  " + addText("") + "\n" + getDescription();
112
    }
113

    
114
    public String getDescription() {
115
        return PluginServices.getText(this, "returns") + ": "
116
                + PluginServices.getText(this, "numeric_value") + "\n"
117
                + PluginServices.getText(this, "description") + ": "
118
                + "Returns the perimeter of polygon or line geometry  of this row in the units of the view.";
119
    }
120
}