001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/io/shpapi/MainFile.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042:
043: ---------------------------------------------------------------------------*/
044:
045: package org.deegree.io.shpapi;
046:
047: import java.io.File;
048: import java.io.IOException;
049: import java.io.RandomAccessFile;
050:
051: import org.deegree.model.spatialschema.ByteUtils;
052:
053: /**
054: * Class representing an ESRI Shape File.
055: * <p>
056: * Uses class ByteUtils modified
057: * from the original package com.bbn.openmap.layer.shape
058: * <br>
059: * Copyright (C) 1998 BBN Corporation 10 Moulton St. Cambridge, MA 02138
060: * <br>
061: *
062: * @version 16.08.2000
063: * @author Andreas Poth
064: *
065: */
066: public class MainFile {
067:
068: /*
069: * A buffer for current record's header.
070: */
071: protected byte[] recHdr = new byte[ShapeConst.SHAPE_FILE_RECORD_HEADER_LENGTH];
072:
073: /*
074: * instance variables
075: */
076: private FileHeader fh;
077: private IndexFile shx;
078:
079: /*
080: * file suffixes for shp
081: */
082: private static final String _shp = ".shp";
083:
084: /*
085: * references to the main file
086: */
087: private RandomAccessFile raf;
088:
089: /**
090: * Construct a MainFile from a file name.
091: */
092: public MainFile(String url) throws IOException {
093:
094: /*
095: * creates raf
096: */
097: raf = new RandomAccessFile(url + _shp, "r");
098:
099: fh = new FileHeader(raf);
100:
101: shx = new IndexFile(url);
102:
103: }
104:
105: /**
106: * Construct a MainFile from a file name.
107: */
108: public MainFile(String url, String rwflag) throws IOException {
109:
110: // delet file if it exists
111: File file = new File(url + _shp);
112:
113: if (rwflag.indexOf('w') > -1 && file.exists())
114: file.delete();
115: file = null;
116:
117: /*
118: * creates raf
119: */
120: raf = new RandomAccessFile(url + _shp, rwflag);
121:
122: fh = new FileHeader(raf, rwflag.indexOf('w') > -1);
123:
124: shx = new IndexFile(url, rwflag);
125:
126: }
127:
128: public void close() {
129: try {
130: raf.close();
131: } catch (Exception ex) {
132: ex.printStackTrace();
133: }
134: try {
135: shx.close();
136: } catch (Exception ex) {
137: ex.printStackTrace();
138: }
139: }
140:
141: /**
142: * method: getFileMBR()<BR>
143: * returns the minimum bounding rectangle of geometries<BR>
144: * within the shape-file
145: */
146: public SHPEnvelope getFileMBR() {
147:
148: return fh.getFileMBR();
149:
150: }
151:
152: /**
153: * method: getRecordNum()<BR>
154: * returns the number of record with in a shape-file<BR>
155: */
156: public int getRecordNum() {
157:
158: return shx.getRecordNum();
159:
160: }
161:
162: /**
163: * method: getRecordMBR(int RecNo)<BR>
164: * returns the minimum bound rectangle of RecNo's Geometrie
165: * of the shape-file<BR>
166: */
167: public SHPEnvelope getRecordMBR(int RecNo) throws IOException {
168:
169: SHPEnvelope recordMBR = null;
170: byte[] recBuf = null;
171:
172: // index in IndexArray (see IndexFile)
173: int iaIndex = RecNo - 1;
174:
175: int off = shx.getRecordOffset(iaIndex);
176:
177: // calculate length from 16-bit words (= 2 bytes) to lenght in bytes
178: int len = shx.getRecordLength(iaIndex) * 2;
179:
180: // off holds the offset of the shape-record in 16-bit words (= 2 byte)
181: // multiply with 2 gets number of bytes to seek
182: long rafPos = off * 2;
183:
184: // fetch shape record
185: raf.seek(rafPos + ShapeConst.SHAPE_FILE_RECORD_HEADER_LENGTH);
186:
187: recBuf = null;
188: recBuf = new byte[len];
189:
190: if (raf.read(recBuf, 0, len) != -1) {
191:
192: int shpType = ByteUtils.readLEInt(recBuf, 0);
193:
194: /*
195: * only for PolyLines, Polygons and MultiPoints
196: * minimum bounding rectangles are defined
197: */
198: if ((shpType == ShapeConst.SHAPE_TYPE_POLYLINE)
199: || (shpType == ShapeConst.SHAPE_TYPE_POLYGON)
200: || (shpType == ShapeConst.SHAPE_TYPE_MULTIPOINT)) {
201:
202: recordMBR = new SHPEnvelope(recBuf);
203:
204: } // end if shpType
205:
206: } // end if result
207:
208: return recordMBR;
209: }
210:
211: /**
212: * method: getByRecNo (int RecNo)<BR>
213: * retruns a ShapeRecord-Geometry by RecorcNumber<BR>
214: */
215: public SHPGeometry getByRecNo(int RecNo) throws IOException {
216:
217: SHPGeometry shpGeom = null;
218: byte[] recBuf = null;
219:
220: // index in IndexArray (see IndexFile)
221: int iaIndex = RecNo - 1;
222:
223: int off = shx.getRecordOffset(iaIndex);
224:
225: // calculate length from 16-bit words (= 2 bytes) to lenght in bytes
226: int len = shx.getRecordLength(iaIndex) * 2;
227:
228: // off holds the offset of the shape-record in 16-bit words (= 2 byte)
229: // multiply with 2 gets number of bytes to seek
230: long rafPos = off * 2;
231:
232: //fetch record header
233: raf.seek(rafPos);
234:
235: recBuf = null;
236: recBuf = new byte[ShapeConst.SHAPE_FILE_RECORD_HEADER_LENGTH];
237:
238: // fetch shape record
239: raf.seek(rafPos + ShapeConst.SHAPE_FILE_RECORD_HEADER_LENGTH);
240:
241: recBuf = null;
242: recBuf = new byte[len];
243:
244: if (raf.read(recBuf, 0, len) != -1) {
245:
246: int shpType = ByteUtils.readLEInt(recBuf, 0);
247:
248: // create a geometry out of record buffer with shapetype
249: if (shpType == ShapeConst.SHAPE_TYPE_POINT) {
250: shpGeom = new SHPPoint(recBuf, 4);
251: } else if (shpType == ShapeConst.SHAPE_TYPE_MULTIPOINT) {
252: shpGeom = new SHPMultiPoint(recBuf);
253: } else if (shpType == ShapeConst.SHAPE_TYPE_POLYLINE) {
254: shpGeom = new SHPPolyLine(recBuf);
255: } else if (shpType == ShapeConst.SHAPE_TYPE_POLYGON) {
256: shpGeom = new SHPPolygon(recBuf);
257: } else if (shpType == ShapeConst.SHAPE_TYPE_POLYGONZ) {
258: shpGeom = new SHPPolygon3D(recBuf);
259: }
260:
261: } // end if result
262:
263: return shpGeom;
264:
265: }
266:
267: /**
268: * method: getShapeType(int RecNo)<BR>
269: * returns the minimum bound rectangle of RecNo's Geometrie
270: * of the shape-file<BR>
271: */
272: public int getShapeTypeByRecNo(int RecNo) throws IOException {
273:
274: byte[] recBuf = null;
275: int shpType = -1;
276:
277: // index in IndexArray (see IndexFile)
278: int iaIndex = RecNo - 1;
279:
280: int off = shx.getRecordOffset(iaIndex);
281:
282: // calculate length from 16-bit words (= 2 bytes) to lenght in bytes
283: int len = shx.getRecordLength(iaIndex) * 2;
284:
285: // off holds the offset of the shape-record in 16-bit words (= 2 byte)
286: // multiply with 2 gets number of bytes to seek
287: long rafPos = off * 2;
288:
289: // fetch shape record
290: raf.seek(rafPos + ShapeConst.SHAPE_FILE_RECORD_HEADER_LENGTH);
291:
292: recBuf = null;
293: recBuf = new byte[len];
294:
295: if (raf.read(recBuf, 0, len) != -1) {
296:
297: shpType = ByteUtils.readLEInt(recBuf, 0);
298:
299: } // end if result
300:
301: return shpType;
302: }
303:
304: /**
305: * method: public void write(byte[] bytearray)<BR>
306: * appends a bytearray to the shape file<BR>
307: */
308: public void write(byte[] bytearray, IndexRecord record,
309: SHPEnvelope mbr) throws IOException {
310: raf.seek(record.offset * 2);
311: raf.write(bytearray);
312: shx.appendRecord(record, mbr);
313: }
314:
315: /**
316: * method: public void writeHeader(int filelength, byte shptype, SHPEnvelope mbr)<BR>
317: * writes a header to the shape and index file<BR>
318: */
319: public void writeHeader(int filelength, byte shptype,
320: SHPEnvelope mbr) throws IOException {
321: fh.writeHeader(filelength, shptype, mbr);
322: shx.writeHeader(shptype, mbr);
323: }
324:
325: }
|