/// <summary>
        /// Adds a relation to the target.
        /// </summary>
        /// <param name="relation"></param>
        public override void AddRelation(SimpleRelation relation)
        {
            if (_base_objects == null)
            { // the base object collection is null.
                throw new InvalidOperationException("No target collection set!");
            }

            // add the relation to the collection.
            _base_objects.Add(relation);
        }
        private bool MoveNextRelation()
        {
            if (_relation_reader == null)
            {
                OracleCommand relation_command = new OracleCommand("select * from relation order by id");
                relation_command.Connection = _connection;
                _relation_reader = relation_command.ExecuteReader();
                if (!_relation_reader.Read())
                {
                    _relation_reader.Close();
                }
                OracleCommand relation_tag_command = new OracleCommand("select * from relation_tags order by relation_id");
                relation_tag_command.Connection = _connection;
                _relation_tag_reader = relation_tag_command.ExecuteReader();
                if (!_relation_tag_reader.IsClosed && !_relation_tag_reader.Read())
                {
                    _relation_tag_reader.Close();
                }
                OracleCommand relation_node_command = new OracleCommand("select * from relation_members order by relation_id,sequence_id");
                relation_node_command.Connection = _connection;
                _relation_member_reader = relation_node_command.ExecuteReader();
                if (!_relation_member_reader.IsClosed && !_relation_member_reader.Read())
                {
                    _relation_member_reader.Close();
                }
            }

            // read next relation.
            if (!_relation_reader.IsClosed)
            {
                // load/parse data.
                long id = _relation_reader.GetInt64(0);
                long changeset_id = _relation_reader.GetInt64(1);
                DateTime timestamp = _relation_reader.GetDateTime(2);
                bool visible = _relation_reader.GetInt64(3) == 1;
                long version = _relation_reader.GetInt64(4);
                string user = _relation_reader.GetString(5);
                long uid = _relation_reader.GetInt64(6);
                SimpleRelation relation = new SimpleRelation();
                relation.Id = id;
                relation.ChangeSetId = changeset_id;
                relation.TimeStamp = timestamp;
                relation.UserId = null;
                relation.UserName = null;
                relation.Version = (ulong)version;
                relation.Visible = visible;
                relation.UserName = user;
                relation.UserId = uid;

                if (!_relation_tag_reader.IsClosed)
                {
                    long returned_id = _relation_tag_reader.GetInt64(0);
                    while (returned_id == relation.Id.Value)
                    {
                        if (relation.Tags == null)
                        {
                            relation.Tags = new Dictionary<string, string>();
                        }
                        string key = _relation_tag_reader.GetString(1);
                        string value = _relation_tag_reader.GetString(2);

                        relation.Tags.Add(key, value);

                        if (!_relation_tag_reader.Read())
                        {
                            _relation_tag_reader.Close();
                            returned_id = -1;
                        }
                        else
                        {
                            returned_id = _relation_tag_reader.GetInt64(0);
                        }
                    }
                }
                if (!_relation_member_reader.IsClosed)
                {
                    long returned_id = _relation_member_reader.GetInt64(0);
                    while (returned_id == relation.Id.Value)
                    {
                        if (relation.Members == null)
                        {
                            relation.Members = new List<SimpleRelationMember>();
                        }
                        string member_type = _relation_member_reader.GetString(1);
                        long member_id = _relation_member_reader.GetInt64(2);
                        object member_role = _relation_member_reader.GetValue(3);

                        SimpleRelationMember member = new SimpleRelationMember();
                        member.MemberId = member_id;
                        if (member_role != DBNull.Value)
                        {
                            member.MemberRole = member_role as string;
                        }
                        switch (member_type)
                        {
                            case "Node":
                                member.MemberType = SimpleRelationMemberType.Node;
                                break;
                            case "Way":
                                member.MemberType = SimpleRelationMemberType.Way;
                                break;
                            case "Relation":
                                member.MemberType = SimpleRelationMemberType.Relation;
                                break;
                        }

                        relation.Members.Add(member);

                        if (!_relation_member_reader.Read())
                        {
                            _relation_member_reader.Close();
                            returned_id = -1;
                        }
                        else
                        {
                            returned_id = _relation_member_reader.GetInt64(0);
                        }
                    }
                }

                // set the current variable!
                _current = relation;

                // advance the reader(s).
                if (!_relation_reader.Read())
                {
                    _relation_reader.Close();
                }
                if (!_relation_tag_reader.IsClosed && !_relation_tag_reader.Read())
                {
                    _relation_tag_reader.Close();
                }
                if (!_relation_member_reader.IsClosed && !_relation_member_reader.Read())
                {
                    _relation_member_reader.Close();
                }
                return true;
            }
            else
            {
                _relation_reader.Close();
                _relation_reader.Dispose();
                _relation_reader = null;

                _relation_tag_reader.Close();
                _relation_tag_reader.Dispose();
                _relation_tag_reader = null;

                _current_type = SimpleOsmGeoType.Relation;

                return false;
            }
        }
        internal static SimpleRelation ConvertToSimple(Osm.Xml.v0_6.relation re)
        {
            SimpleRelation relation = new SimpleRelation();

            // set id
            if (re.idSpecified)
            {
                relation.Id = re.id;
            }

            // set changeset.
            if (re.changesetSpecified)
            {
                relation.ChangeSetId = re.changeset;
            }

            // set visible.
            if (re.visibleSpecified)
            {
                relation.Visible = re.visible;
            }
            else
            { // if visible is not specified it is default true.
                relation.Visible = true;
            }

            // set timestamp.
            if (re.timestampSpecified)
            {
                relation.TimeStamp = re.timestamp;
            }

            // set uid
            if (re.uidSpecified)
            {
                relation.UserId = re.uid;
            }

            // set version
            if (re.versionSpecified)
            {
                relation.Version = re.version;
            }

            // set user
            relation.UserName = re.user;

            // set tags.
            relation.Tags = XmlSimpleConverter.ConvertToTags(re.tag);

            // set members.
            if (re.member != null && re.member.Length > 0)
            {
                relation.Members = new List<SimpleRelationMember>();
                for (int idx = 0; idx < re.member.Length; idx++)
                {
                    OsmSharp.Osm.Xml.v0_6.member mem = re.member[idx];
                    SimpleRelationMember relation_member = new SimpleRelationMember();
                    // set memberid
                    if (mem.refSpecified)
                    {
                        relation_member.MemberId = mem.@ref;
                    }

                    // set role.
                    relation_member.MemberRole = mem.role;

                    // set type.
                    if (mem.typeSpecified)
                    {
                        switch (mem.type)
                        {
                            case OsmSharp.Osm.Xml.v0_6.memberType.node:
                                relation_member.MemberType = SimpleRelationMemberType.Node;
                                break;
                            case OsmSharp.Osm.Xml.v0_6.memberType.way:
                                relation_member.MemberType = SimpleRelationMemberType.Way;
                                break;
                            case OsmSharp.Osm.Xml.v0_6.memberType.relation:
                                relation_member.MemberType = SimpleRelationMemberType.Relation;
                                break;
                        }
                    }

                    relation.Members.Add(relation_member);
                }
            }

            return relation;
        }
