Statistics
| Revision:

root / trunk / extensions / extGeoProcessing / src / com / iver / cit / gvsig / geoprocess / core / MultiShpWriter.java @ 5797

History | View | Annotate | Download (7.78 KB)

1
package com.iver.cit.gvsig.geoprocess.core;
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.IFeature;
8
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
9
import com.iver.cit.gvsig.fmap.drivers.ILayerDefinition;
10
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
11
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition;
12
import com.iver.cit.gvsig.fmap.edition.EditionException;
13
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
14
import com.iver.cit.gvsig.fmap.edition.ISchemaManager;
15
import com.iver.cit.gvsig.fmap.edition.IWriter;
16
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter;
17
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes;
18
import com.iver.utiles.FileUtils;
19

    
20
/**
21
 * This writer wraps three ShpWriters, one for points, one for lines
22
 * and one for polygons geometry types.
23
 * <br>
24
 * 
25
 * It allows you to save a FLyrVect with MULTI shape type in SHP file
26
 * format (that doesnt allow to mix different geometry types).
27
 * To do that, this IWriter creates a different SHP file for any geometry
28
 * type, in a transparent manner for the programmer. <br>
29
 * If your geometries doesnt hava a given geometry type, the Writer wont
30
 * create a file for this geometry type.
31
 *<br>
32
 * <code>
33
 * MultiShpWriter writer = new MultiShpWriter();
34
 * writer.setFile(dxfFile);
35
 * writer.initialize(layerDefinition);
36
 * writer.preProcess();
37
 * # obtain features from an iterator
38
 * writer.process(feature);
39
 * writer.postProcess();
40
 * </code>
41
 * 
42
 */
