com.bbn.openmap.layer.shape
Class ShapeFile

java.lang.Object
  extended by com.bbn.openmap.dataAccess.shape.ShapeUtils
      extended by com.bbn.openmap.layer.shape.ShapeFile
All Implemented Interfaces:
ShapeConstants

public class ShapeFile
extends ShapeUtils

Class representing an ESRI Shape File.

 Usage:
 
 To verify a shape file:
 java com.bbn.openmap.layer.shape.ShapeFile -v shapeFile
 
 Append records from srcShapeFile to destShapeFile:
 java com.bbn.openmap.layer.shape.ShapeFile -a destShapeFile srcShapeFile
 
 Print information about the header and the number of records:
 java com.bbn.openmap.layer.shape.ShapeFile shapeFile
 
 

Version:
$Revision: 1.4 $ $Date: 2005/08/09 18:48:03 $
Author:
Tom Mitchell, Ray Tomlinson, Geoffrey Knauth

Field Summary
static int DEFAULT_RECORD_BUFFER_SIZE
          A default record size.
protected  ESRIBoundingBox fileBounds
          Holds the bounds of the file (four doubles).
protected  long fileLength
          Holds the length of the file, in bytes.
protected  int fileShapeType
          Holds the shape type of the file.
protected  int fileVersion
          Holds the version of the file, as an int.
protected  byte[] header
          The buffer that holds the 100 byte header.
protected  java.io.RandomAccessFile raf
          The read/write class for shape files.
protected  byte[] recBuf
          A buffer for the current record's data.
protected  byte[] recHdr
          A buffer for current record's header.
static int SHAPE_FILE_CODE
          A Shape File's magic number.
static int SHAPE_FILE_VERSION
          The currently handled version of Shape Files.
 
Fields inherited from interface com.bbn.openmap.dataAccess.shape.ShapeConstants
DBF_ATTRIBUTE, DBF_AUTOINCREMENT, DBF_BINARY, DBF_CHARACTER, DBF_DATE, DBF_DOUBLE, DBF_FLOAT, DBF_LOGICAL, DBF_LONG, DBF_MEMO, DBF_NUMERIC, DBF_OLE, DBF_TIMESTAMP, DBF_TYPE_AUTOINCREMENT, DBF_TYPE_BINARY, DBF_TYPE_CHARACTER, DBF_TYPE_DATE, DBF_TYPE_DOUBLE, DBF_TYPE_FLOAT, DBF_TYPE_LOGICAL, DBF_TYPE_LONG, DBF_TYPE_MEMO, DBF_TYPE_NUMERIC, DBF_TYPE_OLE, DBF_TYPE_TIMESTAMP, PARAM_DBF, PARAM_SHP, PARAM_SHX, SHAPE_BOUNDS_ATTRIBUTE, SHAPE_DBF_DASHPATTERN, SHAPE_DBF_DASHPHASE, SHAPE_DBF_DESCRIPTION, SHAPE_DBF_FILLCOLOR, SHAPE_DBF_INFO_ATTRIBUTE, SHAPE_DBF_LINECOLOR, SHAPE_DBF_LINEWIDTH, SHAPE_DBF_SELECTCOLOR, SHAPE_FILE_HEADER_LENGTH, SHAPE_FILE_RECORD_HEADER_LENGTH, SHAPE_INDEX_ATTRIBUTE, SHAPE_MAX_MEASURE_ATTRIBUTE, SHAPE_MAX_Z_ATTRIBUTE, SHAPE_MEASURE_ATTRIBUTE, SHAPE_MIN_MEASURE_ATTRIBUTE, SHAPE_MIN_Z_ATTRIBUTE, SHAPE_TYPE_ARC, SHAPE_TYPE_MULTIPATCH, SHAPE_TYPE_MULTIPOINT, SHAPE_TYPE_MULTIPOINTM, SHAPE_TYPE_MULTIPOINTZ, SHAPE_TYPE_NULL, SHAPE_TYPE_POINT, SHAPE_TYPE_POINTM, SHAPE_TYPE_POINTZ, SHAPE_TYPE_POLYGON, SHAPE_TYPE_POLYGONM, SHAPE_TYPE_POLYGONZ, SHAPE_TYPE_POLYLINE, SHAPE_TYPE_POLYLINEM, SHAPE_TYPE_POLYLINEZ, SHAPE_Z_ATTRIBUTE
 
Constructor Summary
ShapeFile(java.io.File file)
          Construct a ShapeFile from the given File.
ShapeFile(java.lang.String name)
          Construct a ShapeFile from a file name.
 
Method Summary
 void add(ESRIRecord r)
          Adds a record to the end of this file.
 void close()
          Closes the shape file and disposes of resources.
 ESRIBoundingBox getBoundingBox()
          Returns the bounding box of this shape file.
 long getFileLength()
          Returns the length of the file in bytes.
 int getFileVersion()
          Returns the version of the file.
 ESRIRecord getNextRecord()
          Returns the next record from the shape file as an ESRIRecord.
 int getShapeType()
          Returns the shape type of the file.
protected  void initHeader()
          Reads or writes the header of a Shape file.
static void main(java.lang.String[] args)
          The driver for the command line interface.
protected  void readHeader()
          Reads and parses the header of the file.
 void setShapeType(int newShapeType)
          Sets the shape type of the file.
 void verify(boolean repair, boolean verbose)
          Verifies the contents of a shape file.
protected  void verifyRecordBuffer(int size)
          Verifies that the record buffer is big enough to hold the given number of bytes.
protected  void writeHeader()
          Writes a blank header into the shape file.
 
