root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / writers / shp / MultiShpWriter.java @ 10627
History | View | Annotate | Download (8.58 KB)
1 |
package com.iver.cit.gvsig.fmap.edition.writers.shp; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Properties; |
6 |
|
7 |
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException; |
8 |
import com.hardcode.gdbms.driver.exceptions.SchemaEditionException; |
9 |
import com.iver.cit.gvsig.exceptions.visitors.ProcessVisitorException; |
10 |
import com.iver.cit.gvsig.exceptions.visitors.ProcessWriterVisitorException; |
11 |
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException; |
12 |
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException; |
13 |
import com.iver.cit.gvsig.exceptions.visitors.VisitorException; |
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.drivers.FieldDescription; |
17 |
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition; |
18 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
19 |
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition; |
20 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
21 |
import com.iver.cit.gvsig.fmap.edition.ISchemaManager; |
22 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
23 |
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager; |
24 |
import com.iver.utiles.FileUtils; |
25 |
|
26 |
/**
|
27 |
* This writer wraps three ShpWriters, one for points, one for lines and one for
|
28 |
* polygons geometry types. <br>
|
29 |
*
|
30 |
* It allows you to save a FLyrVect with MULTI shape type in SHP file format
|
31 |
* (that doesnt allow to mix different geometry types). To do that, this IWriter
|
32 |
* creates a different SHP file for any geometry type, in a transparent manner
|
33 |
* for the programmer. <br>
|
34 |
* If your geometries doesnt hava a given geometry type, the Writer wont create
|
35 |
* a file for this geometry type. <br>
|
36 |
* <code>
|
37 |
* MultiShpWriter writer = new MultiShpWriter();
|
38 |
* writer.setFile(dxfFile);
|
39 |
* writer.initialize(layerDefinition);
|
40 |
* writer.preProcess();
|
41 |
* # obtain features from an iterator
|
42 |
* writer.process(feature);
|
43 |
* writer.postProcess();
|
44 |
* </code>
|
45 |
*
|
46 |
*/
|
47 |
public class MultiShpWriter implements IWriter { |
48 |
/**
|
49 |
* original file name selected by user
|
50 |
*/
|
51 |
String file = null; |
52 |
|
53 |
/**
|
54 |
* original layer definition
|
55 |
*/
|
56 |
ILayerDefinition layerDefinition;// only works with layers (no only
|
57 |
// tables)
|
58 |
|
59 |
IWriter polygons; |
60 |
|
61 |
File polygonsFile;
|
62 |
|
63 |
IWriter lines; |
64 |
|
65 |
File linesFile;
|
66 |
|
67 |
IWriter points; |
68 |
|
69 |
File pointsFile;
|
70 |
|
71 |
public void preProcess() throws StartWriterVisitorException { |
72 |
} |
73 |
|
74 |
/**
|
75 |
* Returns all ShpWriter's created by this wrapper (only those wich writes a
|
76 |
* type of the processed geometries)
|
77 |
*
|
78 |
* @return
|
79 |
*/
|
80 |
public IWriter[] getWriters() { |
81 |
IWriter[] solution;
|
82 |
ArrayList list = new ArrayList(); |
83 |
if (polygons != null) |
84 |
list.add(polygons); |
85 |
if (lines != null) |
86 |
list.add(lines); |
87 |
if (points != null) |
88 |
list.add(points); |
89 |
solution = new IWriter[list.size()];
|
90 |
list.toArray(solution); |
91 |
return solution;
|
92 |
} |
93 |
|
94 |
public void postProcess() throws StopWriterVisitorException { |
95 |
if (polygons != null) |
96 |
polygons.postProcess(); |
97 |
if (lines != null) |
98 |
lines.postProcess(); |
99 |
if (points != null) |
100 |
points.postProcess(); |
101 |
} |
102 |
|
103 |
/**
|
104 |
* Give access to the Writer that processes polygon geometries (and creates
|
105 |
* it if it hasnt yet)
|
106 |
*
|
107 |
* @return
|
108 |
* @throws EditionException
|
109 |
*/
|
110 |
private IWriter getPolygonsWriter() throws VisitorException { |
111 |
if (polygons == null) { |
112 |
polygons = new ShpWriter();
|
113 |
// TODO Hacer que LayerDefinition sea cloneable
|
114 |
SHPLayerDefinition newDefinition = (SHPLayerDefinition) cloneDef(layerDefinition); |
115 |
// we clone layerdefinition to change
|
116 |
// shape type
|
117 |
newDefinition.setShapeType(FShape.POLYGON); |
118 |
polygonsFile = new File(file + "_POL.shp"); |
119 |
newDefinition.setFile(polygonsFile); |
120 |
((ShpWriter) polygons).setFile(polygonsFile); |
121 |
try {
|
122 |
polygons.initialize(newDefinition); |
123 |
} catch (InitializeWriterException e) {
|
124 |
throw new ProcessWriterVisitorException(getName(),e); |
125 |
} |
126 |
|
127 |
try {
|
128 |
getSchemaManager(polygons, polygonsFile) |
129 |
.createSchema(newDefinition); |
130 |
} catch (SchemaEditionException e) {
|
131 |
throw new ProcessWriterVisitorException(getName(),e); |
132 |
} |
133 |
|
134 |
//AZABALA: no si si es neceario
|
135 |
polygons.preProcess(); |
136 |
} |
137 |
return polygons;
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* Given a Writer, and the file where we want to save persistent features,
|
142 |
* it returns an associated ISchemaManager (whose responsability is to
|
143 |
* create the new schema-for files create the new files)
|
144 |
*
|
145 |
* @param writer
|
146 |
* @param file
|
147 |
* @return
|
148 |
*/
|
149 |
|
150 |
private ISchemaManager getSchemaManager(final IWriter writer, |
151 |
final File file) { |
152 |
return new ShpSchemaManager(file.getAbsolutePath()); |
153 |
} |
154 |
|
155 |
/**
|
156 |
* From a given layer definition, it creates a new layer definition 'clon'
|
157 |
* of the initial. It is useful to avoid local changes made by individual
|
158 |
* Writers to the definition affects the others writers (for example, change
|
159 |
* the shape type of the writer)
|
160 |
*
|
161 |
* @param definition
|
162 |
* @return
|
163 |
*/
|
164 |
private ILayerDefinition cloneDef(ILayerDefinition definition) {
|
165 |
ILayerDefinition solution = null;
|
166 |
if (definition instanceof SHPLayerDefinition) { |
167 |
SHPLayerDefinition def = (SHPLayerDefinition) definition; |
168 |
solution = new SHPLayerDefinition();
|
169 |
solution.setName(def.getName()); |
170 |
FieldDescription[] fields = def.getFieldsDesc();
|
171 |
solution.setFieldsDesc(fields); |
172 |
} |
173 |
return solution;
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* Give access to the Writer that processes line geometries (and creates it
|
178 |
* if it hasnt yet)
|
179 |
*
|
180 |
* @return
|
181 |
* @throws EditionException
|
182 |
*/
|
183 |
private IWriter getLinesWriter() throws VisitorException { |
184 |
if (lines == null) { |
185 |
lines = new ShpWriter();
|
186 |
SHPLayerDefinition newDefinition = (SHPLayerDefinition) cloneDef(layerDefinition); |
187 |
// we clone layerdefinition to change
|
188 |
// shape type
|
189 |
newDefinition.setShapeType(FShape.LINE); |
190 |
linesFile = new File(file + "_LIN.shp"); |
191 |
newDefinition.setFile(linesFile); |
192 |
((ShpWriter) lines).setFile(linesFile); |
193 |
try {
|
194 |
lines.initialize(newDefinition); |
195 |
getSchemaManager(lines, linesFile).createSchema(newDefinition); |
196 |
lines.preProcess(); |
197 |
} catch (InitializeWriterException e) {
|
198 |
throw new ProcessWriterVisitorException(getName(),e); |
199 |
} catch (SchemaEditionException e) {
|
200 |
throw new ProcessWriterVisitorException(getName(),e); |
201 |
} |
202 |
} |
203 |
return lines;
|
204 |
} |
205 |
|
206 |
/**
|
207 |
* Give access to the Writer that processes point geometries (and creates it
|
208 |
* if it hasnt yet)
|
209 |
*
|
210 |
* @return
|
211 |
* @throws EditionException
|
212 |
*/
|
213 |
|
214 |
private IWriter getPointsWriter() throws VisitorException{ |
215 |
if (points == null) { |
216 |
points = new ShpWriter();
|
217 |
SHPLayerDefinition newDefinition = (SHPLayerDefinition) cloneDef(layerDefinition); |
218 |
// we clone layerdefinition to change
|
219 |
// shape type
|
220 |
newDefinition.setShapeType(FShape.POINT); |
221 |
pointsFile = new File(file + "_PT.shp"); |
222 |
newDefinition.setFile(pointsFile); |
223 |
((ShpWriter) points).setFile(pointsFile); |
224 |
try {
|
225 |
points.initialize(newDefinition); |
226 |
getSchemaManager(points, pointsFile).createSchema(newDefinition); |
227 |
} catch (InitializeWriterException e) {
|
228 |
throw new ProcessWriterVisitorException(getName(),e); |
229 |
} catch (SchemaEditionException e) {
|
230 |
throw new ProcessWriterVisitorException(getName(),e); |
231 |
} |
232 |
points.preProcess(); |
233 |
} |
234 |
return points;
|
235 |
} |
236 |
|
237 |
/**
|
238 |
* Giving an edited row, writes it with the Writer associated to its
|
239 |
* geometry type
|
240 |
*/
|
241 |
public void process(IRowEdited row) throws VisitorException { |
242 |
IFeature feature = (IFeature) row.getLinkedRow(); |
243 |
int geometryType = feature.getGeometry().getGeometryType();
|
244 |
switch (geometryType) {
|
245 |
case FShape.POINT:
|
246 |
getPointsWriter().process(row); |
247 |
break;
|
248 |
case FShape.LINE:
|
249 |
case FShape.ELLIPSE:
|
250 |
case FShape.ARC:
|
251 |
case FShape.CIRCLE:
|
252 |
getLinesWriter().process(row); |
253 |
break;
|
254 |
|
255 |
case FShape.POLYGON:
|
256 |
getPolygonsWriter().process(row); |
257 |
break;
|
258 |
} |
259 |
} |
260 |
|
261 |
/**
|
262 |
* Sets the file where save the results
|
263 |
*
|
264 |
* @param f
|
265 |
*/
|
266 |
public void setFile(File f) { |
267 |
file = FileUtils.getFileWithoutExtension(f); |
268 |
} |
269 |
|
270 |
public String getFileName() { |
271 |
return file;
|
272 |
} |
273 |
|
274 |
public String getCapability(String capability) { |
275 |
return ""; |
276 |
} |
277 |
|
278 |
public void setCapabilities(Properties capabilities) { |
279 |
} |
280 |
|
281 |
public boolean canWriteAttribute(int sqlType) { |
282 |
return true; |
283 |
} |
284 |
|
285 |
public void initialize(ITableDefinition layerDefinition) |
286 |
throws InitializeWriterException {
|
287 |
this.layerDefinition = (ILayerDefinition) layerDefinition;
|
288 |
} |
289 |
|
290 |
public String getName() { |
291 |
return "MULTI File Writer"; |
292 |
} |
293 |
|
294 |
public ITableDefinition getTableDefinition() {
|
295 |
return layerDefinition;
|
296 |
} |
297 |
|
298 |
public boolean canAlterTable() { |
299 |
return true; |
300 |
} |
301 |
public boolean canSaveEdits() throws VisitorException { |
302 |
if (getPointsWriter().canSaveEdits())
|
303 |
{ |
304 |
if (getLinesWriter().canSaveEdits())
|
305 |
{ |
306 |
if (getPolygonsWriter().canSaveEdits())
|
307 |
return true; |
308 |
} |
309 |
} |
310 |
return false; |
311 |
} |
312 |
|
313 |
public boolean isWriteAll() { |
314 |
return true; |
315 |
} |
316 |
|
317 |
} |