Statistics
| Revision:

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

History | View | Annotate | Download (4.92 KB)

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

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

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

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

    
49
        public String addText(String s) {
50
                return s.concat(toString()+"()");
51
        }
52
        public double process(Feature feature) {
53
//                ReadableVectorial adapter = getLayer().getSource();
54
                   org.gvsig.fmap.geom.Geometry geom=null;
55
                        geom = feature.getDefaultGeometry();//adapter.getShape(index.get());
56
                ArrayList parts=getXY(geom);
57
                   double perimeter=0;
58
                   for (int j=0;j<parts.size();j++){
59
                   Double[][] xsys=(Double[][])parts.get(j);//getXY(geom);
60
            double dist = 0;
61
        double distAll = 0;
62

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

    
67
            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()));
68
            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()));
69
            dist = vp.distanceWorld(p,p2);
70
            //System.out.println("distancia parcial = "+dist);
71
            distAll += dist;
72
        }
73
        int distanceUnits=vp.getDistanceUnits();
74
                perimeter+= distAll/MapContext.getDistanceTrans2Meter()[distanceUnits];
75
                   }
76
                   return perimeter;
77
        }
78
        public void eval(BSFManager interpreter) throws BSFException {
79
                interpreter.declareBean("jperimeter",this,Perimeter.class);
80
//                interpreter.eval(ExpressionFieldExtension.BEANSHELL,null,-1,-1,"double perimeter(){return jperimeter.process(indexRow);};");
81
                interpreter.exec(ExpressionFieldExtension.JYTHON,null,-1,-1,"def perimeter():\n" +
82
                                "  return jperimeter.process(featureContainer.getFeature())");
83
        }
84
        public String toString() {
85
                return "perimeter";
86
        }
87
        public boolean isEnable() {
88
                if (getLayer()==null)
89
                        return false;
90
                int geomType=org.gvsig.fmap.geom.Geometry.TYPES.POINT;
91
                try {
92
                        FeatureStore store = getLayer().getFeatureStore();
93
                        geomType = store.getDefaultFeatureType().getAttributeDescriptor(store.getDefaultFeatureType().getDefaultGeometryAttributeIndex()).getGeometryType();
94
                } catch (DataException e) {
95
                        NotificationManager.addError(e);
96
                }
97
                return (getType()==IOperator.NUMBER &&
98
                    (geomType==org.gvsig.fmap.geom.Geometry.TYPES.SURFACE
99
            || geomType==org.gvsig.fmap.geom.Geometry.TYPES.MULTISURFACE
100
            || geomType==org.gvsig.fmap.geom.Geometry.TYPES.CURVE
101
            || geomType==org.gvsig.fmap.geom.Geometry.TYPES.MULTICURVE
102
                    ));
103
        }
104
        public String getTooltip(){
105
                return PluginServices.getText(this,"operator")+":  "+addText("")+"\n"+getDescription();
106
        }
107
        public String getDescription() {
108
        return PluginServices.getText(this, "returns") + ": " +
109
        PluginServices.getText(this, "numeric_value") + "\n" +
110
        PluginServices.getText(this, "description") + ": " +
111
        "Returns the perimeter of polygon or line geometry  of this row.";
112
    }
113
}