Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / SHPSHXFromGeometries.java @ 2196

History | View | Annotate | Download (4.61 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;
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.drivers.shp.write.SHPFileWrite;
47
import com.iver.cit.gvsig.fmap.drivers.shp.write.ShapefileException;
48

    
49
import java.io.File;
50
import java.io.IOException;
51
import java.io.RandomAccessFile;
52

    
53
import java.net.MalformedURLException;
54

    
55
import java.nio.channels.FileChannel;
56
import java.nio.channels.WritableByteChannel;
57

    
58

    
59
/**
60
 * DOCUMENT ME!
61
 *
62
 * @author Vicente Caballero Navarro
63
 */
64
public class SHPSHXFromGeometries {
65
        private IGeometry[] geometries = null;
66
        private String shpPath;
67
        private String shxPath;
68
        private int temp = 0;
69
        private int type;
70

    
71
        /**
72
         * Crea un nuevo SHPSHXFromGeometries.
73
         *
74
         * @param geometries DOCUMENT ME!
75
         * @param file DOCUMENT ME!
76
         */
77
        public SHPSHXFromGeometries(IGeometry[] geometries, File file) {
78
                this.geometries = geometries;
79
                setFile(file);
80
        }
81

    
82
        //private FBitSet bitset = null;
83
        //private int num = 0;
84

    
85
        /**
86
         * DOCUMENT ME!
87
         *
88
         * @param f DOCUMENT ME!
89
         */
90
        private void setFile(File f) {
91
                shpPath = f.getAbsolutePath();
92

    
93
                String strFichshx = f.getAbsolutePath().replaceAll("\\.shp", ".shx");
94
                shxPath = strFichshx.replaceAll("\\.SHP", ".SHX");
95
        }
96

    
97
        /**
98
         * DOCUMENT ME!
99
         */
100

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

    
106
        /**
107
         * Finaliza el visitor.
108
         */
109
        public void create() {
110
                SHPFileWrite filewrite;
111

    
112
                try {
113
                        filewrite = new SHPFileWrite((FileChannel) getWriteChannel(shpPath),
114
                                        (FileChannel) getWriteChannel(shxPath));
115

    
116
                        if (geometries.length > 0) {
117
                                type = getTypeShape(geometries[0].getGeometryType());
118
                                filewrite.write(geometries, type);
119
                        }
120
                } catch (ShapefileException e1) {
121
                        e1.printStackTrace();
122
                } catch (MalformedURLException e) {
123
                        e.printStackTrace();
124
                } catch (IOException e) {
125
                        e.printStackTrace();
126
                }
127

    
128
                System.out.println("Acabado SHP y SHX");
129
        }
130

    
131
        /**
132
         * DOCUMENT ME!
133
         *
134
         * @param path DOCUMENT ME!
135
         *
136
         * @return DOCUMENT ME!
137
         *
138
         * @throws IOException DOCUMENT ME!
139
         */
140
        private WritableByteChannel getWriteChannel(String path)
141
                throws IOException {
142
                WritableByteChannel channel;
143

    
144
                File f = new File(path);
145

    
146
                if (!f.exists()) {
147
                        System.out.println("Creando fichero " + f.getAbsolutePath());
148

    
149
                        if (!f.createNewFile()) {
150
                                System.err.print("Error al crear el fichero " +
151
                                        f.getAbsolutePath());
152
                                throw new IOException("Cannot create file " + f);
153
                        }
154
                }
155

    
156
                RandomAccessFile raf = new RandomAccessFile(f, "rw");
157
                channel = raf.getChannel();
158

    
159
                return channel;
160
        }
161

    
162
        /**
163
         * DOCUMENT ME!
164
         *
165
         * @param geometryType DOCUMENT ME!
166
         *
167
         * @return DOCUMENT ME!
168
         */
169
        private int getTypeShape(int geometryType) {
170
        if (geometryType>=FShape.Z){
171
                switch (geometryType - FShape.Z) {
172
                case (FShape.POINT):
173
                        return FConstant.SHAPE_TYPE_POINTZ;
174

    
175
                case (FShape.LINE):
176
                        return FConstant.SHAPE_TYPE_POLYLINEZ;
177

    
178
                case FShape.POLYGON:
179
                        return FConstant.SHAPE_TYPE_POLYGONZ;
180

    
181
                case FShape.MULTIPOINT:
182
                        return FConstant.SHAPE_TYPE_MULTIPOINTZ; //TODO falta aclarar cosas aqu?.
183
        }
184

    
185
        }else{
186
                switch (geometryType) {
187
                        case FShape.POINT:
188
                                return FConstant.SHAPE_TYPE_POINT;
189

    
190
                        case FShape.LINE:
191
                                return FConstant.SHAPE_TYPE_POLYLINE;
192

    
193
                        case FShape.POLYGON:
194
                                return FConstant.SHAPE_TYPE_POLYGON;
195

    
196
                        case FShape.MULTIPOINT:
197
                                return FConstant.SHAPE_TYPE_MULTIPOINT; //TODO falta aclarar cosas aqu?.
198
                }
199
        }
200
                return FConstant.SHAPE_TYPE_NULL;
201
        }
202
}