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