Statistics
| Revision:

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
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.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
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
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
23
import com.iver.utiles.FileUtils;
24

    
25
/**
26
 * This writer wraps three ShpWriters, one for points, one for lines and one for
27
 * polygons geometry types. <br>
28
 *
29
 * 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
 * <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
 *
45
 */
46
public class MultiShpWriter implements IWriter {
47
        /**
48
         * original file name selected by user
49
         */
50
        String file = null;
51

    
52
        /**
53
         * original layer definition
54
         */
55
        ILayerDefinition layerDefinition;// only works with layers (no only
56
                                                                                // tables)
57

    
58
        IWriter polygons;
59

    
60
        File polygonsFile;
61

    
62
        IWriter lines;
63

    
64
        File linesFile;
65

    
66
        IWriter points;
67

    
68
        File pointsFile;
69

    
70
        public void preProcess() throws StartWriterVisitorException {
71
        }
72

    
73
        /**
74
         * Returns all ShpWriter's created by this wrapper (only those wich writes a
75
         * type of the processed geometries)
76
         *
77
         * @return
78
         */
79
        public IWriter[] getWriters() {
80
                IWriter[] solution;
81
                ArrayList list = new ArrayList();
82
                if (polygons != null)
83
                        list.add(polygons);
84
                if (lines != null)
85
                        list.add(lines);
86
                if (points != null)
87
                        list.add(points);
88
                solution = new IWriter[list.size()];
89
                list.toArray(solution);
90
                return solution;
91
        }
92

    
93
        public void postProcess() throws StopWriterVisitorException {
94
                if (polygons != null)
95
                        polygons.postProcess();
96
                if (lines != null)
97
                        lines.postProcess();
98
                if (points != null)
99
                        points.postProcess();
100
        }
101

    
102
        /**
103
         * Give access to the Writer that processes polygon geometries (and creates
104
         * it if it hasnt yet)
105
         *
106
         * @return
107
         * @throws EditionException
108
         */
109
        private IWriter getPolygonsWriter() throws VisitorException {
110
                if (polygons == null) {
111
                        polygons = new ShpWriter();
112
                        // TODO Hacer que LayerDefinition sea cloneable
113
                        SHPLayerDefinition newDefinition = (SHPLayerDefinition) cloneDef(layerDefinition);
114
                        // we clone layerdefinition to change
115
                        // shape type
116
                        newDefinition.setShapeType(FShape.POLYGON);
117
                        newDefinition.setFile(getPolygonsFile());
118
                        ((ShpWriter) polygons).setFile(polygonsFile);
119
                        try {
120
                                polygons.initialize(newDefinition);
121
                        } catch (InitializeWriterException e) {
122
                                throw new ProcessWriterVisitorException(getName(),e);
123
                        }
124

    
125
                        try {
126
                                getSchemaManager(polygons, polygonsFile)
127
                                                .createSchema(newDefinition);
128
                        } catch (SchemaEditionException e) {
129
                                throw new ProcessWriterVisitorException(getName(),e);
130
                        }
131

    
132
                        //AZABALA: no si si es neceario
133
                        polygons.preProcess();
134
                }
135
                return polygons;
136
        }
137

    
138
        /**
139
         * Given a Writer, and the file where we want to save persistent features,
140
         * it returns an associated ISchemaManager (whose responsability is to
141
         * create the new schema-for files create the new files)
142
         *
143
         * @param writer
144
         * @param file
145
         * @return
146
         */
147

    
148
        private ISchemaManager getSchemaManager(final IWriter writer,
149
                        final File file) {
150
                return new ShpSchemaManager(file.getAbsolutePath());
151
        }
152

    
153
        /**
154
         * 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
         *
159
         * @param definition
160
         * @return
161
         */
162
        private ILayerDefinition cloneDef(ILayerDefinition definition) {
163
                ILayerDefinition solution = null;
164
                if (definition instanceof SHPLayerDefinition) {
165
                        SHPLayerDefinition def = (SHPLayerDefinition) definition;
166
                        solution = new SHPLayerDefinition();
167
                        solution.setName(def.getName());
168
                        FieldDescription[] fields = def.getFieldsDesc();
169
                        solution.setFieldsDesc(fields);
170
                }
171
                return solution;
172
        }
173

    
174
        /**
175
         * Give access to the Writer that processes line geometries (and creates it
176
         * if it hasnt yet)
177
         *
178
         * @return
179
         * @throws EditionException
180
         */
181
        private IWriter getLinesWriter() throws VisitorException {
182
                if (lines == null) {
183
                        lines = new ShpWriter();
184
                        SHPLayerDefinition newDefinition = (SHPLayerDefinition) cloneDef(layerDefinition);
185
                        // we clone layerdefinition to change
186
                        // shape type
187
                        newDefinition.setShapeType(FShape.LINE);
188
                        newDefinition.setFile(getLinesFile());
189
                        ((ShpWriter) lines).setFile(linesFile);
190
                        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
                }
200
                return lines;
201
        }
202

    
203
        /**
204
         * Give access to the Writer that processes point geometries (and creates it
205
         * if it hasnt yet)
206
         *
207
         * @return
208
         * @throws EditionException
209
         */
210

    
211
        private IWriter getPointsWriter() throws VisitorException{
212
                if (points == null) {
213
                        points = new ShpWriter();
214
                        SHPLayerDefinition newDefinition = (SHPLayerDefinition) cloneDef(layerDefinition);
215
                        // we clone layerdefinition to change
216
                        // shape type
217
                        newDefinition.setShapeType(FShape.POINT);
218
                        newDefinition.setFile(getPointsFile());
219
                        ((ShpWriter) points).setFile(pointsFile);
220
                        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
                }
230
                return points;
231
        }
232

    
233
        /**
234
         * Giving an edited row, writes it with the Writer associated to its
235
         * geometry type
236
         */
237
        public void process(IRowEdited row) throws VisitorException {
238
                IFeature feature = (IFeature) row.getLinkedRow();
239
                int geometryType = feature.getGeometry().getGeometryType();
240
                switch (geometryType) {
241
                case FShape.POINT:
242
                        getPointsWriter().process(row);
243
                        break;
244
                case FShape.LINE:
245
                case FShape.ELLIPSE:
246
                case FShape.ARC:
247
                case FShape.CIRCLE:
248
                        getLinesWriter().process(row);
249
                        break;
250

    
251
                case FShape.POLYGON:
252
                        getPolygonsWriter().process(row);
253
                        break;
254
                }
255
        }
256

    
257
        /**
258
         * Sets the file where save the results
259
         *
260
         * @param f
261
         */
262
        public void setFile(File f) {
263
                file = FileUtils.getFileWithoutExtension(f);
264
        }
265

    
266
        public String getFileName() {
267
                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
        public void initialize(ITableDefinition layerDefinition)
282
                        throws InitializeWriterException {
283
                this.layerDefinition = (ILayerDefinition) layerDefinition;
284
        }
285

    
286
        public String getName() {
287
                return "MULTI File Writer";
288
        }
289

    
290
        public ITableDefinition getTableDefinition() {
291
                return layerDefinition;
292
        }
293

    
294
        public boolean canAlterTable() {
295
                return true;
296
        }
297
        public boolean canSaveEdits() throws VisitorException {
298
                if (getPointsWriter().canSaveEdits())
299
                        {
300
                                if (getLinesWriter().canSaveEdits())
301
                                {
302
                                        if (getPolygonsWriter().canSaveEdits())
303
                                                return true;
304
                                }
305
                        }
306
                return false;
307
        }
308

    
309
        public boolean isWriteAll() {
310
                return true;
311
        }
312

    
313
        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
}