Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / write / SHPSHXFromSelectedVisitor.java @ 1167

History | View | Annotate | Download (5.67 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.drivers.shp.write;
42

    
43
import com.iver.cit.gvsig.fmap.core.FShape;
44
import com.iver.cit.gvsig.fmap.core.IGeometry;
45
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
46
import com.iver.cit.gvsig.fmap.layers.FBitSet;
47
import com.iver.cit.gvsig.fmap.layers.FLayer;
48
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
49
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
50
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
51
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
52
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
53

    
54
import java.io.File;
55
import java.io.IOException;
56
import java.io.OutputStream;
57
import java.io.RandomAccessFile;
58

    
59
import java.net.MalformedURLException;
60
import java.net.URL;
61

    
62
import java.nio.channels.Channels;
63
import java.nio.channels.FileChannel;
64
import java.nio.channels.WritableByteChannel;
65

    
66

    
67
/**
68
 * Visitor de creaci?n de un shape de los elementos seleccionados.
69
 *
70
 * @author Vicente Caballero Navarro
71
 */
72
public class SHPSHXFromSelectedVisitor implements FeatureVisitor {
73
        private IGeometry[] fgs = null;
74
        private SelectableDataSource sds = null;
75
        private String shpPath;
76
        private String shxPath;
77
        private int temp = 0;
78
        private int type;
79
        private FBitSet bitset = null;
80
        private int num = 0;
81

    
82
        /**
83
         * DOCUMENT ME!
84
         *
85
         * @param f DOCUMENT ME!
86
         */
87
        public void setFile(File f) {
88
                shpPath = f.getAbsolutePath();
89

    
90
                String strFichshx = f.getAbsolutePath().replaceAll("\\.shp", ".shx");
91
                shxPath = strFichshx.replaceAll("\\.SHP", ".SHX");
92
        }
93

    
94
        /**
95
         * DOCUMENT ME!
96
         *
97
         * @param layer DOCUMENT ME!
98
         *
99
         * @return DOCUMENT ME!
100
         */
101

    
102
        /*public void setType(int i) {
103
           type = i;
104
           }
105
         */
106

    
107
        /**
108
         * Inicializa el visitor.
109
         *
110
         * @param layer Capa.
111
         *
112
         * @return True si se inicializa correctamente.
113
         */
114
        public boolean start(FLayer layer) {
115
                if (layer instanceof AlphanumericData && layer instanceof Selectable) {
116
                        try {
117
                                sds = ((AlphanumericData) layer).getRecordset();
118
                                bitset = ((Selectable) layer).getSelection();
119

    
120
                                ///fgs = new IGeometry[(int) sds.getRowCount()];
121
                                fgs = new IGeometry[bitset.cardinality()];
122

    
123
                                ///} catch (DriverException e) {
124
                                ///        e.printStackTrace();
125
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
126
                                e.printStackTrace();
127
                        }
128

    
129
                        return true;
130
                }
131

    
132
                return false;
133
        }
134

    
135
        /**
136
         * Finaliza el visitor.
137
         *
138
         * @param layer Capa.
139
         */
140
        public void stop(FLayer layer) {
141
                SHPFileWrite filewrite;
142

    
143
                try {
144
                        filewrite = new SHPFileWrite((FileChannel) getWriteChannel(shpPath),
145
                                        (FileChannel) getWriteChannel(shxPath));
146

    
147
                        if (fgs.length > 0) {
148
                                type = getTypeShape(fgs[0].getGeometryType());
149
                                filewrite.write(fgs, type);
150
                        }
151
                } catch (ShapefileException e1) {
152
                        e1.printStackTrace();
153
                } catch (MalformedURLException e) {
154
                        e.printStackTrace();
155
                } catch (IOException e) {
156
                        e.printStackTrace();
157
                }
158

    
159
                System.out.println("Acabado SHP y SHX");
160
        }
161

    
162
        /**
163
         * @see com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor#visit(com.iver.cit.gvsig.fmap.core.IGeometry,
164
         *                 int)
165
         */
166
        public void visit(IGeometry g, int index) throws VisitException {
167
                if (bitset.get(index)) {
168
                        fgs[num] = g;
169
                        num++;
170
                }
171
        }
172

    
173

    
174
        /**
175
         * DOCUMENT ME!
176
         *
177
         * @param url DOCUMENT ME!
178
         *
179
         * @return DOCUMENT ME!
180
         *
181
         * @throws IOException DOCUMENT ME!
182
         */
183
        private WritableByteChannel getWriteChannel(String path)
184
                throws IOException {
185
                WritableByteChannel channel;
186

    
187
                        File f = new File(path);
188

    
189
                        if (!f.exists())
190
                        {
191
                            System.out.println("Creando fichero " + f.getAbsolutePath());
192
                            if (!f.createNewFile()) {
193
                                System.err.print("Error al crear el fichero " + f.getAbsolutePath());
194
                                throw new IOException("Cannot create file " + f);
195
                            }
196
                        }
197

    
198
                        RandomAccessFile raf = new RandomAccessFile(f, "rw");
199
                        channel = raf.getChannel();
200

    
201
                return channel;
202
        }
203

    
204
        /**
205
         * DOCUMENT ME!
206
         *
207
         * @return DOCUMENT ME!
208
         */
209
        public IGeometry[] getGeometries() {
210
                return fgs;
211
        }
212

    
213
        /**
214
         * DOCUMENT ME!
215
         *
216
         * @param geometryType DOCUMENT ME!
217
         *
218
         * @return DOCUMENT ME!
219
         */
220
        private int getTypeShape(int geometryType) {
221
                switch (geometryType) {
222
                        case FShape.POINT:
223
                                return FConstant.SHAPE_TYPE_POINT;
224

    
225
                        case FShape.LINE:
226
                                return FConstant.SHAPE_TYPE_POLYLINE;
227

    
228
                        case FShape.POLYGON:
229
                                return FConstant.SHAPE_TYPE_POLYGON;
230

    
231
                        case FShape.MULTI:
232
                                return 0; //TODO falta aclarar cosas aqu?.
233
                }
234

    
235
                return FConstant.SHAPE_TYPE_NULL;
236
        }
237

    
238
        /**
239
         * DOCUMENT ME!
240
         *
241
         * @return DOCUMENT ME!
242
         */
243
        public SelectableDataSource getSDS() {
244
                return sds;
245
        }
246
}