root / trunk / libraries / libExpressions / src / main / java / org / gvsig / operators / Perimeter.java @ 23299
History | View | Annotate | Download (3.45 KB)
1 |
package org.gvsig.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.gvsig.baseclasses.GraphicOperator; |
9 |
import org.gvsig.baseclasses.IOperator; |
10 |
import org.gvsig.baseclasses.Index; |
11 |
import org.gvsig.expresions.EvalOperatorsTask; |
12 |
|
13 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
14 |
import com.iver.andami.messages.NotificationManager; |
15 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
16 |
import com.iver.cit.gvsig.fmap.MapContext; |
17 |
import com.iver.cit.gvsig.fmap.ViewPort; |
18 |
import com.iver.cit.gvsig.fmap.core.FShape; |
19 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
20 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
21 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
22 |
/**
|
23 |
* @author Vicente Caballero Navarro
|
24 |
*/
|
25 |
public class Perimeter 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=null;
|
33 |
try {
|
34 |
geom = adapter.getShape(index.get()); |
35 |
} catch (ExpansionFileReadException e) {
|
36 |
throw new DriverIOException(e); |
37 |
} catch (ReadDriverException e) {
|
38 |
throw new DriverIOException(e); |
39 |
} |
40 |
ArrayList parts=getXY(geom);
|
41 |
double perimeter=0; |
42 |
for (int j=0;j<parts.size();j++){ |
43 |
Double[][] xsys=(Double[][])parts.get(j);//getXY(geom); |
44 |
double dist = 0; |
45 |
double distAll = 0; |
46 |
|
47 |
ViewPort vp = getLayer().getMapContext().getViewPort(); |
48 |
for (int i = 0; i < (xsys[0].length - 1); i++) { |
49 |
dist = 0;
|
50 |
|
51 |
Point2D p = new Point2D.Double(xsys[0][i].doubleValue(), xsys[1][i].doubleValue());//vp.toMapPoint(new Point(event.getXs()[i].intValue(), event.getYs()[i].intValue())); |
52 |
Point2D p2 = new Point2D.Double(xsys[0][i + 1].doubleValue(), xsys[1][i + 1].doubleValue());//vp.toMapPoint(new Point(event.getXs()[i + 1].intValue(), event.getYs()[i + 1].intValue())); |
53 |
dist = vp.distanceWorld(p,p2); |
54 |
//System.out.println("distancia parcial = "+dist);
|
55 |
distAll += dist; |
56 |
} |
57 |
int distanceUnits=vp.getDistanceUnits();
|
58 |
perimeter+= distAll/MapContext.CHANGEM[distanceUnits]; |
59 |
} |
60 |
return perimeter;
|
61 |
} |
62 |
public void eval(BSFManager interpreter) throws BSFException { |
63 |
interpreter.declareBean("jperimeter",this,Perimeter.class); |
64 |
// interpreter.eval(ExpressionFieldExtension.BEANSHELL,null,-1,-1,"double perimeter(){return jperimeter.process(indexRow);};");
|
65 |
interpreter.exec(EvalOperatorsTask.JYTHON,null,-1,-1,"def perimeter():\n" + |
66 |
" return jperimeter.process(indexRow)");
|
67 |
} |
68 |
public String toString() { |
69 |
return "perimeter"; |
70 |
} |
71 |
public boolean isEnable() { |
72 |
if (getLayer()==null) |
73 |
return false; |
74 |
ReadableVectorial adapter = getLayer().getSource(); |
75 |
int type=FShape.POINT;
|
76 |
try {
|
77 |
type=adapter.getShapeType(); |
78 |
} catch (ReadDriverException e) {
|
79 |
NotificationManager.addError(e); |
80 |
} |
81 |
return (getType()==IOperator.NUMBER && (type==FShape.POLYGON || type==FShape.LINE));
|
82 |
} |
83 |
public String getTooltip(){ |
84 |
return "operator"+": "+addText("")+"\n"+getDescription(); |
85 |
} |
86 |
public String getDescription() { |
87 |
return "returns" + ": " + |
88 |
"numeric_value" + "\n" + |
89 |
"description" + ": " + |
90 |
"Returns the perimeter of polygon or line geometry of this row.";
|
91 |
} |
92 |
} |