Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src-file / org / gvsig / data / datastores / vectorial / file / shp / utils / ShapeFileHeader2.java @ 21606

History | View | Annotate | Download (8.34 KB)

1 21606 jldominguez
/*
2
 *    GISToolkit - Geographical Information System Toolkit
3
 *    (C) 2002, Ithaqua Enterprises Inc.
4
 *
5
 *    This library is free software; you can redistribute it and/or
6
 *    modify it under the terms of the GNU Lesser General Public
7
 *    License as published by the Free Software Foundation;
8
 *    version 2.1 of the License.
9
 *
10
 *    This library 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 GNU
13
 *    Lesser General Public License for more details.
14
 *
15
 *    You should have received a copy of the GNU Lesser General Public
16
 *    License along with this library; if not, write to the Free Software
17
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 */
20
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
21
 *
22
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
23
 *
24
 * This program is free software; you can redistribute it and/or
25
 * modify it under the terms of the GNU General Public License
26
 * as published by the Free Software Foundation; either version 2
27
 * of the License, or (at your option) any later version.
28
 *
29
 * This program is distributed in the hope that it will be useful,
30
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32
 * GNU General Public License for more details.
33
 *
34
 * You should have received a copy of the GNU General Public License
35
 * along with this program; if not, write to the Free Software
36
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
37
 *
38
 * For more information, contact:
39
 *
40
 *  Generalitat Valenciana
41
 *   Conselleria d'Infraestructures i Transport
42
 *   Av. Blasco Ib??ez, 50
43
 *   46010 VALENCIA
44
 *   SPAIN
45
 *
46
 *      +34 963862235
47
 *   gvsig@gva.es
48
 *      www.gvsig.gva.es
49
 *
50
 *    or
51
 *
52
 *   IVER T.I. S.A
53
 *   Salamanca 50
54
 *   46005 Valencia
55
 *   Spain
56
 *
57
 *   +34 963163400
58
 *   dac@iver.es
59
 */
60
package org.gvsig.data.datastores.vectorial.file.shp.utils;
61
62
// import java.nio.ByteBuffer;
63
import java.nio.ByteOrder;
64
65
import org.gvsig.datasources.common.IBigByteBuffer;
66
import org.gvsig.datasources.common.IByteBuffer;
67
68
69
70
/**
71
 * Class to represent the header in the shape file.
72
 */
