root / branches / v10 / extensions / extExpressionField / src / com / iver / cit / gvsig / project / documents / table / operators / Area.java @ 20331
History | View | Annotate | Download (4.07 KB)
1 | 10229 | caballero | package com.iver.cit.gvsig.project.documents.table.operators; |
---|---|---|---|
2 | |||
3 | import java.awt.geom.Point2D; |
||
4 | 19023 | vcaballero | import java.util.ArrayList; |
5 | 10229 | caballero | |
6 | import org.apache.bsf.BSFException; |
||
7 | import org.apache.bsf.BSFManager; |
||
8 | import org.cresques.cts.IProjection; |
||
9 | |||
10 | 11111 | caballero | import com.iver.andami.PluginServices; |
11 | 10229 | caballero | import com.iver.cit.gvsig.ExpresionFieldExtension; |
12 | import com.iver.cit.gvsig.fmap.MapContext; |
||
13 | import com.iver.cit.gvsig.fmap.ViewPort; |
||
14 | import com.iver.cit.gvsig.fmap.core.FShape; |
||
15 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
16 | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
||
17 | import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
||
18 | import com.iver.cit.gvsig.fmap.tools.geo.Geo; |
||
19 | import com.iver.cit.gvsig.project.documents.table.GraphicOperator; |
||
20 | import com.iver.cit.gvsig.project.documents.table.IOperator; |
||
21 | import com.iver.cit.gvsig.project.documents.table.Index; |
||
22 | /**
|
||
23 | * @author Vicente Caballero Navarro
|
||
24 | */
|
||
25 | public class Area extends GraphicOperator{ |
||
26 | |||
27 | public String addText(String s) { |
||
28 | return s.concat(toString()+"()"); |
||
29 | } |
||
30 | public double process(Index index) throws DriverIOException { |
||
31 | ReadableVectorial adapter = getLayer().getSource(); |
||
32 | IGeometry geom=adapter.getShape(index.get()); |
||
33 | 20331 | vcaballero | int distanceArea=getLayer().getMapContext().getViewPort().getDistanceArea();
|
34 | return returnArea(geom)/Math.pow(MapContext.getAreaTrans2Meter()[distanceArea],2); |
||
35 | 10229 | caballero | } |
36 | public void eval(BSFManager interpreter) throws BSFException { |
||
37 | 11222 | caballero | interpreter.declareBean("jarea",this,Area.class); |
38 | // interpreter.eval(ExpresionFieldExtension.BEANSHELL,null,-1,-1,"double area(){return area.process(indexRow);};");
|
||
39 | interpreter.exec(ExpresionFieldExtension.JYTHON,null,-1,-1,"def area():\n" + |
||
40 | " return jarea.process(indexRow)");
|
||
41 | 10229 | caballero | } |
42 | public String toString() { |
||
43 | return "area"; |
||
44 | } |
||
45 | public boolean isEnable() { |
||
46 | if (getLayer()==null) |
||
47 | return false; |
||
48 | ReadableVectorial adapter = getLayer().getSource(); |
||
49 | int type=FShape.POINT;
|
||
50 | try {
|
||
51 | type=adapter.getShapeType(); |
||
52 | } catch (DriverIOException e) {
|
||
53 | e.printStackTrace(); |
||
54 | } |
||
55 | return (getType()==IOperator.NUMBER && type==FShape.POLYGON);
|
||
56 | } |
||
57 | |||
58 | private double returnArea(IGeometry geom) { |
||
59 | 19023 | vcaballero | ArrayList parts=getXY(geom);
|
60 | double area=0; |
||
61 | for (int i=0;i<parts.size();i++){ |
||
62 | Double[][] xsys=(Double[][])parts.get(i);//getXY(geom); |
||
63 | 10229 | caballero | Double[] xs=xsys[0]; |
64 | Double[] ys=xsys[1]; |
||
65 | IProjection proj=getLayer().getMapContext().getProjection(); |
||
66 | if (proj.isProjected()) {
|
||
67 | 19023 | vcaballero | area+= returnCoordsArea(xs,ys,new Point2D.Double(xs[xs.length-1].doubleValue(),ys[ys.length-1].doubleValue())); |
68 | 19153 | vcaballero | }else{
|
69 | area+= returnGeoCArea(xs,ys); |
||
70 | 10229 | caballero | } |
71 | 19023 | vcaballero | } |
72 | return area;
|
||
73 | 10229 | caballero | } |
74 | private double returnGeoCArea(Double[] xs,Double[] ys) { |
||
75 | double[] lat=new double[xs.length]; |
||
76 | double[] lon=new double[xs.length]; |
||
77 | for (int K= 0; K < xs.length; K++){ |
||
78 | lon[K]= xs[K].doubleValue()/Geo.Degree; |
||
79 | lat[K]= ys[K].doubleValue()/Geo.Degree; |
||
80 | } |
||
81 | return (Geo.sphericalPolyArea(lat,lon,xs.length-1)*Geo.SqM); |
||
82 | } |
||
83 | /**
|
||
84 | * Calcula el ?rea.
|
||
85 | *
|
||
86 | * @param aux ?ltimo punto.
|
||
87 | *
|
||
88 | * @return ?rea.
|
||
89 | */
|
||
90 | public double returnCoordsArea(Double[] xs,Double[] ys, Point2D point) { |
||
91 | Point2D aux=point;
|
||
92 | double elArea = 0.0; |
||
93 | Point2D pPixel;
|
||
94 | Point2D p = new Point2D.Double(); |
||
95 | Point2D.Double pAnt = new Point2D.Double(); |
||
96 | ViewPort vp = getLayer().getMapContext().getViewPort(); |
||
97 | for (int pos = 0; pos < xs.length-1; pos++) { |
||
98 | pPixel = new Point2D.Double(xs[pos].doubleValue(), |
||
99 | ys[pos].doubleValue()); |
||
100 | p = pPixel; |
||
101 | if (pos == 0) { |
||
102 | pAnt.x = aux.getX(); |
||
103 | pAnt.y = aux.getY(); |
||
104 | } |
||
105 | elArea = elArea + ((pAnt.x - p.getX()) * (pAnt.y + p.getY())); |
||
106 | pAnt.setLocation(p); |
||
107 | } |
||
108 | |||
109 | elArea = elArea + ((pAnt.x - aux.getX()) * (pAnt.y + aux.getY())); |
||
110 | elArea = Math.abs(elArea / 2.0); |
||
111 | return (elArea*(Math.pow(MapContext.CHANGEM[vp.getMapUnits()],2))); |
||
112 | } |
||
113 | 11111 | caballero | public String getTooltip(){ |
114 | return PluginServices.getText(this,"operator")+": "+addText("")+"\n"+getDescription(); |
||
115 | } |
||
116 | public String getDescription() { |
||
117 | return PluginServices.getText(this, "returns") + ": " + |
||
118 | PluginServices.getText(this, "numeric_value") + "\n" + |
||
119 | PluginServices.getText(this, "description") + ": " + |
||
120 | "Returns the area of polygon geometry of this row.";
|
||
121 | } |
||
122 | 10229 | caballero | } |