public void Add(OsmRelation relation, int typeId, IReadOnlyList <RelationItemInfo> itemInfos, BoundingRect rect)
        {
            if (relation == null)
            {
                throw new ArgumentNullException(nameof(relation));
            }
            if (itemInfos == null)
            {
                throw new ArgumentNullException(nameof(itemInfos));
            }
            if (rect == null)
            {
                throw new ArgumentNullException(nameof(rect));
            }

            startPositions.Add(new RelationLocation
            {
                Id     = relation.Id,
                Offset = stream.Position
            });

            idWriter.WriteIncrementOnly((ulong)relation.Id);
            writer.Write7BitEncodedInt((ulong)typeId);

            rect.WriteTo(writer);

            writer.Write7BitEncodedInt((ulong)relation.Items.Count);

            memberIdWriter.Reset();
            memberMidLatWriter.Reset();
            memberMidLonWriter.Reset();

            for (var index = 0; index < relation.Items.Count; index++)
            {
                var member = relation.Items[index];
                writer.Write((byte)member.MemberType);
                memberIdWriter.WriteZigZag(member.Id);
                writer.Write7BitEncodedInt(GetRoleId(member.Role));

                var info = itemInfos[index];
                memberMidLatWriter.WriteZigZag(info.MidLat);
                memberMidLonWriter.WriteZigZag(info.MidLon);
            }
        }
        public void ProcessPrimitives(PrimitiveAccessor accessor, string data)
        {
            if (accessor == null)
            {
                throw new ArgumentNullException(nameof(accessor));
            }

            var nodeIds = accessor.Relations
                          .SelectMany(x => x.Items)
                          .Where(i => i.MemberType == RelationMemberTypes.Node)
                          .Select(i => i.Id)
                          .Distinct()
                          .OrderBy(x => x);

            var nodes = nodesIndex.ReadAllNodesById(nodeIds).ToDictionary(x => x.Id);

            foreach (var relation in accessor.Relations)
            {
                var rect      = new BoundingRect();
                var itemInfos = new List <RelationItemInfo>();

                foreach (var relationItem in relation.Items)
                {
                    switch (relationItem.MemberType)
                    {
                    case RelationMemberTypes.Node:
                        if (!nodes.TryGetValue(relationItem.Id, out var node))
                        {
                            itemInfos.Add(new RelationItemInfo
                            {
                                Id     = (ulong)relationItem.Id,
                                Type   = relationItem.MemberType,
                                MidLat = int.MinValue,
                                MidLon = int.MinValue
                            });
                        }
                        else
                        {
                            rect.Extend(node.Lat, node.Lon);
                            itemInfos.Add(new RelationItemInfo
                            {
                                Id     = (ulong)relationItem.Id,
                                Type   = relationItem.MemberType,
                                MidLat = node.Lat,
                                MidLon = node.Lon
                            });
                        }

                        break;

                    case RelationMemberTypes.Way:
                        var way = waysData.FindWayInfo((ulong)relationItem.Id);
                        if (way == null)
                        {
                            itemInfos.Add(new RelationItemInfo
                            {
                                Id     = (ulong)relationItem.Id,
                                Type   = relationItem.MemberType,
                                MidLat = int.MinValue,
                                MidLon = int.MinValue
                            });
                        }
                        else
                        {
                            rect.Extend(way.Rect);
                            itemInfos.Add(new RelationItemInfo
                            {
                                Id     = (ulong)relationItem.Id,
                                Type   = relationItem.MemberType,
                                MidLat = int.MinValue,
                                MidLon = int.MinValue
                            });
                        }
                        break;

                    case RelationMemberTypes.Relation:
                        itemInfos.Add(new RelationItemInfo
                        {
                            Id     = (ulong)relationItem.Id,
                            Type   = relationItem.MemberType,
                            MidLat = 0,
                            MidLon = 0
                        });
                        // Relations will be assembled recursively.
                        break;

                    default:
                        throw new NotSupportedException("Unknown relation type.");
                    }
                }

                relationsFile.Add(relation, 0, itemInfos, rect); // ToDo: Implement type detection
            }
        }