Statistics
| Revision:

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

History | View | Annotate | Download (6.69 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.cresques.cts.IProjection;
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.FeatureStore;
41
import org.gvsig.fmap.geom.Geometry;
42

    
43
import com.vividsolutions.jts.algorithm.CGAlgorithms;
44
import com.vividsolutions.jts.geom.Coordinate;
45
import com.vividsolutions.jts.geom.CoordinateList;
46
/**
47
 * @author Vicente Caballero Navarro
48
 */
49
public class Area extends GraphicOperator{
50

    
51
        public String addText(String s) {
52
                return s.concat(toString()+"()");
53
        }
54
        public double process(org.gvsig.fmap.dal.feature.Feature feature){
55
//                ReadableVectorial adapter = getLayer().getSource();
56
                   org.gvsig.fmap.geom.Geometry geom=null;
57
//                try {
58
                        geom = feature.getDefaultGeometry();//adapter.getShape(index.get());
59
//                } catch (ExpansionFileReadException e) {
60
//                        throw new DriverIOException(e);
61
//                } catch (ReadDriverException e) {
62
//                        throw new DriverIOException(e);
63
//                }
64
//                   int distanceUnits=getLayer().getMapContext().getViewPort().getDistanceArea();
65
                return returnArea(geom);///Math.pow(MapContext.getAreaTrans2Meter()[distanceUnits],2);
66
        }
67
        public void eval(BSFManager interpreter) throws BSFException {
68
                interpreter.declareBean("jarea",this,Area.class);
69
//                interpreter.eval(ExpressionFieldExtension.BEANSHELL,null,-1,-1,"double area(){return area.process(indexRow);};");
70
                interpreter.exec(ExpressionFieldExtension.JYTHON,null,-1,-1,"def area():\n" +
71
                                "  return jarea.process(featureContainer.getFeature())");
72
        }
73
        public String toString() {
74
                return "area";
75
        }
76
        public boolean isEnable() {
77
                if (getLayer()==null)
78
                        return false;
79
                FeatureStore store;
80
                int geomType=Geometry.TYPES.POINT;
81
                try {
82
                        store = getLayer().getFeatureStore();
83
                        geomType = store.getDefaultFeatureType().getAttributeDescriptor(store.getDefaultFeatureType().getDefaultGeometryAttributeIndex()).getGeometryType();
84
                } catch (DataException e) {
85
                        NotificationManager.addError(e);
86
                }
87
                return (getType()==IOperator.NUMBER &&
88
                    
89
                    (geomType==Geometry.TYPES.SURFACE
90
                    || geomType==Geometry.TYPES.MULTISURFACE)
91
                    
92
                    );
93
        }
94

    
95
        private double returnArea(org.gvsig.fmap.geom.Geometry geom) {
96
                ArrayList parts=getXY(geom);
97
                double area=0;
98
                for (int i=0;i<parts.size();i++){
99
                        Double[][] xsys=(Double[][])parts.get(i);//getXY(geom);
100
                        Double[] xs=xsys[0];
101
                        Double[] ys=xsys[1];
102
                        IProjection proj=getLayer().getMapContext().getProjection();
103
                        if (isCCW(xs, ys)){
104
                                if (proj.isProjected()) {
105
                                        area-= returnCoordsArea(xs,ys,new Point2D.Double(xs[xs.length-1].doubleValue(),ys[ys.length-1].doubleValue()));
106
                                }else{
107
                                        area-= returnGeoCArea(xs,ys);
108
                                }
109
                        }else{
110
                                if (proj.isProjected()) {
111
                                        area+= returnCoordsArea(xs,ys,new Point2D.Double(xs[xs.length-1].doubleValue(),ys[ys.length-1].doubleValue()));
112
                                }else{
113
                                        area+= returnGeoCArea(xs,ys);
114
                                }
115
                        }
116
                }
117
                return area;
118
        }
119

    
120
        public boolean isCCW(Double[] xs, Double[] ys){
121
                CoordinateList coordList = new CoordinateList();
122
                for (int i = 0; i < ys.length; i++) {
123
               Coordinate coord=new Coordinate(xs[i].doubleValue(),ys[i].doubleValue());
124
               coordList.add(coord);
125
                }
126
                if (coordList.isEmpty())
127
                        return true;
128
                return CGAlgorithms.isCCW(coordList.toCoordinateArray());
129
        }
130

    
131

    
132
        private double returnGeoCArea(Double[] xs,Double[] ys) {
133
                double[] lat=new double[xs.length];
134
                double[] lon=new double[xs.length];
135
                for (int K= 0; K < xs.length; K++){
136
                        lon[K]= xs[K].doubleValue()/org.gvsig.fmap.mapcontrol.tools.geo.Geo.Degree;
137
                        lat[K]= ys[K].doubleValue()/org.gvsig.fmap.mapcontrol.tools.geo.Geo.Degree;
138
                }
139
                return (org.gvsig.fmap.mapcontrol.tools.geo.Geo.sphericalPolyArea(lat,lon,xs.length-1)*org.gvsig.fmap.mapcontrol.tools.geo.Geo.SqM);
140
        }
141
        /**
142
         * Calcula el ?rea.
143
         *
144
         * @param aux ?ltimo punto.
145
         *
146
         * @return ?rea.
147
         */
148
        public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) {
149
                Point2D aux=point;
150
                double elArea = 0.0;
151
                Point2D pPixel;
152
                Point2D p = new Point2D.Double();
153
                Point2D.Double pAnt = new Point2D.Double();
154
                org.gvsig.fmap.mapcontext.ViewPort vp = getLayer().getMapContext().getViewPort();
155
                for (int pos = 0; pos < xs.length-1; pos++) {
156
                        pPixel = new Point2D.Double(xs[pos].doubleValue(),
157
                                        ys[pos].doubleValue());
158
                        p = pPixel;
159
                        if (pos == 0) {
160
                                pAnt.x = aux.getX();
161
                                pAnt.y = aux.getY();
162
                        }
163
                        elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY()));
164
                        pAnt.setLocation(p);
165
                }
166

    
167
                elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY()));
168
                elArea = Math.abs(elArea / 2.0);
169
                return (elArea/(Math.pow(org.gvsig.fmap.mapcontext.MapContext.getAreaTrans2Meter()[vp.getDistanceArea()],2)));
170
        }
171
        public String getTooltip(){
172
                return PluginServices.getText(this,"operator")+":  "+addText("")+"\n"+getDescription();
173
        }
174
        public String getDescription() {
175
        return PluginServices.getText(this, "returns") + ": " +
176
        PluginServices.getText(this, "numeric_value") + "\n" +
177
        PluginServices.getText(this, "description") + ": " +
178
        "Returns the area of polygon geometry of this row.";
179
    }
180
}