svn-gvsig-desktop / trunk / extensions / extMeasureGeometry / src / com / iver / gvsig / PerimeterExtension.java @ 5020
History | View | Annotate | Download (4.43 KB)
1 |
package com.iver.gvsig; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
|
6 |
import com.hardcode.gdbms.engine.values.Value; |
7 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
8 |
import com.iver.andami.PluginServices; |
9 |
import com.iver.andami.plugins.Extension; |
10 |
import com.iver.cit.gvsig.fmap.DriverException; |
11 |
import com.iver.cit.gvsig.fmap.FMap; |
12 |
import com.iver.cit.gvsig.fmap.ViewPort; |
13 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
14 |
import com.iver.cit.gvsig.fmap.core.FShape; |
15 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
16 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
17 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
18 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
19 |
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition; |
20 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
21 |
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver; |
22 |
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
23 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
24 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
25 |
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter; |
26 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
27 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
28 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
29 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
30 |
import com.iver.cit.gvsig.gui.View; |
31 |
import com.iver.cit.gvsig.project.ProjectView; |
32 |
import com.iver.gvsig.measure.Operations; |
33 |
|
34 |
/**
|
35 |
* Extension responsible for calculating the perimeter of the geometries selected,
|
36 |
* only when these they be of type line.
|
37 |
* When there is not an active layer, or some of the geometries selected be not of type line
|
38 |
* this tool will remain in not visible way.
|
39 |
*
|
40 |
* @author Vicente Caballero Navarro
|
41 |
*/
|
42 |
public class PerimeterExtension extends Extension { |
43 |
private FMap map;
|
44 |
private FLyrVect lv;
|
45 |
/**
|
46 |
* @see com.iver.andami.plugins.IExtension#initialize()
|
47 |
*/
|
48 |
public void initialize() { |
49 |
} |
50 |
|
51 |
/**
|
52 |
* @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
53 |
*/
|
54 |
public void execute(String actionCommand) { |
55 |
ViewPort vp=map.getViewPort(); |
56 |
Operations operations=new Operations();
|
57 |
ShpWriter writer=((IndexedShpDriver)lv.getSource().getDriver()).getShpWriter(); |
58 |
SHPLayerDefinition lyrDef; |
59 |
try {
|
60 |
lyrDef = Operations.createLayerDefinition(lv); |
61 |
FieldDescription[] fD=lyrDef.getFieldsDesc();
|
62 |
FieldDescription[] newFD = operations.getPerimeterFields(fD);
|
63 |
lyrDef.setFieldsDesc(newFD); |
64 |
|
65 |
ReadableVectorial adapter = lv.getSource(); |
66 |
File newFile = ((VectorialFileDriver)adapter.getDriver()).getFile();
|
67 |
writer.setFile(newFile); |
68 |
writer.initialize(lyrDef); |
69 |
writer.preProcess(); |
70 |
|
71 |
for (int i=0;i<adapter.getShapeCount();i++){ |
72 |
IFeature feat=adapter.getFeature(i); |
73 |
IGeometry geom=feat.getGeometry().cloneGeometry(); |
74 |
Value[] values=feat.getAttributes();
|
75 |
Value[] newValues=new Value[values.length]; |
76 |
for (int j=0;j<values.length-1;j++){ |
77 |
newValues[j]=values[j]; |
78 |
} |
79 |
|
80 |
double perimeter=operations.getPerimeter(geom,vp);
|
81 |
newValues[values.length-1]=ValueFactory.createValue(perimeter);
|
82 |
DefaultFeature df=new DefaultFeature(geom,newValues);
|
83 |
IRowEdited edRow = new DefaultRowEdited(df, IRowEdited.STATUS_ADDED, i);
|
84 |
writer.process(edRow); |
85 |
} |
86 |
writer.postProcess(); |
87 |
adapter.getDriver().reLoad(); |
88 |
} catch (EditionException e) {
|
89 |
e.printStackTrace(); |
90 |
} catch (DriverException e1) {
|
91 |
e1.printStackTrace(); |
92 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
|
93 |
e1.printStackTrace(); |
94 |
} catch (DriverIOException e) {
|
95 |
e.printStackTrace(); |
96 |
} catch (IOException e) { |
97 |
e.printStackTrace(); |
98 |
} |
99 |
} |
100 |
|
101 |
|
102 |
/**
|
103 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
104 |
*/
|
105 |
public boolean isEnabled() { |
106 |
return true; |
107 |
} |
108 |
|
109 |
/**
|
110 |
* @see com.iver.andami.plugins.IExtension#isVisible()
|
111 |
*/
|
112 |
public boolean isVisible() { |
113 |
com.iver.andami.ui.mdiManager.View f = PluginServices.getMDIManager() |
114 |
.getActiveView(); |
115 |
|
116 |
if (f == null) { |
117 |
return false; |
118 |
} |
119 |
if (f instanceof View) { |
120 |
View vista = (View) f; |
121 |
ProjectView model = vista.getModel(); |
122 |
map = model.getMapContext(); |
123 |
FLayers layers = map.getLayers(); |
124 |
FLayer[] layersActives = layers.getActives();
|
125 |
if (layersActives.length == 1) { |
126 |
if (layersActives[0] instanceof FLyrVect) { |
127 |
lv = (FLyrVect) layersActives[0];
|
128 |
try {
|
129 |
int type = lv.getShapeType();
|
130 |
if (FShape.ARC == type || FShape.LINE == type) {
|
131 |
return true; |
132 |
} |
133 |
} catch (DriverException e) {
|
134 |
return false; |
135 |
} |
136 |
} |
137 |
} |
138 |
|
139 |
} |
140 |
return false; |
141 |
} |
142 |
} |