Search in sources :

Example 6 with BlobHeader

use of de.topobyte.osm4j.pbf.util.BlobHeader in project osm4j-pbf by topobyte.

the class BlobParser method parseBlob.

private void parseBlob(DataInput data) throws IOException {
    BlobHeader header = PbfUtil.parseHeader(data);
    Fileformat.Blob blob = PbfUtil.parseBlock(data, header.getDataLength());
    parse(header, blob);
}
Also used : BlobHeader(de.topobyte.osm4j.pbf.util.BlobHeader) Fileformat(de.topobyte.osm4j.pbf.protobuf.Fileformat)

Example 7 with BlobHeader

use of de.topobyte.osm4j.pbf.util.BlobHeader in project osm4j-pbf by topobyte.

the class CountBlocks method main.

public static void main(String[] args) throws IOException {
    if (args.length != 1) {
        System.out.println("usage: " + CountBlocks.class.getSimpleName() + " <filename>");
        System.exit(1);
    }
    InputStream input = new FileInputStream(args[0]);
    DataInputStream data = new DataInputStream(input);
    long nBlocks = 0;
    while (true) {
        try {
            BlobHeader blockHeader = PbfUtil.parseHeader(data);
            input.skip(blockHeader.getDataLength());
            nBlocks++;
        } catch (EOFException eof) {
            break;
        }
    }
    System.out.println("Number of blocks: " + nBlocks);
}
Also used : DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BlobHeader(de.topobyte.osm4j.pbf.util.BlobHeader) EOFException(java.io.EOFException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream)

Example 8 with BlobHeader

use of de.topobyte.osm4j.pbf.util.BlobHeader in project osm4j-pbf by topobyte.

the class EntitySplit method execute.

public void execute() throws IOException {
    while (true) {
        try {
            BlobHeader header = PbfUtil.parseHeader(input);
            Fileformat.Blob blob = PbfUtil.parseBlock(input, header.getDataLength());
            String type = header.getType();
            // requested types
            if (type.equals(Constants.BLOCK_TYPE_DATA)) {
                data(blob);
            } else if (type.equals(Constants.BLOCK_TYPE_HEADER)) {
                if (copyNodes) {
                    blockWriterNodes.write(header.getType(), null, blob);
                }
                if (copyWays) {
                    blockWriterWays.write(header.getType(), null, blob);
                }
                if (copyRelations) {
                    blockWriterRelations.write(header.getType(), null, blob);
                }
            }
        } catch (EOFException eof) {
            break;
        }
    }
    if (copyNodes) {
        outNodes.close();
    }
    if (copyWays) {
        outWays.close();
    }
    if (copyRelations) {
        outRelations.close();
    }
}
Also used : BlobHeader(de.topobyte.osm4j.pbf.util.BlobHeader) EOFException(java.io.EOFException) Fileformat(de.topobyte.osm4j.pbf.protobuf.Fileformat)

Example 9 with BlobHeader

use of de.topobyte.osm4j.pbf.util.BlobHeader in project osm4j-pbf by topobyte.

the class HeaderInfo method main.

public static void main(String[] args) throws IOException {
    if (args.length != 1) {
        System.out.println("usage: " + HeaderInfo.class.getSimpleName() + " <filename>");
        System.exit(1);
    }
    InputStream input = new FileInputStream(args[0]);
    DataInputStream data = new DataInputStream(input);
    boolean passedFirst = false;
    boolean warningShown = false;
    while (true) {
        if (passedFirst && !warningShown) {
            System.out.println("Warning: first block is not a header!");
            System.out.println("... will continue reading until I find one.");
            warningShown = true;
        }
        try {
            BlobHeader header = PbfUtil.parseHeader(data);
            Fileformat.Blob blob = PbfUtil.parseBlock(data, header.getDataLength());
            BlockData blockData = PbfUtil.getBlockData(blob);
            String type = header.getType();
            passedFirst = true;
            if (type.equals(Constants.BLOCK_TYPE_HEADER)) {
                Osmformat.HeaderBlock headerBlock = Osmformat.HeaderBlock.parseFrom(blockData.getBlobData());
                printHeaderInfo(headerBlock);
                return;
            }
        } catch (EOFException eof) {
            if (!passedFirst) {
                System.out.println("End of file before header");
            }
            break;
        }
    }
}
Also used : DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BlobHeader(de.topobyte.osm4j.pbf.util.BlobHeader) Fileformat(de.topobyte.osm4j.pbf.protobuf.Fileformat) DataInputStream(java.io.DataInputStream) Osmformat(de.topobyte.osm4j.pbf.protobuf.Osmformat) FileInputStream(java.io.FileInputStream) EOFException(java.io.EOFException) BlockData(de.topobyte.osm4j.pbf.util.BlockData)