Beispiel #4
0
 /// <summary>
 /// Adds a relation.
 /// </summary>
 /// <param name="relation"></param>
 public void AddRelation(SimpleRelation relation)
 {
     _relation[relation.Id.Value] = relation;
 }
        public override void AddRelation(SimpleRelation relation)
        {
            DataRow relation_row = _relation_table.NewRow();

            // format data and create parameters.
            long? id = relation.Id.Value; // id should alrelations contain a value.
            relation_row["id"] = id.ConvertToDBValue<long>();

            long? changeset_id = relation.ChangeSetId;
            relation_row["changeset_id"] = changeset_id.ConvertToDBValue<long>();

            bool? visible = relation.Visible;
            int visible_int = 1;
            if (!visible.HasValue || !visible.Value)
            {
                visible_int = 0;
            }
            relation_row["visible"] = visible_int;

            DateTime? timestamp = relation.TimeStamp;
            relation_row["timestamp"] = timestamp.ConvertToDBValue<DateTime>();

            ulong? version = relation.Version;
            relation_row["version"] = version.ConvertToDBValue<ulong>();

            // set the usr
            relation_row["usr"] = relation.UserName.ToStringEmptyWhenNull();
            relation_row["usr_id"] = relation.UserId.ConvertToDBValue<long>();

            // add the node and it's tags.
            _relation_table.Rows.Add(relation_row);

            // tags.
            if (relation.Tags != null)
            {
                foreach (KeyValuePair<string, string> tag in relation.Tags)
                {
                    string key = tag.Key;
                    string value = tag.Value;

                    DataRow tag_row = _relation_tags_table.NewRow();
                    tag_row["relation_id"] = id;
                    tag_row["key"] = key.Truncate(255);
                    tag_row["value"] = value.Truncate(255);

                    _relation_tags_table.Rows.Add(tag_row);
                }
            }

            // member.
            if (relation.Members != null)
            {
                long relation_id = relation.Id.Value;

                for (int idx = 0; idx < relation.Members.Count; idx++)
                {
                    SimpleRelationMember member = relation.Members[idx]; ;

                    DataRow tag_row = _relation_members_table.NewRow();
                    tag_row["relation_id"] = id;
                    tag_row["member_type"] = member.MemberType;
                    tag_row["member_id"] = member.MemberId;
                    tag_row["member_role"] = member.MemberRole;
                    tag_row["sequence_id"] = idx;

                    _relation_members_table.Rows.Add(tag_row);
                }
            }

            // bulk insert if needed.
            if (_relation_table.Rows.Count >= _batch_relations)
            {
                this.BulkCopy(_relation_table, "relation");
                this.BulkCopy(_relation_tags_table, "relation_tags");
                this.BulkCopy(_relation_members_table, "relation_members");
                this.CreateRelationTables();
            }
        }
        private void Modify(SimpleRelation relation)
        {
            OracleCommand command;
            string sql = string.Empty;

            DateTime? timestamp = relation.TimeStamp;
            if (timestamp.HasValue)
            {
                sql = "update relation set changeset_id=:changeset_id,visible=:visible,timestamp=to_date('{0}','YYYY/MM/DD HH24:MI'),version=:version, usr=:usr, usr_id=:usr_id where id = :id";
                sql = string.Format(sql, timestamp.Value.ToString("yyyy/MM/dd HH:mm"));
            }
            else
            {
                sql = "update relation set changeset_id=:changeset_id,visible=:visible,timestamp=null,version=:version, usr_id=:usr_id where id = :id";
            }
            command = this.CreateCommand(sql);

            // format data and create parameters.
            long? changeset_id = relation.ChangeSetId;
            command.Parameters.Add(new OracleParameter("changeset_id", changeset_id.ConvertToDBValue<long>()));

            bool? visible = relation.Visible;
            int visible_int = 1;
            if (!visible.HasValue || !visible.Value)
            {
                visible_int = 0;
            }
            command.Parameters.Add("visible", visible_int);

            long? version = (long)relation.Version;
            command.Parameters.Add(new OracleParameter("version", version.ConvertToDBValue<long>()));

            command.Parameters.Add("usr", relation.UserName);
            command.Parameters.Add("usr_id", relation.UserId);

            long? id = relation.Id;
            command.Parameters.Add(new OracleParameter("id", id.ConvertToDBValue<long>()));

            command.ExecuteNonQuery();
            command.Dispose();

            if (this.Exists("relation",id.Value))
            {
                // update tags.
                this.ModifyTags(relation.Id.Value, relation.Tags, "relation_tags", "relation_id");

                // update members
                this.ModifyRelationMembers(relation.Id.Value, relation.Members);
            }

            // raise the modified event.
            this.RaiseChange(SimpleChangeType.Modify, SimpleOsmGeoType.Relation, relation.Id.Value);
        }
