Ejemplo n.º 1
0
        /// <summary>
        /// Tests a simple relation read/write operation.
        /// </summary>
        /// <param name="cache"></param>
        public void DoOsmDataCacheTestRelation(OsmDataCache cache)
        {
            Relation relation = Relation.Create(1, new TagsCollection(
                                                    Tag.Create("relation", "yes")), RelationMember.Create(1, "something", OsmGeoType.Node));

            // test invalid stuff.
            Assert.Throws <ArgumentNullException>(() => cache.AddRelation(null));
            Assert.Throws <Exception>(() => cache.AddRelation(new Relation()));
            Assert.IsNull(cache.GetRelation(relation.Id.Value));

            cache.AddRelation(relation);

            Assert.IsTrue(cache.ContainsRelation(relation.Id.Value));
            Relation readRelation = cache.GetRelation(relation.Id.Value);

            Assert.IsNotNull(readRelation);
            Assert.AreEqual(1, readRelation.Id.Value);
            Assert.IsNotNull(relation.Tags);
            Assert.AreEqual(1, relation.Tags.Count);
            Assert.AreEqual("yes", relation.Tags["relation"]);

            Assert.IsTrue(cache.TryGetRelation(relation.Id.Value, out readRelation));
            Assert.IsNotNull(readRelation);
            Assert.AreEqual(1, readRelation.Id.Value);
            Assert.IsNotNull(relation.Tags);
            Assert.AreEqual(1, relation.Tags.Count);
            Assert.AreEqual("yes", relation.Tags["relation"]);

            Assert.IsTrue(cache.RemoveRelation(relation.Id.Value));
            Assert.IsFalse(cache.ContainsRelation(relation.Id.Value));
            Assert.IsFalse(cache.RemoveRelation(relation.Id.Value));
        }
        /// <summary>
        /// Reports that the relation with the given id was used.
        /// </summary>
        /// <param name="relationId"></param>
        private void ReportRelationUsage(long relationId)
        {
            int relationCount;

            if (_relationsUsedTwiceOrMore.TryGetValue(relationId, out relationCount))
            { // relation is used twice or more.
                relationCount--;
                if (relationCount > 0)
                { // update count.
                    _relationsUsedTwiceOrMore[relationId] = relationCount;
                }
                else
                { // remove twice or more.
                    _relationsUsedTwiceOrMore.Remove(relationId);
                }
            }
            else
            {                                                           // the relation was used for the last time.
                Relation relation = _dataCache.GetRelation(relationId); // get relation before it is removed.
                _dataCache.RemoveRelation(relationId);                  // remove relation.
                _relationsToInclude.Remove(relationId);                 // remove from the to-include list.

                //if (relation.Members != null)
                //{ // also report usage to the members.
                //    foreach (RelationMember member in relation.Members)
                //    {
                //        switch (member.MemberType.Value)
                //        {
                //            case OsmGeoType.Node:
                //                this.ReportNodeUsage(member.MemberId.Value);
                //                break;
                //            case OsmGeoType.Way:
                //                this.ReportWayUsage(member.MemberId.Value);
                //                break;
                //            case OsmGeoType.Relation:
                //                this.ReportRelationUsage(member.MemberId.Value);
                //                break;
                //        }
                //    }
                //}
            }
        }