Package org.openstreetmap.josm.io
Class OsmPbfReader
java.lang.Object
org.openstreetmap.josm.io.AbstractReader
org.openstreetmap.josm.io.OsmPbfReader
Read OSM data from an OSM PBF file
- Since:
- 18695
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classThis could be replaced byBoundedInputStreamfrom Apache Commons IO.private static final classA record class for passing PrimitiveBlock information to the PrimitiveGroup parserNested classes/interfaces inherited from class org.openstreetmap.josm.io.AbstractReader
AbstractReader.BinaryParserWorker, AbstractReader.CommonReader, AbstractReader.NodeReader, AbstractReader.ParserWorker, AbstractReader.RelationReader, AbstractReader.WayReader -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final long[]private static final intThe maximum Blob size.private static final intThe maximum BlobHeader size.private static final doubleNano degreesFields inherited from class org.openstreetmap.josm.io.AbstractReader
cancel, ds, externalIdMap, relations, uploadChangeset, ways -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidAdd a set of tags to a primitiveprivate static LatLoncalculateLatLon(OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord, long lat, long lon) Calculate the actual lat lonprivate static voidcheckRequiredFeatures(HeaderBlock headerBlock) Ensure that we support all the required features in the PBFprivate static long[]decodePackedSInt64(long[] numbers) Convert an array of numbers to an array of longs, decoded from uint (zig zag decoded)protected DataSetdoParseDataSet(InputStream source, ProgressMonitor progressMonitor) private static long[]joinArrays(long[] array1, long[] array2) Join two different arraysprivate voidparse(InputStream source) private static BBoxparseBBox(ByteArrayOutputStream baos, ProtobufRecord current) This parses a bbox from a record (HeaderBBox message)private static BlobparseBlob(BlobHeader header, OsmPbfReader.BoundedInputStream cis, ProtobufParser parser, ByteArrayOutputStream baos) Parse a blob from the PBF fileprivate static BlobHeaderparseBlobHeader(OsmPbfReader.BoundedInputStream cis, ByteArrayOutputStream baos, ProtobufParser parser) Parse a blob headerprivate voidparseDataBlock(ByteArrayOutputStream baos, HeaderBlock headerBlock, Blob blob) Parse a data blob (should be "OSMData")static DataSetparseDataSet(InputStream source, ProgressMonitor progressMonitor) Parse the given input source and return the dataset.private static Info[]parseDenseInfo(ByteArrayOutputStream baos, byte[] bytes) Parse dense infoprivate voidparseDenseNodes(ByteArrayOutputStream baos, byte[] bytes, OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord) Parse dense nodes from a recordprivate static HeaderBlockparseHeaderBlock(Blob blob, ByteArrayOutputStream baos) Parse a header block.private static InfoparseInfo(ByteArrayOutputStream baos, byte[] bytes) Parse info for an objectprivate voidparseNode(ByteArrayOutputStream baos, byte[] bytes, OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord) Parse a singular nodeprivate voidparsePrimitiveGroup(ByteArrayOutputStream baos, byte[] bytes, OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord) Parse a PrimitiveGroup.private voidparseRelation(ByteArrayOutputStream baos, byte[] bytes, OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord) Parse a relation from a PBFprivate static String[]parseStringTable(ByteArrayOutputStream baos, byte[] bytes) Parse the string tableprivate voidparseWay(ByteArrayOutputStream baos, byte[] bytes, OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord) Parse a way from the PBFprivate static voidsetOsmPrimitiveData(OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord, PrimitiveData primitive, Info info) Set the primitive data for an objectMethods inherited from class org.openstreetmap.josm.io.AbstractReader
buildPrimitive, callPostProcessors, deregisterPostprocessor, doParseDataSet, doParseDataSet, getDataSet, getLong, parseAction, parseBounds, parseChangeset, parseChangeset, parseDownloadPolicy, parseId, parseLocked, parseNode, parseNode, parseRelation, parseRelationMember, parseRelationMember, parseTag, parseTimestamp, parseUploadPolicy, parseUser, parseUser, parseVersion, parseVersion, parseVersion, parseVisible, parseWay, prepareDataSet, processChangesetAfterParsing, processNodesAfterParsing, processRelationsAfterParsing, processWaysAfterParsing, registerPostprocessor
-
Field Details
-
EMPTY_LONG
-
NANO_DEGREES
Nano degrees- See Also:
-
MAX_BLOBHEADER_SIZE
The maximum BlobHeader size. BlobHeaders should (but not must) be less than half this- See Also:
-
MAX_BLOB_SIZE
The maximum Blob size. Blobs should (but not must) be less than half this- See Also:
-
-
Constructor Details
-
OsmPbfReader
private OsmPbfReader()
-
-
Method Details
-
parseDataSet
public static DataSet parseDataSet(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.INSTANCEis assumed- Returns:
- the dataset with the parsed data
- Throws:
IllegalDataException- if an error was found while parsing the data from the sourceIllegalArgumentException- if source is null
-
doParseDataSet
protected DataSet doParseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException - Specified by:
doParseDataSetin classAbstractReader- Throws:
IllegalDataException
-
parse
- Throws:
IllegalDataExceptionIOException
-
parseBlobHeader
@Nonnull private static BlobHeader parseBlobHeader(OsmPbfReader.BoundedInputStream cis, ByteArrayOutputStream baos, ProtobufParser parser) throws IOException, IllegalDataException Parse a blob header- Parameters:
cis- A counting stream to ensure we don't read too much databaos- A reusable streamparser- The parser to read from- Returns:
- The BlobHeader message
- Throws:
IOException- if one of the streams has an issueIllegalDataException- If the OSM PBF is (probably) corrupted
-
parseBlob
@Nonnull private static Blob parseBlob(BlobHeader header, OsmPbfReader.BoundedInputStream cis, ProtobufParser parser, ByteArrayOutputStream baos) throws 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 dataparser- The parser to read records frombaos- The reusable output stream- Returns:
- The blob to use elsewhere
- Throws:
IOException- If one of the streams has an issue
-
parseHeaderBlock
@Nonnull private static HeaderBlock parseHeaderBlock(Blob blob, ByteArrayOutputStream baos) throws 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 databaos- The reusable output stream to use- Returns:
- The parsed HeaderBlock
- Throws:
IOException- if one of theInputStreams has a problem
-
checkRequiredFeatures
Ensure that we support all the required features in the PBF- Parameters:
headerBlock- The HeaderBlock to check- Throws:
IllegalDataException- If there exists at least one feature that we do not support
-
parseDataBlock
private void parseDataBlock(ByteArrayOutputStream baos, HeaderBlock headerBlock, Blob blob) throws IOException, IllegalDataException Parse a data blob (should be "OSMData")- Parameters:
baos- The reusable streamheaderBlock- The header block with data source informationblob- The blob to read OSM data from- Throws:
IOException- if we don't support the compression typeIllegalDataException- If an invalid OSM primitive was read
-
parseBBox
@Nullable private static BBox parseBBox(ByteArrayOutputStream baos, ProtobufRecord current) throws IOException This parses a bbox from a record (HeaderBBox message)- Parameters:
baos- The reusableByteArrayOutputStreamto avoid unnecessary allocationscurrent- The current record- Returns:
- The immutable bbox, or
null - Throws:
IOException- If something happens with theInputStreams (probably won't happen)
-
parseStringTable
@Nonnull private static String[] parseStringTable(ByteArrayOutputStream baos, byte[] bytes) throws IOException Parse the string table- Parameters:
baos- The reusable streambytes- The message bytes- Returns:
- The parsed table (reminder: index 0 is empty, note that all strings are already interned by
String.intern()) - Throws:
IOException- if something happened while reading aByteArrayInputStream
-
parsePrimitiveGroup
private void parsePrimitiveGroup(ByteArrayOutputStream baos, byte[] bytes, OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord) throws IllegalDataException, 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 streambytes- The bytes to decodeprimitiveBlockRecord- The record to use for creating the primitives- Throws:
IllegalDataException- if one of the primitive records was invalidIOException- if something happened while reading aByteArrayInputStream
-
parseNode
private void parseNode(ByteArrayOutputStream baos, byte[] bytes, OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord) throws IllegalDataException, IOException Parse a singular node- Parameters:
baos- The reusable streambytes- The bytes to decodeprimitiveBlockRecord- 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 creationIOException- if something happened while reading aByteArrayInputStream
-
parseDenseNodes
private void parseDenseNodes(ByteArrayOutputStream baos, byte[] bytes, OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord) throws IllegalDataException, IOException Parse dense nodes from a record- Parameters:
baos- The reusable output streambytes- The bytes for the dense nodeprimitiveBlockRecord- 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 malformedIOException- if something happened while reading aByteArrayInputStream
-
parseWay
private void parseWay(ByteArrayOutputStream baos, byte[] bytes, OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord) throws IllegalDataException, IOException Parse a way from the PBF- Parameters:
baos- The reusable streambytes- The bytes for the wayprimitiveBlockRecord- Used for common information, like tags- Throws:
IllegalDataException- if an invalid way could have been createdIOException- if something happened while reading aByteArrayInputStream
-
parseRelation
private void parseRelation(ByteArrayOutputStream baos, byte[] bytes, OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord) throws IllegalDataException, IOException Parse a relation from a PBF- Parameters:
baos- The reusable streambytes- The bytes to useprimitiveBlockRecord- Mostly used for tags- Throws:
IllegalDataException- if the PBF had a bad relation definitionIOException- if something happened while reading aByteArrayInputStream
-
parseInfo
Parse info for an object- Parameters:
baos- The reusable stream to usebytes- The bytes to decode- Returns:
- The info for an object
- Throws:
IOException- if something happened while reading aByteArrayInputStream
-
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 datalat- The latitude from the PBFlon- The longitude from the PBF- Returns:
- The actual
LatLon, accounting for PBF offset and granularity changes
-
addTags
Add a set of tags to a primitive- Parameters:
primitive- The primitive to add tags tokeys- The keys (must match the size of the values)values- The values (must match the size of the keys)
-
setOsmPrimitiveData
private static void setOsmPrimitiveData(OsmPbfReader.PrimitiveBlockRecord primitiveBlockRecord, PrimitiveData primitive, Info info) Set the primitive data for an object- Parameters:
primitiveBlockRecord- The record with data for the current primitive (currently usesOsmPbfReader.PrimitiveBlockRecord.stringTableandOsmPbfReader.PrimitiveBlockRecord.dateGranularity).primitive- The primitive to add the information toinfo- The specific info for the primitive
-
decodePackedSInt64
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
Join two different arrays- Parameters:
array1- The first arrayarray2- 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(ByteArrayOutputStream baos, byte[] bytes) throws IllegalDataException, IOException Parse dense info- Parameters:
baos- The reusable streambytes- The bytes to decode- Returns:
- The dense info array
- Throws:
IllegalDataException- If the data has mismatched array lengthsIOException- if something happened while reading aByteArrayInputStream
-