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