Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / SHPSHXFromGeometries.java @ 11051

History | View | Annotate | Download (4.61 KB)

1 1270 vcaballero
/* 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;
42
43
import java.io.File;
44
import java.io.IOException;
45
import java.io.RandomAccessFile;
46
import java.net.MalformedURLException;
47
import java.nio.channels.FileChannel;
48
import java.nio.channels.WritableByteChannel;
49
50 10627 caballero
import com.iver.cit.gvsig.fmap.core.FShape;
51
import com.iver.cit.gvsig.fmap.core.IGeometry;
52
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
53
import com.iver.cit.gvsig.fmap.drivers.shp.write.SHPFileWrite;
54
import com.iver.cit.gvsig.fmap.drivers.shp.write.ShapefileException;
55 1270 vcaballero
56 10627 caballero
57 1270 vcaballero
/**
58
 * DOCUMENT ME!
59
 *
60
 * @author Vicente Caballero Navarro
61
 */
62
public class SHPSHXFromGeometries {
63
        private IGeometry[] geometries = null;
64
        private String shpPath;
65
        private String shxPath;
66
        private int temp = 0;
67
        private int type;
68
69
        /**
70
         * Crea un nuevo SHPSHXFromGeometries.
71
         *
72
         * @param geometries DOCUMENT ME!
73
         * @param file DOCUMENT ME!
74
         */
75
        public SHPSHXFromGeometries(IGeometry[] geometries, File file) {
76
                this.geometries = geometries;
77
                setFile(file);
78
        }
79
80
        //private FBitSet bitset = null;
81
        //private int num = 0;
82
83
        /**
84
         * DOCUMENT ME!
85
         *
86
         * @param f DOCUMENT ME!
87
         */
88
        private void setFile(File f) {
89
                shpPath = f.getAbsolutePath();
90
91
                String strFichshx = f.getAbsolutePath().replaceAll("\\.shp", ".shx");
92
                shxPath = strFichshx.replaceAll("\\.SHP", ".SHX");
93
        }
94
95
        /**
96
         * DOCUMENT ME!
97
         */
98
99
        /*public void setType(int i) {
100
           type = i;
101
           }
102
         */
103
104
        /**
105
         * Finaliza el visitor.
106
         */
107
        public void create() {
108
                SHPFileWrite filewrite;
109
110
                try {
111
                        filewrite = new SHPFileWrite((FileChannel) getWriteChannel(shpPath),
112
                                        (FileChannel) getWriteChannel(shxPath));
113
114
                        if (geometries.length > 0) {
115
                                type = getTypeShape(geometries[0].getGeometryType());
116
                                filewrite.write(geometries, type);
117
                        }
118
                } catch (ShapefileException e1) {
119
                        e1.printStackTrace();
120
                } catch (MalformedURLException e) {
121
                        e.printStackTrace();
122
                } catch (IOException e) {
123
                        e.printStackTrace();
124
                }
125
126
                System.out.println("Acabado SHP y SHX");
127
        }
128
129
        /**
130
         * DOCUMENT ME!
131
         *
132
         * @param path DOCUMENT ME!
133
         *
134
         * @return DOCUMENT ME!
135
         *
136
         * @throws IOException DOCUMENT ME!
137
         */
138
        private WritableByteChannel getWriteChannel(String path)
139
                throws IOException {
140
                WritableByteChannel channel;
141
142
                File f = new File(path);
143
144
                if (!f.exists()) {
145
                        System.out.println("Creando fichero " + f.getAbsolutePath());
146
147
                        if (!f.createNewFile()) {
148
                                System.err.print("Error al crear el fichero " +
149
                                        f.getAbsolutePath());
150
                                throw new IOException("Cannot create file " + f);
151
                        }
152
                }
153
154
                RandomAccessFile raf = new RandomAccessFile(f, "rw");
155
                channel = raf.getChannel();
156
157
                return channel;
158
        }
159
160
        /**
161
         * DOCUMENT ME!
162
         *
163
         * @param geometryType DOCUMENT ME!
164
         *
165
         * @return DOCUMENT ME!
166
         */
167
        private int getTypeShape(int geometryType) {
168 2196 vcaballero
        if (geometryType>=FShape.Z){
169
                switch (geometryType - FShape.Z) {
170
                case (FShape.POINT):
171
                        return FConstant.SHAPE_TYPE_POINTZ;
172
173
                case (FShape.LINE):
174
                        return FConstant.SHAPE_TYPE_POLYLINEZ;
175
176
                case FShape.POLYGON:
177
                        return FConstant.SHAPE_TYPE_POLYGONZ;
178
179
                case FShape.MULTIPOINT:
180
                        return FConstant.SHAPE_TYPE_MULTIPOINTZ; //TODO falta aclarar cosas aqu?.
181
        }
182
183
        }else{
184 1270 vcaballero
                switch (geometryType) {
185
                        case FShape.POINT:
186
                                return FConstant.SHAPE_TYPE_POINT;
187
188
                        case FShape.LINE:
189
                                return FConstant.SHAPE_TYPE_POLYLINE;
190
191
                        case FShape.POLYGON:
192
                                return FConstant.SHAPE_TYPE_POLYGON;
193
194 2196 vcaballero
                        case FShape.MULTIPOINT:
195
                                return FConstant.SHAPE_TYPE_MULTIPOINT; //TODO falta aclarar cosas aqu?.
196 1270 vcaballero
                }
197 2196 vcaballero
        }
198 1270 vcaballero
                return FConstant.SHAPE_TYPE_NULL;
199
        }
200
}