Class ShapefileReader

  • All Implemented Interfaces:
    Closeable, AutoCloseable, FileReader

    public class ShapefileReader
    extends Object
    implements FileReader, Closeable
    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
    • 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 not
        gf - The geometry factory used to build the geometries
        onlyRandomAccess - 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
      • getHeader

        public ShapefileHeader getHeader()
        Get the header. Its parsed in the constructor.
        Returns:
        The header that is associated with this file.
      • 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
      • 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)
        Throws:
        IOException
        UnsupportedOperationException
      • 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)
        Throws:
        IOException
        UnsupportedOperationException
      • 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 occurs
        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
      • 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 interface FileReader
        Returns:
        id for the reader.
      • setFlatGeometry

        public void setFlatGeometry​(boolean flatGeometry)