Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-file / org / gvsig / data / datastores / vectorial / file / shp_mem / simplify / ShapeFileHeader.java @ 22037

History | View | Annotate | Download (5.73 KB)

1
package org.gvsig.data.datastores.vectorial.file.shp_mem.simplify;
2

    
3
import java.io.IOException;
4
import java.nio.ByteOrder;
5

    
6
import org.apache.log4j.Logger;
7
import org.gvsig.data.datastores.vectorial.file.shp_util.FalseByteBuffer;
8
import org.gvsig.datasources.common.IByteBuffer;
9

    
10
/**
11
 * Class to represent the header in the shape file.
12
 */
13
public class ShapeFileHeader {
14

    
15
        /**
16
         * Shape Type Value Shape Type 0 Null Shape 1 Point 3 PolyLine 5 Polygon 8
17
         * MultiPoint 11 PointZ 13 PolyLineZ 15 PolygonZ 18 MultiPointZ 21 PointM 23
18
         * PolyLineM 25 PolygonM 28 MultiPointM 31 MultiPatch
19
         */
20

    
21
        /* The null shape type, there is no shape for this record. */
22
        public static final int SHAPE_NULL = 0;
23

    
24
        public static final int SHAPE_POINT = 1;
25

    
26
        public static final int SHAPE_POLYLINE = 3;
27

    
28
        public static final int SHAPE_POLYGON = 5;
29

    
30
        public static final int SHAPE_MULTIPOINT = 8;
31

    
32
        public static final int SHAPE_POINTZ = 11;
33

    
34
        public static final int SHAPE_POLYLINEZ = 13;
35

    
36
        public static final int SHAPE_POLYGONZ = 15;
37

    
38
        public static final int SHAPE_MULTIPOINTZ = 18;
39

    
40
        public static final int SHAPE_POINTM = 21;
41

    
42
        public static final int SHAPE_POLYLINEM = 23;
43

    
44
        public static final int SHAPE_POLYGONM = 25;
45

    
46
        public static final int SHAPE_MULTIPOINTM = 28;
47

    
48
        public static final int SHAPE_MULTIPATCH = 31;
49

    
50
        private static Logger logger = Logger.getLogger(ShapeFileHeader.class);
51

    
52
        /** File Code, must be the value 9994 */
53
        public int myFileCode = 9994;
54

    
55
        /** Unused 1; */
56
        public int myUnused1 = 0;
57

    
58
        /** Unused 2; */
59
        public int myUnused2 = 0;
60

    
61
        /** Unused 3; */
62
        public int myUnused3 = 0;
63

    
64
        /** Unused 4; */
65
        public int myUnused4 = 0;
66

    
67
        /** Unused 5; */
68
        public int myUnused5 = 0;
69

    
70
        /** File Length; */
71
        public int myFileLength = 0;
72

    
73
        /** Version of the file. */
74
        public int myVersion = 1000;
75

    
76
        public int myShapeType = 0;
77

    
78
        /** BoundingBox Xmin */
79
        public double myXmin = 0;
80

    
81
        /** BoundingBox Ymin */
82
        public double myYmin = 0;
83

    
84
        /** BoundingBox Xmax */
85
        public double myXmax = 0;
86

    
87
        /** BoundingBox Ymax */
88
        public double myYmax = 0;
89

    
90
        /** BoundingBox Zmin */
91
        public double myZmin = 0;
92

    
93
        /** BoundingBox Zmax */
94
        public double myZmax = 0;
95

    
96
        /** BoundingBox Zmin */
97
        public double myMmin = 0;
98

    
99
        /** BoundingBox Zmax */
100
        public double myMmax = 0;
101

    
102
        // notify about warnings.
103
        private boolean myWarning = true;
104

    
105
        /**
106
         * ShapeFileHeader constructor comment.
107
         */
108
        public ShapeFileHeader() {
109
                super();
110
        }
111

    
112
        /**
113
         * Return the file code.
114
         * 
115
         * @return Entero.
116
         */
117
        public int getFileCode() {
118
                return myFileCode;
119
        }
120

    
121
        /**
122
         * Return the version of the file.
123
         * 
124
         * @return Versi?n.
125
         */
126
        public int getVersion() {
127
                return myVersion;
128
        }
129

    
130
        /**
131
         * Get the extents of the shape file.
132
         * 
133
         * @return FullExtent.
134
         */
135
        public java.awt.geom.Rectangle2D.Double getFileExtents() {
136
                return new java.awt.geom.Rectangle2D.Double(myXmin, myYmin, myXmax
137
                                - myXmin, myYmax - myYmin);
138
        }
139

    
140
        /**
141
         * Print warnings to system.out.
142
         * 
143
         * @param inWarning
144
         *            boolean.
145
         */
146
        public void setWarnings(boolean inWarning) {
147
                myWarning = inWarning;
148
        }
149

    
150
        /**
151
         * Return the length of the header in 16 bit words..
152
         * 
153
         * @return Longitud de la cabecera.
154
         */
155
        public int getHeaderLength() {
156
                return 50;
157
        }
158

    
159
        /**
160
         * Return the number of 16 bit words in the shape file as recorded in the
161
         * header
162
         * 
163
         * @return Longitud del fichero.
164
         */
165
        public int getFileLength() {
166
                return myFileLength;
167
        }
168

    
169
        /**
170
         * Read the header from the shape file.
171
         * 
172
         * @param in
173
         *            ByteBuffer.
174
         */
175
        public void readHeader(FalseByteBuffer in) {
176
                // the first four bytes are integers
177
                // in.setLittleEndianMode(false);
178
                in.setByteOrder(ByteOrder.BIG_ENDIAN);
179
                myFileCode = in.getInt();
180

    
181
                if (myFileCode != 9994) {
182
                        warn("File Code = " + myFileCode + " Not equal to 9994");
183
                }
184

    
185
                // From 4 to 8 are unused.
186
                myUnused1 = in.getInt();
187

    
188
                // From 8 to 12 are unused.
189
                myUnused2 = in.getInt();
190

    
191
                // From 12 to 16 are unused.
192
                myUnused3 = in.getInt();
193

    
194
                // From 16 to 20 are unused.
195
                myUnused4 = in.getInt();
196

    
197
                // From 20 to 24 are unused.
198
                myUnused5 = in.getInt();
199

    
200
                // From 24 to 28 are the file length.
201
                myFileLength = in.getInt();
202

    
203
                // From 28 to 32 are the File Version.
204
                in.setByteOrder(ByteOrder.LITTLE_ENDIAN);
205
                myVersion = in.getInt();
206

    
207
                // From 32 to 36 are the Shape Type.
208
                myShapeType = in.getInt();
209

    
210
                // From 36 to 44 are Xmin.
211
                myXmin = in.getDouble(); // Double.longBitsToDouble(in.getLong());
212

    
213
                // From 44 to 52 are Ymin.
214
                myYmin = in.getDouble();
215

    
216
                // From 52 to 60 are Xmax.
217
                myXmax = in.getDouble();
218

    
219
                // From 60 to 68 are Ymax.
220
                myYmax = in.getDouble();
221

    
222
                // From 68 to 76 are Zmin.
223
                myZmin = in.getDouble();
224

    
225
                // From 76 to 84 are Zmax.
226
                myZmax = in.getDouble();
227

    
228
                // From 84 to 92 are Mmin.
229
                myMmin = in.getDouble();
230

    
231
                // From 92 to 100 are Mmax.
232
                myMmax = in.getDouble();
233

    
234
                // that is all 100 bytes of the header.
235
        }
236

    
237
        public void write(IByteBuffer out, int type, int numGeoms, int length,
238
                        double minX, double minY, double maxX, double maxY, double minZ,
239
                        double maxZ, double minM, double maxM) throws IOException {
240
                out.order(ByteOrder.BIG_ENDIAN);
241

    
242
                out.putInt(myFileCode);
243

    
244
                for (int i = 0; i < 5; i++) {
245
                        out.putInt(0); // Skip unused part of header
246
                }
247

    
248
                out.putInt(length);
249

    
250
                out.order(ByteOrder.LITTLE_ENDIAN);
251

    
252
                out.putInt(myVersion);
253
                out.putInt(type);
254

    
255
                // write the bounding box
256
                out.putDouble(minX);
257
                out.putDouble(minY);
258
                out.putDouble(maxX);
259
                out.putDouble(maxY);
260
                /*
261
                 * out.putDouble(minZ); out.putDouble(minZ); out.putDouble(maxM);
262
                 * out.putDouble(maxM);
263
                 */
264
                out.order(ByteOrder.BIG_ENDIAN);
265
                for (int i = 0; i < 8; i++) {
266
                        out.putInt(0); // Skip unused part of header
267
                }
268

    
269
        }
270

    
271
        /**
272
         * Muestra por consola los warning.
273
         * 
274
         * @param inWarn
275
         *            warning.
276
         */
277
        private void warn(String inWarn) {
278
                if (myWarning) {
279
                        logger.warn(inWarn);
280
                }
281
        }
282
}