/// <summary> /// Initializes a new instance of the PbfReader class that read data form specified file. /// </summary> /// <param name="path">The path to the input file.</param> /// <param name="settings">The OsmReaderSettings object that determines behaviour of PbfReader.</param> public PbfReader(string path, OsmReaderSettings settings) { _input = new FileStream(path, FileMode.Open, FileAccess.Read); _cache = new Queue <IEntityInfo>(); this.Settings = settings; this.Settings.IsReadOnly = true; BlobHeader blobHeader = null; while ((blobHeader = this.ReadBlobHeader()) != null) { try { if (blobHeader.Type == "OSMHeader") { OsmHeader osmHeader = (OsmHeader)this.ReadBlob(blobHeader); this.ProcessOsmHeader(osmHeader); return; } else if (blobHeader.Type == "OSMData") { throw new InvalidDataException("Input stream doesn't contain an 'OSMHeader' block before 'OSMData' block."); } else { _input.Seek(blobHeader.DataSize, SeekOrigin.Current); } } catch (ProtoException ex) { throw new InvalidDataException("Input stream contains unsupported data", ex); } } throw new InvalidDataException("Input stream doesn't contain an 'OSMHeader' block."); }
/// <summary> /// Writes PBF file header to the underlaying stream. /// </summary> private void WriteHeader() { OsmHeader header = new OsmHeader(); header.RequiredFeatures.Add("OsmSchema-V0.6"); if (this.Settings.UseDenseFormat) { header.RequiredFeatures.Add("DenseNodes"); } if (this.Settings.WriteMetadata) { header.OptionalFeatures.Add("Has_Metadata"); } using (MemoryStream stream = new MemoryStream()) { Serializer.Serialize <OsmHeader>(stream, header); byte[] buffer = new byte[stream.Length]; Array.Copy(stream.GetBuffer(), buffer, stream.Length); this.WriteBlob("OSMHeader", buffer); } }
/// <summary> /// Initializes a new instance of the PbfReader class that read data form specified stream. /// </summary> /// <param name="input">The input stream.</param> /// <param name="settings">The OsmReaderSettings object that determines behaviour of PbfReader.</param> public PbfReader(Stream input, OsmReaderSettings settings) { _input = input; _cache = new Queue <IEntityInfo>(); this.Settings = settings; this.Settings.IsReadOnly = true; BlobHeader blobHeader = null; while ((blobHeader = this.ReadBlobHeader()) != null) { if (blobHeader.Type == "OSMHeader") { OsmHeader osmHeader = (OsmHeader)this.ReadBlob(blobHeader); this.ProcessOsmHeader(osmHeader); return; } else if (blobHeader.Type == "OSMData") { throw new InvalidDataException("Input stream doesn't contain an 'OSMHeader' block before 'OSMData' block."); } else { _input.Seek(blobHeader.DataSize, SeekOrigin.Current); } } throw new InvalidDataException("Input stream doesn't contain an 'OSMHeader' block."); }
/// <summary> /// Checks OsmHeader required features and if any of required features isn't supported, NotSupportedException is thrown. /// </summary> /// <param name="header">OsmHeader object to process.</param> private void ProcessOsmHeader(OsmHeader header) { string[] supportedFeatures = new string[] { "OsmSchema-V0.6", "DenseNodes" }; foreach (var required in header.RequiredFeatures) { if (supportedFeatures.Contains(required) == false) { throw new NotSupportedException(string.Format("Processing specified PBF file requires '{0}' feature which isn't supported by PbfReader.", required)); } } }
public async Task <OsmHeader> ReadHeader() { await reader.BeginReadMessageAsync(length); var header = new OsmHeader(); while (reader.State == ProtobufReaderState.Field) { switch (reader.FieldNumber) { case 1: header.BoundBox = await ParseBoundBoxAsync(); break; case 4: header.RequiredFeatures = await reader.ReadStringAsync(); break; case 5: header.OptionalFeatures = await reader.ReadStringAsync(); break; case 16: header.WritingProgram = await reader.ReadStringAsync(); break; case 17: header.Source = await reader.ReadStringAsync(); break; default: await reader.SkipAsync(); break; } } await reader.EndReadMessageAsync(); return(header); }