svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / project / documents / table / operators / Area.java @ 10254
History | View | Annotate | Download (3.34 KB)
1 |
package com.iver.cit.gvsig.project.documents.table.operators; |
---|---|
2 |
|
3 |
import java.awt.geom.Point2D; |
4 |
|
5 |
import org.apache.bsf.BSFException; |
6 |
import org.apache.bsf.BSFManager; |
7 |
import org.cresques.cts.IProjection; |
8 |
|
9 |
import com.iver.cit.gvsig.ExpresionFieldExtension; |
10 |
import com.iver.cit.gvsig.fmap.MapContext; |
11 |
import com.iver.cit.gvsig.fmap.ViewPort; |
12 |
import com.iver.cit.gvsig.fmap.core.FShape; |
13 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
14 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
15 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
16 |
import com.iver.cit.gvsig.fmap.tools.geo.Geo; |
17 |
import com.iver.cit.gvsig.project.documents.table.GraphicOperator; |
18 |
import com.iver.cit.gvsig.project.documents.table.IOperator; |
19 |
import com.iver.cit.gvsig.project.documents.table.Index; |
20 |
/**
|
21 |
* @author Vicente Caballero Navarro
|
22 |
*/
|
23 |
public class Area extends GraphicOperator{ |
24 |
|
25 |
public String addText(String s) { |
26 |
return s.concat(toString()+"()"); |
27 |
} |
28 |
public double process(Index index) throws DriverIOException { |
29 |
ReadableVectorial adapter = getLayer().getSource(); |
30 |
IGeometry geom=adapter.getShape(index.get()); |
31 |
int distanceUnits=getLayer().getMapContext().getViewPort().getDistanceUnits();
|
32 |
return returnArea(geom)/Math.pow(MapContext.CHANGEM[distanceUnits],2); |
33 |
} |
34 |
public void eval(BSFManager interpreter) throws BSFException { |
35 |
interpreter.declareBean("area",this,Area.class); |
36 |
interpreter.eval(ExpresionFieldExtension.BEANSHELL,null,-1,-1,"double area(){return area.process(indexRow);};"); |
37 |
} |
38 |
public String toString() { |
39 |
return "area"; |
40 |
} |
41 |
public boolean isEnable() { |
42 |
if (getLayer()==null) |
43 |
return false; |
44 |
ReadableVectorial adapter = getLayer().getSource(); |
45 |
int type=FShape.POINT;
|
46 |
try {
|
47 |
type=adapter.getShapeType(); |
48 |
} catch (DriverIOException e) {
|
49 |
e.printStackTrace(); |
50 |
} |
51 |
return (getType()==IOperator.NUMBER && type==FShape.POLYGON);
|
52 |
} |
53 |
|
54 |
private double returnArea(IGeometry geom) { |
55 |
Double[][] xsys=getXY(geom); |
56 |
Double[] xs=xsys[0]; |
57 |
Double[] ys=xsys[1]; |
58 |
IProjection proj=getLayer().getMapContext().getProjection(); |
59 |
if (proj.isProjected()) {
|
60 |
return returnCoordsArea(xs,ys,new Point2D.Double(xs[xs.length-1].doubleValue(),ys[ys.length-1].doubleValue())); |
61 |
} |
62 |
return returnGeoCArea(xs,ys);
|
63 |
} |
64 |
private double returnGeoCArea(Double[] xs,Double[] ys) { |
65 |
double[] lat=new double[xs.length]; |
66 |
double[] lon=new double[xs.length]; |
67 |
for (int K= 0; K < xs.length; K++){ |
68 |
lon[K]= xs[K].doubleValue()/Geo.Degree; |
69 |
lat[K]= ys[K].doubleValue()/Geo.Degree; |
70 |
} |
71 |
return (Geo.sphericalPolyArea(lat,lon,xs.length-1)*Geo.SqM); |
72 |
} |
73 |
/**
|
74 |
* Calcula el ?rea.
|
75 |
*
|
76 |
* @param aux ?ltimo punto.
|
77 |
*
|
78 |
* @return ?rea.
|
79 |
*/
|
80 |
public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) { |
81 |
Point2D aux=point;
|
82 |
double elArea = 0.0; |
83 |
Point2D pPixel;
|
84 |
Point2D p = new Point2D.Double(); |
85 |
Point2D.Double pAnt = new Point2D.Double(); |
86 |
ViewPort vp = getLayer().getMapContext().getViewPort(); |
87 |
for (int pos = 0; pos < xs.length-1; pos++) { |
88 |
pPixel = new Point2D.Double(xs[pos].doubleValue(), |
89 |
ys[pos].doubleValue()); |
90 |
p = pPixel; |
91 |
if (pos == 0) { |
92 |
pAnt.x = aux.getX(); |
93 |
pAnt.y = aux.getY(); |
94 |
} |
95 |
elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY())); |
96 |
pAnt.setLocation(p); |
97 |
} |
98 |
|
99 |
elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY())); |
100 |
elArea = Math.abs(elArea / 2.0); |
101 |
return (elArea*(Math.pow(MapContext.CHANGEM[vp.getMapUnits()],2))); |
102 |
} |
103 |
} |