001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2002, Centre for Computational Geography
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.data.shapefile.shp;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.nio.ByteBuffer;
023: import java.nio.ByteOrder;
024: import java.nio.channels.FileChannel;
025:
026: /**
027: *
028: * @author jamesm
029: * @author Ian Schneider
030: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/shapefile/src/main/java/org/geotools/data/shapefile/shp/ShapefileHeader.java $
031: */
032: public class ShapefileHeader {
033:
034: public static final int MAGIC = 9994;
035: public static final int VERSION = 1000;
036: private int fileCode = -1;
037: private int fileLength = -1;
038: private int version = -1;
039: private ShapeType shapeType = ShapeType.UNDEFINED;
040: private double minX;
041: private double maxX;
042: private double minY;
043: private double maxY;
044:
045: private void checkMagic(boolean strict) throws java.io.IOException {
046: if (fileCode != MAGIC) {
047: String message = "Wrong magic number, expected " + MAGIC
048: + ", got " + fileCode;
049: if (!strict) {
050: System.err.println(message);
051: } else {
052: throw new java.io.IOException(message);
053: }
054: }
055: }
056:
057: private void checkVersion(boolean strict)
058: throws java.io.IOException {
059: if (version != VERSION) {
060: String message = "Wrong version, expected " + MAGIC
061: + ", got " + version;
062: if (!strict) {
063: System.err.println(message);
064: } else {
065: throw new java.io.IOException(message);
066: }
067: }
068: }
069:
070: public void read(ByteBuffer file, boolean strict)
071: throws java.io.IOException {
072: file.order(ByteOrder.BIG_ENDIAN);
073: fileCode = file.getInt();
074:
075: checkMagic(strict);
076:
077: // skip 5 ints...
078: file.position(file.position() + 20);
079:
080: fileLength = file.getInt();
081:
082: file.order(ByteOrder.LITTLE_ENDIAN);
083: version = file.getInt();
084: checkVersion(strict);
085: shapeType = ShapeType.forID(file.getInt());
086:
087: minX = file.getDouble();
088: minY = file.getDouble();
089: maxX = file.getDouble();
090: maxY = file.getDouble();
091:
092: //skip remaining unused bytes
093: file.order(ByteOrder.BIG_ENDIAN);//well they may not be unused forever...
094: file.position(file.position() + 32);
095:
096: }
097:
098: public void write(ByteBuffer file, ShapeType type, int numGeoms,
099: int length, double minX, double minY, double maxX,
100: double maxY) throws IOException {
101: file.order(ByteOrder.BIG_ENDIAN);
102:
103: file.putInt(MAGIC);
104:
105: for (int i = 0; i < 5; i++) {
106: file.putInt(0); //Skip unused part of header
107: }
108:
109: file.putInt(length);
110:
111: file.order(ByteOrder.LITTLE_ENDIAN);
112:
113: file.putInt(VERSION);
114: file.putInt(type.id);
115:
116: //write the bounding box
117: file.putDouble(minX);
118: file.putDouble(minY);
119: file.putDouble(maxX);
120: file.putDouble(maxY);
121:
122: //skip remaining unused bytes
123: file.order(ByteOrder.BIG_ENDIAN);
124: for (int i = 0; i < 8; i++) {
125: file.putInt(0); //Skip unused part of header
126: }
127: }
128:
129: public ShapeType getShapeType() {
130: return shapeType;
131: }
132:
133: public int getVersion() {
134: return version;
135: }
136:
137: public int getFileLength() {
138: return fileLength;
139: }
140:
141: public double minX() {
142: return minX;
143: }
144:
145: public double minY() {
146: return minY;
147: }
148:
149: public double maxX() {
150: return maxX;
151: }
152:
153: public double maxY() {
154: return maxY;
155: }
156:
157: public String toString() {
158: String res = new String("ShapeFileHeader[ size " + fileLength
159: + " version " + version + " shapeType " + shapeType
160: + " bounds " + minX + "," + minY + "," + maxX + ","
161: + maxY + " ]");
162: return res;
163: }
164:
165: public static void main(String[] args) throws Exception {
166: FileChannel channel = new FileInputStream(new File(args[0]))
167: .getChannel();
168: System.out.println(ShapefileReader.readHeader(channel, true));
169: channel.close();
170: }
171: }
|