root / trunk / extensions / extMeasureGeometry / src / com / iver / gvsig / AddXYPointThemeExtension.java @ 6877
History | View | Annotate | Download (4.92 KB)
1 |
package com.iver.gvsig; |
---|---|
2 |
|
3 |
import java.awt.geom.PathIterator; |
4 |
import java.io.File; |
5 |
import java.io.IOException; |
6 |
|
7 |
import com.hardcode.gdbms.engine.values.Value; |
8 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
9 |
import com.iver.andami.PluginServices; |
10 |
import com.iver.andami.plugins.Extension; |
11 |
import com.iver.cit.gvsig.fmap.DriverException; |
12 |
import com.iver.cit.gvsig.fmap.FMap; |
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.gui.View; |
32 |
import com.iver.cit.gvsig.project.ProjectView; |
33 |
import com.iver.gvsig.measure.Operations; |
34 |
|
35 |
/**
|
36 |
* Extension responsible for calculating the perimeter of the geometries selected,
|
37 |
* only when these they be of type line.
|
38 |
* When there is not an active layer, or some of the geometries selected be not of type line
|
39 |
* this tool will remain in not visible way.
|
40 |
*
|
41 |
* @author Vicente Caballero Navarro
|
42 |
*/
|
43 |
public class AddXYPointThemeExtension extends Extension { |
44 |
|
45 |
private FMap 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 |
Operations operations=new Operations();
|
58 |
ShpWriter writer=((IndexedShpDriver)lv.getSource().getDriver()).getShpWriter(); |
59 |
SHPLayerDefinition lyrDef; |
60 |
try {
|
61 |
ReadableVectorial adapter = lv.getSource(); |
62 |
int numRows=adapter.getShapeCount();
|
63 |
IGeometry[] geometries=new IGeometry[numRows]; |
64 |
Object[] values=new Object[numRows]; |
65 |
for (int i=0;i<numRows;i++){ |
66 |
IFeature feat=adapter.getFeature(i); |
67 |
geometries[i]=feat.getGeometry().cloneGeometry(); |
68 |
values[i]=feat.getAttributes(); |
69 |
} |
70 |
|
71 |
|
72 |
|
73 |
lyrDef = Operations.createLayerDefinition(lv); |
74 |
|
75 |
FieldDescription[] newFD = operations.getXYFields(lyrDef);
|
76 |
|
77 |
lyrDef.setFieldsDesc(newFD); |
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+2]; |
88 |
for (int j=0;j<vals.length;j++){ |
89 |
newValues[j]=vals[j]; |
90 |
} |
91 |
|
92 |
double[] theData=new double[6]; |
93 |
PathIterator gpxi = geom.getPathIterator(null); |
94 |
gpxi.currentSegment(theData); |
95 |
newValues[newValues.length-2]=ValueFactory.createValue(theData[0]); |
96 |
newValues[newValues.length-1]=ValueFactory.createValue(theData[1]); |
97 |
DefaultFeature df=new DefaultFeature(geom,newValues);
|
98 |
IRowEdited edRow = new DefaultRowEdited(df, IRowEdited.STATUS_ADDED, i);
|
99 |
writer.process(edRow); |
100 |
} |
101 |
writer.postProcess(); |
102 |
adapter.getDriver().reload(); |
103 |
|
104 |
VectorialFileAdapter newAdapter = new VectorialFileAdapter(newFile);
|
105 |
newAdapter.setDriver(adapter.getDriver()); |
106 |
|
107 |
|
108 |
lv.setSource(newAdapter); |
109 |
lv.setRecordset(newAdapter.getRecordset()); |
110 |
|
111 |
} catch (EditionException e) {
|
112 |
e.printStackTrace(); |
113 |
} catch (DriverException e1) {
|
114 |
e1.printStackTrace(); |
115 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e1) {
|
116 |
e1.printStackTrace(); |
117 |
} catch (DriverIOException e) {
|
118 |
e.printStackTrace(); |
119 |
} catch (IOException e) { |
120 |
e.printStackTrace(); |
121 |
} |
122 |
} |
123 |
|
124 |
|
125 |
|
126 |
/**
|
127 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
128 |
*/
|
129 |
public boolean isEnabled() { |
130 |
return true; |
131 |
} |
132 |
|
133 |
/**
|
134 |
* @see com.iver.andami.plugins.IExtension#isVisible()
|
135 |
*/
|
136 |
public boolean isVisible() { |
137 |
com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager() |
138 |
.getActiveView(); |
139 |
|
140 |
if (f == null) { |
141 |
return false; |
142 |
} |
143 |
if (f instanceof View) { |
144 |
View vista = (View) f; |
145 |
ProjectView model = vista.getModel(); |
146 |
map = model.getMapContext(); |
147 |
FLayers layers = map.getLayers(); |
148 |
FLayer[] layersActives = layers.getActives();
|
149 |
if (layersActives.length == 1) { |
150 |
if (layersActives[0] instanceof FLyrVect) { |
151 |
lv = (FLyrVect) layersActives[0];
|
152 |
try {
|
153 |
if (lv.getShapeType() == FShape.POINT) {
|
154 |
return true; |
155 |
} |
156 |
} catch (DriverException e) {
|
157 |
return false; |
158 |
} |
159 |
} |
160 |
} |
161 |
} |
162 |
return false; |
163 |
} |
164 |
|
165 |
} |