Example 10 with BlobHeader

use of de.topobyte.osm4j.pbf.util.BlobHeader in project osm4j-pbf by topobyte.

the class PbfIterator method advanceBlock.

private void advanceBlock() throws IOException {
    BlobHeader header = PbfUtil.parseHeader(input);
    Fileformat.Blob blob = PbfUtil.parseBlock(input, header.getDataLength());
    BlockData blockData = PbfUtil.getBlockData(blob);
    String type = header.getType();
    if (type.equals(Constants.BLOCK_TYPE_DATA)) {
        beyondBounds = true;
        Osmformat.PrimitiveBlock block = Osmformat.PrimitiveBlock.parseFrom(blockData.getBlobData());
        PrimParser primParser = new PrimParser(block, fetchMetadata);
        for (Osmformat.PrimitiveGroup group : block.getPrimitivegroupList()) {
            if (group.getNodesCount() > 0) {
                pushNodes(primParser, group.getNodesList());
            }
            if (group.hasDense()) {
                pushNodes(primParser, group.getDense());
            }
            if (group.getWaysCount() > 0) {
                pushWays(primParser, group.getWaysList());
            }
            if (group.getRelationsCount() > 0) {
                pushRelations(primParser, group.getRelationsList());
            }
        }
    } else if (type.equals(Constants.BLOCK_TYPE_HEADER)) {
        Osmformat.HeaderBlock block = Osmformat.HeaderBlock.parseFrom(blockData.getBlobData());
        HeaderBBox bbox = block.getBbox();
        if (bbox != null && !beyondBounds) {
            this.bounds = PbfUtil.bounds(bbox);
        }
        beyondBounds = true;
    } else {
        throw new IOException("invalid PBF block");
    }
}
Also used : HeaderBBox(de.topobyte.osm4j.pbf.protobuf.Osmformat.HeaderBBox) BlobHeader(de.topobyte.osm4j.pbf.util.BlobHeader) Fileformat(de.topobyte.osm4j.pbf.protobuf.Fileformat) IOException(java.io.IOException) BlockData(de.topobyte.osm4j.pbf.util.BlockData) Osmformat(de.topobyte.osm4j.pbf.protobuf.Osmformat)

Aggregations

BlobHeader (de.topobyte.osm4j.pbf.util.BlobHeader)12 Fileformat (de.topobyte.osm4j.pbf.protobuf.Fileformat)10 EOFException (java.io.EOFException)10 BlockData (de.topobyte.osm4j.pbf.util.BlockData)6 DataInputStream (java.io.DataInputStream)6 FileInputStream (java.io.FileInputStream)6 InputStream (java.io.InputStream)6 BlockWriter (de.topobyte.osm4j.pbf.seq.BlockWriter)4 FileOutputStream (java.io.FileOutputStream)4 OutputStream (java.io.OutputStream)4 Osmformat (de.topobyte.osm4j.pbf.protobuf.Osmformat)3 IOException (java.io.IOException)2 ByteString (com.google.protobuf.ByteString)1 HeaderBBox (de.topobyte.osm4j.pbf.protobuf.Osmformat.HeaderBBox)1