Statistics
| Revision:

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

History | View | Annotate | Download (6.37 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 URL shpURL = null;
76
        private URL shxURL = null;
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
                try {
89
                        shpURL = f.toURL();
90

    
91
                        String strFichshx = f.getAbsolutePath().replaceAll("\\.shp", ".shx");
92
                        strFichshx = strFichshx.replaceAll("\\.SHP", ".SHX");
93
                        shxURL = new URL("file://" + strFichshx);
94
                } catch (MalformedURLException e) {
95
                        e.printStackTrace();
96
                }
97
        }
98

    
99
        /**
100
         * DOCUMENT ME!
101
         *
102
         * @param layer DOCUMENT ME!
103
         *
104
         * @return DOCUMENT ME!
105
         */
106

    
107
        /*public void setType(int i) {
108
           type = i;
109
           }
110
         */
111

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

    
125
                                ///fgs = new IGeometry[(int) sds.getRowCount()];
126
                                fgs = new IGeometry[bitset.cardinality()];
127

    
128
                                ///} catch (DriverException e) {
129
                                ///        e.printStackTrace();
130
                        } catch (com.iver.cit.gvsig.fmap.DriverException e) {
131
                                e.printStackTrace();
132
                        }
133

    
134
                        return true;
135
                }
136

    
137
                return false;
138
        }
139

    
140
        /**
141
         * Finaliza el visitor.
142
         *
143
         * @param layer Capa.
144
         */
145
        public void stop(FLayer layer) {
146
                SHPFileWrite filewrite;
147

    
148
                try {
149
                        filewrite = new SHPFileWrite((FileChannel) getWriteChannel(
150
                                                getStorageURL(shpURL)),
151
                                        (FileChannel) getWriteChannel(getStorageURL(shxURL)));
152

    
153
                        if (fgs.length > 0) {
154
                                type = getTypeShape(fgs[0].getGeometryType());
155
                                filewrite.write(fgs, type);
156
                        }
157
                } catch (ShapefileException e1) {
158
                        e1.printStackTrace();
159
                } catch (MalformedURLException e) {
160
                        e.printStackTrace();
161
                } catch (IOException e) {
162
                        e.printStackTrace();
163
                }
164

    
165
                System.out.println("Acabado SHP y SHX");
166
        }
167

    
168
        /**
169
         * @see com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor#visit(com.iver.cit.gvsig.fmap.core.IGeometry,
170
         *                 int)
171
         */
172
        public void visit(IGeometry g, int index) throws VisitException {
173
                if (bitset.get(index)) {
174
                        fgs[num] = g;
175
                        num++;
176
                }
177
        }
178

    
179
        /**
180
         * DOCUMENT ME!
181
         *
182
         * @param url DOCUMENT ME!
183
         *
184
         * @return DOCUMENT ME!
185
         *
186
         * @throws java.net.MalformedURLException DOCUMENT ME!
187
         */
188
        protected URL getStorageURL(URL url) throws java.net.MalformedURLException {
189
                return (temp == 0) ? url : getStorageFile(url).toURL();
190
        }
191

    
192
        /**
193
         * DOCUMENT ME!
194
         *
195
         * @param url DOCUMENT ME!
196
         *
197
         * @return DOCUMENT ME!
198
         *
199
         * @throws IOException DOCUMENT ME!
200
         */
201
        private WritableByteChannel getWriteChannel(URL url)
202
                throws IOException {
203
                WritableByteChannel channel;
204

    
205
                if (url.getProtocol().equals("file")) {
206
                        File f = new File(url.getFile());
207

    
208
                        if (!f.exists() && !f.createNewFile()) {
209
                                throw new IOException("Cannot create file " + f);
210
                        }
211

    
212
                        RandomAccessFile raf = new RandomAccessFile(f, "rw");
213
                        channel = raf.getChannel();
214
                } else {
215
                        OutputStream out = url.openConnection().getOutputStream();
216
                        channel = Channels.newChannel(out);
217
                }
218

    
219
                return channel;
220
        }
221

    
222
        /**
223
         * DOCUMENT ME!
224
         *
225
         * @param url DOCUMENT ME!
226
         *
227
         * @return DOCUMENT ME!
228
         */
229
        protected File getStorageFile(URL url) {
230
                String f = url.getFile();
231
                f = temp + f.substring(f.lastIndexOf("/") + 1);
232

    
233
                File tf = new File(System.getProperty("java.io.tmpdir"), f);
234

    
235
                return tf;
236
        }
237

    
238
        /**
239
         * DOCUMENT ME!
240
         *
241
         * @return DOCUMENT ME!
242
         */
243
        public IGeometry[] getGeometries() {
244
                return fgs;
245
        }
246

    
247
        /**
248
         * DOCUMENT ME!
249
         *
250
         * @param geometryType DOCUMENT ME!
251
         *
252
         * @return DOCUMENT ME!
253
         */
254
        private int getTypeShape(int geometryType) {
255
                switch (geometryType) {
256
                        case FShape.POINT:
257
                                return FConstant.SHAPE_TYPE_POINT;
258

    
259
                        case FShape.LINE:
260
                                return FConstant.SHAPE_TYPE_POLYLINE;
261

    
262
                        case FShape.POLYGON:
263
                                return FConstant.SHAPE_TYPE_POLYGON;
264

    
265
                        case FShape.MULTI:
266
                                return 0; //TODO falta aclarar cosas aqu?.
267
                }
268

    
269
                return FConstant.SHAPE_TYPE_NULL;
270
        }
271

    
272
        /**
273
         * DOCUMENT ME!
274
         *
275
         * @return DOCUMENT ME!
276
         */
277
        public SelectableDataSource getSDS() {
278
                return sds;
279
        }
280
}