Beispiel #7
0
        /// <summary>
        /// Converts this relation into it's simple counterpart.
        /// </summary>
        /// <returns></returns>
        public override SimpleOsmGeo ToSimple()
        {
            SimpleRelation relation = new SimpleRelation();
            relation.Id = this.Id;
            relation.ChangeSetId = this.ChangeSetId;
            relation.Tags = this.Tags;
            relation.TimeStamp = this.TimeStamp;
            relation.UserId = this.UserId;
            relation.UserName = this.User;
            relation.Version = (ulong?)this.Version;
            relation.Visible = this.Visible;

            relation.Members = new List<SimpleRelationMember>();
            foreach (RelationMember member in this.Members)
            {
                SimpleRelationMember simple_member = new SimpleRelationMember();
                simple_member.MemberId = member.Member.Id;
                simple_member.MemberRole = member.Role;
                switch(member.Member.Type)
                {
                    case OsmType.Node:
                        simple_member.MemberType = SimpleRelationMemberType.Node;
                        break;
                    case OsmType.Relation:
                        simple_member.MemberType = SimpleRelationMemberType.Relation;
                        break;
                    case OsmType.Way:
                        simple_member.MemberType = SimpleRelationMemberType.Way;
                        break;
                }
                relation.Members.Add(simple_member);
            }
            return relation;
        }
