root / trunk / extensions / extMeasureGeometry / src / com / iver / gvsig / PerimeterExtension.java @ 7304
History | View | Annotate | Download (5.01 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.MapContext; |
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.UtilFunctions; |
26 |
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter; |
27 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
28 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
29 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
30 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
31 |
import com.iver.cit.gvsig.fmap.layers.VectorialFileAdapter; |
32 |
import com.iver.cit.gvsig.project.documents.view.ProjectView; |
33 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
34 |
import com.iver.gvsig.measure.Operations; |
35 |
|
36 |
/**
|
37 |
* Extension responsible for calculating the perimeter of the geometries selected,
|
38 |
* only when these they be of type line.
|
39 |
* When there is not an active layer, or some of the geometries selected be not of type line
|
40 |
* this tool will remain in not visible way.
|
41 |
*
|
42 |
* @author Vicente Caballero Navarro
|
43 |
*/
|
44 |
public class PerimeterExtension extends Extension { |
45 |
private MapContext map;
|
46 |
private FLyrVect lv;
|
47 |
/**
|
48 |
* @see com.iver.andami.plugins.IExtension#initialize()
|
49 |
*/
|
50 |
public void initialize() { |
51 |
} |
52 |
|
53 |
/**
|
54 |
* @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
|
55 |
*/
|
56 |
public void execute(String actionCommand) { |
57 |
ViewPort vp=map.getViewPort(); |
58 |
Operations operations=new Operations();
|
59 |
|
60 |
ShpWriter writer=((IndexedShpDriver)lv.getSource().getDriver()).getShpWriter(); |
61 |
SHPLayerDefinition lyrDef; |
62 |
try {
|
63 |
ReadableVectorial adapter = lv.getSource(); |
64 |
int numRows=adapter.getShapeCount();
|
65 |
IGeometry[] geometries=new IGeometry[numRows]; |
66 |
Object[] values=new Object[numRows]; |
67 |
for (int i=0;i<numRows;i++){ |
68 |
IFeature feat=adapter.getFeature(i); |
69 |
geometries[i]=feat.getGeometry().cloneGeometry(); |
70 |
values[i]=feat.getAttributes(); |
71 |
} |
72 |
|
73 |
lyrDef = Operations.createLayerDefinition(lv); |
74 |
FieldDescription[] fD=lyrDef.getFieldsDesc();
|
75 |
FieldDescription[] newFD = operations.getPerimeterFields(fD);
|
76 |
lyrDef.setFieldsDesc(newFD); |
77 |
|
78 |
|
79 |
File newFile = ((VectorialFileDriver)adapter.getDriver()).getFile();
|
80 |
writer.setFile(newFile); |
81 |
writer.initialize(lyrDef); |
82 |
writer.preProcess(); |
83 |
|
84 |
for (int i=0;i<numRows;i++){ |
85 |
IGeometry geom=geometries[i]; |
86 |
Value[] vals=(Value[])values[i]; |
87 |
Value[] newValues=new Value[vals.length+1]; |
88 |
for (int j=0;j<vals.length;j++){ |
89 |
newValues[j]=vals[j]; |
90 |
} |
91 |
|
92 |
double perimeter=operations.getPerimeter(geom,vp);
|
93 |
newValues[newValues.length-1]=ValueFactory.createValue(perimeter);
|
94 |
DefaultFeature df=new DefaultFeature(geom,newValues);
|
95 |
IRowEdited edRow = new DefaultRowEdited(df, IRowEdited.STATUS_ADDED, i);
|
96 |
writer.process(edRow); |
97 |
} |
98 |
writer.postProcess(); |
99 |
adapter.getDriver().reload(); |
100 |
|
101 |
VectorialFileAdapter newAdapter = new VectorialFileAdapter(newFile);
|
102 |
newAdapter.setDriver(adapter.getDriver()); |
103 |
|
104 |
|
105 |
lv.setSource(newAdapter); |
106 |
lv.setRecordset(newAdapter.getRecordset()); |
107 |
|
108 |
|
109 |
} catch (EditionException e) {
|
110 |
e.printStackTrace(); |
111 |
} catch (DriverException e1) {
|
112 |
e1.printStackTrace(); |
113 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
|
114 |
e1.printStackTrace(); |
115 |
} catch (DriverIOException e) {
|
116 |
e.printStackTrace(); |
117 |
} catch (IOException e) { |
118 |
e.printStackTrace(); |
119 |
} |
120 |
} |
121 |
|
122 |
|
123 |
/**
|
124 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
125 |
*/
|
126 |
public boolean isEnabled() { |
127 |
return true; |
128 |
} |
129 |
|
130 |
/**
|
131 |
* @see com.iver.andami.plugins.IExtension#isVisible()
|
132 |
*/
|
133 |
public boolean isVisible() { |
134 |
com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager() |
135 |
.getActiveWindow(); |
136 |
|
137 |
if (f == null) { |
138 |
return false; |
139 |
} |
140 |
if (f instanceof View) { |
141 |
View vista = (View) f; |
142 |
ProjectView model = vista.getModel(); |
143 |
map = model.getMapContext(); |
144 |
FLayers layers = map.getLayers(); |
145 |
FLayer[] layersActives = layers.getActives();
|
146 |
if (layersActives.length == 1) { |
147 |
if (layersActives[0] instanceof FLyrVect) { |
148 |
lv = (FLyrVect) layersActives[0];
|
149 |
try {
|
150 |
int type = lv.getShapeType();
|
151 |
if (FShape.ARC == type || FShape.LINE == type) {
|
152 |
return true; |
153 |
} |
154 |
} catch (DriverException e) {
|
155 |
return false; |
156 |
} |
157 |
} |
158 |
} |
159 |
|
160 |
} |
161 |
return false; |
162 |
} |
163 |
} |