org.geotools.data.shapefile.shp
Class ShapefileReader

java.lang.Object
  extended byorg.geotools.data.shapefile.shp.ShapefileReader

public class ShapefileReader
extends java.lang.Object

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

Nested Class Summary
 class ShapefileReader.Record
          The reader returns only one Record instance in its lifetime.
 
Constructor Summary
ShapefileReader(java.nio.channels.ReadableByteChannel channel, boolean strict, boolean useMemoryMapped, Lock lock)
          Creates a new instance of ShapeFile.
ShapefileReader(java.nio.channels.ReadableByteChannel channel, Lock lock)
          Default constructor.
 
Method Summary
 void close()
          Clean up any resources.
static java.nio.ByteBuffer ensureCapacity(java.nio.ByteBuffer buffer, int size, boolean useMemoryMappedBuffer)
           
static int fill(java.nio.ByteBuffer buffer, java.nio.channels.ReadableByteChannel channel)
           
 int getCount(int count)
          Parses the shpfile counting the records.
 ShapefileHeader getHeader()
          Get the header.
 void goTo(int offset)
          Needs better data, what is the requirements for offset?
 boolean hasNext()
          If there exists another record.
static void main(java.lang.String[] args)
           
 ShapefileReader.Record nextRecord()
          Fetch the next record information.
static ShapefileHeader readHeader(java.nio.channels.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)
           
 java.lang.Object shapeAt(int offset)
          TODO needs better java docs!!!
 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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ShapefileReader

public ShapefileReader(java.nio.channels.ReadableByteChannel channel,
                       boolean strict,
                       boolean useMemoryMapped,
                       Lock lock)
                throws java.io.IOException,
                       ShapefileException
Creates a new instance of ShapeFile.

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

ShapefileReader

public ShapefileReader(java.nio.channels.ReadableByteChannel channel,
                       Lock lock)
                throws java.io.IOException,
                       ShapefileException
Default constructor. Calls ShapefileReader(channel,true).

Parameters:
channel -
Throws:
java.io.IOException
ShapefileException
Method Detail

readHeader

public static ShapefileHeader readHeader(java.nio.channels.ReadableByteChannel channel,
                                         boolean strict)
                                  throws java.io.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:
java.io.IOException - If problems arise.

ensureCapacity

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

fill

public static int fill(java.nio.ByteBuffer buffer,
                       java.nio.channels.ReadableByteChannel channel)
                throws java.io.IOException
Throws:
java.io.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 java.io.IOException
Clean up any resources. Closes the channel.

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

supportsRandomAccess

public boolean supportsRandomAccess()

hasNext

public boolean hasNext()
                throws java.io.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:
java.io.IOException

transferTo

public int transferTo(ShapefileWriter writer,
                      int recordNum,
                      double[] bounds)
               throws java.io.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:
java.io.IOException

nextRecord

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

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

goTo

public void goTo(int offset)
          throws java.io.IOException,
                 java.lang.UnsupportedOperationException
Needs better data, what is the requirements for offset?

Parameters:
offset -
Throws:
java.io.IOException
java.lang.UnsupportedOperationException

shapeAt

public java.lang.Object shapeAt(int offset)
                         throws java.io.IOException,
                                java.lang.UnsupportedOperationException
TODO needs better java docs!!! What is offset?

Parameters:
offset -
Returns:
Throws:
java.io.IOException
java.lang.UnsupportedOperationException

recordAt

public ShapefileReader.Record recordAt(int offset)
                                throws java.io.IOException,
                                       java.lang.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.

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:
java.io.IOException - thrown in a read error occurs
java.lang.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

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception

setHandler

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


Copyright © GeoTools. All Rights Reserved.