Beispiel #8
0
        public void init()
        {
            // initialize the connection.
            APIConnection api_instance = new APIConnection(
            "http://api06.dev.openstreetmap.org/",
            "osmsharp", "osmsharp");

            // open a changeset.
            long changeset_id = api_instance.ChangeSetOpen("An example comment!");

            // initialize the nodes.
            SimpleNode node1 = new SimpleNode();
            node1.Latitude = -0.494497;
            node1.Longitude = -24.119325;
            node1.Tags = new Dictionary<string, string>();
            node1.Tags.Add("type", "testnode1");
            node1.Visible = true;
            node1 = api_instance.NodeCreate(node1); // create
            SimpleNode node2 = new SimpleNode();
            node2.Latitude = -0.494497 + 0.0001f;
            node2.Longitude = -24.119325 + 0.0001f;
            node2.Tags = new Dictionary<string, string>();
            node2.Tags.Add("type", "testnode2");
            node2.Visible = true;
            node2 = api_instance.NodeCreate(node2); // create

            // initialize the way.
            SimpleWay way = new SimpleWay();
            way.Tags = new Dictionary<string, string>();
            way.Tags.Add("type", "testway");
            way.Nodes = new List<long>();
            way.Visible = true;
            way.Nodes.Add(node1.Id.Value);
            way.Nodes.Add(node2.Id.Value);
            way = api_instance.WayCreate(way); // create

            // initialize the relation.
            SimpleRelation relation = new SimpleRelation();
            relation.Tags = new Dictionary<string, string>();
            relation.Tags.Add("type", "testrelation");
            relation.Members = new List<SimpleRelationMember>();
            relation.Visible = true;
            relation.Members.Add(new SimpleRelationMember()
            {
                MemberId = node1.Id.Value,
                MemberRole = "some_nodes_role",
                MemberType = SimpleRelationMemberType.Node
            });
            relation.Members.Add(new SimpleRelationMember()
            {
                MemberId = node2.Id.Value,
                MemberRole = "some_nodes_role",
                MemberType = SimpleRelationMemberType.Node
            });
            relation.Members.Add(new SimpleRelationMember()
            {
                MemberId = way.Id.Value,
                MemberRole = "some_ways_role",
                MemberType = SimpleRelationMemberType.Way
            });

            // save the relation.
            relation = api_instance.RelationCreate(relation);

            // close the changeset.
            api_instance.ChangeSetClose();
        }
