root / trunk / extensions / extMeasureGeometry / src / com / iver / gvsig / PerimeterExtension.java @ 6877
History | View | Annotate | Download (4.96 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 | 5715 | fjp | import com.iver.cit.gvsig.fmap.edition.UtilFunctions; |
26 | 4796 | caballero | 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 | 5732 | fjp | 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 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 | 5005 | jorpiell | public class PerimeterExtension 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 | 6366 | fjp | |
60 | 4796 | caballero | ShpWriter writer=((IndexedShpDriver)lv.getSource().getDriver()).getShpWriter(); |
61 | SHPLayerDefinition lyrDef; |
||
62 | try {
|
||
63 | 5695 | caballero | 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 | 4796 | caballero | lyrDef = Operations.createLayerDefinition(lv); |
74 | FieldDescription[] fD=lyrDef.getFieldsDesc();
|
||
75 | FieldDescription[] newFD = operations.getPerimeterFields(fD);
|
||
76 | lyrDef.setFieldsDesc(newFD); |
||
77 | |||
78 | 5695 | caballero | |
79 | 4796 | caballero | File newFile = ((VectorialFileDriver)adapter.getDriver()).getFile();
|
80 | writer.setFile(newFile); |
||
81 | writer.initialize(lyrDef); |
||
82 | writer.preProcess(); |
||
83 | |||
84 | 5695 | caballero | 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 | 4796 | caballero | } |
91 | |||
92 | double perimeter=operations.getPerimeter(geom,vp);
|
||
93 | 5695 | caballero | newValues[newValues.length-1]=ValueFactory.createValue(perimeter);
|
94 | 6366 | fjp | DefaultFeature df=new DefaultFeature(geom,newValues);
|
95 | 4796 | caballero | IRowEdited edRow = new DefaultRowEdited(df, IRowEdited.STATUS_ADDED, i);
|
96 | writer.process(edRow); |
||
97 | } |
||
98 | writer.postProcess(); |
||
99 | 6366 | fjp | adapter.getDriver().reload(); |
100 | |||
101 | 5732 | fjp | VectorialFileAdapter newAdapter = new VectorialFileAdapter(newFile);
|
102 | newAdapter.setDriver(adapter.getDriver()); |
||
103 | |||
104 | 6366 | fjp | |
105 | 5732 | fjp | lv.setSource(newAdapter); |
106 | lv.setRecordset(newAdapter.getRecordset()); |
||
107 | |||
108 | 6366 | fjp | |
109 | 4796 | caballero | } 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 | 5020 | caballero | } catch (IOException e) { |
118 | e.printStackTrace(); |
||
119 | 4796 | caballero | } |
120 | } |
||
121 | |||
122 | |||
123 | /**
|
||
124 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#isEnabled()
|
125 | 4796 | caballero | */
|
126 | public boolean isEnabled() { |
||
127 | return true; |
||
128 | } |
||
129 | |||
130 | /**
|
||
131 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#isVisible()
|
132 | 4796 | caballero | */
|
133 | public boolean isVisible() { |
||
134 | 6877 | cesar | com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager() |
135 | 4796 | caballero | .getActiveView(); |
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 | } |