Package org.arakhne.afc.io.shape
Class AbstractCommonShapeFileReader<E>
- java.lang.Object
-
- org.arakhne.afc.io.shape.AbstractCommonShapeFileReader<E>
-
- Type Parameters:
E
- is the type of element in the shape file.
- All Implemented Interfaces:
AutoCloseable
,Iterable<E>
- Direct Known Subclasses:
AbstractShapeFileReader
,ShapeFileIndexReader
public abstract class AbstractCommonShapeFileReader<E> extends Object implements Iterable<E>, AutoCloseable
This class is an abstract reader for all the ESRI Shape file formats.To have a lower memory foot-print, call
disableSeek()
. Indeed, the seek feature forces this reader to maintain a buffer of all the file content.The specification of the ESRI Shape file format is described in the July 98 specification document.
- Since:
- 14.0
- Version:
- 17.0 2020-01-04 14:41:52
- Author:
- Stéphane GALLAND, Olivier LAMOTTE
- Maven Group Id:
- org.arakhne.afc.advanced
- Maven Artifact Id:
- shapefile
-
-
Field Summary
Fields Modifier and Type Field Description protected static int
BLOCK_SIZE
Size of a block of data in the buffer, when the seek feature was disable.protected ShapeElementType
expectedShapeType
Is the type of element stored inside the shape file.protected int
fileSize
Size in bytes of the input stream.protected double
maxm
Bounding box in the file header.protected double
maxx
Bounding box in the file header.protected double
maxy
Bounding box in the file header.protected double
maxz
Bounding box in the file header.protected double
minm
Bounding box in the file header.protected double
minx
Bounding box in the file header.protected double
miny
Bounding box in the file header.protected double
minz
Bounding box in the file header.
-
Constructor Summary
Constructors Constructor Description AbstractCommonShapeFileReader(File file)
Constructor.AbstractCommonShapeFileReader(InputStream inputStream)
Constructor.AbstractCommonShapeFileReader(URL file)
Constructor.AbstractCommonShapeFileReader(ReadableByteChannel inputStream)
Constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
close()
Close the stream.void
disableSeek()
Disable the seek feature to have lower memory foot-print.protected void
ensureAvailableBytes(int amount)
Ensure that the reading buffer is containing enoug bytes to read.ESRIBounds
getBoundsFromHeader()
Replies the bounds read from the shape file header.int
getFileReadingPosition()
Replies the count of bytes read from the shape file.int
getFileSize()
Replies the total size of the Shape file.ShapeElementType
getShapeElementType()
Replies the type of the elements stored in the file.Progression
getTaskProgression()
Replies the task progression.protected boolean
isHeaderRead()
Replies if the header was read.boolean
isSeekEnabled()
Replies if the seek feature is enabled.Iterator<E>
iterator()
Returns an iterator over a set of elements of type E.Iterator<E>
iterator(boolean disableSeek)
Returns an iterator over a set of elements of type E.<EE extends E>
Iterator<EE>iterator(Class<EE> type)
Returns an iterator over a set of elements of type E.<EE extends E>
Iterator<EE>iterator(Class<EE> type, boolean disableSeek)
Returns an iterator over a set of elements of type E.protected void
postHeaderReadingStage()
Called just after the header was read.protected void
postReadingStage(boolean success)
Called after all the entries was read.protected boolean
postRecordReadingStage(E element_representation)
Called just after an entry was read but just before the dBase attributes were read.protected void
preReadingStage()
Called after the reader was initialized and before the header of the shape file was read.E
read()
Read the elements for a shape file.protected double
readBEDouble()
Read a big endian 8-byte floating point number.protected int
readBEInt()
Read a big endian 4-byte integer.void
readHeader()
Read the header of the shape file.protected double
readLEDouble()
Read a little endian 8-byte floating point number.protected int
readLEInt()
Read a little endian 4-byte integer.protected abstract E
readRecord(int recordIndex)
Read a record according to its type.abstract void
seek(int recordIndex)
Move the reading head at the specified record index.protected void
setReadingPosition(int recordIndex, int byteIndex)
Set the reading position, excluding the header.void
setTaskProgression(Progression progressBar)
Set the task progression associated to this writer.protected void
skipBytes(int amount)
Skip an amount of bytes.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Field Detail
-
BLOCK_SIZE
protected static final int BLOCK_SIZE
Size of a block of data in the buffer, when the seek feature was disable.- See Also:
- Constant Field Values
-
fileSize
protected int fileSize
Size in bytes of the input stream.
-
minx
protected double minx
Bounding box in the file header.
-
miny
protected double miny
Bounding box in the file header.
-
minz
protected double minz
Bounding box in the file header.
-
minm
protected double minm
Bounding box in the file header.
-
maxx
protected double maxx
Bounding box in the file header.
-
maxy
protected double maxy
Bounding box in the file header.
-
maxz
protected double maxz
Bounding box in the file header.
-
maxm
protected double maxm
Bounding box in the file header.
-
expectedShapeType
protected ShapeElementType expectedShapeType
Is the type of element stored inside the shape file.
-
-
Constructor Detail
-
AbstractCommonShapeFileReader
public AbstractCommonShapeFileReader(ReadableByteChannel inputStream)
Constructor.- Parameters:
inputStream
- is the stream to read
-
AbstractCommonShapeFileReader
public AbstractCommonShapeFileReader(InputStream inputStream)
Constructor.- Parameters:
inputStream
- is the stream to read
-
AbstractCommonShapeFileReader
public AbstractCommonShapeFileReader(File file) throws IOException
Constructor.- Parameters:
file
- is the file to read- Throws:
IOException
- if cannot read.
-
AbstractCommonShapeFileReader
public AbstractCommonShapeFileReader(URL file) throws IOException
Constructor.- Parameters:
file
- is the file to read- Throws:
IOException
- if cannot read.
-
-
Method Detail
-
isHeaderRead
@Pure protected boolean isHeaderRead()
Replies if the header was read.- Returns:
true
if the header was read, otherwisefalse
-
getTaskProgression
@Pure public Progression getTaskProgression()
Replies the task progression.- Returns:
- the task progression object associated to this writer, or
null
-
setTaskProgression
public void setTaskProgression(Progression progressBar)
Set the task progression associated to this writer.- Parameters:
progressBar
- is the task progression object associated to this writer, ornull
-
getFileSize
public int getFileSize() throws IOException
Replies the total size of the Shape file.- Returns:
- the size of the shape file in bytes
- Throws:
IOException
- in case of error.
-
getShapeElementType
public ShapeElementType getShapeElementType()
Replies the type of the elements stored in the file.This type of element is extracted from header.
- Returns:
- the type of element, or
null
if the header could not be read.
-
getFileReadingPosition
public int getFileReadingPosition() throws IOException
Replies the count of bytes read from the shape file.- Returns:
- the count of bytes read from shape file.
- Throws:
IOException
- in case of error.
-
getBoundsFromHeader
public ESRIBounds getBoundsFromHeader()
Replies the bounds read from the shape file header.- Returns:
- the bounds or
null
-
readHeader
public final void readHeader() throws IOException
Read the header of the shape file.- Throws:
IOException
- in case of error.
-
disableSeek
public void disableSeek() throws IOException
Disable the seek feature to have lower memory foot-print. This function should be print before any invocation (directly or indirectly) toreadHeader()
.- Throws:
IOException
- in case of error.
-
isSeekEnabled
@Pure public boolean isSeekEnabled()
Replies if the seek feature is enabled.- Returns:
true
if the seek feature is enabled, otherwisefalse
-
seek
public abstract void seek(int recordIndex) throws IOException
Move the reading head at the specified record index.If the index is negative, the next record to read is assumed to be the first record. If the index is greater or equals to the count of records, the exception
EOFException
will be thrown.- Parameters:
recordIndex
- is the index of record to reply at the next read.- Throws:
IOException
- in case of error.
-
read
public E read() throws IOException
Read the elements for a shape file.- Returns:
- the list of the elements read from the Shape file or
null
on end-of-file. - Throws:
IOException
- in case of error.
-
close
public void close() throws IOException
Close the stream.- Specified by:
close
in interfaceAutoCloseable
- Throws:
IOException
- in case of error.
-
readRecord
protected abstract E readRecord(int recordIndex) throws EOFException, IOException
Read a record according to its type.- Parameters:
recordIndex
- is the index of the read record.- Returns:
- the element extracted from the record
- Throws:
EOFException
- if EOF is reach to early.IOException
- in case of error.
-
preReadingStage
protected void preReadingStage() throws IOException
Called after the reader was initialized and before the header of the shape file was read.- Throws:
IOException
- in case of error.
-
postReadingStage
protected void postReadingStage(boolean success) throws IOException
Called after all the entries was read.- Parameters:
success
- istrue
is the reading was successfull, otherwhisefalse
.- Throws:
IOException
- in case of error.
-
postRecordReadingStage
protected boolean postRecordReadingStage(E element_representation) throws IOException
Called just after an entry was read but just before the dBase attributes were read.- Parameters:
element_representation
- is the value returned by the reading function.- Returns:
true
if the object is assumed to be valid (ie. replies by the reading function, otherwhisefalse
.- Throws:
IOException
- in case of error.
-
postHeaderReadingStage
protected void postHeaderReadingStage() throws IOException
Called just after the header was read.- Throws:
IOException
- in case of error.
-
iterator
public Iterator<E> iterator()
Returns an iterator over a set of elements of type E. The seek feature is automatically disabled when invoking this function. Seeiterator(boolean)
to prevent seek disabling.- Specified by:
iterator
in interfaceIterable<E>
- Returns:
- an Iterator.
- See Also:
iterator(boolean)
,iterator(Class,boolean)
,disableSeek()
-
iterator
public Iterator<E> iterator(boolean disableSeek)
Returns an iterator over a set of elements of type E. The seek feature is not automatically disabled when invoking this function. Seeiterator()
to disable seek automatically.- Parameters:
disableSeek
- indicates if the seek feature must be disabled or not.- Returns:
- an Iterator.
- See Also:
iterator()
,iterator(Class)
,iterator(Class,boolean)
,disableSeek()
-
iterator
public <EE extends E> Iterator<EE> iterator(Class<EE> type)
Returns an iterator over a set of elements of type E. The seek feature is automatically disabled when invoking this function. Seeiterator(boolean)
to prevent seek disabling.- Type Parameters:
EE
- is the type of the elements to reply.- Parameters:
type
- is the type of the elements to reply.- Returns:
- an Iterator.
- See Also:
iterator()
,iterator(boolean)
,iterator(Class,boolean)
,disableSeek()
-
iterator
public <EE extends E> Iterator<EE> iterator(Class<EE> type, boolean disableSeek)
Returns an iterator over a set of elements of type E. The seek feature is not automatically disabled when invoking this function. Seeiterator()
to disable seek automatically.- Type Parameters:
EE
- is the type of the elements to reply.- Parameters:
type
- is the type of the elements to reply.disableSeek
- indicates if the seek feature must be disabled or not.- Returns:
- an Iterator.
- See Also:
iterator()
,iterator(boolean)
,iterator(Class)
,disableSeek()
-
setReadingPosition
protected void setReadingPosition(int recordIndex, int byteIndex) throws IOException
Set the reading position, excluding the header.- Parameters:
recordIndex
- is the index of the next record to read.byteIndex
- is the index of the next byte to read (excluding the header).- Throws:
IOException
- in case of error.
-
ensureAvailableBytes
protected void ensureAvailableBytes(int amount) throws IOException
Ensure that the reading buffer is containing enoug bytes to read.- Parameters:
amount
- is the count of expected bytes- Throws:
IOException
- in case of error.
-
skipBytes
protected void skipBytes(int amount) throws IOException
Skip an amount of bytes.- Parameters:
amount
- the amount to skip.- Throws:
IOException
- in case of error.
-
readBEInt
protected final int readBEInt() throws IOException
Read a big endian 4-byte integer.- Returns:
- a big endian 4-byte integer.
- Throws:
IOException
- in case of error.
-
readBEDouble
protected final double readBEDouble() throws IOException
Read a big endian 8-byte floating point number.- Returns:
- a big endian 8-byte floating point number.
- Throws:
IOException
- in case of error.
-
readLEInt
protected final int readLEInt() throws IOException
Read a little endian 4-byte integer.- Returns:
- a little endian 4-byte integer.
- Throws:
IOException
- in case of error.
-
readLEDouble
protected final double readLEDouble() throws IOException
Read a little endian 8-byte floating point number.- Returns:
- a little endian 8-byte floating point number.
- Throws:
IOException
- in case of error.
-
-