Statistics
| Revision:

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

History | View | Annotate | Download (7.53 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
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 6093 fjp
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager;
18 5905 azabala
import com.iver.utiles.FileUtils;
19
20
/**
21 6093 fjp
 * This writer wraps three ShpWriters, one for points, one for lines and one for
22
 * polygons geometry types. <br>
23 5905 azabala
 *
24 6093 fjp
 * 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 5905 azabala
 * <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 6093 fjp
public class MultiShpWriter implements IWriter {
42 5905 azabala
        /**
43
         * original file name selected by user
44
         */
45
        String file = null;
46 6093 fjp
47 5905 azabala
        /**
48
         * original layer definition
49
         */
50 6093 fjp
        ILayerDefinition layerDefinition;// only works with layers (no only
51
                                                                                // tables)
52
53 5905 azabala
        IWriter polygons;
54 6093 fjp
55 5905 azabala
        File polygonsFile;
56 6093 fjp
57 5905 azabala
        IWriter lines;
58 6093 fjp
59 5905 azabala
        File linesFile;
60 6093 fjp
61 5905 azabala
        IWriter points;
62 6093 fjp
63 5905 azabala
        File pointsFile;
64 6093 fjp
65 5905 azabala
        public void preProcess() throws EditionException {
66
        }
67 6093 fjp
68 5905 azabala
        /**
69 6093 fjp
         * Returns all ShpWriter's created by this wrapper (only those wich writes a
70
         * type of the processed geometries)
71
         *
72 5905 azabala
         * @return
73
         */
74 6093 fjp
        public IWriter[] getWriters() {
75 5905 azabala
                IWriter[] solution;
76
                ArrayList list = new ArrayList();
77 6093 fjp
                if (polygons != null)
78 5905 azabala
                        list.add(polygons);
79 6093 fjp
                if (lines != null)
80 5905 azabala
                        list.add(lines);
81 6093 fjp
                if (points != null)
82 5905 azabala
                        list.add(points);
83
                solution = new IWriter[list.size()];
84
                list.toArray(solution);
85
                return solution;
86
        }
87 6093 fjp
88 5905 azabala
        public void postProcess() throws EditionException {
89 6093 fjp
                if (polygons != null)
90 5905 azabala
                        polygons.postProcess();
91 6093 fjp
                if (lines != null)
92 5905 azabala
                        lines.postProcess();
93 6093 fjp
                if (points != null)
94 5905 azabala
                        points.postProcess();
95
        }
96 6093 fjp
97 5905 azabala
        /**
98 6093 fjp
         * Give access to the Writer that processes polygon geometries (and creates
99
         * it if it hasnt yet)
100
         *
101 5905 azabala
         * @return
102
         * @throws EditionException
103
         */
104 6093 fjp
        private IWriter getPolygonsWriter() throws EditionException {
105
                if (polygons == null) {
106 5905 azabala
                        polygons = new ShpWriter();
107 6093 fjp
                        // TODO Hacer que LayerDefinition sea cloneable
108
                        SHPLayerDefinition newDefinition = (SHPLayerDefinition) cloneDef(layerDefinition);
109
                        // we clone layerdefinition to change
110
                        // shape type
111 5905 azabala
                        newDefinition.setShapeType(FShape.POLYGON);
112 6093 fjp
                        polygonsFile = new File(file + "_POL.shp");
113 5905 azabala
                        newDefinition.setFile(polygonsFile);
114 6093 fjp
                        ((ShpWriter) polygons).setFile(polygonsFile);
115 5905 azabala
                        polygons.initialize(newDefinition);
116 6093 fjp
117
                        getSchemaManager(polygons, polygonsFile)
118
                                        .createSchema(newDefinition);
119 6519 azabala
120
                        //AZABALA: no si si es neceario
121
                        polygons.preProcess();
122 5905 azabala
                }
123
                return polygons;
124
        }
125 6093 fjp
126 5905 azabala
        /**
127
         * Given a Writer, and the file where we want to save persistent features,
128 6093 fjp
         * it returns an associated ISchemaManager (whose responsability is to
129
         * create the new schema-for files create the new files)
130
         *
131 5905 azabala
         * @param writer
132
         * @param file
133
         * @return
134
         */
135
136 6093 fjp
        private ISchemaManager getSchemaManager(final IWriter writer,
137
                        final File file) {
138
                return new ShpSchemaManager(file.getAbsolutePath());
139 5905 azabala
        }
140 6093 fjp
141 5905 azabala
        /**
142 6093 fjp
         * 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 5905 azabala
         * @param definition
148
         * @return
149
         */
150 6093 fjp
        private ILayerDefinition cloneDef(ILayerDefinition definition) {
151 5905 azabala
                ILayerDefinition solution = null;
152 6093 fjp
                if (definition instanceof SHPLayerDefinition) {
153
                        SHPLayerDefinition def = (SHPLayerDefinition) definition;
154 5905 azabala
                        solution = new SHPLayerDefinition();
155
                        solution.setName(def.getName());
156 6093 fjp
                        FieldDescription[] fields = def.getFieldsDesc();
157 5905 azabala
                        solution.setFieldsDesc(fields);
158
                }
159
                return solution;
160
        }
161 6093 fjp
162 5905 azabala
        /**
163 6093 fjp
         * Give access to the Writer that processes line geometries (and creates it
164
         * if it hasnt yet)
165
         *
166 5905 azabala
         * @return
167
         * @throws EditionException
168
         */
169 6093 fjp
        private IWriter getLinesWriter() throws EditionException {
170
                if (lines == null) {
171 5905 azabala
                        lines = new ShpWriter();
172 6093 fjp
                        SHPLayerDefinition newDefinition = (SHPLayerDefinition) cloneDef(layerDefinition);
173
                        // we clone layerdefinition to change
174
                        // shape type
175 5905 azabala
                        newDefinition.setShapeType(FShape.LINE);
176 6093 fjp
                        linesFile = new File(file + "_LIN.shp");
177 5905 azabala
                        newDefinition.setFile(linesFile);
178 6093 fjp
                        ((ShpWriter) lines).setFile(linesFile);
179 5905 azabala
                        lines.initialize(newDefinition);
180 6093 fjp
                        getSchemaManager(lines, linesFile).createSchema(newDefinition);
181 6519 azabala
                        lines.preProcess();
182 5905 azabala
                }
183
                return lines;
184
        }
185 6093 fjp
186 5905 azabala
        /**
187 6093 fjp
         * Give access to the Writer that processes point geometries (and creates it
188
         * if it hasnt yet)
189
         *
190 5905 azabala
         * @return
191
         * @throws EditionException
192
         */
193 6093 fjp
194
        private IWriter getPointsWriter() throws EditionException {
195
                if (points == null) {
196 5905 azabala
                        points = new ShpWriter();
197 6093 fjp
                        SHPLayerDefinition newDefinition = (SHPLayerDefinition) cloneDef(layerDefinition);
198
                        // we clone layerdefinition to change
199
                        // shape type
200 5905 azabala
                        newDefinition.setShapeType(FShape.POINT);
201 6093 fjp
                        pointsFile = new File(file + "_PT.shp");
202 5905 azabala
                        newDefinition.setFile(pointsFile);
203 6093 fjp
                        ((ShpWriter) points).setFile(pointsFile);
204 5905 azabala
                        points.initialize(newDefinition);
205 6093 fjp
206
                        getSchemaManager(points, pointsFile).createSchema(newDefinition);
207 6519 azabala
                        points.preProcess();
208 5905 azabala
                }
209
                return points;
210
        }
211 6093 fjp
212 5905 azabala
        /**
213 6093 fjp
         * Giving an edited row, writes it with the Writer associated to its
214
         * geometry type
215 5905 azabala
         */
216
        public void process(IRowEdited row) throws EditionException {
217
                IFeature feature = (IFeature) row.getLinkedRow();
218
                int geometryType = feature.getGeometry().getGeometryType();
219 6093 fjp
                switch (geometryType) {
220 5905 azabala
                case FShape.POINT:
221
                        getPointsWriter().process(row);
222 6093 fjp
                        break;
223 5905 azabala
                case FShape.LINE:
224
                case FShape.ELLIPSE:
225
                case FShape.ARC:
226
                case FShape.CIRCLE:
227
                        getLinesWriter().process(row);
228 6093 fjp
                        break;
229
230 5905 azabala
                case FShape.POLYGON:
231
                        getPolygonsWriter().process(row);
232 6093 fjp
                        break;
233 5905 azabala
                }
234
        }
235 6093 fjp
236 5905 azabala
        /**
237
         * Sets the file where save the results
238 6093 fjp
         *
239 5905 azabala
         * @param f
240
         */
241 6093 fjp
        public void setFile(File f) {
242
                file = FileUtils.getFileWithoutExtension(f);
243 5905 azabala
        }
244 6093 fjp
245
        public String getFileName() {
246 5905 azabala
                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 6093 fjp
        public void initialize(ITableDefinition layerDefinition)
261
                        throws EditionException {
262 5905 azabala
                this.layerDefinition = (ILayerDefinition) layerDefinition;
263
        }
264
265
        public String getName() {
266
                return "MULTI File Writer";
267
        }
268 6259 fjp
269
        public ITableDefinition getTableDefinition() {
270
                return layerDefinition;
271
        }
272 6356 fjp
273
        public boolean canAlterTable() {
274
                return true;
275
        }
276 6856 fjp
        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 8913 fjp
292
        public boolean isWriteAll() {
293
                return true;
294
        }
295 6856 fjp
296 5905 azabala
}