root / branches / v2_0_0_prep / extensions / extExpressionField / src / org / gvsig / expressionfield / project / documents / table / operators / Area.java @ 29628
History | View | Annotate | Download (5.45 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 && geomType==Geometry.TYPES.SURFACE);
|
63 |
} |
64 |
|
65 |
private double returnArea(org.gvsig.fmap.geom.Geometry geom) { |
66 |
ArrayList parts=getXY(geom);
|
67 |
double area=0; |
68 |
for (int i=0;i<parts.size();i++){ |
69 |
Double[][] xsys=(Double[][])parts.get(i);//getXY(geom); |
70 |
Double[] xs=xsys[0]; |
71 |
Double[] ys=xsys[1]; |
72 |
IProjection proj=getLayer().getMapContext().getProjection(); |
73 |
if (isCCW(xs, ys)){
|
74 |
if (proj.isProjected()) {
|
75 |
area-= returnCoordsArea(xs,ys,new Point2D.Double(xs[xs.length-1].doubleValue(),ys[ys.length-1].doubleValue())); |
76 |
}else{
|
77 |
area-= returnGeoCArea(xs,ys); |
78 |
} |
79 |
}else{
|
80 |
if (proj.isProjected()) {
|
81 |
area+= returnCoordsArea(xs,ys,new Point2D.Double(xs[xs.length-1].doubleValue(),ys[ys.length-1].doubleValue())); |
82 |
}else{
|
83 |
area+= returnGeoCArea(xs,ys); |
84 |
} |
85 |
} |
86 |
} |
87 |
return area;
|
88 |
} |
89 |
|
90 |
public boolean isCCW(Double[] xs, Double[] ys){ |
91 |
CoordinateList coordList = new CoordinateList();
|
92 |
for (int i = 0; i < ys.length; i++) { |
93 |
Coordinate coord=new Coordinate(xs[i].doubleValue(),ys[i].doubleValue());
|
94 |
coordList.add(coord); |
95 |
} |
96 |
if (coordList.isEmpty())
|
97 |
return true; |
98 |
return CGAlgorithms.isCCW(coordList.toCoordinateArray());
|
99 |
} |
100 |
|
101 |
|
102 |
private double returnGeoCArea(Double[] xs,Double[] ys) { |
103 |
double[] lat=new double[xs.length]; |
104 |
double[] lon=new double[xs.length]; |
105 |
for (int K= 0; K < xs.length; K++){ |
106 |
lon[K]= xs[K].doubleValue()/org.gvsig.fmap.mapcontrol.tools.geo.Geo.Degree; |
107 |
lat[K]= ys[K].doubleValue()/org.gvsig.fmap.mapcontrol.tools.geo.Geo.Degree; |
108 |
} |
109 |
return (org.gvsig.fmap.mapcontrol.tools.geo.Geo.sphericalPolyArea(lat,lon,xs.length-1)*org.gvsig.fmap.mapcontrol.tools.geo.Geo.SqM); |
110 |
} |
111 |
/**
|
112 |
* Calcula el ?rea.
|
113 |
*
|
114 |
* @param aux ?ltimo punto.
|
115 |
*
|
116 |
* @return ?rea.
|
117 |
*/
|
118 |
public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) { |
119 |
Point2D aux=point;
|
120 |
double elArea = 0.0; |
121 |
Point2D pPixel;
|
122 |
Point2D p = new Point2D.Double(); |
123 |
Point2D.Double pAnt = new Point2D.Double(); |
124 |
org.gvsig.fmap.mapcontext.ViewPort vp = getLayer().getMapContext().getViewPort(); |
125 |
for (int pos = 0; pos < xs.length-1; pos++) { |
126 |
pPixel = new Point2D.Double(xs[pos].doubleValue(), |
127 |
ys[pos].doubleValue()); |
128 |
p = pPixel; |
129 |
if (pos == 0) { |
130 |
pAnt.x = aux.getX(); |
131 |
pAnt.y = aux.getY(); |
132 |
} |
133 |
elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY())); |
134 |
pAnt.setLocation(p); |
135 |
} |
136 |
|
137 |
elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY())); |
138 |
elArea = Math.abs(elArea / 2.0); |
139 |
return (elArea/(Math.pow(org.gvsig.fmap.mapcontext.MapContext.getAreaTrans2Meter()[vp.getDistanceArea()],2))); |
140 |
} |
141 |
public String getTooltip(){ |
142 |
return PluginServices.getText(this,"operator")+": "+addText("")+"\n"+getDescription(); |
143 |
} |
144 |
public String getDescription() { |
145 |
return PluginServices.getText(this, "returns") + ": " + |
146 |
PluginServices.getText(this, "numeric_value") + "\n" + |
147 |
PluginServices.getText(this, "description") + ": " + |
148 |
"Returns the area of polygon geometry of this row.";
|
149 |
} |
150 |
} |