/// <summary> /// Processes all ways from the PrimitiveGroup and adds them to the output queue. /// </summary> /// <param name="block">The PrimitiveBlock that contains specified PrimitiveGroup.</param> /// <param name="group">The PrimitiveGroup with nodes to process.</param> private void ProcessWays(PrimitiveBlock block, PrimitiveGroup group) { if (group.Ways == null) { return; } foreach (var way in group.Ways) { long refStore = 0; List <long> refs = new List <long>(way.Refs.Count); for (int i = 0; i < way.Refs.Count; i++) { refStore += (long)way.Refs[i]; refs.Add(refStore); } List <Tag> tags = new List <Tag>(); if (way.Keys != null) { for (int i = 0; i < way.Keys.Count; i++) { tags.Add(new Tag(block.StringTable[way.Keys[i]], block.StringTable[way.Values[i]])); } } EntityMetadata metadata = this.ProcessMetadata(way.Metadata, block); WayInfo parsed = new WayInfo((long)way.ID, new TagsCollection(tags), refs, metadata); _cache.Enqueue(parsed); } }
//Makes the map piece by piece private void MakeMap(MapType type) { //Initializes the Array of Arrays that holds every block in the game map = new PrimitiveBlock[BIOME_WIDTH][]; backMap = new PrimitiveBlock[BIOME_WIDTH][]; for (int i = 0; i < BIOME_WIDTH; i++) { map[i] = new PrimitiveBlock[BIOME_HEIGHT]; backMap[i] = new PrimitiveBlock[BIOME_HEIGHT]; } for (int x = 0; x < BIOME_WIDTH; x++) { for (int y = 0; y < BIOME_HEIGHT; y++) { map[x][y] = new PrimitiveBlock(x, y); backMap[x][y] = new PrimitiveBlock(x, y); } } switch (type) { case MapType.FOREST: MakeForest(); break; case MapType.PLAINS: MakePlains(); break; } }
/// <summary> /// Converts the PBF node into an OsmSharp-node. /// </summary> /// <param name="block"></param> /// <param name="node"></param> /// <returns></returns> internal OsmSharp.Osm.Node ConvertNode(PrimitiveBlock block, OsmSharp.Osm.PBF.Node node) { var simpleNode = new OsmSharp.Osm.Node(); simpleNode.ChangeSetId = node.info.changeset; simpleNode.Id = node.id; simpleNode.Latitude = .000000001 * ((double)block.lat_offset + ((double)block.granularity * (double)node.lat)); simpleNode.Longitude = .000000001 * ((double)block.lon_offset + ((double)block.granularity * (double)node.lon)); simpleNode.Tags = new TagsCollection(node.keys.Count); if (node.keys.Count > 0) { for (int tag_idx = 0; tag_idx < node.keys.Count; tag_idx++) { string key = Encoding.UTF8.GetString(block.stringtable.s[(int)node.keys[tag_idx]]); string value = Encoding.UTF8.GetString(block.stringtable.s[(int)node.vals[tag_idx]]); simpleNode.Tags.Add(new Tag() { Key = key, Value = value }); } } simpleNode.TimeStamp = Utilities.FromUnixTime((long)node.info.timestamp * (long)block.date_granularity); simpleNode.Visible = true; simpleNode.Version = (uint)node.info.version; simpleNode.UserId = node.info.uid; simpleNode.UserName = Encoding.UTF8.GetString(block.stringtable.s[node.info.user_sid]); simpleNode.Version = (ulong)node.info.version; simpleNode.Visible = true; return(simpleNode); }
private IEnumerable <Node> ProcessNodes(PrimitiveBlock block, PrimitiveGroup group, AttributeRegistry attributeRegistry) { if (group.Nodes == null) { yield break; } foreach (PbfNode node in group.Nodes) { double lat = 1E-09 * (block.LatOffset + (block.Granularity * node.Latitude)); double lon = 1E-09 * (block.LonOffset + (block.Granularity * node.Longitude)); List <Tag> tags = null; if (node.Keys != null) { tags = node.Keys.Select((t, i) => new Tag() { Typ = attributeRegistry.GetAttributeValueId(OsmAttribute.TagType, block.StringTable[t]), Value = block.StringTable[node.Values[i]] }).ToList(); } yield return(new Node(node.ID, lat, lon, tags ?? new List <Tag>())); } }
/// <summary> /// Reads next OSM entity from the stream. /// </summary> /// <returns>Parsed OSM entity from the stream or null if no other entity is available.</returns> public IEntityInfo Read() { if (_cache.Count > 0) { return(_cache.Dequeue()); } else { BlobHeader blobHeader = null; while (_cache.Count == 0 && (blobHeader = this.ReadBlobHeader()) != null) { PrimitiveBlock data = this.ReadBlob(blobHeader) as PrimitiveBlock; if (data != null) { foreach (PrimitiveGroup group in data.PrimitiveGroup) { this.ProcessPrimitiveGroup(data, group); } } } } if (_cache.Count > 0) { return(_cache.Dequeue()); } else { return(null); } }
private void FlushBlock() { if (this._currentEntities.Count == 0) { return; } PrimitiveBlock block = new PrimitiveBlock(); block.Encode(this._reverseStringTable, this._currentEntities); this._currentEntities.Clear(); this._reverseStringTable.Clear(); this._buffer.SetLength(0L); ((TypeModel)this._runtimeTypeModel).Serialize((Stream)this._buffer, (object)block); byte[] array = this._buffer.ToArray(); this._buffer.SetLength(0L); if (this._compress) { throw new NotSupportedException(); } ((TypeModel)this._runtimeTypeModel).Serialize((Stream)this._buffer, (object)new Blob() { raw = array }); ((TypeModel)this._runtimeTypeModel).SerializeWithLengthPrefix(this._stream, (object)new BlobHeader() { datasize = (int)this._buffer.Length, indexdata = (byte[])null, type = Encoder.OSMData }, this._blobHeaderType, (PrefixStyle)3, 0); this._buffer.Seek(0L, SeekOrigin.Begin); this._buffer.CopyTo(this._stream); }
/// <summary> /// Processes OSM entities in Primitive group. /// </summary> /// <param name="block">The PrimitiveBlock that contains specified PrimitiveGroup.</param> /// <param name="group">The PrimitiveGroup to process.</param> private void ProcessPrimitiveGroup(PrimitiveBlock block, PrimitiveGroup group) { this.ProcessNodes(block, group); this.ProcessDenseNodes(block, group); this.ProcessWays(block, group); this.ProcessRelations(block, group); }
//Generates all the dirt in the map //A dirt block will only be generated at a specific position iff // - Its y-coordinate is at or below the max dirt height for that x-coordinate // - There is not a cave located at its position private void GenerateDirt() { //Variables used to calculate the average height of the top level of dirt topLevelSum = 0.0; numTopLevels = 0; avgTopLevel = 0.0; //The max height of every column of blocks maxHeights = new int[BIOME_WIDTH]; for (int i = 0; i < BIOME_WIDTH; i++) { maxHeights[i] = -1; } //Variables used to calculate the average height of the top level of dirt topLevelSum = 0.0; numTopLevels = 0; avgTopLevel = 0.0; //Generates dirt for (int x = 0; x < BIOME_WIDTH; x++, xHeight += xHeightFreq, xCave += xCaveFreq) { //The top level that the dirt will go to double topLevel = PerlinNoise1D.getNoise(xHeight) * BIOME_HEIGHT * topLevelScale; if (topLevel > (double)BIOME_HEIGHT - 1) { topLevel = BIOME_HEIGHT - 1; } topLevelSum += topLevel; ++numTopLevels; avgTopLevel = topLevelSum / numTopLevels; yCave = 0.0; double thresh = 1.0; for (int y = (int)topLevel; y >= 0; y--, yCave += yCaveFreq) { double n = ((SimplexNoise2D.octaveNoise2D(5, 0.1, 0.2, xCave, yCave) + 1.0) / 2.0 * 5.0); //Builds a dirt block if it is below the max height generated by the height generator and if there isnt a cave there if ((int)n > 1 || y > topLevel * topLevelScale * 1.2) { if (maxHeights[x] == -1) { maxHeights[x] = y; } map[x][y].SetBT(BlockType.DIRT); } if (n >= thresh) { backMap[x][y] = new PrimitiveBlock(x, y, BlockType.DIRT); } else { if (rand.Next(15) == 0) { thresh = rand.NextDouble() + 1; } } } } }
/// <summary> /// Processes all nodes in non-dense format from the PrimitiveGroup and adds them to the output queue. /// </summary> /// <param name="block">The PrimitiveBlock that contains specified PrimitiveGroup.</param> /// <param name="group">The PrimitiveGroup with nodes to process.</param> private void ProcessNodes(PrimitiveBlock block, PrimitiveGroup group) { if (group.Nodes == null) { return; } foreach (PbfNode node in group.Nodes) { double lat = 1E-09 * (block.LatOffset + (block.Granularity * node.Latitude)); double lon = 1E-09 * (block.LonOffset + (block.Granularity * node.Longitude)); List <Tag> tags = new List <Tag>(); if (node.Keys != null) { for (int i = 0; i < node.Keys.Count; i++) { tags.Add(new Tag(block.StringTable[node.Keys[i]], block.StringTable[node.Values[i]])); } } EntityMetadata metadata = this.ProcessMetadata(node.Metadata, block); NodeInfo parsed = new NodeInfo((long)node.ID, lat, lon, new TagsCollection(tags), metadata); _cache.Enqueue(parsed); } }
/// <summary> /// Converts a PBF way into an OsmSharp-relation. /// </summary> /// <param name="block"></param> /// <param name="relation"></param> /// <returns></returns> internal OsmSharp.Osm.Relation ConvertRelation(PrimitiveBlock block, OsmSharp.Osm.PBF.Relation relation) { var simpleRelation = new OsmSharp.Osm.Relation(); simpleRelation.Id = relation.id; if (relation.types.Count > 0) { simpleRelation.Members = new List <OsmSharp.Osm.RelationMember>(); long member_id = 0; for (int member_idx = 0; member_idx < relation.types.Count; member_idx++) { member_id = member_id + relation.memids[member_idx]; string role = Encoding.UTF8.GetString( block.stringtable.s[relation.roles_sid[member_idx]]); var member = new OsmSharp.Osm.RelationMember(); member.MemberId = member_id; member.MemberRole = role; switch (relation.types[member_idx]) { case Relation.MemberType.NODE: member.MemberType = OsmSharp.Osm.OsmGeoType.Node; break; case Relation.MemberType.WAY: member.MemberType = OsmSharp.Osm.OsmGeoType.Way; break; case Relation.MemberType.RELATION: member.MemberType = OsmSharp.Osm.OsmGeoType.Relation; break; } simpleRelation.Members.Add(member); } } simpleRelation.Tags = new TagsCollection(relation.keys.Count); if (relation.keys.Count > 0) { for (int tag_idx = 0; tag_idx < relation.keys.Count; tag_idx++) { string key = Encoding.UTF8.GetString(block.stringtable.s[(int)relation.keys[tag_idx]]); string value = Encoding.UTF8.GetString(block.stringtable.s[(int)relation.vals[tag_idx]]); simpleRelation.Tags.Add(new Tag(key, value)); } } if (relation.info != null) { // read metadata if any. simpleRelation.ChangeSetId = relation.info.changeset; simpleRelation.TimeStamp = Utilities.FromUnixTime((long)relation.info.timestamp * (long)block.date_granularity); simpleRelation.UserId = relation.info.uid; simpleRelation.UserName = Encoding.UTF8.GetString(block.stringtable.s[relation.info.user_sid]); simpleRelation.Version = (ulong)relation.info.version; } simpleRelation.Visible = true; return(simpleRelation); }
/// <summary> /// Decodes the given block. /// </summary> public static bool Decode(this PrimitiveBlock block, IPBFOsmPrimitiveConsumer primitivesConsumer, bool ignoreNodes, bool ignoreWays, bool ignoreRelations) { bool hasNodes, hasWays, hasRelations; return(block.Decode(primitivesConsumer, ignoreNodes, ignoreWays, ignoreRelations, out hasNodes, out hasWays, out hasRelations)); }
private void MakeWatchTower(int sx, int sy) { //Builds the horizontal platform that an enemy commander will stand on map[sx][sy] = new PrimitiveBlock(sx, sy, BlockType.PLATFORM, Platform.WATCH_TOWER_LEFT); map[sx + 1][sy] = new PrimitiveBlock(sx + 1, sy, BlockType.PLATFORM, Platform.WATCH_TOWER_MIDDLES[rand.Next(Platform.WATCH_TOWER_MIDDLES.Length)]); map[sx + 2][sy] = new PrimitiveBlock(sx + 2, sy, BlockType.PLATFORM, Platform.WATCH_TOWER_MIDDLES[rand.Next(Platform.WATCH_TOWER_MIDDLES.Length)]); map[sx + 3][sy] = new PrimitiveBlock(sx + 3, sy, BlockType.PLATFORM, Platform.WATCH_TOWER_MIDDLES[rand.Next(Platform.WATCH_TOWER_MIDDLES.Length)]); map[sx + 4][sy] = new PrimitiveBlock(sx + 4, sy, BlockType.PLATFORM, Platform.WATCH_TOWER_LEFT, true, false); //CODE FOR PILLARS //Builds the left pillar that supports the platform for (int y = sy - 1; y >= 0; y--) { if (map[sx][y].bt == BlockType.AIR) { backMap[sx][y].SetBT(BlockType.BACKGROUND_BLOCK); backMap[sx][y].spr = BackgroundBlock.WATCH_TOWER_PILLAR; } else { break; } } //Creates the right pillar that supports the platform for (int y = sy - 1; y >= 0; y--) { if (map[sx + 4][y].bt == BlockType.AIR) { backMap[sx + 4][y].SetBT(BlockType.BACKGROUND_BLOCK); backMap[sx + 4][y].spr = BackgroundBlock.WATCH_TOWER_PILLAR; } else { break; } } //Creates the left pillar that supports the roof backMap[sx][sy + 1] = new PrimitiveBlock(sx, sy + 1, BlockType.BACKGROUND_BLOCK, BackgroundBlock.WATCH_TOWER_PILLAR); backMap[sx][sy + 2] = new PrimitiveBlock(sx, sy + 2, BlockType.BACKGROUND_BLOCK, BackgroundBlock.WATCH_TOWER_PILLAR); //Creates the right pillar that supports the roof backMap[sx + 4][sy + 1] = new PrimitiveBlock(sx + 4, sy + 1, BlockType.BACKGROUND_BLOCK, BackgroundBlock.WATCH_TOWER_PILLAR); backMap[sx + 4][sy + 2] = new PrimitiveBlock(sx + 4, sy + 2, BlockType.BACKGROUND_BLOCK, BackgroundBlock.WATCH_TOWER_PILLAR); for (int i = 0; i < 5; i++) { //Adds the roof map[sx + i][sy + 4] = new PrimitiveBlock(sx + i, sy + 4, BlockType.ROOF, Roof.WATCH_TOWER_ROOF_TOP[i]); map[sx + i][sy + 3] = new PrimitiveBlock(sx + i, sy + 3, BlockType.ROOF, Roof.WATCH_TOWER_ROOF_BOTTOM[i]); //Makes space in between the roof and the platform for the enemy commander to walk map[sx + i][sy + 2].SetBT(BlockType.AIR); map[sx + i][sy + 1].SetBT(BlockType.AIR); } }
/// <summary> /// Flushes the current block of data. /// </summary> private void FlushBlock() { if (_currentEntities.Count == 0) { return; } // encode into block. var block = new PrimitiveBlock(); Encoder.Encode(block, _reverseStringTable, _currentEntities, _compress); _currentEntities.Clear(); _reverseStringTable.Clear(); // serialize. _buffer.SetLength(0); _runtimeTypeModel.Serialize(_buffer, block); var blockBytes = _buffer.ToArray(); _buffer.SetLength(0); // create blob. var blob = new Blob(); blob.raw_size = blockBytes.Length; if (_compress) { using (var target = new MemoryStream()) { using (var source = new MemoryStream(blockBytes)) using (var deflate = new DeflaterOutputStream(target)) { source.CopyTo(deflate); } blob.zlib_data = target.ToArray(); } } else { blob.raw = blockBytes; } _runtimeTypeModel.Serialize(_buffer, blob); // create blobheader. var blobHeader = new BlobHeader(); blobHeader.datasize = (int)_buffer.Length; blobHeader.indexdata = null; blobHeader.type = Encoder.OSMData; _runtimeTypeModel.SerializeWithLengthPrefix(_stream, blobHeader, _blobHeaderType, ProtoBuf.PrefixStyle.Fixed32BigEndian, 0); // serialize to stream. _buffer.Seek(0, SeekOrigin.Begin); _buffer.CopyTo(_stream); }
public void TestDecodeRelation() { var block = new PrimitiveBlock(); block.date_granularity = 1000; block.granularity = 100; block.lat_offset = 0; block.lon_offset = 0; block.stringtable = new StringTable(); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes(string.Empty)); // always encode empty string as '0'. block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("highway")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("residential")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("Ben")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("fake role")); var pbfRelation = new OsmSharp.IO.PBF.Relation() { id = 1, info = new Info() { changeset = 10, timestamp = 10, uid = 100, user_sid = 3, version = 2 } }; pbfRelation.keys.Add(1); pbfRelation.vals.Add(2); pbfRelation.memids.Add(10); pbfRelation.memids.Add(1); // delta-encoding. pbfRelation.roles_sid.Add(4); pbfRelation.roles_sid.Add(4); pbfRelation.types.Add(OsmSharp.IO.PBF.Relation.MemberType.NODE); pbfRelation.types.Add(OsmSharp.IO.PBF.Relation.MemberType.WAY); var relation = Encoder.DecodeRelation(block, pbfRelation); Assert.IsNotNull(relation); Assert.AreEqual(1, relation.Id); Assert.AreEqual(10, relation.ChangeSetId); Assert.AreEqual(PBFExtensions.FromUnixTime(10000), relation.TimeStamp); Assert.AreEqual(OsmSharp.OsmGeoType.Relation, relation.Type); Assert.AreEqual(100, relation.UserId); Assert.AreEqual("Ben", relation.UserName); Assert.AreEqual(2, relation.Version); Assert.AreEqual(2, relation.Members.Length); Assert.AreEqual(10, relation.Members[0].Id); Assert.AreEqual(OsmSharp.OsmGeoType.Node, relation.Members[0].Type); Assert.AreEqual("fake role", relation.Members[0].Role); Assert.AreEqual(11, relation.Members[1].Id); Assert.AreEqual(OsmSharp.OsmGeoType.Way, relation.Members[1].Type); Assert.AreEqual("fake role", relation.Members[1].Role); }
public void TestEncodeStrings() { var block = new PrimitiveBlock(); var reverseStringTable = new Dictionary <string, int>(); Assert.AreEqual(1, Encoder.EncodeString(block, reverseStringTable, "Ben")); Assert.AreEqual(2, Encoder.EncodeString(block, reverseStringTable, "highway")); Assert.AreEqual(3, Encoder.EncodeString(block, reverseStringTable, "residential")); Assert.AreEqual(1, Encoder.EncodeString(block, reverseStringTable, "Ben")); Assert.AreEqual(2, Encoder.EncodeString(block, reverseStringTable, "highway")); Assert.AreEqual(4, Encoder.EncodeString(block, reverseStringTable, "Some other string")); Assert.AreEqual(5, Encoder.EncodeString(block, reverseStringTable, "Ban")); }
private IEnumerable <Node> ProcessDenseNodes(PrimitiveBlock block, PrimitiveGroup group, AttributeRegistry attributeRegistry) { if (group.DenseNodes == null) { yield break; } long idStore = 0; long latStore = 0; long lonStore = 0; int keyValueIndex = 0; for (int i = 0; i < group.DenseNodes.Id.Count; i++) { idStore += group.DenseNodes.Id[i]; lonStore += group.DenseNodes.Longitude[i]; latStore += group.DenseNodes.Latitude[i]; double lat = 1E-09 * (block.LatOffset + (block.Granularity * latStore)); double lon = 1E-09 * (block.LonOffset + (block.Granularity * lonStore)); var tags = new List <Tag>(); if (group.DenseNodes.KeysVals.Count > 0) { while (group.DenseNodes.KeysVals[keyValueIndex] != 0) { string key = block.StringTable[group.DenseNodes.KeysVals[keyValueIndex++]]; string value = block.StringTable[group.DenseNodes.KeysVals[keyValueIndex++]]; var tagType = attributeRegistry.GetAttributeValueId(OsmAttribute.TagType, key); var tag = new Tag() { Typ = tagType, Value = value }; tags.Add(tag); } keyValueIndex++; } var node = new Node() { NodeId = idStore, Latitude = lat, Longitude = lon, Tags = tags }; yield return(node); } }
/// <summary> /// Moves the PBF reader to the next primitive or returns one of the cached ones. /// </summary> /// <returns></returns> private KeyValuePair <PrimitiveBlock, object> MoveToNextPrimitive() { KeyValuePair <PrimitiveBlock, object> next = this.DeQueuePrimitive(); if (next.Value == null) { PrimitiveBlock block = _reader.MoveNext(); if (block != null) { _decompressor.ProcessPrimitiveBlock(block); next = this.DeQueuePrimitive(); } } return(next); }
private KeyValuePair <PrimitiveBlock, object> MoveToNextPrimitive(bool ignoreNodes, bool ignoreWays, bool ignoreRelations) { KeyValuePair <PrimitiveBlock, object> keyValuePair = this.DeQueuePrimitive(); if (keyValuePair.Value == null) { PrimitiveBlock block = this._reader.MoveNext(); while (block != null && !block.Decode((IPBFOsmPrimitiveConsumer)this, ignoreNodes, ignoreWays, ignoreRelations)) { block = this._reader.MoveNext(); } keyValuePair = this.DeQueuePrimitive(); } return(keyValuePair); }
/// <summary> /// Processes all relations from the PrimitiveGroup and adds them to the output queue. /// </summary> /// <param name="block">The PrimitiveBlock that contains specified PrimitiveGroup.</param> /// <param name="group">The PrimitiveGroup with nodes to process.</param> private void ProcessRelations(PrimitiveBlock block, PrimitiveGroup group) { if (group.Relations == null) { return; } foreach (var relation in group.Relations) { long memberRefStore = 0; List <RelationMemberInfo> members = new List <RelationMemberInfo>(); for (int i = 0; i < relation.MemberIds.Count; i++) { memberRefStore += (long)relation.MemberIds[i]; string role = block.StringTable[relation.RolesIndexes[i]]; EntityType memberType = 0; switch (relation.Types[i]) { case PbfRelationMemberType.Node: memberType = EntityType.Node; break; case PbfRelationMemberType.Way: memberType = EntityType.Way; break; case PbfRelationMemberType.Relation: memberType = EntityType.Relation; break; } members.Add(new RelationMemberInfo() { MemberType = memberType, Reference = memberRefStore, Role = role }); } List <Tag> tags = new List <Tag>(); if (relation.Keys != null) { for (int i = 0; i < relation.Keys.Count; i++) { tags.Add(new Tag(block.StringTable[relation.Keys[i]], block.StringTable[relation.Values[i]])); } } EntityMetadata metadata = this.ProcessMetadata(relation.Metadata, block); RelationInfo parsed = new RelationInfo((long)relation.ID, new TagsCollection(tags), members, metadata); _cache.Enqueue(parsed); } }
public void TestDecodeBlockWithRelation() { var block = new PrimitiveBlock(); block.date_granularity = 1000; block.granularity = 100; block.lat_offset = 0; block.lon_offset = 0; block.stringtable = new StringTable(); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("highway")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("residential")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("Ben")); var pbfRelation = new OsmSharp.IO.PBF.Relation() { id = 1, info = new Info() { changeset = 10, timestamp = 10, uid = 100, user_sid = 2, version = 2 } }; pbfRelation.keys.Add(0); pbfRelation.vals.Add(1); pbfRelation.memids.Add(10); pbfRelation.memids.Add(1); // delta-encoding. pbfRelation.roles_sid.Add(3); pbfRelation.roles_sid.Add(3); pbfRelation.types.Add(OsmSharp.IO.PBF.Relation.MemberType.NODE); pbfRelation.types.Add(OsmSharp.IO.PBF.Relation.MemberType.WAY); var primitiveGroup = new PrimitiveGroup(); primitiveGroup.relations.Add(pbfRelation); block.primitivegroup.Add(primitiveGroup); var primitivesConsumer = new PrimitivesConsumerMock(); block.Decode(primitivesConsumer, false, false, false); Assert.AreEqual(0, primitivesConsumer.Nodes.Count); Assert.AreEqual(0, primitivesConsumer.Ways.Count); Assert.AreEqual(1, primitivesConsumer.Relations.Count); }
public void TestEncodeWay() { var block = new PrimitiveBlock(); block.date_granularity = 1000; block.granularity = 100; block.lat_offset = 0; block.lon_offset = 0; block.stringtable = new StringTable(); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes(string.Empty)); // always encode empty string as '0'. block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("highway")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("residential")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("Ben")); var way = new OsmSharp.Osm.Way(); way.Id = 1; way.ChangeSetId = 1; way.Tags = new OsmSharp.Collections.Tags.TagsCollection(); way.Tags.Add("name", "Ben"); way.TimeStamp = DateTime.Now; way.UserId = 1; way.UserName = "******"; way.Version = 1; way.Visible = true; way.Nodes = new List <long>(); way.Nodes.Add(1); way.Nodes.Add(2); var pbfWay = Encoder.EncodeWay(block, new Dictionary <string, int>(), way); Assert.IsNotNull(pbfWay); Assert.AreEqual(1, pbfWay.id); Assert.AreEqual(2, pbfWay.refs.Count); Assert.AreEqual(1, pbfWay.refs[0]); Assert.AreEqual(1, pbfWay.refs[1]); Assert.AreEqual(1, pbfWay.info.changeset); Assert.AreEqual(Encoder.EncodeTimestamp(way.TimeStamp.Value, block.date_granularity), pbfWay.info.timestamp); Assert.AreEqual(1, pbfWay.info.uid); Assert.AreEqual("Ben", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfWay.info.user_sid])); Assert.AreEqual(1, pbfWay.info.version); Assert.AreEqual(1, pbfWay.keys.Count); Assert.AreEqual("name", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfWay.keys[0]])); Assert.AreEqual(1, pbfWay.vals.Count); Assert.AreEqual("Ben", System.Text.Encoding.UTF8.GetString(block.stringtable.s[(int)pbfWay.vals[0]])); }
public void TestDecodeWay() { var block = new PrimitiveBlock(); block.date_granularity = 1000; block.granularity = 100; block.lat_offset = 0; block.lon_offset = 0; block.stringtable = new StringTable(); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes(string.Empty)); // always encode empty string as '0'. block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("highway")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("residential")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("Ben")); var pbfWay = new OsmSharp.IO.PBF.Way() { id = 1, info = new Info() { changeset = 10, timestamp = 10, uid = 100, user_sid = 3, version = 2 } }; pbfWay.keys.Add(1); pbfWay.vals.Add(2); pbfWay.refs.Add(0); pbfWay.refs.Add(1); var way = Encoder.DecodeWay(block, pbfWay); Assert.IsNotNull(way); Assert.AreEqual(1, way.Id); Assert.AreEqual(10, way.ChangeSetId); Assert.AreEqual(PBFExtensions.FromUnixTime(10000), way.TimeStamp); Assert.AreEqual(OsmSharp.OsmGeoType.Way, way.Type); Assert.AreEqual(100, way.UserId); Assert.AreEqual("Ben", way.UserName); Assert.AreEqual(2, way.Version); Assert.AreEqual(2, way.Nodes.Length); Assert.AreEqual(0, way.Nodes[0]); Assert.AreEqual(1, way.Nodes[1]); }
public void TestDecodeNode() { var block = new PrimitiveBlock(); block.date_granularity = 1000; block.granularity = 100; block.lat_offset = 0; block.lon_offset = 0; block.stringtable = new StringTable(); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes(string.Empty)); // always encode empty string as '0'. block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("highway")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("residential")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("Ben")); var pbfNode = new OsmSharp.IO.PBF.Node() { id = 1, info = new Info() { changeset = 10, timestamp = 10, uid = 100, user_sid = 3, version = 2 }, lat = Encoder.EncodeLatLon(10.9f, block.lat_offset, block.granularity), lon = Encoder.EncodeLatLon(11.0f, block.lat_offset, block.granularity) }; pbfNode.keys.Add(1); pbfNode.vals.Add(2); var node = Encoder.DecodeNode(block, pbfNode); Assert.IsNotNull(node); Assert.AreEqual(1, node.Id); Assert.AreEqual(10, node.ChangeSetId); Assert.AreEqual(10.9f, node.Latitude); Assert.AreEqual(11.0f, node.Longitude); Assert.AreEqual(PBFExtensions.FromUnixTime(10000), node.TimeStamp); Assert.AreEqual(OsmSharp.OsmGeoType.Node, node.Type); Assert.AreEqual(100, node.UserId); Assert.AreEqual("Ben", node.UserName); Assert.AreEqual(2, node.Version); }
/// <summary> /// Clears internal buffer for specified EntityType and writes all buffered data the underlaying storage. /// </summary> /// <param name="entityType">The type of entity to process</param> private void Flush(EntityType entityType) { PrimitiveBlock primitiveBlock = this.BuildPrimitiveBlock(entityType); if (primitiveBlock == null) { return; } MemoryStream primitiveBlockStream = new MemoryStream(); Serializer.Serialize <PrimitiveBlock>(primitiveBlockStream, primitiveBlock); byte[] buffer = new byte[primitiveBlockStream.Length]; Array.Copy(primitiveBlockStream.GetBuffer(), buffer, primitiveBlockStream.Length); this.WriteBlob("OSMData", buffer); }
/// <summary> /// Moves the PBF reader to the next primitive or returns one of the cached ones. /// </summary> /// <returns></returns> private KeyValuePair <PrimitiveBlock, object> MoveToNextPrimitive(bool ignoreNodes, bool ignoreWays, bool ignoreRelations) { KeyValuePair <PrimitiveBlock, object> next = this.DeQueuePrimitive(); if (next.Value == null) { PrimitiveBlock block = _reader.MoveNext(); if (block != null) { while (!_decompressor.ProcessPrimitiveBlock(block, ignoreNodes, ignoreWays, ignoreRelations)) { block = _reader.MoveNext(); } next = this.DeQueuePrimitive(); } } return(next); }
/// <summary> /// Flushes the current block of data. /// </summary> private void FlushBlock() { if (_currentEntities.Count == 0) { return; } // encode into block. var block = new PrimitiveBlock(); Encoder.Encode(block, _reverseStringTable, _currentEntities); _currentEntities.Clear(); _reverseStringTable.Clear(); // serialize. _buffer.SetLength(0); _runtimeTypeModel.Serialize(_buffer, block); var blockBytes = _buffer.ToArray(); _buffer.SetLength(0); if (_compress) { // compress buffer. throw new NotSupportedException(); } // create blob. var blob = new Blob(); blob.raw = blockBytes; _runtimeTypeModel.Serialize(_buffer, blob); // create blobheader. var blobHeader = new BlobHeader(); blobHeader.datasize = (int)_buffer.Length; blobHeader.indexdata = null; blobHeader.type = Encoder.OSMData; _runtimeTypeModel.SerializeWithLengthPrefix(_stream, blobHeader, _blobHeaderType, ProtoBuf.PrefixStyle.Fixed32BigEndian, 0); // serialize to stream. _buffer.Seek(0, SeekOrigin.Begin); _buffer.CopyTo(_stream); }
public void TestDecodeBlockWithWay() { var block = new PrimitiveBlock(); block.date_granularity = 1000; block.granularity = 100; block.lat_offset = 0; block.lon_offset = 0; block.stringtable = new StringTable(); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("highway")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("residential")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("Ben")); var pbfWay = new OsmSharp.IO.PBF.Way() { id = 1, info = new Info() { changeset = 10, timestamp = 10, uid = 100, user_sid = 2, version = 2 } }; pbfWay.keys.Add(0); pbfWay.vals.Add(1); pbfWay.refs.Add(0); pbfWay.refs.Add(1); var primitiveGroup = new PrimitiveGroup(); primitiveGroup.ways.Add(pbfWay); block.primitivegroup.Add(primitiveGroup); var primitivesConsumer = new PrimitivesConsumerMock(); block.Decode(primitivesConsumer, false, false, false); Assert.AreEqual(0, primitivesConsumer.Nodes.Count); Assert.AreEqual(1, primitivesConsumer.Ways.Count); Assert.AreEqual(0, primitivesConsumer.Relations.Count); }
public void TestDecodeBlockWithNode() { var block = new PrimitiveBlock(); block.date_granularity = 1000; block.granularity = 100; block.lat_offset = 0; block.lon_offset = 0; block.stringtable = new StringTable(); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("highway")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("residential")); block.stringtable.s.Add(System.Text.Encoding.UTF8.GetBytes("Ben")); var node = new Node() { id = 1, info = new Info() { changeset = 10, timestamp = 10, uid = 100, user_sid = 2, version = 2 }, lat = Encoder.EncodeLatLon(10.9, block.lat_offset, block.granularity), lon = Encoder.EncodeLatLon(11.0, block.lat_offset, block.granularity) }; node.keys.Add(0); node.vals.Add(1); var primitiveGroup = new PrimitiveGroup(); primitiveGroup.nodes.Add(node); block.primitivegroup.Add(primitiveGroup); var primitivesConsumer = new PrimitivesConsumerMock(); block.Decode(primitivesConsumer, false, false, false); Assert.AreEqual(1, primitivesConsumer.Nodes.Count); Assert.AreEqual(0, primitivesConsumer.Ways.Count); Assert.AreEqual(0, primitivesConsumer.Relations.Count); }
/// <summary> /// Reads and parses a primitive block (PrimitiveBlock) /// </summary> /// <param name="primitiveBlock">Primitive block to parse</param> private void ReadPrimitiveBlock(PrimitiveBlock primitiveBlock) { double latOffset = .000000001 * primitiveBlock.lat_offset; double lonOffset = .000000001 * primitiveBlock.lon_offset; double granularity = .000000001 * primitiveBlock.granularity; foreach (PrimitiveGroup primitiveGroup in primitiveBlock.primitivegroup) { if (primitiveGroup.dense != null) { ReadDenseNodes(primitiveBlock, primitiveGroup.dense, latOffset, lonOffset, granularity); } if (primitiveGroup.ways != null) { ReadWays(primitiveBlock, primitiveGroup.ways); } if (primitiveGroup.relations != null) { //ReadRelations(primitiveGroup.relations); } } }
/// <summary> /// Processes entity metadata. /// </summary> /// <param name="serializedMetadata">Serilized metadata.</param> /// <param name="block">PrimitiveBlock that contains metadata being processed.</param> /// <returns>Parsed metadata.</returns> private EntityMetadata ProcessMetadata(PbfMetadata serializedMetadata, PrimitiveBlock block) { EntityMetadata metadata = null; if (this.Settings.ReadMetadata && serializedMetadata != null) { //PBF has no field for 'visible' property, true is default value for OSM entity read from PBF file metadata = new EntityMetadata() { Visible = true }; if (serializedMetadata.Changeset.HasValue) { metadata.Changeset = (int)serializedMetadata.Changeset.Value; } if (serializedMetadata.Timestamp.HasValue) { metadata.Timestamp = _unixEpoch.AddMilliseconds(serializedMetadata.Timestamp.Value * block.DateGranularity); } if (serializedMetadata.UserID.HasValue) { metadata.Uid = serializedMetadata.UserID.Value; } if (serializedMetadata.UserNameIndex.HasValue) { metadata.User = block.StringTable[serializedMetadata.UserNameIndex.Value]; } if (serializedMetadata.Version.HasValue) { metadata.Version = serializedMetadata.Version.Value; } } return(metadata); }
/// <summary> /// Processes a relation. /// </summary> /// <param name="block"></param> /// <param name="relation"></param> void IPBFOsmPrimitiveConsumer.ProcessRelation(PrimitiveBlock block, Relation relation) { this.QueuePrimitive(block, relation); }
/// <summary> /// Processes a node. /// </summary> /// <param name="block"></param> /// <param name="node"></param> void IPBFOsmPrimitiveConsumer.ProcessNode(PrimitiveBlock block, Node node) { this.QueuePrimitive(block, node); }
/// <summary> /// Converts a PBF way into an OsmSharp-way. /// </summary> /// <param name="block"></param> /// <param name="way"></param> /// <returns></returns> internal OsmSharp.Osm.Way ConvertWay(PrimitiveBlock block, OsmSharp.Osm.PBF.Way way) { var simpleWay = new OsmSharp.Osm.Way(); simpleWay.Id = way.id; simpleWay.Nodes = new List<long>(way.refs.Count); long node_id = 0; for (int node_idx = 0; node_idx < way.refs.Count; node_idx++) { node_id = node_id + way.refs[node_idx]; simpleWay.Nodes.Add(node_id); } simpleWay.Tags = new TagsCollection(way.keys.Count); if (way.keys.Count > 0) { for (int tag_idx = 0; tag_idx < way.keys.Count; tag_idx++) { string key = Encoding.UTF8.GetString(block.stringtable.s[(int)way.keys[tag_idx]]); string value = Encoding.UTF8.GetString(block.stringtable.s[(int)way.vals[tag_idx]]); simpleWay.Tags.Add(new Tag(key, value)); } } if (way.info != null) { // add the metadata if any. simpleWay.ChangeSetId = way.info.changeset; simpleWay.TimeStamp = Utilities.FromUnixTime((long)way.info.timestamp * (long)block.date_granularity); simpleWay.UserId = way.info.uid; simpleWay.UserName = Encoding.UTF8.GetString(block.stringtable.s[way.info.user_sid]); simpleWay.Version = (ulong)way.info.version; } simpleWay.Visible = true; return simpleWay; }
/// <summary> /// Converts a PBF way into an OsmSharp-relation. /// </summary> /// <param name="block"></param> /// <param name="relation"></param> /// <returns></returns> internal OsmSharp.Osm.Relation ConvertRelation(PrimitiveBlock block, OsmSharp.Osm.PBF.Relation relation) { var simpleRelation = new OsmSharp.Osm.Relation(); simpleRelation.Id = relation.id; if (relation.types.Count > 0) { simpleRelation.Members = new List<OsmSharp.Osm.RelationMember>(); long member_id = 0; for (int member_idx = 0; member_idx < relation.types.Count; member_idx++) { member_id = member_id + relation.memids[member_idx]; string role = Encoding.UTF8.GetString( block.stringtable.s[relation.roles_sid[member_idx]]); var member = new OsmSharp.Osm.RelationMember(); member.MemberId = member_id; member.MemberRole = role; switch (relation.types[member_idx]) { case Relation.MemberType.NODE: member.MemberType = OsmSharp.Osm.OsmGeoType.Node; break; case Relation.MemberType.WAY: member.MemberType = OsmSharp.Osm.OsmGeoType.Way; break; case Relation.MemberType.RELATION: member.MemberType = OsmSharp.Osm.OsmGeoType.Relation; break; } simpleRelation.Members.Add(member); } } simpleRelation.Tags = new TagsCollection(relation.keys.Count); if (relation.keys.Count > 0) { for (int tag_idx = 0; tag_idx < relation.keys.Count; tag_idx++) { string key = Encoding.UTF8.GetString(block.stringtable.s[(int)relation.keys[tag_idx]]); string value = Encoding.UTF8.GetString(block.stringtable.s[(int)relation.vals[tag_idx]]); simpleRelation.Tags.Add(new Tag(key, value)); } } if (relation.info != null) { // read metadata if any. simpleRelation.ChangeSetId = relation.info.changeset; simpleRelation.TimeStamp = Utilities.FromUnixTime((long)relation.info.timestamp * (long)block.date_granularity); simpleRelation.UserId = relation.info.uid; simpleRelation.UserName = Encoding.UTF8.GetString(block.stringtable.s[relation.info.user_sid]); simpleRelation.Version = (ulong)relation.info.version; } simpleRelation.Visible = true; return simpleRelation; }
/// <summary> /// Converts the PBF node into an OsmSharp-node. /// </summary> /// <param name="block"></param> /// <param name="node"></param> /// <returns></returns> internal OsmSharp.Osm.Node ConvertNode(PrimitiveBlock block, OsmSharp.Osm.PBF.Node node) { var simpleNode = new OsmSharp.Osm.Node(); simpleNode.ChangeSetId = node.info.changeset; simpleNode.Id = node.id; simpleNode.Latitude = .000000001 * ((double)block.lat_offset + ((double)block.granularity * (double)node.lat)); simpleNode.Longitude = .000000001 * ((double)block.lon_offset + ((double)block.granularity * (double)node.lon)); simpleNode.Tags = new TagsCollection(node.keys.Count); if (node.keys.Count > 0) { for (int tag_idx = 0; tag_idx < node.keys.Count; tag_idx++) { string key = Encoding.UTF8.GetString(block.stringtable.s[(int)node.keys[tag_idx]]); string value = Encoding.UTF8.GetString(block.stringtable.s[(int)node.vals[tag_idx]]); simpleNode.Tags.Add(new Tag() { Key = key, Value = value }); } } simpleNode.TimeStamp = Utilities.FromUnixTime((long)node.info.timestamp * (long)block.date_granularity); simpleNode.Visible = true; simpleNode.Version = (uint)node.info.version; simpleNode.UserId = node.info.uid; simpleNode.UserName = Encoding.UTF8.GetString(block.stringtable.s[node.info.user_sid]); simpleNode.Version = (ulong)node.info.version; simpleNode.Visible = true; return simpleNode; }
/// <summary> /// Consumes a primitive block. /// </summary> /// <param name="block"></param> public void ProcessPrimitiveBlock(PrimitiveBlock block) { if (block.primitivegroup != null) { foreach (PrimitiveGroup primitivegroup in block.primitivegroup) { if (primitivegroup.dense != null) { int key_vals_idx = 0; long current_id = 0; long current_lat = 0; long current_lon = 0; long current_changeset = 0; long current_timestamp = 0; int current_uid = 0; int current_user_sid = 0; int current_version = 0; for (int idx = 0; idx < primitivegroup.dense.id.Count; idx++) { // do the delta decoding stuff. current_id = current_id + primitivegroup.dense.id[idx]; current_lat = current_lat + primitivegroup.dense.lat[idx]; current_lon = current_lon + primitivegroup.dense.lon[idx]; if (primitivegroup.dense.denseinfo != null) { // add all the metadata. current_changeset = current_changeset + primitivegroup.dense.denseinfo.changeset[idx]; current_timestamp = current_timestamp + primitivegroup.dense.denseinfo.timestamp[idx]; current_uid = current_uid + primitivegroup.dense.denseinfo.uid[idx]; current_user_sid = current_user_sid + primitivegroup.dense.denseinfo.user_sid[idx]; current_version = current_version + primitivegroup.dense.denseinfo.version[idx]; } Node node = new Node(); node.id = current_id; node.info = new Info(); node.info.changeset = current_changeset; node.info.timestamp = (int)current_timestamp; node.info.uid = current_uid; node.info.user_sid = current_user_sid; node.info.version = current_version; node.lat = current_lat; node.lon = current_lon; // get the keys/vals. List<int> keys_vals = primitivegroup.dense.keys_vals; List<uint> keys = new List<uint>(); List<uint> vals = new List<uint>(); while (keys_vals.Count > key_vals_idx && keys_vals[key_vals_idx] != 0) { node.keys.Add((uint)keys_vals[key_vals_idx]); key_vals_idx++; node.vals.Add((uint)keys_vals[key_vals_idx]); key_vals_idx++; } key_vals_idx++; _primitives_consumer.ProcessNode(block, node); } } else { if (primitivegroup.nodes != null) { foreach (Node node in primitivegroup.nodes) { _primitives_consumer.ProcessNode(block, node); } } if (primitivegroup.ways != null) { foreach (Way way in primitivegroup.ways) { _primitives_consumer.ProcessWay(block, way); } } if (primitivegroup.relations != null) { foreach (Relation relation in primitivegroup.relations) { _primitives_consumer.ProcessRelation(block, relation); } } } } } }
/// <summary> /// Processes a way. /// </summary> /// <param name="block"></param> /// <param name="way"></param> void IPBFOsmPrimitiveConsumer.ProcessWay(PrimitiveBlock block, Way way) { this.QueuePrimitive(block, way); }
/// <summary> /// Queues the primitives. /// </summary> /// <param name="block"></param> /// <param name="primitive"></param> private void QueuePrimitive(PrimitiveBlock block, object primitive) { _cachedPrimitives.Enqueue(new KeyValuePair<PrimitiveBlock, object>(block, primitive)); }
private void logWay(PrimitiveBlock pb, Way way) { Console.WriteLine("\n\n Way " + way.Id); //Console.WriteLine("\n refs:"); //for(int i = 0; i < way.RefsCount; i++) // Console.WriteLine(way.GetRefs(i)); Console.WriteLine("\n keyvals:"); for(int i = 0; i < way.KeysCount; i++) { Console.WriteLine("{0}={1}", pb.Stringtable.GetS( (int)way.GetKeys(i)).ToStringUtf8(), pb.Stringtable.GetS( (int)way.GetVals(i)).ToStringUtf8()); } }