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