Beispiel #1
0
  private void init(boolean strict, GeometryFactory gf) throws IOException, ShapefileException {
    geometryFactory = gf;

    if (channel instanceof FileChannel && useMemoryMappedBuffer) {
      FileChannel fc = (FileChannel) channel;
      buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
      buffer.position(0);
      this.currentOffset = 0;
    } else {
      // force useMemoryMappedBuffer to false
      this.useMemoryMappedBuffer = false;
      // start small
      buffer = NIOUtilities.allocate(1024);
      fill(buffer, channel);
      buffer.flip();
      this.currentOffset = 0;
    }
    header = new ShapefileHeader();
    header.read(buffer, strict);
    fileShapeType = header.getShapeType();
    handler = fileShapeType.getShapeHandler(gf);
    if (handler == null) {
      throw new IOException("Unsuported shape type:" + fileShapeType);
    }

    headerTransfer = ByteBuffer.allocate(8);
    headerTransfer.order(ByteOrder.BIG_ENDIAN);

    // make sure the record end is set now...
    record.end = this.toFileOffset(buffer.position());
  }
Beispiel #2
0
 /**
  * A short cut for reading the header from the given channel.
  *
  * @param channel The channel to read from.
  * @param warningListener
  * @throws IOException If problems arise.
  * @return A ShapefileHeader object.
  */
 public static ShapefileHeader readHeader(
     ReadableByteChannel channel, WarningListener warningListener) throws IOException {
   ByteBuffer buffer = ByteBuffer.allocateDirect(100);
   if (channel.read(buffer) != 100) {
     throw new EOFException("Premature end of header");
   }
   buffer.flip();
   ShapefileHeader header = new ShapefileHeader();
   header.read(buffer, warningListener);
   return header;
 }
Beispiel #3
0
  private void init(WarningListener warningListener) throws IOException, ShapefileException {
    header = readHeader(channel, warningListener);
    fileShapeType = header.getShapeType();
    handler = fileShapeType.getShapeHandler();

    // recordHeader = ByteBuffer.allocateDirect(8);
    // recordHeader.order(ByteOrder.BIG_ENDIAN);

    if (handler == null) {
      throw new IOException("Unsuported shape type:" + fileShapeType);
    }
    buffer = new ReadBufferManager(channel);
  }
Beispiel #4
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();
  }