Example #1
0
        public IEnumerable <Relation> ReadRelations(string fileName, AttributeRegistry attributeRegistry)
        {
            using (var file = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                BlobHeader blobHeader = null;

                while ((blobHeader = ReadBlobHeader(file)) != null)
                {
                    var block = ReadBlob(file, blobHeader) as PrimitiveBlock;
                    if (block != null)
                    {
                        foreach (PrimitiveGroup group in block.PrimitiveGroup)
                        {
                            if (group.Relations == null)
                            {
                                continue;
                            }

                            foreach (var relation in group.Relations)
                            {
                                var rel = new Relation
                                {
                                    RelationId = relation.ID
                                };
                                long memberRefStore = 0;

                                for (int i = 0; i < relation.MemberIds.Count; i++)
                                {
                                    memberRefStore += relation.MemberIds[i];
                                    string role = block.StringTable[relation.RolesIndexes[i]];

                                    rel.Members.Add(
                                        new Relation.Member
                                    {
                                        Type = attributeRegistry.GetAttributeValueId(OsmAttribute.MemberType, relation.Types[i].ToString()),
                                        Ref  = memberRefStore,
                                        Role = attributeRegistry.GetAttributeValueId(OsmAttribute.MemberRole, role),
                                    });
                                }


                                var usedTagTypes = new HashSet <int>();
                                if (relation.Keys != null)
                                {
                                    for (int i = 0; i < relation.Keys.Count; i++)
                                    {
                                        var tagType = attributeRegistry.GetAttributeValueId(OsmAttribute.TagType, block.StringTable[relation.Keys[i]]);
                                        if (!usedTagTypes.Contains(tagType))
                                        {
                                            rel.Tags.Add(new Tag
                                            {
                                                Value = block.StringTable[relation.Values[i]],
                                                Typ   = tagType
                                            });
                                            usedTagTypes.Add(tagType);
                                        }
                                    }
                                }
                                yield return(rel);
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        private IEnumerable <Node> ProcessNodes(PrimitiveBlock block, PrimitiveGroup group, AttributeRegistry attributeRegistry)
        {
            if (group.Nodes == null)
            {
                yield break;
            }

            foreach (PbfNode node in group.Nodes)
            {
                double lat = 1E-09 * (block.LatOffset + (block.Granularity * node.Latitude));
                double lon = 1E-09 * (block.LonOffset + (block.Granularity * node.Longitude));


                var tags = new List <Tag>();
                if (node.Keys != null)
                {
                    tags.AddRange(node.Keys.Select((t, i) => new Tag()
                    {
                        Typ   = attributeRegistry.GetAttributeValueId(OsmAttribute.TagType, block.StringTable[t]),
                        Value = block.StringTable[node.Values[i]]
                    }));
                }

                yield return(new Node()
                {
                    Latitude = lat,
                    Longitude = lon,
                    NodeId = node.ID,
                    Tags = tags
                });
            }
        }
Example #3
0
        private IEnumerable <Node> ProcessDenseNodes(PrimitiveBlock block, PrimitiveGroup group, AttributeRegistry attributeRegistry)
        {
            if (group.DenseNodes == null)
            {
                yield break;
            }

            long idStore  = 0;
            long latStore = 0;
            long lonStore = 0;

            int keyValueIndex = 0;


            for (int i = 0; i < group.DenseNodes.Id.Count; i++)
            {
                idStore  += group.DenseNodes.Id[i];
                lonStore += group.DenseNodes.Longitude[i];
                latStore += group.DenseNodes.Latitude[i];

                double lat = 1E-09 * (block.LatOffset + (block.Granularity * latStore));
                double lon = 1E-09 * (block.LonOffset + (block.Granularity * lonStore));

                var tags = new List <Tag>();
                if (group.DenseNodes.KeysVals.Count > 0)
                {
                    while (group.DenseNodes.KeysVals[keyValueIndex] != 0)
                    {
                        string key     = block.StringTable[group.DenseNodes.KeysVals[keyValueIndex++]];
                        string value   = block.StringTable[group.DenseNodes.KeysVals[keyValueIndex++]];
                        var    tagType = attributeRegistry.GetAttributeValueId(OsmAttribute.TagType, key);
                        var    tag     = new Tag()
                        {
                            Typ   = tagType,
                            Value = value
                        };
                        tags.Add(tag);
                    }
                    keyValueIndex++;
                }

                var node = new Node()
                {
                    NodeId    = idStore,
                    Latitude  = lat,
                    Longitude = lon,
                    Tags      = tags
                };
                yield return(node);
            }
        }
Example #4
0
        public IEnumerable <Way> ReadWays(string fileName, AttributeRegistry attributeRegistry)
        {
            using (var file = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (var reader = XmlReader.Create(file))
                {
                    reader.MoveToContent();
                    reader.ReadStartElement("osm");

                    bool foundFirstElement = false;
                    while (reader.IsStartElement())
                    {
                        if (reader.Name != "way")
                        {
                            reader.Skip();
                            if (foundFirstElement)
                            {
                                yield break;
                            }
                            continue;
                        }

                        foundFirstElement = true;
                        var wayId = long.Parse(reader.GetAttribute("id"));
                        var way   = new Way()
                        {
                            WayId = wayId
                        };
                        reader.ReadStartElement(); // node
                        reader.Skip();

                        while (reader.Name == "nd")
                        {
                            way.NodeRefs.Add(long.Parse(reader.GetAttribute("ref")));
                            reader.ReadStartElement();
                            reader.Skip();
                        }

                        var usedTagTypes = new HashSet <int>();
                        while (reader.Name == "tag")
                        {
                            var tagType = attributeRegistry.GetAttributeValueId(OsmAttribute.TagType, reader.GetAttribute("k"));

                            if (!usedTagTypes.Contains(tagType))
                            {
                                way.Tags.Add(new Tag
                                {
                                    Value = reader.GetAttribute("v"),
                                    Typ   = tagType
                                });
                                usedTagTypes.Add(tagType);
                            }

                            reader.ReadStartElement();
                            reader.Skip();
                        }

                        if (reader.NodeType == XmlNodeType.EndElement)
                        {
                            reader.ReadEndElement();
                        }
                        yield return(way);
                    }
                }
            }
        }
Example #5
0
        public IEnumerable <Node> ReadNodes(string fileName, AttributeRegistry attributeRegistry)
        {
            using (var file = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (var reader = XmlReader.Create(file))
                {
                    reader.MoveToContent();
                    reader.ReadStartElement("osm");

                    bool foundFirstElement = false;
                    while (reader.IsStartElement())
                    {
                        if (reader.Name != "node")
                        {
                            reader.Skip();
                            if (foundFirstElement)
                            {
                                yield break;
                            }
                            continue;
                        }
                        foundFirstElement = true;
                        var node = new Node()
                        {
                            NodeId    = long.Parse(reader.GetAttribute("id")),
                            Latitude  = double.Parse(reader.GetAttribute("lat"), CultureInfo.InvariantCulture),
                            Longitude = double.Parse(reader.GetAttribute("lon"), CultureInfo.InvariantCulture),
                        };

                        reader.ReadStartElement();
                        reader.Skip();

                        var usedTagTypes = new HashSet <int>();
                        while (reader.Name == "tag")
                        {
                            var key     = reader.GetAttribute("k");
                            var tagType = attributeRegistry.GetAttributeValueId(OsmAttribute.TagType, key);
                            if (!usedTagTypes.Contains(tagType))
                            {
                                node.Tags.Add(new Tag
                                {
                                    Value = reader.GetAttribute("v"),
                                    Typ   = tagType
                                });
                                usedTagTypes.Add(tagType);
                            }

                            reader.ReadStartElement();
                            reader.Skip();
                        }

                        if (reader.NodeType == XmlNodeType.EndElement)
                        {
                            reader.ReadEndElement();
                        }

                        yield return(node);
                    }
                }
            }
        }
Example #6
0
        public IEnumerable <Relation> ReadRelations(string fileName, AttributeRegistry attributeRegistry)
        {
            using (var file = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (var reader = XmlReader.Create(file))
                {
                    reader.MoveToContent();
                    reader.ReadStartElement("osm");

                    bool foundFirstElement = false;
                    while (reader.IsStartElement())
                    {
                        if (reader.Name != "relation")
                        {
                            reader.Skip();
                            if (foundFirstElement)
                            {
                                yield break;
                            }
                            continue;
                        }

                        foundFirstElement = true;
                        var relationId = long.Parse(reader.GetAttribute("id"));
                        var relation   = new Relation()
                        {
                            RelationId = relationId
                        };
                        reader.ReadStartElement(); // node
                        reader.Skip();

                        while (reader.Name == "member")
                        {
                            relation.Members.Add(
                                new Relation.Member
                            {
                                Type = attributeRegistry.GetAttributeValueId(OsmAttribute.MemberType, reader.GetAttribute("type")),
                                Ref  = long.Parse(reader.GetAttribute("ref")),
                                Role = attributeRegistry.GetAttributeValueId(OsmAttribute.MemberRole, reader.GetAttribute("role")),
                            });
                            reader.ReadStartElement();
                            reader.Skip();
                        }

                        var usedTagTypes = new HashSet <int>();
                        while (reader.Name == "tag")
                        {
                            var tagType = attributeRegistry.GetAttributeValueId(OsmAttribute.TagType, reader.GetAttribute("k"));
                            if (!usedTagTypes.Contains(tagType))
                            {
                                relation.Tags.Add(new Tag
                                {
                                    Value = reader.GetAttribute("v"),
                                    Typ   = tagType
                                });
                                usedTagTypes.Add(tagType);
                            }
                            reader.ReadStartElement();
                            reader.Skip();
                        }

                        if (reader.NodeType == XmlNodeType.EndElement)
                        {
                            reader.ReadEndElement();
                        }

                        yield return(relation);
                    }
                }
            }
        }