예제 #1
0
 /** Close the underlying Channels. */
 @Override
 public void close() throws IOException {
   try {
     if (shpChannel != null && shpChannel.isOpen()) {
       shpChannel.close();
     }
   } finally {
     shx.close();
   }
   shpChannel = null;
   handler = null;
   shapeBuffer = null;
 }
예제 #2
0
  /**
   * Write the headers for this shapefile including the bounds, shape type, the number of geometries
   * and the total fileLength (in actual bytes, NOT 16 bit words).
   */
  public void writeHeaders(
      final Envelope bounds,
      final ShapeType type,
      final int numberOfGeometries,
      final int fileLength)
      throws IOException {

    try {
      handler = type.getShapeHandler(true);
    } catch (DataStoreException se) {
      throw new RuntimeException("unexpected Exception", se);
    }
    if (shapeBuffer == null) allocateBuffers();

    ShapefileHeader.write(
        shapeBuffer,
        type,
        fileLength / 2,
        bounds.getMinX(),
        bounds.getMinY(),
        bounds.getMaxX(),
        bounds.getMaxY());

    shx.moveToHeaderStart();
    shx.writeHeader(
        type,
        50 + 4 * numberOfGeometries,
        bounds.getMinX(),
        bounds.getMinY(),
        bounds.getMaxX(),
        bounds.getMaxY());

    offset = 50;
    this.type = type;
    cnt = 0;

    shpChannel.position(0);
    drain();
  }
예제 #3
0
  /**
   * Write a single Geometry to this shapefile. The Geometry must be compatable with the ShapeType
   * assigned during the writing of the headers.
   */
  public void writeGeometry(final Geometry g) throws IOException {
    if (shapeBuffer == null) throw new IOException("Must write headers first");
    lp = shapeBuffer.position();

    // see doc for handling null geometries
    // http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
    int length;
    if (g == null) {
      length = writeNullGeometry();
    } else {
      length = writeNonNullGeometry(g);
    }

    assert (length * 2 == (shapeBuffer.position() - lp) - 8);

    lp = shapeBuffer.position();

    // write to the shx
    shx.writeRecord(offset, length);
    offset += length + 4;

    drain();
    assert (shapeBuffer.position() == 0);
  }
예제 #4
0
 /**
  * Allocate internal buffers and position the channels to the beginning or the record section of
  * the shapefile. The headers MUST be rewritten after this operation, or the file may be
  * corrupt...
  */
 public void skipHeaders() throws IOException {
   if (shapeBuffer == null) allocateBuffers();
   shpChannel.position(100);
   shx.moveToRecordStart();
 }