Beispiel #9
0
        public void APITestRelationCreateGetUpdate()
        {
            // intialize the connection.
            APIConnection api_instance = new APIConnection("http://api06.dev.openstreetmap.org/",
                "osmsharp", "osmsharp");

            // open a changeset.
            long changeset_id = api_instance.ChangeSetOpen("Simple Relation Creation Test");

            // initialize the relation.
            SimpleRelation relation = new SimpleRelation();
            relation.Tags = new Dictionary<string, string>();
            relation.Tags.Add("type", "testrelation");
            relation.Members = new List<SimpleRelationMember>();
            relation.Visible = true;

            // initialize the nodes.
            SimpleNode node = new SimpleNode();
            node.Latitude = -0.494497;
            node.Longitude = -24.119325;
            node.Tags = new Dictionary<string, string>();
            node.Tags.Add("type", "testnode1");
            node.Visible = true;
            node = api_instance.NodeCreate(node);
            relation.Members.Add(new SimpleRelationMember()
            {
                MemberId = node.Id.Value,
                MemberRole = "some_nodes_role",
                MemberType = SimpleRelationMemberType.Node
            });
            node = new SimpleNode();
            node.Latitude = -0.494497 + 0.0001f;
            node.Longitude = -24.119325 + 0.0001f;
            node.Tags = new Dictionary<string, string>();
            node.Tags.Add("type", "testnode2");
            node.Visible = true;
            node = api_instance.NodeCreate(node);
            relation.Members.Add(new SimpleRelationMember()
            {
                MemberId = node.Id.Value,
                MemberRole = "some_nodes_role",
                MemberType = SimpleRelationMemberType.Node
            });

            // save the relation.
            relation = api_instance.RelationCreate(relation);

            // close the changeset.
            api_instance.ChangeSetClose();

            // check if the id now has a value.
            Assert.IsTrue(relation.Id.HasValue);

            // get the new relation id.
            long relation_id = relation.Id.Value;

            // open new changeset.
            changeset_id = api_instance.ChangeSetOpen("Simple Relation Update Test");

            // get the relation.
            SimpleRelation api_relation = api_instance.RelationGet(relation.Id.Value);
            api_relation.Tags.Add("another_tag", "test adding a tag!");
            api_instance.RelationUpdate(api_relation);

            // close the current changeset.
            api_instance.ChangeSetClose();

            // get the api relation.
            api_relation = api_instance.RelationGet(relation.Id.Value);

            Assert.AreEqual(2, api_relation.Tags.Count);
            Assert.IsTrue(api_relation.Tags.ContainsKey("another_tag"));
            Assert.AreEqual("test adding a tag!", api_relation.Tags["another_tag"]);
        }
Beispiel #10
0
        /// <summary>
        /// Converts an API v6 xml node to a SimpleRelation object.
        /// </summary>
        /// <param name="xml_relation"></param>
        /// <returns></returns>
        private SimpleRelation Convertv6XmlRelation(relation xml_relation)
        {
            SimpleRelation relation = new SimpleRelation();
            relation.Id = xml_relation.id;

            if (xml_relation.tag != null)
            {
                relation.Tags = new Dictionary<string, string>();
                foreach (Osm.Xml.v0_6.tag xml_tag in xml_relation.tag)
                {
                    relation.Tags.Add(xml_tag.k, xml_tag.v);
                }
            }

            if (xml_relation.member != null)
            {
                relation.Members = new List<SimpleRelationMember>();
                foreach (Osm.Xml.v0_6.member xml_member in xml_relation.member)
                {
                    SimpleRelationMemberType? member_type = null;
                    switch (xml_member.type)
                    {
                        case memberType.node:
                            member_type = SimpleRelationMemberType.Node;
                            break;
                        case memberType.way:
                            member_type = SimpleRelationMemberType.Way;
                            break;
                        case memberType.relation:
                            member_type = SimpleRelationMemberType.Relation;
                            break;
                    }

                    relation.Members.Add(new SimpleRelationMember()
                    {
                        MemberId = xml_member.@ref,
                        MemberRole = xml_member.role,
                        MemberType = member_type
                    });
                }
            }

            relation.ChangeSetId = xml_relation.changeset;
            relation.TimeStamp = xml_relation.timestamp;
            relation.UserName = xml_relation.user;
            relation.UserId = xml_relation.uid;
            relation.Version = xml_relation.version;
            relation.Visible = xml_relation.visible;

            return relation;
        }
