Class OsmPbfReader

    • Method Detail

      • parseDataSet

        public static DataSet parseDataSet​(java.io.InputStream source,
                                           ProgressMonitor progressMonitor)
                                    throws IllegalDataException
        Parse the given input source and return the dataset.
        Parameters:
        source - the source input stream. Must not be null.
        progressMonitor - the progress monitor. If null, NullProgressMonitor.INSTANCE is assumed
        Returns:
        the dataset with the parsed data
        Throws:
        IllegalDataException - if an error was found while parsing the data from the source
        java.lang.IllegalArgumentException - if source is null
      • parseBlob

        @Nonnull
        private static Blob parseBlob​(BlobHeader header,
                                      OsmPbfReader.BoundedInputStream cis,
                                      ProtobufParser parser,
                                      java.io.ByteArrayOutputStream baos)
                               throws java.io.IOException
        Parse a blob from the PBF file
        Parameters:
        header - The header with the blob information (most critically, the length of the blob)
        cis - Used to ensure we don't read too much data
        parser - The parser to read records from
        baos - The reusable output stream
        Returns:
        The blob to use elsewhere
        Throws:
        java.io.IOException - If one of the streams has an issue
      • parseHeaderBlock

        @Nonnull
        private static HeaderBlock parseHeaderBlock​(Blob blob,
                                                    java.io.ByteArrayOutputStream baos)
                                             throws java.io.IOException
        Parse a header block. This assumes that the parser has hit a string with the text "OSMHeader".
        Parameters:
        blob - The blob with the header block data
        baos - The reusable output stream to use
        Returns:
        The parsed HeaderBlock
        Throws:
        java.io.IOException - if one of the InputStreams has a problem
      • parseDataBlock

        private void parseDataBlock​(java.io.ByteArrayOutputStream baos,
                                    HeaderBlock headerBlock,
                                    Blob blob)
                             throws java.io.IOException,
                                    IllegalDataException
        Parse a data blob (should be "OSMData")
        Parameters:
        baos - The reusable stream
        headerBlock - The header block with data source information
        blob - The blob to read OSM data from
        Throws:
        java.io.IOException - if we don't support the compression type
        IllegalDataException - If an invalid OSM primitive was read
      • parseBBox

        @Nullable
        private static BBox parseBBox​(java.io.ByteArrayOutputStream baos,
                                      ProtobufRecord current)
                               throws java.io.IOException
        This parses a bbox from a record (HeaderBBox message)
        Parameters:
        baos - The reusable ByteArrayOutputStream to avoid unnecessary allocations
        current - The current record
        Returns:
        The immutable bbox, or null
        Throws:
        java.io.IOException - If something happens with the InputStreams (probably won't happen)
      • parseStringTable

        @Nonnull
        private static java.lang.String[] parseStringTable​(java.io.ByteArrayOutputStream baos,
                                                           byte[] bytes)
                                                    throws java.io.IOException
        Parse the string table
        Parameters:
        baos - The reusable stream
        bytes - The message bytes
        Returns:
        The parsed table (reminder: index 0 is empty, note that all strings are already interned by String.intern())
        Throws:
        java.io.IOException - if something happened while reading a ByteArrayInputStream
      • parsePrimitiveGroup

        private void parsePrimitiveGroup​(java.io.ByteArrayOutputStream baos,
                                         byte[] bytes,
                                         OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord)
                                  throws IllegalDataException,
                                         java.io.IOException
        Parse a PrimitiveGroup. Note: this parsing implementation doesn't check and make certain that all primitives in the group are the same type.
        Parameters:
        baos - The reusable stream
        bytes - The bytes to decode
        primitiveBlockRecord - The record to use for creating the primitives
        Throws:
        IllegalDataException - if one of the primitive records was invalid
        java.io.IOException - if something happened while reading a ByteArrayInputStream
      • parseNode

        private void parseNode​(java.io.ByteArrayOutputStream baos,
                               byte[] bytes,
                               OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord)
                        throws IllegalDataException,
                               java.io.IOException
        Parse a singular node
        Parameters:
        baos - The reusable stream
        bytes - The bytes to decode
        primitiveBlockRecord - The record to use (mostly for tags and lat/lon calculations)
        Throws:
        IllegalDataException - if the PBF did not provide all the data necessary for node creation
        java.io.IOException - if something happened while reading a ByteArrayInputStream
      • parseDenseNodes

        private void parseDenseNodes​(java.io.ByteArrayOutputStream baos,
                                     byte[] bytes,
                                     OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord)
                              throws IllegalDataException,
                                     java.io.IOException
        Parse dense nodes from a record
        Parameters:
        baos - The reusable output stream
        bytes - The bytes for the dense node
        primitiveBlockRecord - Used for data that is common between several different objects.
        Throws:
        IllegalDataException - if the nodes could not be parsed, or one of the nodes would be malformed
        java.io.IOException - if something happened while reading a ByteArrayInputStream
      • parseWay

        private void parseWay​(java.io.ByteArrayOutputStream baos,
                              byte[] bytes,
                              OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord)
                       throws IllegalDataException,
                              java.io.IOException
        Parse a way from the PBF
        Parameters:
        baos - The reusable stream
        bytes - The bytes for the way
        primitiveBlockRecord - Used for common information, like tags
        Throws:
        IllegalDataException - if an invalid way could have been created
        java.io.IOException - if something happened while reading a ByteArrayInputStream
      • parseRelation

        private void parseRelation​(java.io.ByteArrayOutputStream baos,
                                   byte[] bytes,
                                   OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord)
                            throws IllegalDataException,
                                   java.io.IOException
        Parse a relation from a PBF
        Parameters:
        baos - The reusable stream
        bytes - The bytes to use
        primitiveBlockRecord - Mostly used for tags
        Throws:
        IllegalDataException - if the PBF had a bad relation definition
        java.io.IOException - if something happened while reading a ByteArrayInputStream
      • parseInfo

        @Nonnull
        private static Info parseInfo​(java.io.ByteArrayOutputStream baos,
                                      byte[] bytes)
                               throws java.io.IOException
        Parse info for an object
        Parameters:
        baos - The reusable stream to use
        bytes - The bytes to decode
        Returns:
        The info for an object
        Throws:
        java.io.IOException - if something happened while reading a ByteArrayInputStream
      • calculateLatLon

        @Nonnull
        private static LatLon calculateLatLon​(OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord,
                                              long lat,
                                              long lon)
        Calculate the actual lat lon
        Parameters:
        primitiveBlockRecord - The record with offset and granularity data
        lat - The latitude from the PBF
        lon - The longitude from the PBF
        Returns:
        The actual LatLon, accounting for PBF offset and granularity changes
      • addTags

        private static void addTags​(Tagged primitive,
                                    java.util.List<java.lang.String> keys,
                                    java.util.List<java.lang.String> values)
        Add a set of tags to a primitive
        Parameters:
        primitive - The primitive to add tags to
        keys - The keys (must match the size of the values)
        values - The values (must match the size of the keys)
      • decodePackedSInt64

        @Nonnull
        private static long[] decodePackedSInt64​(long[] numbers)
        Convert an array of numbers to an array of longs, decoded from uint (zig zag decoded)
        Parameters:
        numbers - The numbers to convert
        Returns:
        The long array (the same array that was passed in)
      • joinArrays

        @Nonnull
        private static long[] joinArrays​(long[] array1,
                                         long[] array2)
        Join two different arrays
        Parameters:
        array1 - The first array
        array2 - The second array
        Returns:
        The joined arrays -- may return one of the original arrays, if the other is empty
      • parseDenseInfo

        @Nonnull
        private static Info[] parseDenseInfo​(java.io.ByteArrayOutputStream baos,
                                             byte[] bytes)
                                      throws IllegalDataException,
                                             java.io.IOException
        Parse dense info
        Parameters:
        baos - The reusable stream
        bytes - The bytes to decode
        Returns:
        The dense info array
        Throws:
        IllegalDataException - If the data has mismatched array lengths
        java.io.IOException - if something happened while reading a ByteArrayInputStream