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 intBLOCK_SIZESize of a block of data in the buffer, when the seek feature was disable.protected ShapeElementTypeexpectedShapeTypeIs the type of element stored inside the shape file.protected intfileSizeSize in bytes of the input stream.protected doublemaxmBounding box in the file header.protected doublemaxxBounding box in the file header.protected doublemaxyBounding box in the file header.protected doublemaxzBounding box in the file header.protected doubleminmBounding box in the file header.protected doubleminxBounding box in the file header.protected doubleminyBounding box in the file header.protected doubleminzBounding 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 voidclose()Close the stream.voiddisableSeek()Disable the seek feature to have lower memory foot-print.protected voidensureAvailableBytes(int amount)Ensure that the reading buffer is containing enoug bytes to read.ESRIBoundsgetBoundsFromHeader()Replies the bounds read from the shape file header.intgetFileReadingPosition()Replies the count of bytes read from the shape file.intgetFileSize()Replies the total size of the Shape file.ShapeElementTypegetShapeElementType()Replies the type of the elements stored in the file.ProgressiongetTaskProgression()Replies the task progression.protected booleanisHeaderRead()Replies if the header was read.booleanisSeekEnabled()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 voidpostHeaderReadingStage()Called just after the header was read.protected voidpostReadingStage(boolean success)Called after all the entries was read.protected booleanpostRecordReadingStage(E element_representation)Called just after an entry was read but just before the dBase attributes were read.protected voidpreReadingStage()Called after the reader was initialized and before the header of the shape file was read.Eread()Read the elements for a shape file.protected doublereadBEDouble()Read a big endian 8-byte floating point number.protected intreadBEInt()Read a big endian 4-byte integer.voidreadHeader()Read the header of the shape file.protected doublereadLEDouble()Read a little endian 8-byte floating point number.protected intreadLEInt()Read a little endian 4-byte integer.protected abstract EreadRecord(int recordIndex)Read a record according to its type.abstract voidseek(int recordIndex)Move the reading head at the specified record index.protected voidsetReadingPosition(int recordIndex, int byteIndex)Set the reading position, excluding the header.voidsetTaskProgression(Progression progressBar)Set the task progression associated to this writer.protected voidskipBytes(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:
trueif 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 IOExceptionReplies 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
nullif the header could not be read.
-
getFileReadingPosition
public int getFileReadingPosition() throws IOExceptionReplies 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 IOExceptionRead the header of the shape file.- Throws:
IOException- in case of error.
-
disableSeek
public void disableSeek() throws IOExceptionDisable 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:
trueif the seek feature is enabled, otherwisefalse
-
seek
public abstract void seek(int recordIndex) throws IOExceptionMove 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
EOFExceptionwill 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
nullon end-of-file. - Throws:
IOException- in case of error.
-
close
public void close() throws IOExceptionClose the stream.- Specified by:
closein 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 IOExceptionCalled 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 IOExceptionCalled after all the entries was read.- Parameters:
success- istrueis 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:
trueif 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 IOExceptionCalled 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:
iteratorin 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 IOExceptionSet 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 IOExceptionEnsure 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 IOExceptionSkip an amount of bytes.- Parameters:
amount- the amount to skip.- Throws:
IOException- in case of error.
-
readBEInt
protected final int readBEInt() throws IOExceptionRead 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 IOExceptionRead 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 IOExceptionRead 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 IOExceptionRead a little endian 8-byte floating point number.- Returns:
- a little endian 8-byte floating point number.
- Throws:
IOException- in case of error.
-
-