Beispiel #11
0
        /// <summary>
        /// Updates the given relation by adding it's changes to the current changeset.
        /// </summary>
        /// <param name="relation"></param>
        public void RelationUpdate(SimpleRelation relation)
        {
            if (_current_changeset == null)
            {
                throw new InvalidOperationException("No open changeset found!");
            }
            if (!relation.Id.HasValue)
            {
                throw new ArgumentOutOfRangeException("Cannot update an object without an id!");
            }

            // build a new node.
            relation xml_relation = relation.ConvertTo();
            xml_relation.changeset = _current_changeset.id;
            xml_relation.changesetSpecified = true;

            // encapsulate into an osm object.
            OsmSharp.Osm.Xml.v0_6.osm osm = new Osm.Xml.v0_6.osm();
            osm.relation = new Osm.Xml.v0_6.relation[1];
            osm.relation[0] = xml_relation;

            // serialize the changeset.
            XmlSerializer serializer = new XmlSerializer(typeof(OsmSharp.Osm.Xml.v0_6.osm));
            MemoryStream mem_stream = new MemoryStream();
            Stream stream = mem_stream;
            serializer.Serialize(stream, osm);
            stream.Flush();
            mem_stream.Flush();
            byte[] put_data = mem_stream.ToArray();

            // do the api call.
            string response_string = this.DoApiCall(true, string.Format("api/0.6/relation/{0}", relation.Id.Value),
                Method.PUT, put_data);
        }
