Package org.geotools.data.shapefile.shp
Class ShapefileReader
Object
ShapefileReader
- All Implemented Interfaces:
Closeable
,AutoCloseable
,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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal class
The reader returns only one Record instance in its lifetime. -
Constructor Summary
ConstructorsConstructorDescriptionShapefileReader
(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
Modifier and TypeMethodDescriptionvoid
close()
Clean up any resources.void
Disables .shx file usage.static int
fill
(ByteBuffer buffer, ReadableByteChannel channel) int
getCount
(int count) Parses the shpfile counting the records.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.id()
An id for the reader.Fetch the next record information.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) shapeAt
(int offset) Returns the shape at the specified byte distance from the beginning of the file.boolean
int
transferTo
(ShapefileWriter writer, int recordNum, double[] bounds) Transfer (by bytes) the data at the current record to the ShapefileWriter.
-
Constructor Details
-
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 Details
-
disableShxUsage
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
- Throws:
IOException
-
getHeader
Get the header. Its parsed in the constructor.- Returns:
- The header that is associated with this file.
-
close
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
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
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
Fetch the next record information.- Returns:
- The record instance associated with this reader.
- Throws:
IOException
-
goTo
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
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
Parses the shpfile counting the records.- Returns:
- the number of non-null records in the shapefile
- Throws:
DataSourceException
-
setHandler
- Parameters:
handler
- The handler to set.
-
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)
-