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()); }
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); }
/** * Bulk write method for writing a collection of (hopefully) like geometries of the given * ShapeType. */ public void write(final GeometryCollection geometries, final ShapeType type) throws IOException, DataStoreException { handler = type.getShapeHandler(true); writeHeaders(geometries, type); lp = shapeBuffer.position(); for (int i = 0, ii = geometries.getNumGeometries(); i < ii; i++) { Geometry g = geometries.getGeometryN(i); writeGeometry(g); } close(); }
/** * 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(); }