Beispiel #12
0
        /// <summary>
        /// Creates a new relation.
        /// </summary>
        /// <param name="relation"></param>
        /// <returns></returns>
        public SimpleRelation RelationCreate(SimpleRelation relation)
        {
            if (_current_changeset == null)
            {
                throw new InvalidOperationException("No open changeset found!");
            }

            // build a new node.
            relation xml_relation = relation.ConvertTo();
            xml_relation.changeset = _current_changeset.id;
            xml_relation.changesetSpecified = true;

            // encapsulate into an osm object.
            OsmSharp.Osm.Xml.v0_6.osm osm = new Osm.Xml.v0_6.osm();
            osm.relation = new Osm.Xml.v0_6.relation[1];
            osm.relation[0] = xml_relation;

            // serialize the changeset.
            XmlSerializer serializer = new XmlSerializer(typeof(OsmSharp.Osm.Xml.v0_6.osm));
            MemoryStream mem_stream = new MemoryStream();
            Stream stream = mem_stream;
            serializer.Serialize(stream, osm);
            stream.Flush();
            mem_stream.Flush();
            byte[] put_data = mem_stream.ToArray();

            // do the api call.
            string response_string = this.DoApiCall(true, "api/0.6/relation/create",
                Method.PUT, put_data);

            // get the id-response.
            long id;
            if (!long.TryParse(response_string, out id))
            { // invalid response!
                throw new APIException(string.Format("Invalid response when creating a new relation: {0}", response_string));
            }
            relation.Id = id;
            return relation;
        }
        /// <summary>
        /// Adds a given relation.
        /// </summary>
        /// <param name="simple_relation"></param>
        public override void AddRelation(SimpleRelation simple_relation)
        {
            Relation relation = OsmBaseFactory.CreateRelationFrom(_string_table, simple_relation, _nodes, _ways, _relations);
            if (relation != null)
            {
                _relations[relation.Id] = relation;

                _source.AddRelation(relation);
            }
        }
        /// <summary>
        /// Converts simple primitives.
        /// </summary>
        /// <param name="pbf_primitive"></param>
        /// <returns></returns>
        internal SimpleOsmGeo Convert(KeyValuePair<PrimitiveBlock, object> pbf_primitive)
        {
            if (pbf_primitive.Value == null || pbf_primitive.Key == null)
            {
                throw new ArgumentNullException("pbf_primitive");
            }

            PrimitiveBlock block = pbf_primitive.Key; // get the block properties this object comes from.
            if (pbf_primitive.Value is Node)
            {
                Node node = (pbf_primitive.Value as Node);
                SimpleNode simple_node = new SimpleNode();
                simple_node.ChangeSetId = node.info.changeset;
                simple_node.Id = node.id;
                simple_node.Latitude = .000000001 * ((double)block.lat_offset
                    + ((double)block.granularity * (double)node.lat));
                simple_node.Longitude = .000000001 * ((double)block.lon_offset
                    + ((double)block.granularity * (double)node.lon));
                simple_node.Tags = new Dictionary<string, string>();
                for (int tag_idx = 0; tag_idx < node.keys.Count; tag_idx++)
                {
                    string key = ASCIIEncoding.ASCII.GetString(block.stringtable.s[(int)node.keys[tag_idx]]);
                    string value = ASCIIEncoding.ASCII.GetString(block.stringtable.s[(int)node.vals[tag_idx]]);

                    if (!simple_node.Tags.ContainsKey(key))
                    {
                        simple_node.Tags.Add(new KeyValuePair<string, string>(key, value));
                    }
                }
                simple_node.TimeStamp = OsmSharp.Tools.Utilities.FromUnixTime((long)node.info.timestamp *
                    (long)block.date_granularity);
                simple_node.Visible = true;
                simple_node.Version = (uint)node.info.version;
                simple_node.UserId = node.info.uid;
                simple_node.UserName = ASCIIEncoding.ASCII.GetString(block.stringtable.s[node.info.user_sid]);
                simple_node.Version = (ulong)node.info.version;
                simple_node.Visible = true;

                return simple_node;
            }
            else if (pbf_primitive.Value is Way)
            {
                Way way = (pbf_primitive.Value as Way);

                SimpleWay simple_way = new SimpleWay();
                simple_way.Id = way.id;
                simple_way.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];
                    simple_way.Nodes.Add(node_id);
                }
                simple_way.Tags = new Dictionary<string, string>();
                for (int tag_idx = 0; tag_idx < way.keys.Count; tag_idx++)
                {
                    string key = ASCIIEncoding.ASCII.GetString(block.stringtable.s[(int)way.keys[tag_idx]]);
                    string value = ASCIIEncoding.ASCII.GetString(block.stringtable.s[(int)way.vals[tag_idx]]);

                    if (!simple_way.Tags.ContainsKey(key))
                    {
                        simple_way.Tags.Add(new KeyValuePair<string, string>(key, value));
                    }
                }
                if (way.info != null)
                { // add the metadata if any.
                    simple_way.ChangeSetId = way.info.changeset;
                    simple_way.TimeStamp = OsmSharp.Tools.Utilities.FromUnixTime((long)way.info.timestamp *
                        (long)block.date_granularity);
                    simple_way.UserId = way.info.uid;
                    simple_way.UserName = ASCIIEncoding.ASCII.GetString(block.stringtable.s[way.info.user_sid]);
                    simple_way.Version = (ulong)way.info.version;
                }
                simple_way.Visible = true;

                return simple_way;
            }
            else if (pbf_primitive.Value is Relation)
            {
                Relation relation = (pbf_primitive.Value as Relation);

                SimpleRelation simple_relation = new SimpleRelation();
                simple_relation.Id = relation.id;
                simple_relation.Members = new List<SimpleRelationMember>();
                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 = ASCIIEncoding.ASCII.GetString(
                        block.stringtable.s[relation.roles_sid[member_idx]]);
                    SimpleRelationMember member = new SimpleRelationMember();
                    member.MemberId = member_id;
                    member.MemberRole = role;
                    switch(relation.types[member_idx])
                    {
                        case Relation.MemberType.NODE:
                            member.MemberType = SimpleRelationMemberType.Node;
                            break;
                        case Relation.MemberType.WAY:
                            member.MemberType = SimpleRelationMemberType.Way;
                            break;
                        case Relation.MemberType.RELATION:
                            member.MemberType = SimpleRelationMemberType.Relation;
                            break;
                    }

                    simple_relation.Members.Add(member);
                }
                simple_relation.Tags = new Dictionary<string, string>();
                for (int tag_idx = 0; tag_idx < relation.keys.Count; tag_idx++)
                {
                    string key = ASCIIEncoding.ASCII.GetString(block.stringtable.s[(int)relation.keys[tag_idx]]);
                    string value = ASCIIEncoding.ASCII.GetString(block.stringtable.s[(int)relation.vals[tag_idx]]);

                    if (!simple_relation.Tags.ContainsKey(key))
                    {
                        simple_relation.Tags.Add(new KeyValuePair<string, string>(key, value));
                    }
                }
                if (relation.info != null)
                { // read metadata if any.
                    simple_relation.ChangeSetId = relation.info.changeset;
                    simple_relation.TimeStamp = OsmSharp.Tools.Utilities.FromUnixTime((long)relation.info.timestamp *
                        (long)block.date_granularity);
                    simple_relation.UserId = relation.info.uid;
                    simple_relation.UserName = ASCIIEncoding.ASCII.GetString(block.stringtable.s[relation.info.user_sid]);
                    simple_relation.Version = (ulong)relation.info.version;
                }
                simple_relation.Visible = true;

                return simple_relation;
            }
            throw new Exception(string.Format("PBF primitive with type {0} not supported!",
                pbf_primitive.GetType().ToString()));
        }
 /// <summary>
 /// Adds a relation.
 /// </summary>
 /// <param name="relation"></param>
 public override void AddRelation(SimpleRelation relation)
 {
 }
        private void Create(SimpleRelation relation)
        {
            OracleCommand command;
            DateTime? timestamp = relation.TimeStamp;
            string timestamp_str = timestamp.Value.ToString("HH:mm dd/MM/yyyy");

            command = this.CreateCommand(string.Format(
                "insert into relation (id,changeset_id,timestamp,visible,version,usr,usr_id) values (:id,:changeset_id,to_date('{0}','HH24:MI DD/MM/YYYY'),:visible,:version,:usr,:usr_id)",
                timestamp_str));

            // format data and create parameters.
            long? id = relation.Id;
            command.Parameters.Add(new OracleParameter("id", id.ConvertToDBValue<long>()));

            long? changeset_id = relation.ChangeSetId;
            command.Parameters.Add(new OracleParameter("changeset_id", changeset_id.ConvertToDBValue<long>()));

            bool? visible = relation.Visible;
            int visible_int = 1;
            if (!visible.HasValue)
            {
                visible_int = 0;
            }
            command.Parameters.Add(new OracleParameter("visible", visible_int));

            long? version = (long)relation.Version;
            command.Parameters.Add(new OracleParameter("version", version.ConvertToDBValue<long>()));

            command.Parameters.Add("usr", relation.UserName);
            command.Parameters.Add("usr_id", relation.UserId);

            command.ExecuteNonQuery();
            command.Dispose();

            // insert tags.
            this.CreateTags(relation.Id.Value, relation.Tags, "relation_tags", "relation_id");

            // insert members.
            this.CreateRelationMembers(relation.Id.Value, relation.Members);
        }
        private void Delete(SimpleRelation relation)
        {
            OracleCommand command;

            command = this.CreateCommand("delete from relation_members where relation_id = :relation_id");
            command.Parameters.Add(new OracleParameter("relation_id", relation.Id.Value));
            command.ExecuteNonQuery();
            command.Dispose();

            command = this.CreateCommand("delete from relation_tags where relation_id = :relation_id");
            command.Parameters.Add(new OracleParameter("relation_id", relation.Id.Value));
            command.ExecuteNonQuery();
            command.Dispose();

            command = this.CreateCommand("delete from relation where id = :id");
            command.Parameters.Add(new OracleParameter("id", relation.Id.Value));
            command.ExecuteNonQuery();
            command.Dispose();
        }
 /// <summary>
 /// Adds a relation to the target.
 /// </summary>
 /// <param name="relation"></param>
 public abstract void AddRelation(SimpleRelation relation);
 /// <summary>
 /// Adds a relation to this database.
 /// </summary>
 /// <param name="relation"></param>
 public override void AddRelation(SimpleRelation relation)
 {
     //string relation_key = relation.GetRedisKey();
     //_relation_type_client.SetEntry(relation_key, relation);
 }