Search in sources :

Example 1 with BlobHeader

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

the class CopyGroupwise method main.

public static void main(String[] args) throws IOException {
    if (args.length != 2) {
        System.out.println("usage: " + CopyGroupwise.class.getSimpleName() + " <input> <output>");
        System.exit(1);
    }
    InputStream input = new FileInputStream(args[0]);
    OutputStream output = new FileOutputStream(args[1]);
    DataInputStream data = new DataInputStream(input);
    BlockWriter blockWriter = new BlockWriter(output);
    while (true) {
        try {
            BlobHeader header = PbfUtil.parseHeader(data);
            Fileformat.Blob blob = PbfUtil.parseBlock(data, header.getDataLength());
            String type = header.getType();
            if (type.equals(Constants.BLOCK_TYPE_DATA)) {
                BlockData blockData = PbfUtil.getBlockData(blob);
                Osmformat.PrimitiveBlock primBlock = Osmformat.PrimitiveBlock.parseFrom(blockData.getBlobData());
                Osmformat.PrimitiveBlock.Builder builder = Osmformat.PrimitiveBlock.newBuilder();
                for (int i = 0; i < primBlock.getPrimitivegroupCount(); i++) {
                    Osmformat.PrimitiveGroup.Builder groupBuilder = Osmformat.PrimitiveGroup.newBuilder();
                    Osmformat.PrimitiveGroup group = primBlock.getPrimitivegroup(i);
                    List<Osmformat.Node> nodes = group.getNodesList();
                    Osmformat.DenseNodes dense = group.getDense();
                    List<Osmformat.Way> ways = group.getWaysList();
                    List<Osmformat.Relation> relations = group.getRelationsList();
                    groupBuilder.addAllNodes(nodes);
                    if (group.hasDense()) {
                        groupBuilder.setDense(dense);
                    }
                    groupBuilder.addAllWays(ways);
                    groupBuilder.addAllRelations(relations);
                    builder.addPrimitivegroup(groupBuilder.build());
                }
                builder.setGranularity(primBlock.getGranularity());
                builder.setDateGranularity(primBlock.getDateGranularity());
                builder.setStringtable(primBlock.getStringtable());
                Osmformat.PrimitiveBlock block = builder.build();
                ByteString message = block.toByteString();
                blockWriter.write(header.getType(), null, blockData.getCompression(), message);
            } else if (type.equals(Constants.BLOCK_TYPE_HEADER)) {
                blockWriter.write(header.getType(), null, blob);
            }
        } catch (EOFException eof) {
            break;
        }
    }
    output.close();
}
Also used : BlobHeader(de.topobyte.osm4j.pbf.util.BlobHeader) ByteString(com.google.protobuf.ByteString) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Fileformat(de.topobyte.osm4j.pbf.protobuf.Fileformat) ByteString(com.google.protobuf.ByteString) Osmformat(de.topobyte.osm4j.pbf.protobuf.Osmformat) EOFException(java.io.EOFException) BlockWriter(de.topobyte.osm4j.pbf.seq.BlockWriter) BlockData(de.topobyte.osm4j.pbf.util.BlockData) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream)

Example 2 with BlobHeader

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

the class Uncompress method main.

public static void main(String[] args) throws IOException {
    if (args.length != 2) {
        System.out.println("usage: " + Uncompress.class.getSimpleName() + " <input> <output>");
        System.exit(1);
    }
    InputStream input = new FileInputStream(args[0]);
    OutputStream output = new FileOutputStream(args[1]);
    DataInputStream data = new DataInputStream(input);
    BlockWriter blockWriter = new BlockWriter(output);
    while (true) {
        try {
            BlobHeader header = PbfUtil.parseHeader(data);
            Fileformat.Blob blob = PbfUtil.parseBlock(data, header.getDataLength());
            BlockData blockData = PbfUtil.getBlockData(blob);
            blockWriter.write(header.getType(), null, Compression.NONE, blockData.getBlobData());
        } catch (EOFException eof) {
            break;
        }
    }
    output.close();
}
Also used : DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BlobHeader(de.topobyte.osm4j.pbf.util.BlobHeader) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) EOFException(java.io.EOFException) Fileformat(de.topobyte.osm4j.pbf.protobuf.Fileformat) BlockWriter(de.topobyte.osm4j.pbf.seq.BlockWriter) DataInputStream(java.io.DataInputStream) BlockData(de.topobyte.osm4j.pbf.util.BlockData) FileInputStream(java.io.FileInputStream)

