org.geotools.data.shapefile.shp
Class ShapefileReader

Object
  extended by ShapefileReader
All Implemented Interfaces:
FileReader

public class ShapefileReader
extends Object
implements FileReader

The general use of this class is:

 
 FileChannel in = new FileInputStream("thefile.dbf").getChannel();
 ShapefileReader r = new ShapefileReader( in ) while (r.hasNext()) { Geometry
 shape = (Geometry) r.nextRecord().shape() // do stuff } r.close();
 
 
You don't have to immediately ask for the shape from the record. The record will contain the bounds of the shape and will only read the shape when the shape() method is called. This ShapefileReader.Record is the same object every time, so if you need data from the Record, be sure to copy it.

Author:
jamesm, aaime, Ian Schneider
Module:
trunk/gt/modules   (gt-modules.jar) (Maven report) (SVN head)

Nested Class Summary
 class ShapefileReader.Record
          The reader returns only one Record instance in its lifetime.
 
Constructor Summary
ShapefileReader(ShpFiles shapefileFiles, boolean strict, boolean useMemoryMapped)
          Creates a new instance of ShapeFile.
 
Method Summary
 void close()
          Clean up any resources.
 void disableShxUsage()
          Disables .shx file usage.
static ByteBuffer ensureCapacity(ByteBuffer buffer, int size, boolean useMemoryMappedBuffer)
           
static int fill(ByteBuffer buffer, ReadableByteChannel channel)
           
 int getCount(int count)
          Parses the shpfile counting the records.
 ShapefileHeader getHeader()
          Get the header.
 void goTo(int offset)
          Moves the reader to the specified byte offset in the file.
 boolean hasNext()
          If there exists another record.
 String id()
          An id for the reader.
 ShapefileReader.Record nextRecord()
          Fetch the next record information.
static ShapefileHeader readHeader(ReadableByteChannel channel, boolean strict)
          A short cut for reading the header from the given channel.
 ShapefileReader.Record recordAt(int offset)
          Sets the current location of the byteStream to offset and returns the next record.
 void setHandler(ShapeHandler handler)
           
 Object shapeAt(int offset)
          Returns the shape at the specified byte distance from the beginning of the file.
 boolean supportsRandomAccess()
           
 int transferTo(ShapefileWriter writer, int recordNum, double[] bounds)
          Transfer (by bytes) the data at the current record to the ShapefileWriter.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ShapefileReader

public ShapefileReader(ShpFiles shapefileFiles,
                       boolean strict,
                       boolean useMemoryMapped)
                throws IOException,
                       ShapefileException
Creates a new instance of ShapeFile.

Parameters:
shapefileFiles - The ReadableByteChannel this reader will use.
strict - True to make the header parsing throw Exceptions if the version or magic number are incorrect.
Throws:
IOException - If problems arise.
ShapefileException - If for some reason the file contains invalid records.
Method Detail

disableShxUsage

public void disableShxUsage()
                     throws IOException
Disables .shx file usage. By doing so you drop support for sparse shapefiles, the .shp will have to be without holes, all the valid shapefile records will have to be contiguous.

Throws:
IOException

readHeader

public static ShapefileHeader readHeader(ReadableByteChannel channel,
                                         boolean strict)
                                  throws IOException
A short cut for reading the header from the given channel.

Parameters:
channel - The channel to read from.
strict - True to make the header parsing throw Exceptions if the version or magic number are incorrect.
Returns:
A ShapefileHeader object.
Throws:
IOException - If problems arise.

ensureCapacity

public static ByteBuffer ensureCapacity(ByteBuffer buffer,
                                        int size,
                                        boolean useMemoryMappedBuffer)

fill

public static int fill(ByteBuffer buffer,
                       ReadableByteChannel channel)
                throws IOException
Throws:
IOException

getHeader

public ShapefileHeader getHeader()
Get the header. Its parsed in the constructor.

Returns:
The header that is associated with this file.

close

public void close()
           throws IOException
Clean up any resources. Closes the channel.

Throws:
IOException - If errors occur while closing the channel.

supportsRandomAccess

public boolean supportsRandomAccess()

hasNext

public boolean hasNext()
                throws IOException
If there exists another record. Currently checks the stream for the presence of 8 more bytes, the length of a record. If this is true and the record indicates the next logical record number, there exists more records.

Returns:
True if has next record, false otherwise.
Throws:
IOException

transferTo

public int transferTo(ShapefileWriter writer,
                      int recordNum,
                      double[] bounds)
               throws IOException
Transfer (by bytes) the data at the current record to the ShapefileWriter.

Parameters:
bounds - double array of length four for transfering the bounds into
Returns:
The length of the record transfered in bytes
Throws:
IOException

nextRecord

public ShapefileReader.Record nextRecord()
                                  throws IOException
Fetch the next record information.

Returns:
The record instance associated with this reader.
Throws:
IOException

goTo

public void goTo(int offset)
          throws IOException,
                 UnsupportedOperationException
Moves the reader to the specified byte offset in the file. Mind that:

Parameters:
offset -
Throws:
IOException
UnsupportedOperationException

shapeAt

public Object shapeAt(int offset)
               throws IOException,
                      UnsupportedOperationException
Returns the shape at the specified byte distance from the beginning of the file. Mind that:

Parameters:
offset -
Throws:
IOException
UnsupportedOperationException

recordAt

public ShapefileReader.Record recordAt(int offset)
                                throws IOException,
                                       UnsupportedOperationException
Sets the current location of the byteStream to offset and returns the next record. Usually used in conjuctions with the shx file or some other index file. Mind that:

Parameters:
offset - If using an shx file the offset would be: 2 * (index.getOffset(i))
Returns:
The record after the offset location in the bytestream
Throws:
IOException - thrown in a read error occurs
UnsupportedOperationException - thrown if not a random access file

getCount

public int getCount(int count)
             throws DataSourceException
Parses the shpfile counting the records.

Returns:
the number of non-null records in the shapefile
Throws:
DataSourceException

setHandler

public void setHandler(ShapeHandler handler)
Parameters:
handler - The handler to set.

id

public String id()
Description copied from interface: FileReader
An id for the reader. This is only used for debugging.

Specified by:
id in interface FileReader
Returns:
id for the reader.


Copyright © 1996-2009 Geotools. All Rights Reserved.