Package org.geotools.data.shapefile.shp
Class ShapefileReader
- Object
-
- ShapefileReader
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,FileReader
public class ShapefileReader extends Object implements FileReader, Closeable
The general use of this class is:
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.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();
- Author:
- jamesm, aaime, Ian Schneider
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
ShapefileReader.Record
The reader returns only one Record instance in its lifetime.
-
Constructor Summary
Constructors Constructor Description ShapefileReader(ShpFiles shapefileFiles, boolean strict, boolean useMemoryMapped, GeometryFactory gf)
Creates a new instance of ShapeFile.ShapefileReader(ShpFiles shapefileFiles, boolean strict, boolean useMemoryMapped, GeometryFactory gf, boolean onlyRandomAccess)
Creates a new instance of ShapeFile.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Clean up any resources.void
disableShxUsage()
Disables .shx file usage.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.ShapefileReader.Record
recordAt(int offset)
Sets the current location of the byteStream to offset and returns the next record.void
setFlatGeometry(boolean flatGeometry)
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.
-
-
-
Constructor Detail
-
ShapefileReader
public ShapefileReader(ShpFiles shapefileFiles, boolean strict, boolean useMemoryMapped, GeometryFactory gf) 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.
-
ShapefileReader
public ShapefileReader(ShpFiles shapefileFiles, boolean strict, boolean useMemoryMapped, GeometryFactory gf, boolean onlyRandomAccess) 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.useMemoryMapped
- Wheter to enable memory mapping or notgf
- The geometry factory used to build the geometriesonlyRandomAccess
- When true sets up the reader to do exclusively read driven by goTo(x) and thus avoids opening the .shx file- 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
-
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.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- 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:- it's your responsibility to ensure the offset corresponds to the actual beginning of a shape struct
- once you call this, reading with hasNext/next on sparse shapefiles will be broken (we don't know anymore at which shape we are)
-
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:- it's your responsibility to ensure the offset corresponds to the actual beginning of a shape struct
- once you call this, reading with hasNext/next on sparse shapefiles will be broken (we don't know anymore at which shape we are)
-
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:- it's your responsibility to ensure the offset corresponds to the actual beginning of a shape struct
- once you call this, reading with hasNext/next on sparse shapefiles will be broken (we don't know anymore at which shape we are)
- 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 occursUnsupportedOperationException
- 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 interfaceFileReader
- Returns:
- id for the reader.
-
setFlatGeometry
public void setFlatGeometry(boolean flatGeometry)
-
-