73
public class ShapeFileHeader2 {
74
    /**
75
     * Shape Type Value Shape Type 0 Null Shape 1 Point 3 PolyLine 5 Polygon 8
76
     * MultiPoint 11 PointZ 13 PolyLineZ 15 PolygonZ 18 MultiPointZ 21 PointM
77
     * 23 PolyLineM 25 PolygonM 28 MultiPointM 31 MultiPatch
78
     */
79
80
    /* The null shape type, there is no shape for this record. */
81
    public static final int SHAPE_NULL = 0;
82
    public static final int SHAPE_POINT = 1;
83
    public static final int SHAPE_POLYLINE = 3;
84
    public static final int SHAPE_POLYGON = 5;
85
    public static final int SHAPE_MULTIPOINT = 8;
86
    public static final int SHAPE_POINTZ = 11;
87
    public static final int SHAPE_POLYLINEZ = 13;
88
    public static final int SHAPE_POLYGONZ = 15;
89
    public static final int SHAPE_MULTIPOINTZ = 18;
90
    public static final int SHAPE_POINTM = 21;
91
    public static final int SHAPE_POLYLINEM = 23;
92
    public static final int SHAPE_POLYGONM = 25;
93
    public static final int SHAPE_MULTIPOINTM = 28;
94
    public static final int SHAPE_MULTIPATCH = 31;
95
96
    /** File Code, must be the value 9994 */
97
    public int myFileCode = 9994;
98
99
    /** Unused 1; */
100
    public int myUnused1 = 0;
101
102
    /** Unused 2; */
103
    public int myUnused2 = 0;
104
105
    /** Unused 3; */
106
    public int myUnused3 = 0;
107
108
    /** Unused 4; */
109
    public int myUnused4 = 0;
110
111
    /** Unused 5; */
112
    public int myUnused5 = 0;
113
114
    /** File Length; */
115
    public int myFileLength = 0;
116
117
    /** Version of the file. */
118
    public int myVersion = 1000;
119
    public int myShapeType = 0;
120
121
    /** BoundingBox Xmin */
122
    public double myXmin = 0;
123
124
    /** BoundingBox Ymin */
125
    public double myYmin = 0;
126
127
    /** BoundingBox Xmax */
128
    public double myXmax = 0;
129
130
    /** BoundingBox Ymax */
131
    public double myYmax = 0;
132
133
    /** BoundingBox Zmin */
134
    public double myZmin = 0;
135
136
    /** BoundingBox Zmax */
137
    public double myZmax = 0;
138
139
    /** BoundingBox Zmin */
140
    public double myMmin = 0;
141
142
    /** BoundingBox Zmax */
143
    public double myMmax = 0;
144
145
    // notify about warnings.
146
    private boolean myWarning = true;
147
148
    /**
149
     * ShapeFileHeader constructor comment.
150
     */
151
    public ShapeFileHeader2() {
152
        super();
153
    }
154
155
    /**
156
     * Return the file code.
157
     *
158
     * @return Entero.
159
     */
160
    public int getFileCode() {
161
        return myFileCode;
162
    }
163
164
    /**
165
     * Return the version of the file.
166
     *
167
     * @return Versi?n.
168
     */
169
    public int getVersion() {
170
        return myVersion;
171
    }
172
173
    /**
174
     * Get the extents of the shape file.
175
     *
176
     * @return FullExtent.
177
     */
178
    public java.awt.geom.Rectangle2D.Double getFileExtents() {
179
        return new java.awt.geom.Rectangle2D.Double(myXmin, myYmin,
180
            myXmax - myXmin, myYmax - myYmin);
181
    }
182
183
    /**
184
     * Print warnings to system.out.
185
     *
186
     * @param inWarning boolean.
187
     */
188
    public void setWarnings(boolean inWarning) {
189
        myWarning = inWarning;
190
    }
191
192
    /**
193
     * Return the length of the header in 16 bit words..
194
     *
195
     * @return Longitud de la cabecera.
196
     */
197
    public int getHeaderLength() {
198
        return 50;
199
    }
200
201
    /**
202
     * Return the number of 16 bit words in the shape file as recorded in the
203
     * header
204
     *
205
     * @return Longitud del fichero.
206
     */
207
    public int getFileLength() {
208
        return myFileLength;
209
    }
210
211
    /**
212
     * Read the header from the shape file.
213
     *
214
     * @param in ByteBuffer.
215
     */
216
    public void readHeader(IBigByteBuffer in) {
217
        // the first four bytes are integers
218
        // in.setLittleEndianMode(false);
219
        in.order(ByteOrder.BIG_ENDIAN);
220
        myFileCode = in.getInt();
221
222
        if (myFileCode != 9994) {
223
            warn("File Code = " + myFileCode + " Not equal to 9994");
224
        }
225
226
        // From 4 to 8 are unused.
227
        myUnused1 = in.getInt();
228
229
        // From 8 to 12 are unused.
230
        myUnused2 = in.getInt();
231
232
        // From 12 to 16 are unused.
233
        myUnused3 = in.getInt();
234
235
        // From 16 to 20 are unused.
236
        myUnused4 = in.getInt();
237
238
        // From 20 to 24 are unused.
239
        myUnused5 = in.getInt();
240
241
        // From 24 to 28 are the file length.
242
        myFileLength = in.getInt();
243
244
        // From 28 to 32 are the File Version.
245
        in.order(ByteOrder.LITTLE_ENDIAN);
246
        myVersion = in.getInt();
247
248
        // From 32 to 36 are the Shape Type.
249
        myShapeType = in.getInt();
250
251
        // From 36 to 44 are Xmin.
252
        myXmin = in.getDouble(); // Double.longBitsToDouble(in.getLong());
253
254
        // From 44 to 52 are Ymin.
255
        myYmin = in.getDouble();
256
257
        // From 52 to 60 are Xmax.
258
        myXmax = in.getDouble();
259
260
        // From 60 to 68 are Ymax.
261
        myYmax = in.getDouble();
262
263
        // From 68 to 76 are Zmin.
264
        myZmin = in.getDouble();
265
266
        // From 76 to 84 are Zmax.
267
        myZmax = in.getDouble();
268
269
        // From 84 to 92 are Mmin.
270
        myMmin = in.getDouble();
271
272
        // From 92 to 100 are Mmax.
273
        myMmax = in.getDouble();
274
275
        // that is all 100 bytes of the header.
276
    }
277
    public void write(IByteBuffer out,int type,
278
                    int numGeoms,int length,double minX,double minY,double maxX,double maxY,double minZ,double maxZ,double minM,double maxM){
279
                      out.order(ByteOrder.BIG_ENDIAN);
280
281
                      out.putInt(myFileCode);
282
283
                      for (int i = 0; i < 5; i++) {
284
                        out.putInt(0); //Skip unused part of header
285
                      }
286
287
                      out.putInt(length);
288
289
                      out.order(ByteOrder.LITTLE_ENDIAN);
290
291
                      out.putInt(myVersion);
292
                      out.putInt(type);
293
294
                      //write the bounding box
295
                      out.putDouble(minX);
296
                      out.putDouble(minY);
297
                      out.putDouble(maxX);
298
                      out.putDouble(maxY);
299
                      /*
300
                      out.putDouble(minZ);
301
                      out.putDouble(minZ);
302
                      out.putDouble(maxM);
303
                      out.putDouble(maxM);*/
304
                      out.order(ByteOrder.BIG_ENDIAN);
305
                      for (int i = 0; i < 8; i++) {
306
                        out.putInt(0); //Skip unused part of header
307
                      }
308
309
                    }
310
311
    /**
312
     * Muestra por consola los warning.
313
     *
314
     * @param inWarn warning.
315
     */
316
    private void warn(String inWarn) {
317
        if (myWarning) {
318
            System.out.print("WARNING: ");
319
            System.out.println(inWarn);
320
        }
321
    }
322
}