public static List <Road> GetRoads(OsmFile file)
    {
        List <Road> roads = new List <Road>();

        foreach (OsmWay way in file.GetWays())
        {
            List <OsmNode> nodes  = way.GetNodes();
            Vector2[]      points = new Vector2[nodes.Count];

            for (int i = 0; i < nodes.Count; i++)
            {
                points[i] = nodes[i].Coordinate;
            }

            int   lanes = 1;
            float width = 0;

            Road.RoadType roadType = Road.RoadType.Default;

            string lanesStr    = way.GetTagValue("lanes");
            string widthStr    = way.GetTagValue("width");
            string highwaysStr = way.GetTagValue("highway");

            if (lanesStr != null)
            {
                lanes = Convert.ToInt32(lanesStr);
            }

            if (widthStr != null)
            {
                width = Convert.ToSingle(widthStr, CultureInfoHelper.EnUSInfo);
            }

            if (highwaysStr != null)
            {
                roadType = Road.GetRoadType(highwaysStr);
            }

            if (roadType != Road.RoadType.Default)
            {
                roads.Add(new Road(lanes, width, points, roadType));
            }
        }

        return(roads);
    }
예제 #2
0
파일: Program.cs 프로젝트: Levrum/Levrum
        public static string loadosm(List <string> args)
        {
            if (args.Count == 0)
            {
                return("Usage: loadosm <filename>");
            }

            FileInfo file = new FileInfo(args[0]);

            if (!file.Exists)
            {
                return(string.Format("File {0} does not exist!", args[0]));
            }

            DateTime loadStart = DateTime.Now;

            OsmFile = new OsmFile(file);
            OsmFile.Load(true, true);
            DateTime loadEnd = DateTime.Now;

            return(string.Format("Loaded {0} nodes, {1} ways, and {2} relations from {3} in {4} seconds.", OsmFile.Nodes.Count, OsmFile.Ways.Count, OsmFile.Relations.Count, file.Name, (loadEnd - loadStart).TotalSeconds));
        }
예제 #3
0
        public OsmFile Parse(string xmlFileSource)
        {
            var osm = new OsmFile
            {
                Nodes  = new List <OsmNode>(),
                Ways   = new List <OsmWay>(),
                Bounds = new OsmBounds()
            };
            var doc = new XmlDocument();

            doc.LoadXml(xmlFileSource);
            var root = doc.DocumentElement;

            ApplyAttributes(root.Attributes, osm);

            foreach (XmlElement xmlNode in root)
            {
                ParseChild(xmlNode, osm);
            }

            return(osm);
        }
    public static List <Area> GetAreas(OsmFile file)
    {
        List <Area> areas = new List <Area>();

        foreach (OsmWay way in file.GetWays())
        {
            List <OsmNode> nodes  = way.GetNodes();
            Vector2[]      points = new Vector2[nodes.Count];

            for (int i = 0; i < nodes.Count; i++)
            {
                points[i] = nodes[i].Coordinate;
            }

            Area.AreaType areaType = Area.AreaType.Default;

            string landuseStr = way.GetTagValue("landuse");
            string leisureStr = way.GetTagValue("leisure");

            if (landuseStr != null)
            {
                areaType = Area.GetAreaType(landuseStr);
            }

            if (leisureStr != null)
            {
                areaType = Area.GetAreaType(leisureStr);
            }

            if (areaType != Area.AreaType.Default)
            {
                areas.Add(new Area(points, areaType));
            }
        }

        return(areas);
    }
예제 #5
0
 public OsmBuilder(OsmFile osm)
 {
     Osm = osm;
 }
예제 #6
0
        private object ParseChild(XmlElement xmlNode, OsmFile osm)
        {
            var name   = xmlNode.Name;
            var attrs  = xmlNode.Attributes;
            var result = default(object);

            if (name == "bounds")
            {
                var bounds = new OsmBounds();
                ApplyAttributes(attrs, bounds);
                osm.Bounds = bounds;

                result = bounds;
            }
            else if (name == "node")
            {
                var node = new OsmNode();
                ApplyAttributes(attrs, node);
                osm.Nodes.Add(node);;

                foreach (XmlElement xmlNodeChildNode in xmlNode.ChildNodes)
                {
                    var childElement = ParseChild(xmlNodeChildNode, osm);

                    if (childElement.GetType() == typeof(OsmTag))
                    {
                        node.Tags.Add((OsmTag)childElement);
                    }
                }

                result = node;
            }
            else if (name == "tag")
            {
                var tag = new OsmTag();
                ApplyAttributes(attrs, tag);

                result = tag;
            }
            else if (name == "way")
            {
                var way = new OsmWay();
                ApplyAttributes(attrs, way);
                osm.Ways.Add(way);

                foreach (XmlElement xmlNodeChildNode in xmlNode.ChildNodes)
                {
                    var childElement = ParseChild(xmlNodeChildNode, osm);

                    if (childElement.GetType() == typeof(OsmTag))
                    {
                        way.Tags.Add((OsmTag)childElement);
                    }
                    else if (childElement.GetType() == typeof(OsmNd))
                    {
                        way.Nds.Add((OsmNd)childElement);
                    }
                }

                result = way;
            }
            else if (name == "nd")
            {
                var nd = new OsmNd();
                ApplyAttributes(attrs, nd);

                result = nd;
            }

            return(result);
        }
 protected override void ExecuteThread()
 {
     OsmFile = new OsmFile(response);
     Roads   = OsmFileParser.GetRoads(OsmFile);
     Areas   = OsmFileParser.GetAreas(OsmFile);
 }