Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1003 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / writers / shp / MultiShpWriter.java @ 12271

History | View | Annotate | Download (7.53 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.iver.cit.gvsig.fmap.core.FShape;
8
import com.iver.cit.gvsig.fmap.core.IFeature;
9
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
10
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
11
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
12
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
13
import com.iver.cit.gvsig.fmap.edition.EditionException;
14
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
15
import com.iver.cit.gvsig.fmap.edition.ISchemaManager;
16
import com.iver.cit.gvsig.fmap.edition.IWriter;
17
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
18
import com.iver.utiles.FileUtils;
19

    
20
/**
21
 * This writer wraps three ShpWriters, one for points, one for lines and one for
22
 * polygons geometry types. <br>
23
 * 
24
 * It allows you to save a FLyrVect with MULTI shape type in SHP file format
25
 * (that doesnt allow to mix different geometry types). To do that, this IWriter
26
 * creates a different SHP file for any geometry type, in a transparent manner
27
 * for the programmer. <br>
28
 * If your geometries doesnt hava a given geometry type, the Writer wont create
29
 * a file for this geometry type. <br>
30
 * <code>
31
 * MultiShpWriter writer = new MultiShpWriter();
32
 * writer.setFile(dxfFile);
33
 * writer.initialize(layerDefinition);
34
 * writer.preProcess();
35
 * # obtain features from an iterator
36
 * writer.process(feature);
37
 * writer.postProcess();
38
 * </code>
39
 * 
40
 */
41
public class MultiShpWriter implements IWriter {
42
        /**
43
         * original file name selected by user
44
         */
45
        String file = null;
46

    
47
        /**
48
         * original layer definition
49
         */
50
        ILayerDefinition layerDefinition;// only works with layers (no only
51
                                                                                // tables)
52

    
53
        IWriter polygons;
54

    
55
        File polygonsFile;
56

    
57
        IWriter lines;
58

    
59
        File linesFile;
60

    
61
        IWriter points;
62

    
63
        File pointsFile;
64

    
65
        public void preProcess() throws EditionException {
66
        }
67

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

    
88
        public void postProcess() throws EditionException {
89
                if (polygons != null)
90
                        polygons.postProcess();
91
                if (lines != null)
92
                        lines.postProcess();
93
                if (points != null)
94
                        points.postProcess();
95
        }
96

    
97
        /**
98
         * Give access to the Writer that processes polygon geometries (and creates
99
         * it if it hasnt yet)
100
         * 
101
         * @return
102
         * @throws EditionException
103
         */
104
        private IWriter getPolygonsWriter() throws EditionException {
105
                if (polygons == null) {
106
                        polygons = new ShpWriter();
107
                        // TODO Hacer que LayerDefinition sea cloneable
108
                        SHPLayerDefinition newDefinition = (SHPLayerDefinition) cloneDef(layerDefinition);
109
                        // we clone layerdefinition to change
110
                        // shape type
111
                        newDefinition.setShapeType(FShape.POLYGON);
112
                        polygonsFile = new File(file + "_POL.shp");
113
                        newDefinition.setFile(polygonsFile);
114
                        ((ShpWriter) polygons).setFile(polygonsFile);
115
                        polygons.initialize(newDefinition);
116

    
117
                        getSchemaManager(polygons, polygonsFile)
118
                                        .createSchema(newDefinition);
119
                        
120
                        //AZABALA: no si si es neceario
121
                        polygons.preProcess();
122
                }
123
                return polygons;
124
        }
125

    
126
        /**
127
         * Given a Writer, and the file where we want to save persistent features,
128
         * it returns an associated ISchemaManager (whose responsability is to
129
         * create the new schema-for files create the new files)
130
         * 
131
         * @param writer
132
         * @param file
133
         * @return
134
         */
135

    
136
        private ISchemaManager getSchemaManager(final IWriter writer,
137
                        final File file) {
138
                return new ShpSchemaManager(file.getAbsolutePath());
139
        }
140

    
141
        /**
142
         * From a given layer definition, it creates a new layer definition 'clon'
143
         * of the initial. It is useful to avoid local changes made by individual
144
         * Writers to the definition affects the others writers (for example, change
145
         * the shape type of the writer)
146
         * 
147
         * @param definition
148
         * @return
149
         */
150
        private ILayerDefinition cloneDef(ILayerDefinition definition) {
151
                ILayerDefinition solution = null;
152
                if (definition instanceof SHPLayerDefinition) {
153
                        SHPLayerDefinition def = (SHPLayerDefinition) definition;
154
                        solution = new SHPLayerDefinition();
155
                        solution.setName(def.getName());
156
                        FieldDescription[] fields = def.getFieldsDesc();
157
                        solution.setFieldsDesc(fields);
158
                }
159
                return solution;
160
        }
161

    
162
        /**
163
         * Give access to the Writer that processes line geometries (and creates it
164
         * if it hasnt yet)
165
         * 
166
         * @return
167
         * @throws EditionException
168
         */
169
        private IWriter getLinesWriter() throws EditionException {
170
                if (lines == null) {
171
                        lines = new ShpWriter();
172
                        SHPLayerDefinition newDefinition = (SHPLayerDefinition) cloneDef(layerDefinition);
173
                        // we clone layerdefinition to change
174
                        // shape type
175
                        newDefinition.setShapeType(FShape.LINE);
176
                        linesFile = new File(file + "_LIN.shp");
177
                        newDefinition.setFile(linesFile);
178
                        ((ShpWriter) lines).setFile(linesFile);
179
                        lines.initialize(newDefinition);
180
                        getSchemaManager(lines, linesFile).createSchema(newDefinition);
181
                        lines.preProcess();
182
                }
183
                return lines;
184
        }
185

    
186
        /**
187
         * Give access to the Writer that processes point geometries (and creates it
188
         * if it hasnt yet)
189
         * 
190
         * @return
191
         * @throws EditionException
192
         */
193

    
194
        private IWriter getPointsWriter() throws EditionException {
195
                if (points == null) {
196
                        points = new ShpWriter();
197
                        SHPLayerDefinition newDefinition = (SHPLayerDefinition) cloneDef(layerDefinition);
198
                        // we clone layerdefinition to change
199
                        // shape type
200
                        newDefinition.setShapeType(FShape.POINT);
201
                        pointsFile = new File(file + "_PT.shp");
202
                        newDefinition.setFile(pointsFile);
203
                        ((ShpWriter) points).setFile(pointsFile);
204
                        points.initialize(newDefinition);
205

    
206
                        getSchemaManager(points, pointsFile).createSchema(newDefinition);
207
                        points.preProcess();
208
                }
209
                return points;
210
        }
211

    
212
        /**
213
         * Giving an edited row, writes it with the Writer associated to its
214
         * geometry type
215
         */
216
        public void process(IRowEdited row) throws EditionException {
217
                IFeature feature = (IFeature) row.getLinkedRow();
218
                int geometryType = feature.getGeometry().getGeometryType();
219
                switch (geometryType) {
220
                case FShape.POINT:
221
                        getPointsWriter().process(row);
222
                        break;
223
                case FShape.LINE:
224
                case FShape.ELLIPSE:
225
                case FShape.ARC:
226
                case FShape.CIRCLE:
227
                        getLinesWriter().process(row);
228
                        break;
229

    
230
                case FShape.POLYGON:
231
                        getPolygonsWriter().process(row);
232
                        break;
233
                }
234
        }
235

    
236
        /**
237
         * Sets the file where save the results
238
         * 
239
         * @param f
240
         */
241
        public void setFile(File f) {
242
                file = FileUtils.getFileWithoutExtension(f);
243
        }
244

    
245
        public String getFileName() {
246
                return file;
247
        }
248

    
249
        public String getCapability(String capability) {
250
                return "";
251
        }
252

    
253
        public void setCapabilities(Properties capabilities) {
254
        }
255

    
256
        public boolean canWriteAttribute(int sqlType) {
257
                return true;
258
        }
259

    
260
        public void initialize(ITableDefinition layerDefinition)
261
                        throws EditionException {
262
                this.layerDefinition = (ILayerDefinition) layerDefinition;
263
        }
264

    
265
        public String getName() {
266
                return "MULTI File Writer";
267
        }
268

    
269
        public ITableDefinition getTableDefinition() {
270
                return layerDefinition;
271
        }
272

    
273
        public boolean canAlterTable() {
274
                return true;
275
        }
276
        public boolean canSaveEdits() {
277
                try {
278
                        if (getPointsWriter().canSaveEdits())
279
                        {
280
                                if (getLinesWriter().canSaveEdits())
281
                                {
282
                                        if (getPolygonsWriter().canSaveEdits())
283
                                                return true;
284
                                }
285
                        }
286
                } catch (EditionException e) {
287
                        e.printStackTrace();
288
                }
289
                return false;
290
        }
291

    
292
        public boolean isWriteAll() {
293
                return true;
294
        }
295
        
296
}