43
public class MultiShpWriter implements IWriter{
44
        /**
45
         * original file name selected by user
46
         */
47
        String file = null;
48
        /**
49
         * original layer definition
50
         */
51
        ILayerDefinition layerDefinition;//only works with layers (no only tables)
52
        IWriter polygons;
53
        File polygonsFile;
54
        IWriter lines;
55
        File linesFile;
56
        IWriter points;
57
        File pointsFile;
58
        
59
        public void preProcess() throws EditionException {
60
        }
61
        /**
62
         * Returns all ShpWriter's created by this wrapper
63
         * (only those wich writes a type of the processed geometries)
64
         * @return
65
         */
66
        public IWriter[] getWriters(){
67
                IWriter[] solution;
68
                ArrayList list = new ArrayList();
69
                if(polygons != null)
70
                        list.add(polygons);
71
                if(lines != null)
72
                        list.add(lines);
73
                if(points != null)
74
                        list.add(points);
75
                solution = new IWriter[list.size()];
76
                list.toArray(solution);
77
                return solution;
78
        }
79
        
80
        
81
        public void postProcess() throws EditionException {
82
                if(polygons != null)
83
                        polygons.postProcess();
84
                if(lines != null)
85
                        lines.postProcess();
86
                if(points != null)
87
                        points.postProcess();
88
        }
89
        
90
        /**
91
         * Give access to the Writer that processes polygon geometries
92
         * (and creates it if it hasnt yet)
93
         * @return
94
         * @throws EditionException
95
         */
96
        private IWriter getPolygonsWriter() throws EditionException{
97
                if(polygons == null){
98
                        polygons = new ShpWriter();
99
                        //TODO Hacer que LayerDefinition sea cloneable
100
                        SHPLayerDefinition newDefinition = 
101
                                (SHPLayerDefinition) cloneDef(layerDefinition);
102
                        //we clone layerdefinition to change
103
                        //shape type
104
                        newDefinition.setShapeType(XTypes.POLYGON);
105
                        polygonsFile = new File(file+"_POL.shp");
106
                        newDefinition.setFile(polygonsFile);
107
                        ((ShpWriter)polygons).setFile(polygonsFile);
108
                        polygons.initialize(newDefinition);
109
                        
110
                        getSchemaManager(polygons, polygonsFile).
111
                                createOrAlterSchema(newDefinition);
112
                }
113
                return polygons;
114
        }
115
        
116
        /**
117
         * Given a Writer, and the file where we want to save persistent features,
118
         * it returns an associated ISchemaManager (whose responsability is 
119
         * to create the new schema-for files create the new files)
120
         * @param writer
121
         * @param file
122
         * @return
123
         */
124
        
125
        private ISchemaManager getSchemaManager(final IWriter writer, 
126
                                                                                                final File file){
127
                return new ISchemaManager(){
128
                        public void createOrAlterSchema(ITableDefinition layerDefinition) throws EditionException {
129
                                writer.preProcess();
130
                        }
131

    
132
                        public void drop() throws EditionException {
133
                                if(!file.delete())
134
                                        throw new EditionException("No se pudo borrar el fichero shp");
135
                                
136
                                String strFichshx = file.
137
                                                                getAbsolutePath().
138
                                                                replaceAll("\\.shp", ".shx");
139
                                String shxPath = strFichshx.replaceAll("\\.SHP", ".SHX");
140
                                if(! new File(shxPath).delete())
141
                                        throw new EditionException("No se puede borrar el fichero shx");
142

    
143
                                String strFichDbf = file.
144
                                                                getAbsolutePath().
145
                                                                replaceAll("\\.shp", ".dbf");
146
                                String dbfPath = strFichDbf.replaceAll("\\.SHP", ".DBF");
147
                                if(! new File(dbfPath).delete())
148
                                        throw new EditionException("No se puede borrar el fichero dbf");        
149
                        }
150
                };
151
        }
152
                
153
        /**
154
         * From a given layer definition, it creates a new layer definition
155
         * 'clon' of the initial. 
156
         * It is useful to avoid local changes made by individual Writers to the
157
         * definition affects the others writers (for example, change the shape type
158
         * of the writer)
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 = 
169
                                def.getFieldsDesc();
170
                        solution.setFieldsDesc(fields);
171
                }
172
                return solution;
173
        }
174
        
175
        /**
176
         * Give access to the Writer that processes line geometries
177
         * (and creates it if it hasnt yet)
178
         * @return
179
         * @throws EditionException
180
         */
181
        private IWriter getLinesWriter() throws EditionException{
182
                if(lines == null){
183
                        lines = new ShpWriter();
184
                        SHPLayerDefinition newDefinition = 
185
                                (SHPLayerDefinition) cloneDef(layerDefinition);
186
                        //we clone layerdefinition to change
187
                        //shape type
188
                        newDefinition.setShapeType(XTypes.LINE);
189
                        linesFile = new File(file+"_LIN.shp");
190
                        newDefinition.setFile(linesFile);
191
                        ((ShpWriter)lines).setFile(linesFile);
192
                        lines.initialize(newDefinition);
193
                        getSchemaManager(lines, linesFile).
194
                                createOrAlterSchema(newDefinition);
195
                }
196
                return lines;
197
        }
198
        
199
        /**
200
         * Give access to the Writer that processes point geometries
201
         * (and creates it if it hasnt yet)
202
         * @return
203
         * @throws EditionException
204
         */
205
        
206
        private IWriter getPointsWriter() throws EditionException{
207
                if(points == null){
208
                        points = new ShpWriter();
209
                        //TODO Hacer que LayerDefinition sea cloneable
210
                        SHPLayerDefinition newDefinition = 
211
                                (SHPLayerDefinition) cloneDef(layerDefinition);
212
                        //we clone layerdefinition to change
213
                        //shape type
214
                        newDefinition.setShapeType(XTypes.POINT);
215
                        pointsFile = new File(file+"_PT.shp");
216
                        newDefinition.setFile(pointsFile);
217
                        ((ShpWriter)points).setFile(pointsFile);
218
                        points.initialize(newDefinition);
219
                        
220
                        getSchemaManager(points, pointsFile).
221
                                createOrAlterSchema(newDefinition);
222
                }
223
                return points;
224
        }
225
        
226
        /**
227
         * Giving an edited row, writes it with the Writer associated to
228
         * its geometry type
229
         */
230
        public void process(IRowEdited row) throws EditionException {
231
                IFeature feature = (IFeature) row.getLinkedRow();
232
                int geometryType = feature.getGeometry().getGeometryType();
233
                switch(geometryType){
234
                case XTypes.POINT:
235
                        getPointsWriter().process(row);
236
                break;
237
                case XTypes.LINE:
238
                case XTypes.ELLIPSE:
239
                case XTypes.ARC:
240
                case XTypes.CIRCLE:
241
                        getLinesWriter().process(row);
242
                break;
243
                
244
                case XTypes.POLYGON:
245
                        getPolygonsWriter().process(row);
246
                break;
247
                }
248
        }
249
        
250
        /**
251
         * Sets the file where save the results
252
         * @param f
253
         */
254
        public void setFile(File f)
255
        {
256
                file = FileUtils.
257
                                getFileWithoutExtension(f);
258
        }
259
        
260
        public String getFileName(){
261
                return file;
262
        }
263

    
264
        public String getCapability(String capability) {
265
                return "";
266
        }
267

    
268
        public void setCapabilities(Properties capabilities) {
269
        }
270

    
271
        public boolean canWriteAttribute(int sqlType) {
272
                return true;
273
        }
274

    
275
        public void initialize(ITableDefinition layerDefinition) throws EditionException {
276
                this.layerDefinition = (ILayerDefinition) layerDefinition;
277
        }
278

    
279
        public String getName() {
280
                return "MULTI File Writer";
281
        }
282
}