Example 3 with BlobHeader

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

the class CompressDeflate method main.

public static void main(String[] args) throws IOException {
    if (args.length != 2) {
        System.out.println("usage: " + CompressDeflate.class.getSimpleName() + " <input> <output>");
        System.exit(1);
    }
    InputStream input = new FileInputStream(args[0]);
    OutputStream output = new FileOutputStream(args[1]);
    DataInputStream data = new DataInputStream(input);
    BlockWriter blockWriter = new BlockWriter(output);
    while (true) {
        try {
            BlobHeader header = PbfUtil.parseHeader(data);
            Fileformat.Blob blob = PbfUtil.parseBlock(data, header.getDataLength());
            BlockData blockData = PbfUtil.getBlockData(blob);
            blockWriter.write(header.getType(), null, Compression.DEFLATE, blockData.getBlobData());
        } catch (EOFException eof) {
            break;
        }
    }
    output.close();
}
Also used : DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BlobHeader(de.topobyte.osm4j.pbf.util.BlobHeader) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) EOFException(java.io.EOFException) Fileformat(de.topobyte.osm4j.pbf.protobuf.Fileformat) BlockWriter(de.topobyte.osm4j.pbf.seq.BlockWriter) DataInputStream(java.io.DataInputStream) BlockData(de.topobyte.osm4j.pbf.util.BlockData) FileInputStream(java.io.FileInputStream)

Example 4 with BlobHeader

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

the class CompressLz4 method main.

public static void main(String[] args) throws IOException {
    if (args.length != 2) {
        System.out.println("usage: " + CompressLz4.class.getSimpleName() + " <input> <output>");
        System.exit(1);
    }
    InputStream input = new FileInputStream(args[0]);
    OutputStream output = new FileOutputStream(args[1]);
    DataInputStream data = new DataInputStream(input);
    BlockWriter blockWriter = new BlockWriter(output);
    while (true) {
        try {
            BlobHeader header = PbfUtil.parseHeader(data);
            Fileformat.Blob blob = PbfUtil.parseBlock(data, header.getDataLength());
            BlockData blockData = PbfUtil.getBlockData(blob);
            blockWriter.write(header.getType(), null, Compression.LZ4, blockData.getBlobData());
        } catch (EOFException eof) {
            break;
        }
    }
    output.close();
}
Also used : DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BlobHeader(de.topobyte.osm4j.pbf.util.BlobHeader) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) EOFException(java.io.EOFException) Fileformat(de.topobyte.osm4j.pbf.protobuf.Fileformat) BlockWriter(de.topobyte.osm4j.pbf.seq.BlockWriter) DataInputStream(java.io.DataInputStream) BlockData(de.topobyte.osm4j.pbf.util.BlockData) FileInputStream(java.io.FileInputStream)

Example 5 with BlobHeader

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

the class PbfFile method buildBlockIndex.

public void buildBlockIndex() throws IOException {
    headerBlockInfo = null;
    dataBlockInfos.clear();
    file.seek(0);
    while (true) {
        try {
            long pos = file.getFilePointer();
            int headerSize = file.readInt();
            BlobHeader header = PbfUtil.parseHeader(file, headerSize);
            file.skipBytes(header.getDataLength());
            BlockInfo info = new BlockInfo(pos, headerSize, header.getDataLength());
            if (header.getType().equals(Constants.BLOCK_TYPE_DATA)) {
                dataBlockInfos.add(info);
            } else if (header.getType().equals(Constants.BLOCK_TYPE_HEADER)) {
                if (headerBlockInfo == null) {
                    headerBlockInfo = info;
                } else {
                    throw new IOException("Multiple header blocks");
                }
            } else {
                throw new IOException("invalid PBF block");
            }
        } catch (EOFException eof) {
            break;
        }
    }
    blockIndexInitialized = true;
}
Also used : BlobHeader(de.topobyte.osm4j.pbf.util.BlobHeader) EOFException(java.io.EOFException) IOException(java.io.IOException)

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