Search in sources :

Example 1 with StreamSplitter

use of org.openstreetmap.osmosis.pbf2.v0_6.impl.StreamSplitter in project osmosis by openstreetmap.

the class PbfReader method run.

@Override
public void run() {
    StreamSplitter streamSplitter = null;
    ExecutorService executorService;
    if (workers > 0) {
        executorService = Executors.newFixedThreadPool(workers);
    } else {
        executorService = MoreExecutors.newDirectExecutorService();
    }
    try {
        InputStream inputStream = supplier.get();
        // Create a stream splitter to break the PBF stream into blobs.
        streamSplitter = new StreamSplitter(new DataInputStream(inputStream));
        // Obtain the header block.
        Osmformat.HeaderBlock header = new HeaderSeeker().apply(streamSplitter);
        // Get the pipeline metadata (e.g. do ways include location information) from header.
        Map<String, Object> metadata = new HeaderMetadataReader().apply(header);
        sink.initialize(metadata);
        // Get Bound information from the header.
        BoundContainer bound = new HeaderBoundReader().apply(header);
        sink.process(bound);
        // Process all blobs of data in the stream using threads from the
        // executor service. We allow the decoder to issue an extra blob
        // than there are workers to ensure there is another blob
        // immediately ready for processing when a worker thread completes.
        // The main thread is responsible for splitting blobs from the
        // request stream, and sending decoded entities to the sink.
        PbfDecoder pbfDecoder = new PbfDecoder(streamSplitter, executorService, workers + 1, sink);
        pbfDecoder.run();
        sink.complete();
    } finally {
        sink.close();
        executorService.shutdownNow();
        if (streamSplitter != null) {
            streamSplitter.close();
        }
    }
}
Also used : HeaderBoundReader(org.openstreetmap.osmosis.pbf2.v0_6.impl.HeaderBoundReader) HeaderMetadataReader(org.openstreetmap.osmosis.pbf2.v0_6.impl.HeaderMetadataReader) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DataInputStream(java.io.DataInputStream) Osmformat(crosby.binary.Osmformat) HeaderSeeker(org.openstreetmap.osmosis.pbf2.v0_6.impl.HeaderSeeker) BoundContainer(org.openstreetmap.osmosis.core.container.v0_6.BoundContainer) PbfDecoder(org.openstreetmap.osmosis.pbf2.v0_6.impl.PbfDecoder) ExecutorService(java.util.concurrent.ExecutorService) StreamSplitter(org.openstreetmap.osmosis.pbf2.v0_6.impl.StreamSplitter)

Aggregations

Osmformat (crosby.binary.Osmformat)1 DataInputStream (java.io.DataInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ExecutorService (java.util.concurrent.ExecutorService)1 BoundContainer (org.openstreetmap.osmosis.core.container.v0_6.BoundContainer)1 HeaderBoundReader (org.openstreetmap.osmosis.pbf2.v0_6.impl.HeaderBoundReader)1 HeaderMetadataReader (org.openstreetmap.osmosis.pbf2.v0_6.impl.HeaderMetadataReader)1 HeaderSeeker (org.openstreetmap.osmosis.pbf2.v0_6.impl.HeaderSeeker)1 PbfDecoder (org.openstreetmap.osmosis.pbf2.v0_6.impl.PbfDecoder)1 StreamSplitter (org.openstreetmap.osmosis.pbf2.v0_6.impl.StreamSplitter)1