Methods inherited from class com.bbn.openmap.dataAccess.shape.ShapeUtils
getStringForType, readBEInt, readBox, readLEDouble, readLEInt, readLEInt, readLELong, readPoint, writeBEInt, writeBox, writeLEDouble, writeLEInt, writeLELong, writePoint
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SHAPE_FILE_CODE

public static final int SHAPE_FILE_CODE
A Shape File's magic number.

See Also:
Constant Field Values

SHAPE_FILE_VERSION

public static final int SHAPE_FILE_VERSION
The currently handled version of Shape Files.

See Also:
Constant Field Values

DEFAULT_RECORD_BUFFER_SIZE

public static final int DEFAULT_RECORD_BUFFER_SIZE
A default record size. Automatically increased on demand.

See Also:
Constant Field Values

raf

protected java.io.RandomAccessFile raf
The read/write class for shape files.


header

protected byte[] header
The buffer that holds the 100 byte header.


fileLength

protected long fileLength
Holds the length of the file, in bytes.


fileVersion

protected int fileVersion
Holds the version of the file, as an int.


fileShapeType

protected int fileShapeType
Holds the shape type of the file.


fileBounds

protected ESRIBoundingBox fileBounds
Holds the bounds of the file (four doubles).


recHdr

protected byte[] recHdr
A buffer for current record's header.


recBuf

protected byte[] recBuf
A buffer for the current record's data.

Constructor Detail

ShapeFile

public ShapeFile(java.lang.String name)
          throws java.io.IOException
Construct a ShapeFile from a file name.

Throws:
java.io.IOException - if something goes wrong opening or reading the file.

ShapeFile

public ShapeFile(java.io.File file)
          throws java.io.IOException
Construct a ShapeFile from the given File.

Parameters:
file - A file object representing an ESRI Shape File
Throws:
java.io.IOException - if something goes wrong opening or reading the file.
Method Detail

initHeader

protected void initHeader()
                   throws java.io.IOException
Reads or writes the header of a Shape file. If the file is empty, a blank header is written and then read. If the file is not empty, the header is read.

After this function runs, the file pointer is set to byte 100, the first byte of the first record in the file.

Throws:
java.io.IOException - if something goes wrong reading or writing the shape file

writeHeader

protected void writeHeader()
                    throws java.io.IOException
Writes a blank header into the shape file.

Throws:
java.io.IOException - if something goes wrong writing the shape file

readHeader

protected void readHeader()
                   throws java.io.IOException
Reads and parses the header of the file. Values from the header are stored in the fields of this class.

Throws:
java.io.IOException - if something goes wrong reading the file
See Also:
header, fileVersion, fileLength, fileShapeType, fileBounds

getFileLength

public long getFileLength()
Returns the length of the file in bytes.

Returns:
the file length

getFileVersion

public int getFileVersion()
Returns the version of the file. The only currently supported version is 1000 (which represents version 1).

Returns:
the file version

getShapeType

public int getShapeType()
Returns the shape type of the file. Shape files do not mix shape types; all the shapes are of the same type.

Returns:
the file's shape type

setShapeType

public void setShapeType(int newShapeType)
                  throws java.io.IOException,
                         java.lang.IllegalArgumentException
Sets the shape type of the file. If the file has a shape type already, it cannot be set. If it does not have a shape type, it is set and written to the file in the header.

Shape types are enumerated in the class ShapeUtils.

Parameters:
newShapeType - the new shape type
Throws:
java.io.IOException - if something goes wrong writing the file
java.lang.IllegalArgumentException - if file already has a shape type
See Also:
ShapeUtils

getBoundingBox

public ESRIBoundingBox getBoundingBox()
Returns the bounding box of this shape file. The bounding box is the smallest rectangle that encloses all the shapes in the file.

Returns:
the bounding box

getNextRecord

public ESRIRecord getNextRecord()
                         throws java.io.IOException
Returns the next record from the shape file as an ESRIRecord. Each successive call gets the next record. There is no way to go back a record. When there are no more records, null is returned.

Returns:
a record, or null if there are no more records
Throws:
java.io.IOException - if something goes wrong reading the file

add

public void add(ESRIRecord r)
         throws java.io.IOException
Adds a record to the end of this file. The record is written to the file at the end of the last record.

Parameters:
r - the record to be added
Throws:
java.io.IOException - if something goes wrong writing to the file

close

public void close()
           throws java.io.IOException
Closes the shape file and disposes of resources.

Throws:
java.io.IOException - if something goes wrong closing the file

verify

public void verify(boolean repair,
                   boolean verbose)
            throws java.io.IOException
Verifies the contents of a shape file. The header is verified for file length, bounding box, and shape type. The records are verified for shape type and record number. The file is verified for proper termination (EOF at the end of a record).

Parameters:
repair - NOT CURRENTLY USED - would signal that the file should be repaired if possible
verbose - NOT CURRENTLY USED - would cause the verifier to display progress and status
Throws:
java.io.IOException - if something goes wrong reading or writing the file

verifyRecordBuffer

protected void verifyRecordBuffer(int size)
Verifies that the record buffer is big enough to hold the given number of bytes. If it is not big enough a new buffer is created that can hold the given number of bytes.

Parameters:
size - the number of bytes the buffer needs to hold

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
The driver for the command line interface. Reads the command line arguments and executes appropriate calls.

See the file documentation for usage.

Parameters:
args - the command line arguments
Throws:
java.io.IOException - if something goes wrong reading or writing the file


Copyright (C) BBNT Solutions LLC; See http://openmap.bbn.com/ for details