/// <summary> /// Creates a new way from a SimpleWay. /// </summary> /// <param name="simpleWay"></param> /// <param name="nodeSource"></param> /// <returns></returns> public static CompleteWay CreateFrom(Way simpleWay, INodeSource nodeSource) { if (simpleWay == null) { throw new ArgumentNullException("simpleWay"); } if (nodeSource == null) { throw new ArgumentNullException("nodeSource"); } if (simpleWay.Id == null) { throw new Exception("simpleWay.id is null"); } CompleteWay way = Create(simpleWay.Id.Value); way.ChangeSetId = simpleWay.ChangeSetId; if (simpleWay.Tags != null) { foreach (Tag pair in simpleWay.Tags) { way.Tags.Add(pair); } } if (simpleWay.Nodes != null) { for (int idx = 0; idx < simpleWay.Nodes.Count; idx++) { long nodeId = simpleWay.Nodes[idx]; Node node = nodeSource.GetNode(nodeId); if (node == null) { return(null); } Node completeNode = node; if (completeNode != null) { way.Nodes.Add(completeNode); } else { return(null); } } } way.TimeStamp = simpleWay.TimeStamp; way.User = simpleWay.UserName; way.UserId = simpleWay.UserId; way.Version = simpleWay.Version.HasValue ? (long)simpleWay.Version.Value : (long?)null; way.Visible = simpleWay.Visible.HasValue && simpleWay.Visible.Value; return(way); }
public static CompleteWay CreateFrom(Way simpleWay, INodeSource nodeSource) { if (simpleWay == null) { throw new ArgumentNullException("simpleWay"); } if (nodeSource == null) { throw new ArgumentNullException("nodeSource"); } if (!simpleWay.Id.HasValue) { throw new Exception("simpleWay.id is null"); } CompleteWay completeWay = CompleteWay.Create(simpleWay.Id.Value); completeWay.ChangeSetId = simpleWay.ChangeSetId; if (simpleWay.Tags != null) { foreach (Tag tag in simpleWay.Tags) { completeWay.Tags.Add(tag); } } if (simpleWay.Nodes != null) { for (int index = 0; index < simpleWay.Nodes.Count; ++index) { long node1 = simpleWay.Nodes[index]; Node node2 = nodeSource.GetNode(node1); if (node2 == null) { return((CompleteWay)null); } Node node3 = node2; if (node3 == null) { return((CompleteWay)null); } completeWay.Nodes.Add(node3); } } completeWay.TimeStamp = simpleWay.TimeStamp; completeWay.User = simpleWay.UserName; completeWay.UserId = simpleWay.UserId; completeWay.Version = simpleWay.Version.HasValue ? new long?((long)simpleWay.Version.Value) : new long?(); completeWay.Visible = simpleWay.Visible.HasValue && simpleWay.Visible.Value; return(completeWay); }
/// <summary> /// Creates a new way. /// </summary> /// <returns></returns> public static CompleteWay CreateFrom(Way simpleWay, INodeSource nodeSource) { if (simpleWay == null) throw new ArgumentNullException("simpleWay"); if (nodeSource == null) throw new ArgumentNullException("nodeSource"); if (simpleWay.Id == null) throw new Exception("simpleWay.id is null"); var way = Create(simpleWay.Id.Value); way.ChangeSetId = simpleWay.ChangeSetId; if (simpleWay.Tags != null) { foreach (var pair in simpleWay.Tags) { way.Tags.Add(pair); } } if (simpleWay.Nodes != null) { for (int idx = 0; idx < simpleWay.Nodes.Count; idx++) { var nodeId = simpleWay.Nodes[idx]; var node = nodeSource.GetNode(nodeId); if (node == null) { return null; } var completeNode = node; if (completeNode != null) { way.Nodes.Add(completeNode); } else { return null; } } } way.TimeStamp = simpleWay.TimeStamp; way.User = simpleWay.UserName; way.UserId = simpleWay.UserId; way.Version = simpleWay.Version.HasValue ? (long)simpleWay.Version.Value : (long?)null; way.Visible = simpleWay.Visible.HasValue && simpleWay.Visible.Value; return way; }
/// <summary> /// Converts an Xml relation to an Osm domain model relation. /// </summary> /// <param name="xml_obj"></param> /// <param name="node_source"></param> /// <param name="way_source"></param> /// <param name="relation_source"></param> /// <returns></returns> public static Relation ConvertFrom(this relation xml_obj, INodeSource node_source, IWaySource way_source, IRelationSource relation_source) { // create a new node and immidiately set the id. Relation new_obj = OsmBaseFactory.CreateRelation(xml_obj.id); // set the members for (int idx = 0; idx < xml_obj.member.Length; idx++) { member member = xml_obj.member[idx]; if (member.refSpecified && member.typeSpecified) { switch (member.type) { case memberType.node: long node_id = member.@ref; Node member_node = node_source.GetNode(node_id); if (member_node != null) { RelationMember node_member = new RelationMember(); node_member.Member = member_node; node_member.Role = member.role; new_obj.Members.Add(node_member); } else { // relation cannot be converted; node was not found! return null; } break; case memberType.relation: long relation_id = member.@ref; Relation member_relation = relation_source.GetRelation(relation_id); if (member_relation != null) { RelationMember relation_member = new RelationMember(); relation_member.Member = member_relation; relation_member.Role = member.role; new_obj.Members.Add(relation_member); } else { // relation cannot be converted; relation was not found! return null; } break; case memberType.way: long way_id = member.@ref; Way member_way = way_source.GetWay(way_id); if (member_way != null) { RelationMember way_member = new RelationMember(); way_member.Member = member_way; way_member.Role = member.role; new_obj.Members.Add(way_member); } else { // relation cannot be converted; way was not found! return null; } break; } } else { // way cannot be converted; member could not be created! return null; } } // set the tags. if (xml_obj.tag != null) { foreach (Osm.Xml.v0_6.tag tag in xml_obj.tag) { new_obj.Tags.Add(tag.k, tag.v); } } // set the user info. if (xml_obj.uidSpecified) { new_obj.UserId = xml_obj.uid; } new_obj.User = xml_obj.user; // set the changeset info. if (xml_obj.changesetSpecified) { new_obj.ChangeSetId = xml_obj.changeset; } // set the timestamp flag. if (xml_obj.timestampSpecified) { new_obj.TimeStamp = xml_obj.timestamp; } // set the visible flag. new_obj.Visible = xml_obj.visible; return new_obj; }
/// <summary> /// Converts an Xml way to an Osm domain model way. /// </summary> /// <param name="xml_obj"></param> /// <param name="node_source"></param> /// <returns></returns> public static Way ConvertFrom(this way xml_obj, INodeSource node_source) { // create a new node and immidiately set the id. Way new_obj = OsmBaseFactory.CreateWay((int)xml_obj.id); // set the nodes for (int idx = 0; idx < xml_obj.nd.Length;idx++) { nd node = xml_obj.nd[idx]; if (node.refSpecified) { int node_id = (int)node.@ref; Node child_node = node_source.GetNode(node_id); if (child_node != null) { new_obj.Nodes.Add(child_node); } else { // way cannot be converted; node was not found! return null; } } else { // way cannot be converted; node was not found! return null; } } // set the tags. if (xml_obj.tag != null) { foreach (Osm.Xml.v0_6.tag tag in xml_obj.tag) { new_obj.Tags.Add(tag.k, tag.v); } } // set the user info. if (xml_obj.uidSpecified) { new_obj.UserId = (int)xml_obj.uid; } new_obj.User = xml_obj.user; // set the changeset info. if (xml_obj.changesetSpecified) { new_obj.ChangeSetId = (int)xml_obj.changeset; } // set the timestamp flag. if (xml_obj.timestampSpecified) { new_obj.TimeStamp = xml_obj.timestamp; } // set the visible flag. new_obj.Visible = xml_obj.visible; return new_obj; }