public void ProcessRelation(PrimitiveBlock block, Relation relation)
 {
     this.Relations.Add(relation);
 }
        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 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(Relation.MemberType.NODE);
            pbfRelation.types.Add(Relation.MemberType.WAY);

            var relation = Encoder.DecodeRelation(block, pbfRelation);
            Assert.IsNotNull(relation);
            Assert.AreEqual(1, relation.Id);
            Assert.AreEqual(10, relation.ChangeSetId);
            Assert.AreEqual(Utilities.FromUnixTime(10000), relation.TimeStamp);
            Assert.AreEqual(OsmSharp.Osm.OsmGeoType.Relation, relation.Type);
            Assert.AreEqual(100, relation.UserId);
            Assert.AreEqual("Ben", relation.UserName);
            Assert.AreEqual(2, relation.Version);
            Assert.AreEqual(2, relation.Members.Count);
            Assert.AreEqual(10, relation.Members[0].MemberId);
            Assert.AreEqual(OsmSharp.Osm.OsmGeoType.Node, relation.Members[0].MemberType);
            Assert.AreEqual("fake role", relation.Members[0].MemberRole);
            Assert.AreEqual(11, relation.Members[1].MemberId);
            Assert.AreEqual(OsmSharp.Osm.OsmGeoType.Way, relation.Members[1].MemberType);
            Assert.AreEqual("fake role", relation.Members[1].MemberRole);
        }
        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 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(Relation.MemberType.NODE);
            pbfRelation.types.Add(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);
        }
Beispiel #4
0
        public static Relation EncodeRelation(PrimitiveBlock block, Dictionary <string, int> reverseStringTable, OsmSharp.Osm.Relation relation)
        {
            Relation relation1 = new Relation();

            relation1.id   = relation.Id.Value;
            relation1.info = new Info();
            long?nullable;

            if (relation.ChangeSetId.HasValue)
            {
                Info info = relation1.info;
                nullable = relation.ChangeSetId;
                long num = nullable.Value;
                info.changeset = num;
            }
            if (relation.TimeStamp.HasValue)
            {
                relation1.info.timestamp = Encoder.EncodeTimestamp(relation.TimeStamp.Value, (long)block.date_granularity);
            }
            nullable = relation.UserId;
            if (nullable.HasValue)
            {
                Info info = relation1.info;
                nullable = relation.UserId;
                int num = (int)nullable.Value;
                info.uid = num;
            }
            relation1.info.user_sid = Encoder.EncodeString(block, reverseStringTable, relation.UserName);
            relation1.info.version  = 0;
            if (relation.Version.HasValue)
            {
                relation1.info.version = (int)relation.Version.Value;
            }
            if (relation.Tags != null)
            {
                foreach (Tag tag in relation.Tags)
                {
                    relation1.keys.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Key));
                    relation1.vals.Add((uint)Encoder.EncodeString(block, reverseStringTable, tag.Value));
                }
            }
            if (relation.Members != null && relation.Members.Count > 0)
            {
                List <long> memids1 = relation1.memids;
                nullable = relation.Members[0].MemberId;
                long num1 = nullable.Value;
                memids1.Add(num1);
                relation1.roles_sid.Add(Encoder.EncodeString(block, reverseStringTable, relation.Members[0].MemberRole));
                OsmGeoType?memberType = relation.Members[0].MemberType;
                switch (memberType.Value)
                {
                case OsmGeoType.Node:
                    relation1.types.Add(Relation.MemberType.NODE);
                    break;

                case OsmGeoType.Way:
                    relation1.types.Add(Relation.MemberType.WAY);
                    break;

                case OsmGeoType.Relation:
                    relation1.types.Add(Relation.MemberType.RELATION);
                    break;
                }
                for (int index = 1; index < relation.Members.Count; ++index)
                {
                    List <long> memids2 = relation1.memids;
                    nullable = relation.Members[index].MemberId;
                    long num2 = nullable.Value;
                    nullable = relation.Members[index - 1].MemberId;
                    long num3 = nullable.Value;
                    long num4 = num2 - num3;
                    memids2.Add(num4);
                    relation1.roles_sid.Add(Encoder.EncodeString(block, reverseStringTable, relation.Members[index].MemberRole));
                    memberType = relation.Members[index].MemberType;
                    switch (memberType.Value)
                    {
                    case OsmGeoType.Node:
                        relation1.types.Add(Relation.MemberType.NODE);
                        break;

                    case OsmGeoType.Way:
                        relation1.types.Add(Relation.MemberType.WAY);
                        break;

                    case OsmGeoType.Relation:
                        relation1.types.Add(Relation.MemberType.RELATION);
                        break;
                    }
                }
            }
            return(relation1);
        }
Beispiel #5
0
 public static OsmSharp.Osm.Relation DecodeRelation(PrimitiveBlock block, Relation pbfRelation)
 {
     OsmSharp.Osm.Relation relation = new OsmSharp.Osm.Relation();
     Encoder.DecodeRelation(block, pbfRelation, relation);
     return(relation);
 }
Beispiel #6
0
        public static OsmSharp.Osm.Relation DecodeRelation(PrimitiveBlock block, Relation pbfRelation, OsmSharp.Osm.Relation relation)
        {
            if (relation.Members != null && relation.Members.Count > 0)
            {
                relation.Members.Clear();
            }
            if (relation.Tags != null)
            {
                relation.Tags.Clear();
            }
            if (relation.Members == null)
            {
                relation.Members = new List <RelationMember>(pbfRelation.memids.Count);
            }
            if (relation.Tags == null)
            {
                relation.Tags = (TagsCollectionBase) new TagsCollection(pbfRelation.keys.Count);
            }
            relation.Id = new long?(pbfRelation.id);
            long num = 0;

            for (int index = 0; index < pbfRelation.types.Count; ++index)
            {
                num += pbfRelation.memids[index];
                string         str            = Encoding.UTF8.GetString(block.stringtable.s[pbfRelation.roles_sid[index]]);
                RelationMember relationMember = new RelationMember();
                relationMember.MemberId   = new long?(num);
                relationMember.MemberRole = str;
                switch (pbfRelation.types[index])
                {
                case Relation.MemberType.NODE:
                    relationMember.MemberType = new OsmGeoType?(OsmGeoType.Node);
                    break;

                case Relation.MemberType.WAY:
                    relationMember.MemberType = new OsmGeoType?(OsmGeoType.Way);
                    break;

                case Relation.MemberType.RELATION:
                    relationMember.MemberType = new OsmGeoType?(OsmGeoType.Relation);
                    break;
                }
                relation.Members.Add(relationMember);
            }
            for (int index = 0; index < pbfRelation.keys.Count; ++index)
            {
                string key = Encoding.UTF8.GetString(block.stringtable.s[(int)pbfRelation.keys[index]]);
                string str = Encoding.UTF8.GetString(block.stringtable.s[(int)pbfRelation.vals[index]]);
                relation.Tags.Add(new Tag(key, str));
            }
            if (pbfRelation.info != null)
            {
                relation.ChangeSetId = new long?(pbfRelation.info.changeset);
                relation.TimeStamp   = new DateTime?(Encoder.DecodeTimestamp(pbfRelation.info.timestamp, (long)block.date_granularity));
                relation.UserId      = new long?((long)pbfRelation.info.uid);
                relation.UserName    = (string)null;
                if (block.stringtable != null)
                {
                    relation.UserName = Encoding.UTF8.GetString(block.stringtable.s[pbfRelation.info.user_sid]);
                }
                relation.Version = new ulong?((ulong)pbfRelation.info.version);
            }
            relation.Visible = new bool?(true);
            return(relation);
        }