View Javadoc

1   /*
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.fmap.dal.store.shp.utils;
61  
62  import java.nio.ByteBuffer;
63  import java.nio.ByteOrder;
64  
65  import org.gvsig.utils.bigfile.BigByteBuffer2;
66  
67  
68  
69  /**
70   * Class to represent the header in the shape file.
71   */
72  public class ShapeFileHeader2 {
73      /**
74       * Shape Type Value Shape Type 0 Null Shape 1 Point 3 PolyLine 5 Polygon 8
75       * MultiPoint 11 PointZ 13 PolyLineZ 15 PolygonZ 18 MultiPointZ 21 PointM
76       * 23 PolyLineM 25 PolygonM 28 MultiPointM 31 MultiPatch
77       */
78  
79      /* The null shape type, there is no shape for this record. */
80      public static final int SHAPE_NULL = 0;
81      public static final int SHAPE_POINT = 1;
82      public static final int SHAPE_POLYLINE = 3;
83      public static final int SHAPE_POLYGON = 5;
84      public static final int SHAPE_MULTIPOINT = 8;
85      public static final int SHAPE_POINTZ = 11;
86      public static final int SHAPE_POLYLINEZ = 13;
87      public static final int SHAPE_POLYGONZ = 15;
88      public static final int SHAPE_MULTIPOINTZ = 18;
89      public static final int SHAPE_POINTM = 21;
90      public static final int SHAPE_POLYLINEM = 23;
91      public static final int SHAPE_POLYGONM = 25;
92      public static final int SHAPE_MULTIPOINTM = 28;
93      public static final int SHAPE_MULTIPATCH = 31;
94  
95      /** File Code, must be the value 9994 */
96      public int myFileCode = 9994;
97  
98      /** Unused 1; */
99      public int myUnused1 = 0;
100 
101     /** Unused 2; */
102     public int myUnused2 = 0;
103 
104     /** Unused 3; */
105     public int myUnused3 = 0;
106 
107     /** Unused 4; */
108     public int myUnused4 = 0;
109 
110     /** Unused 5; */
111     public int myUnused5 = 0;
112 
113     /** File Length; */
114     public int myFileLength = 0;
115 
116     /** Version of the file. */
117     public int myVersion = 1000;
118     public int myShapeType = 0;
119 
120     /** BoundingBox Xmin */
121     public double myXmin = 0;
122 
123     /** BoundingBox Ymin */
124     public double myYmin = 0;
125 
126     /** BoundingBox Xmax */
127     public double myXmax = 0;
128 
129     /** BoundingBox Ymax */
130     public double myYmax = 0;
131 
132     /** BoundingBox Zmin */
133     public double myZmin = 0;
134 
135     /** BoundingBox Zmax */
136     public double myZmax = 0;
137 
138     /** BoundingBox Zmin */
139     public double myMmin = 0;
140 
141     /** BoundingBox Zmax */
142     public double myMmax = 0;
143 
144     // notify about warnings.
145     private boolean myWarning = true;
146 
147     /**
148      * ShapeFileHeader constructor comment.
149      */
150     public ShapeFileHeader2() {
151         super();
152     }
153 
154     /**
155      * Return the file code.
156      *
157      * @return Entero.
158      */
159     public int getFileCode() {
160         return myFileCode;
161     }
162 
163     /**
164      * Return the version of the file.
165      *
166      * @return Versión.
167      */
168     public int getVersion() {
169         return myVersion;
170     }
171 
172     /**
173      * Get the extents of the shape file.
174      *
175      * @return FullExtent.
176      */
177     public java.awt.geom.Rectangle2D.Double getFileExtents() {
178         return new java.awt.geom.Rectangle2D.Double(myXmin, myYmin,
179             myXmax - myXmin, myYmax - myYmin);
180     }
181 
182     /**
183      * Print warnings to system.out.
184      *
185      * @param inWarning boolean.
186      */
187     public void setWarnings(boolean inWarning) {
188         myWarning = inWarning;
189     }
190 
191     /**
192      * Return the length of the header in 16 bit words..
193      *
194      * @return Longitud de la cabecera.
195      */
196     public int getHeaderLength() {
197         return 50;
198     }
199 
200     /**
201      * Return the number of 16 bit words in the shape file as recorded in the
202      * header
203      *
204      * @return Longitud del fichero.
205      */
206     public int getFileLength() {
207         return myFileLength;
208     }
209 
210     /**
211      * Read the header from the shape file.
212      *
213      * @param in ByteBuffer.
214      */
215     public void readHeader(BigByteBuffer2 in) {
216         // the first four bytes are integers
217         // in.setLittleEndianMode(false);
218         in.order(ByteOrder.BIG_ENDIAN);
219         myFileCode = in.getInt();
220 
221         if (myFileCode != 9994) {
222             warn("File Code = " + myFileCode + " Not equal to 9994");
223         }
224 
225         // From 4 to 8 are unused.
226         myUnused1 = in.getInt();
227 
228         // From 8 to 12 are unused.
229         myUnused2 = in.getInt();
230 
231         // From 12 to 16 are unused.
232         myUnused3 = in.getInt();
233 
234         // From 16 to 20 are unused.
235         myUnused4 = in.getInt();
236 
237         // From 20 to 24 are unused.
238         myUnused5 = in.getInt();
239 
240         // From 24 to 28 are the file length.
241         myFileLength = in.getInt();
242 
243         // From 28 to 32 are the File Version.
244         in.order(ByteOrder.LITTLE_ENDIAN);
245         myVersion = in.getInt();
246 
247         // From 32 to 36 are the Shape Type.
248         myShapeType = in.getInt();
249 
250         // From 36 to 44 are Xmin.
251         myXmin = in.getDouble(); // Double.longBitsToDouble(in.getLong());
252 
253         // From 44 to 52 are Ymin.
254         myYmin = in.getDouble();
255 
256         // From 52 to 60 are Xmax.
257         myXmax = in.getDouble();
258 
259         // From 60 to 68 are Ymax.
260         myYmax = in.getDouble();
261 
262         // From 68 to 76 are Zmin.
263         myZmin = in.getDouble();
264 
265         // From 76 to 84 are Zmax.
266         myZmax = in.getDouble();
267 
268         // From 84 to 92 are Mmin.
269         myMmin = in.getDouble();
270 
271         // From 92 to 100 are Mmax.
272         myMmax = in.getDouble();
273 
274         // that is all 100 bytes of the header.
275     }
276     public void write(ByteBuffer out,int type,
277   		  int numGeoms,int length,double minX,double minY,double maxX,double maxY,double minZ,double maxZ,double minM,double maxM){
278   		    out.order(ByteOrder.BIG_ENDIAN);
279 
280   		    out.putInt(myFileCode);
281 
282   		    for (int i = 0; i < 5; i++) {
283   		      out.putInt(0); //Skip unused part of header
284   		    }
285 
286   		    out.putInt(length);
287 
288   		    out.order(ByteOrder.LITTLE_ENDIAN);
289 
290   		    out.putInt(myVersion);
291   		    out.putInt(type);
292 
293   		    //write the bounding box
294   		    out.putDouble(minX);
295   		    out.putDouble(minY);
296   		    out.putDouble(maxX);
297   		    out.putDouble(maxY);
298   		    /*
299   		    out.putDouble(minZ);
300   		    out.putDouble(minZ);
301   		    out.putDouble(maxM);
302   		    out.putDouble(maxM);*/
303   		    out.order(ByteOrder.BIG_ENDIAN);
304   		    for (int i = 0; i < 8; i++) {
305   		      out.putInt(0); //Skip unused part of header
306   		    }
307 
308   		  }
309 
310     /**
311      * Muestra por consola los warning.
312      *
313      * @param inWarn warning.
314      */
315     private void warn(String inWarn) {
316         if (myWarning) {
317             System.out.print("WARNING: ");
318             System.out.println(inWarn);
319         }
320     }
321 }