Statistics
| Revision:

root / trunk / libraries / libExpressions / src / main / java / org / gvsig / operators / Area.java @ 23299

History | View | Annotate | Download (4.46 KB)

1
package org.gvsig.operators;
2

    
3
import java.awt.geom.Point2D;
4
import java.util.ArrayList;
5

    
6
import org.apache.bsf.BSFException;
7
import org.apache.bsf.BSFManager;
8
import org.cresques.cts.IProjection;
9
import org.gvsig.baseclasses.GraphicOperator;
10
import org.gvsig.baseclasses.IOperator;
11
import org.gvsig.baseclasses.Index;
12
import org.gvsig.expresions.EvalOperatorsTask;
13

    
14
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
15
import com.iver.andami.PluginServices;
16
import com.iver.andami.messages.NotificationManager;
17
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
18
import com.iver.cit.gvsig.fmap.MapContext;
19
import com.iver.cit.gvsig.fmap.ViewPort;
20
import com.iver.cit.gvsig.fmap.core.FShape;
21
import com.iver.cit.gvsig.fmap.core.IGeometry;
22
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
23
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
24
import com.iver.cit.gvsig.fmap.tools.geo.Geo;
25
/**
26
 * @author Vicente Caballero Navarro
27
 */
28
public class Area extends GraphicOperator{
29

    
30
        public String addText(String s) {
31
                return s.concat(toString()+"()");
32
        }
33
        public double process(Index index) throws DriverIOException {
34
                ReadableVectorial adapter = getLayer().getSource();
35
                   IGeometry geom=null;
36
                try {
37
                        geom = adapter.getShape(index.get());
38
                } catch (ExpansionFileReadException e) {
39
                        throw new DriverIOException(e);
40
                } catch (ReadDriverException e) {
41
                        throw new DriverIOException(e);
42
                }
43
                   int distanceUnits=getLayer().getMapContext().getViewPort().getDistanceUnits();
44
                return returnArea(geom)/Math.pow(MapContext.CHANGEM[distanceUnits],2);
45
        }
46
        public void eval(BSFManager interpreter) throws BSFException {
47
                interpreter.declareBean("jarea",this,Area.class);
48
//                interpreter.eval(ExpressionFieldExtension.BEANSHELL,null,-1,-1,"double area(){return area.process(indexRow);};");
49
                interpreter.exec(EvalOperatorsTask.JYTHON,null,-1,-1,"def area():\n" +
50
                                "  return jarea.process(indexRow)");
51
        }
52
        public String toString() {
53
                return "area";
54
        }
55
        public boolean isEnable() {
56
                if (getLayer()==null)
57
                        return false;
58
                ReadableVectorial adapter = getLayer().getSource();
59
                int type=FShape.POINT;
60
                try {
61
                        type=adapter.getShapeType();
62
                } catch (ReadDriverException e) {
63
                        NotificationManager.addError(e);
64
                }
65
                return (getType()==IOperator.NUMBER && type==FShape.POLYGON);
66
        }
67

    
68
        private double returnArea(IGeometry geom) {
69
                ArrayList parts=getXY(geom);
70
                double area=0;
71
                for (int i=0;i<parts.size();i++){
72
                Double[][] xsys=(Double[][])parts.get(i);//getXY(geom);
73
                Double[] xs=xsys[0];
74
                Double[] ys=xsys[1];
75
                IProjection proj=getLayer().getMapContext().getProjection();
76
                if (proj.isProjected()) {
77
                        area+= returnCoordsArea(xs,ys,new Point2D.Double(xs[xs.length-1].doubleValue(),ys[ys.length-1].doubleValue()));
78
                }else{
79
                        area+= returnGeoCArea(xs,ys);
80
                }
81
                }
82
                return area;
83
        }
84
        private double returnGeoCArea(Double[] xs,Double[] ys) {
85
                double[] lat=new double[xs.length];
86
                double[] lon=new double[xs.length];
87
                for (int K= 0; K < xs.length; K++){
88
                        lon[K]= xs[K].doubleValue()/Geo.Degree;
89
                        lat[K]= ys[K].doubleValue()/Geo.Degree;
90
                }
91
                return (Geo.sphericalPolyArea(lat,lon,xs.length-1)*Geo.SqM);
92
        }
93
        /**
94
         * Calcula el ?rea.
95
         *
96
         * @param aux ?ltimo punto.
97
         *
98
         * @return ?rea.
99
         */
100
        public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) {
101
                Point2D aux=point;
102
                double elArea = 0.0;
103
                Point2D pPixel;
104
                Point2D p = new Point2D.Double();
105
                Point2D.Double pAnt = new Point2D.Double();
106
                ViewPort vp = getLayer().getMapContext().getViewPort();
107
                for (int pos = 0; pos < xs.length-1; pos++) {
108
                        pPixel = new Point2D.Double(xs[pos].doubleValue(),
109
                                        ys[pos].doubleValue());
110
                        p = pPixel;
111
                        if (pos == 0) {
112
                                pAnt.x = aux.getX();
113
                                pAnt.y = aux.getY();
114
                        }
115
                        elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY()));
116
                        pAnt.setLocation(p);
117
                }
118

    
119
                elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY()));
120
                elArea = Math.abs(elArea / 2.0);
121
                return (elArea*(Math.pow(MapContext.CHANGEM[vp.getMapUnits()],2)));
122
        }
123
        public String getTooltip(){
124
                return PluginServices.getText(this,"operator")+":  "+addText("")+"\n"+getDescription();
125
        }
126
        public String getDescription() {
127
        return PluginServices.getText(this, "returns") + ": " +
128
        PluginServices.getText(this, "numeric_value") + "\n" +
129
        PluginServices.getText(this, "description") + ": " +
130
        "Returns the area of polygon geometry of this row.";
131
    }
132
}