public static int ReadBinary(byte[] buf, int ofs, out OsmRelation relation) { int p = 0; ulong tmp; p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp); long id = (long)tmp; p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp); var values = new KeyValuePair <string, string> [tmp]; for (int i = 0; i < values.Length; i++) { string key, val; p += ProtoBuf.ReadString(buf, ofs + p, out key); p += ProtoBuf.ReadString(buf, ofs + p, out val); values[i] = new KeyValuePair <string, string>(key, val); } p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp); var members = new OsmRelationMember[tmp]; for (int i = 0; i < members.Length; i++) { p += OsmRelationMember.ReadBinary(buf, ofs + p, out members[i]); } relation = new OsmRelation(id, values, members); return(p); }
public static int ReadBinary(byte[] buf, int ofs, out OsmNode node) { int p = 0; ulong tmp; p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp); long id = (long)tmp; p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp); int latCode = ProtoBuf.SignedInt32((uint)tmp); p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp); int lonCode = ProtoBuf.SignedInt32((uint)tmp); p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp); var values = tmp == 0 ? emptyValues : new KeyValuePair <string, string> [tmp]; for (int i = 0; i < values.Length; i++) { string key, val; p += ProtoBuf.ReadString(buf, ofs + p, out key); p += ProtoBuf.ReadString(buf, ofs + p, out val); values[i] = new KeyValuePair <string, string>(key, val); } node = new OsmNode(id, latCode, lonCode, values); return(p); }
public static int ReadBinary(byte[] buf, int ofs, out OsmWay way) { int p = 0; ulong tmp; p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp); long id = (long)tmp; p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp); var values = new KeyValuePair <string, string> [tmp]; for (int i = 0; i < values.Length; i++) { string key, val; p += ProtoBuf.ReadString(buf, ofs + p, out key); p += ProtoBuf.ReadString(buf, ofs + p, out val); values[i] = new KeyValuePair <string, string>(key, val); } p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp); way = new OsmWay(id, values, new long[tmp]); for (int i = 0; i < way.nodeIds.Length; i++) { p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp); way.nodeIds[i] = (long)tmp; } return(p); }
static void DecodeNodeBlock(byte[] buf, int bufLen) { long id = 0; long pos = 0; var keyDict = new Dictionary <string, int>(); var valDict = new Dictionary <string, int>(); var outputNodes = new List <DecodeNodeBlockValue>(); var outputValues = new List <int>(); for (int p = 0; p < bufLen;) { DecodeNodeBlockValue nodeValue; ulong tmp; p += ProtoBuf.ReadVarInt(buf, p, out tmp); id += ProtoBuf.SignedInt64(tmp); nodeValue.id = id; p += ProtoBuf.ReadVarInt(buf, p, out tmp); pos += ProtoBuf.SignedInt64(tmp); nodeValue.pos = pos; p += ProtoBuf.ReadVarInt(buf, p, out tmp); int valueCount = (int)(uint)tmp; nodeValue.valueCount = valueCount; nodeValue.valuePos = outputValues.Count / 2; for (int i = 0; i < valueCount; i++) { string key, val; p += ProtoBuf.ReadString(buf, p, out key); if (!keyDict.ContainsKey(key)) { keyDict.Add(key, keyDict.Count); } outputValues.Add(keyDict[key]); p += ProtoBuf.ReadString(buf, p, out val); if (!valDict.ContainsKey(val)) { valDict.Add(val, valDict.Count); } outputValues.Add(valDict[val]); } outputNodes.Add(nodeValue); } // --- sort --- long lastId = outputNodes[outputNodes.Count - 1].id; Console.WriteLine(" sort: " + outputNodes.Count + " nodes"); outputNodes.Sort((x, y) => x.pos.CompareTo(y.pos)); // --- write --- Console.WriteLine("write: " + outputNodes.Count + " nodes"); WriteNodeBlock(keyDict, valDict, outputValues, outputNodes, lastId); }
public static int ReadBinary(byte[] buf, int ofs, out OsmRelationMember member) { int p = 0; ulong tmp; p += ProtoBuf.ReadVarInt(buf, ofs + p, out tmp); long id = (long)tmp; var type = (MemberType)buf[ofs + p]; p++; string str; p += ProtoBuf.ReadString(buf, ofs + p, out str); member = new OsmRelationMember(id, type, str); return(p); }
/// <summary> /// Dekodiert die Werte aus einem PBF-Stream /// </summary> /// <param name="buf">Buffer, worraus die Werte gelesen werden sollen</param> /// <param name="ofs">Startposition innerhalb des Buffers</param> /// <param name="headerBlock">HeaderBlock-Struktur mit den ausgelesenen Werten</param> /// <returns>Anzahl der gelesenen Bytes aus dem Buffer</returns> public static int Decode(byte[] buf, int ofs, out HeaderBlock headerBlock) { /***** * message HeaderBlock * { * optional HeaderBBox bbox = 1; * * // Additional tags to aid in parsing this dataset * repeated string required_features = 4; * repeated string optional_features = 5; * * optional string writingprogram = 16; * * optional string source = 17; // From the bbox field. * * // Tags that allow continuing an Osmosis replication * // replication timestamp, expressed in seconds since the epoch, * // otherwise the same value as in the "timestamp=..." field * // in the state.txt file used by Osmosis * optional int64 osmosis_replication_timestamp = 32; * * // replication sequence number (sequenceNumber in state.txt) * optional int64 osmosis_replication_sequence_number = 33; * * // replication base URL (from Osmosis' configuration.txt file) * optional string osmosis_replication_base_url = 34; * } *****/ int len = 0; ulong tmp; var tmps = new List <string>(); headerBlock = new HeaderBlock(); // --- optional HeaderBBox bbox = 1; --- if (buf[ofs + len] == (1 << 3 | 2)) { len++; len += HeaderBBox.Decode(buf, ofs + len, out headerBlock.bbox); } // --- repeated string required_features = 4; --- tmps.Clear(); while (buf[ofs + len] == (4 << 3 | 2)) { len++; string feature; len += ProtoBuf.ReadString(buf, ofs + len, out feature); tmps.Add(feature); switch (feature) { case "OsmSchema-V0.6": break; case "DenseNodes": break; default: throw new PbfParseException("required feature not supported: \"" + feature + "\""); } } headerBlock.requiredFeatures = tmps.ToArray(); // --- repeated string optional_features = 5; --- tmps.Clear(); while (buf[ofs + len] == (5 << 3 | 2)) { len++; string feature; len += ProtoBuf.ReadString(buf, ofs + len, out feature); tmps.Add(feature); switch (feature) { case "Has_Metadata": break; case "Sort.Type_then_ID": break; default: throw new PbfParseException("optional feature not supported: \"" + feature + "\""); } } headerBlock.optionalFeatures = tmps.ToArray(); // --- optional string writingprogram = 16; --- if (ProtoBuf.PeekVarInt(buf, ofs + len) == (16 << 3 | 2)) { len += 2; len += ProtoBuf.ReadString(buf, ofs + len, out headerBlock.writingprogram); } else { headerBlock.writingprogram = ""; } // --- optional string source = 17; --- if (ProtoBuf.PeekVarInt(buf, ofs + len) == (17 << 3 | 2)) { len += 2; len += ProtoBuf.ReadString(buf, ofs + len, out headerBlock.source); } else { headerBlock.source = ""; } // --- optional int64 osmosis_replication_timestamp = 32; --- if (ProtoBuf.PeekVarInt(buf, ofs + len) == (32 << 3 | 0)) { len += 2; len += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp); headerBlock.replicationTimestamp = (long)tmp; } // --- optional int64 osmosis_replication_sequence_number = 33; --- if (ProtoBuf.PeekVarInt(buf, ofs + len) == (33 << 3 | 0)) { len += 2; len += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp); headerBlock.replicationSequenceNumber = (long)tmp; } // --- optional string osmosis_replication_base_url = 34; --- if (ProtoBuf.PeekVarInt(buf, ofs + len) == (34 << 3 | 2)) { len += 2; len += ProtoBuf.ReadString(buf, ofs + len, out headerBlock.replicationBaseUrl); } else { headerBlock.replicationBaseUrl = ""; } return(len); }
/// <summary> /// dekodiert nur den Header eines Blockes (benötigt mindestens 32 Bytes) /// </summary> /// <param name="buf">Buffer, welcher ausgelesen werden soll</param> /// <param name="ofs">Startposition innerhalb des Buffers</param> /// <param name="result">fertig gelesenes Ergebnis</param> /// <returns>Größe des gesamten Blockes in Bytes</returns> public static int DecodeQuick(byte[] buf, int ofs, out OsmBlob result) { /***** * message BlobHeader * { * required string type = 1; * optional bytes indexdata = 2; * required int32 datasize = 3; * } *****/ int len = 0; int blobHeaderLen; len += ProtoBuf.ReadInt32Fix(buf, ofs + len, out blobHeaderLen); result = new OsmBlob(); // --- required string type = 1; --- string type; if (buf[ofs + len++] != (1 << 3 | 2)) { throw new PbfParseException(); } len += ProtoBuf.ReadString(buf, ofs + len, out type); // --- optional bytes indexdata = 2; --- if (buf[ofs + len] == (2 << 3 | 2)) { throw new NotImplementedException(); } // --- required int32 datasize = 3; --- if (buf[ofs + len++] != (3 << 3 | 0)) { throw new PbfParseException(); } ulong tmp; len += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp); result.blobLen = len + (int)tmp; if (len - sizeof(int) != blobHeaderLen) { throw new PbfParseException(); } /***** * message Blob * { * optional bytes raw = 1; // keine Kompression * optional int32 raw_size = 2; // Nur gesetzt, wenn komprimiert, auf die unkomprimierte Größe * optional bytes zlib_data = 3; * // optional bytes lzma_data = 4; // GEPLANT. * // optional bytes OBSOLETE_bzip2_data = 5; // Veraltet. * } *****/ // --- optional bytes raw = 1; --- if (buf[ofs + len] == (1 << 3 | 2)) { throw new NotSupportedException(); } // --- optional int32 raw_size = 2; --- if (buf[ofs + len] == (2 << 3 | 0)) { len++; len += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp); result.rawSize = (int)tmp; } // --- optional bytes zlib_data = 3; --- if (buf[ofs + len] == (3 << 3 | 2)) { len++; len += ProtoBuf.ReadVarInt(buf, ofs + len, out tmp); result.zlibOfs = len; result.zlibLen = (int)tmp; len += (int)tmp; } else { // --- optional bytes lzma_data = 4; --- if (buf[ofs + len] == (4 << 3 | 2)) { throw new NotSupportedException(); } // --- optional bytes OBSOLETE_bzip2_data = 5; --- if (buf[ofs + len] == (5 << 3 | 2)) { throw new NotSupportedException(); } } if (len != result.blobLen) { throw new PbfParseException(); } switch (type) { case "OSMHeader": break; case "OSMData": break; default: throw new Exception("unknown Type: " + type); } return(len); }