コード例 #1
0
        /// <summary>
        /// Reads the ways from the file.
        /// </summary>
        /// <returns>The ways.</returns>
        public IEnumerable <MapShape> ReadWays()
        {
            do
            {
                switch (reader.Name)
                {
                case "node":
                    var node = MapDefinition.ParseNode(reader);
                    this.nodes.Add(node.Index, node);
                    break;

                case "way":
                    var nextWay = ParseMapWay();
                    this.ways.Add(nextWay.Id, nextWay);
                    yield return(MapShape.Create(nextWay, nextWay.Tags));

                    break;

                case "relation":
                {
                    foreach (var relation in this.TryParseRelation())
                    {
                        yield return(relation);
                    }
                }
                break;
                }
            } while (reader.Read());

            yield break;
        }
コード例 #2
0
 /// <summary>
 /// Parse the first set of nodes.
 /// </summary>
 /// <param name="reader">The xml reader.</param>
 private void ParseFirstSetOfNodes()
 {
     while (this.reader.Read() && string.Equals(this.reader.Name, "node", System.StringComparison.OrdinalIgnoreCase))
     {
         var node = MapDefinition.ParseNode(reader);
         this.nodes.Add(node.Index, node);
     }
 }
コード例 #3
0
        /// <summary>
        /// Parse a map way.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="map">The map.</param>
        /// <returns>The map way.</returns>
        private MapWay ParseMapWay()
        {
            if (false == string.Equals("way", this.reader.Name, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException($"Excpected a 'way' but got {this.reader.Name}");
            }

            long           id    = 0;
            List <MapNode> nodes = new List <MapNode>();
            List <KeyValuePair <string, string> > tags = new List <KeyValuePair <string, string> >();

            while (reader.MoveToNextAttribute())
            {
                switch (this.reader.Name)
                {
                case "id":
                    id = long.Parse(reader.Value);
                    break;
                }
            }

            while (this.reader.Read())
            {
                switch (this.reader.Name)
                {
                case "way":
                    goto loopexit;

                case "nd":
                {
                    var node = this.ParseNodeRef();
                    nodes.Add(node);
                }
                break;

                case "tag":
                {
                    var tag = MapDefinition.ParseTag(reader);
                    tags.Add(tag);
                }
                break;

                default:
                    break;
                }
            }

loopexit:
            return(new MapWay(id, nodes, tags));
        }
コード例 #4
0
        private IEnumerable <MapShape> TryParseRelation()
        {
            if (false == string.Equals("relation", this.reader.Name, StringComparison.OrdinalIgnoreCase))
            {
                throw new ArgumentException($"Excpected a 'relation' but got {this.reader.Name}");
            }

            long id = 0;

            while (reader.MoveToNextAttribute())
            {
                switch (this.reader.Name)
                {
                case "id":
                    id = long.Parse(reader.Value);
                    break;
                }
            }

            List <MapWay> outer = new List <MapWay>();
            List <MapWay> inner = new List <MapWay>();
            List <KeyValuePair <string, string> > tags = new List <KeyValuePair <string, string> >();

            while (this.reader.Read())
            {
                switch (this.reader.Name)
                {
                case "relation":
                    goto loopexit;

                case "member":
                {
                    if (this.TryParseMember(out string role, out var mapWay))
                    {
                        if (string.Equals("outer", role, StringComparison.OrdinalIgnoreCase))
                        {
                            outer.Add(mapWay);
                        }
                        else if (string.Equals("inner", role, StringComparison.OrdinalIgnoreCase))
                        {
                            inner.Add(mapWay);
                        }
                    }
                }
                break;

                case "tag":
                {
                    var tag = MapDefinition.ParseTag(reader);
                    tags.Add(tag);
                }
                break;

                default:
                    break;
                }
            }

loopexit:

            if (false == outer.Any())
            {
                yield break;
            }

            var outerFlat = MapDefinition.FlattenMapways(outer).ToList();

            foreach (var outerWay in outerFlat)
            {
                yield return(MapShape.Create(outerWay, tags));
            }
        }