svn-gvsig-desktop / tags / v2_0_0_Build_2057 / extensions / extExpressionField / src / org / gvsig / expressionfield / project / documents / table / operators / Area.java @ 39155
History | View | Annotate | Download (5.53 KB)
1 |
package org.gvsig.expressionfield.project.documents.table.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.andami.PluginServices; |
10 |
import org.gvsig.andami.messages.NotificationManager; |
11 |
import org.gvsig.expressionfield.ExpressionFieldExtension; |
12 |
import org.gvsig.expressionfield.project.documents.table.GraphicOperator; |
13 |
import org.gvsig.expressionfield.project.documents.table.IOperator; |
14 |
import org.gvsig.fmap.dal.exception.DataException; |
15 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
16 |
import org.gvsig.fmap.geom.Geometry; |
17 |
|
18 |
import com.vividsolutions.jts.algorithm.CGAlgorithms; |
19 |
import com.vividsolutions.jts.geom.Coordinate; |
20 |
import com.vividsolutions.jts.geom.CoordinateList; |
21 |
/**
|
22 |
* @author Vicente Caballero Navarro
|
23 |
*/
|
24 |
public class Area extends GraphicOperator{ |
25 |
|
26 |
public String addText(String s) { |
27 |
return s.concat(toString()+"()"); |
28 |
} |
29 |
public double process(org.gvsig.fmap.dal.feature.Feature feature){ |
30 |
// ReadableVectorial adapter = getLayer().getSource();
|
31 |
org.gvsig.fmap.geom.Geometry geom=null;
|
32 |
// try {
|
33 |
geom = feature.getDefaultGeometry();//adapter.getShape(index.get());
|
34 |
// } catch (ExpansionFileReadException e) {
|
35 |
// throw new DriverIOException(e);
|
36 |
// } catch (ReadDriverException e) {
|
37 |
// throw new DriverIOException(e);
|
38 |
// }
|
39 |
// int distanceUnits=getLayer().getMapContext().getViewPort().getDistanceArea();
|
40 |
return returnArea(geom);///Math.pow(MapContext.getAreaTrans2Meter()[distanceUnits],2); |
41 |
} |
42 |
public void eval(BSFManager interpreter) throws BSFException { |
43 |
interpreter.declareBean("jarea",this,Area.class); |
44 |
// interpreter.eval(ExpressionFieldExtension.BEANSHELL,null,-1,-1,"double area(){return area.process(indexRow);};");
|
45 |
interpreter.exec(ExpressionFieldExtension.JYTHON,null,-1,-1,"def area():\n" + |
46 |
" return jarea.process(featureContainer.getFeature())");
|
47 |
} |
48 |
public String toString() { |
49 |
return "area"; |
50 |
} |
51 |
public boolean isEnable() { |
52 |
if (getLayer()==null) |
53 |
return false; |
54 |
FeatureStore store; |
55 |
int geomType=Geometry.TYPES.POINT;
|
56 |
try {
|
57 |
store = getLayer().getFeatureStore(); |
58 |
geomType = store.getDefaultFeatureType().getAttributeDescriptor(store.getDefaultFeatureType().getDefaultGeometryAttributeIndex()).getGeometryType(); |
59 |
} catch (DataException e) {
|
60 |
NotificationManager.addError(e); |
61 |
} |
62 |
return (getType()==IOperator.NUMBER &&
|
63 |
|
64 |
(geomType==Geometry.TYPES.SURFACE |
65 |
|| geomType==Geometry.TYPES.MULTISURFACE) |
66 |
|
67 |
); |
68 |
} |
69 |
|
70 |
private double returnArea(org.gvsig.fmap.geom.Geometry geom) { |
71 |
ArrayList parts=getXY(geom);
|
72 |
double area=0; |
73 |
for (int i=0;i<parts.size();i++){ |
74 |
Double[][] xsys=(Double[][])parts.get(i);//getXY(geom); |
75 |
Double[] xs=xsys[0]; |
76 |
Double[] ys=xsys[1]; |
77 |
IProjection proj=getLayer().getMapContext().getProjection(); |
78 |
if (isCCW(xs, ys)){
|
79 |
if (proj.isProjected()) {
|
80 |
area-= returnCoordsArea(xs,ys,new Point2D.Double(xs[xs.length-1].doubleValue(),ys[ys.length-1].doubleValue())); |
81 |
}else{
|
82 |
area-= returnGeoCArea(xs,ys); |
83 |
} |
84 |
}else{
|
85 |
if (proj.isProjected()) {
|
86 |
area+= returnCoordsArea(xs,ys,new Point2D.Double(xs[xs.length-1].doubleValue(),ys[ys.length-1].doubleValue())); |
87 |
}else{
|
88 |
area+= returnGeoCArea(xs,ys); |
89 |
} |
90 |
} |
91 |
} |
92 |
return area;
|
93 |
} |
94 |
|
95 |
public boolean isCCW(Double[] xs, Double[] ys){ |
96 |
CoordinateList coordList = new CoordinateList();
|
97 |
for (int i = 0; i < ys.length; i++) { |
98 |
Coordinate coord=new Coordinate(xs[i].doubleValue(),ys[i].doubleValue());
|
99 |
coordList.add(coord); |
100 |
} |
101 |
if (coordList.isEmpty())
|
102 |
return true; |
103 |
return CGAlgorithms.isCCW(coordList.toCoordinateArray());
|
104 |
} |
105 |
|
106 |
|
107 |
private double returnGeoCArea(Double[] xs,Double[] ys) { |
108 |
double[] lat=new double[xs.length]; |
109 |
double[] lon=new double[xs.length]; |
110 |
for (int K= 0; K < xs.length; K++){ |
111 |
lon[K]= xs[K].doubleValue()/org.gvsig.fmap.mapcontrol.tools.geo.Geo.Degree; |
112 |
lat[K]= ys[K].doubleValue()/org.gvsig.fmap.mapcontrol.tools.geo.Geo.Degree; |
113 |
} |
114 |
return (org.gvsig.fmap.mapcontrol.tools.geo.Geo.sphericalPolyArea(lat,lon,xs.length-1)*org.gvsig.fmap.mapcontrol.tools.geo.Geo.SqM); |
115 |
} |
116 |
/**
|
117 |
* Calcula el ?rea.
|
118 |
*
|
119 |
* @param aux ?ltimo punto.
|
120 |
*
|
121 |
* @return ?rea.
|
122 |
*/
|
123 |
public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) { |
124 |
Point2D aux=point;
|
125 |
double elArea = 0.0; |
126 |
Point2D pPixel;
|
127 |
Point2D p = new Point2D.Double(); |
128 |
Point2D.Double pAnt = new Point2D.Double(); |
129 |
org.gvsig.fmap.mapcontext.ViewPort vp = getLayer().getMapContext().getViewPort(); |
130 |
for (int pos = 0; pos < xs.length-1; pos++) { |
131 |
pPixel = new Point2D.Double(xs[pos].doubleValue(), |
132 |
ys[pos].doubleValue()); |
133 |
p = pPixel; |
134 |
if (pos == 0) { |
135 |
pAnt.x = aux.getX(); |
136 |
pAnt.y = aux.getY(); |
137 |
} |
138 |
elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY())); |
139 |
pAnt.setLocation(p); |
140 |
} |
141 |
|
142 |
elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY())); |
143 |
elArea = Math.abs(elArea / 2.0); |
144 |
return (elArea/(Math.pow(org.gvsig.fmap.mapcontext.MapContext.getAreaTrans2Meter()[vp.getDistanceArea()],2))); |
145 |
} |
146 |
public String getTooltip(){ |
147 |
return PluginServices.getText(this,"operator")+": "+addText("")+"\n"+getDescription(); |
148 |
} |
149 |
public String getDescription() { |
150 |
return PluginServices.getText(this, "returns") + ": " + |
151 |
PluginServices.getText(this, "numeric_value") + "\n" + |
152 |
PluginServices.getText(this, "description") + ": " + |
153 |
"Returns the area of polygon geometry of this row.";
|
154 |
} |
155 |
} |