Ejemplo n.º 1
0
        public bool Contains(Rect2D a)
        {
            if (x > a.x + a.w)
            {
                return(false);
            }
            if (x + w < a.x)
            {
                return(false);
            }
            if (y > a.y + a.h)
            {
                return(false);
            }
            if (y + h < a.y)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public bool Load(string filename)
        {
            XmlDocument osm = new XmlDocument();

            osm.Load(filename);

            XmlElement root = osm.DocumentElement;

            Console.Out.WriteLine("version " + root.Attributes.GetNamedItem("version").InnerText);

            XmlNodeList boundsl = root.GetElementsByTagName("bounds");

            if (boundsl.Count != 1)
            {
                Console.Out.WriteLine("bounds node count " + boundsl.Count);
                return(true);
            }
            else
            {
                double minlat = double.Parse(boundsl[0].Attributes.GetNamedItem("minlat").Value, NumberFormatInfo.InvariantInfo);
                double maxlat = double.Parse(boundsl[0].Attributes.GetNamedItem("maxlat").Value, NumberFormatInfo.InvariantInfo);
                double minlon = double.Parse(boundsl[0].Attributes.GetNamedItem("minlon").Value, NumberFormatInfo.InvariantInfo);
                double maxlon = double.Parse(boundsl[0].Attributes.GetNamedItem("maxlon").Value, NumberFormatInfo.InvariantInfo);

                bounds    = new Rect2D(minlon, minlat, maxlon - minlon, maxlat - minlat);
                centerLon = bounds.x + bounds.w * 0.5;
                centerLat = bounds.y + bounds.h * 0.5;
                b         = Utils.LonLatToXYZ(centerLon, centerLat);

                Console.Out.WriteLine("Bounds: (" + bounds.x + " x " + bounds.y + ") (" + bounds.w + " x " + bounds.h + ")");
            }

            foreach (XmlNode n in root)
            {
                //Console.Out.WriteLine("node name " + n.Name);
                if (n.Name == "node")
                {
                    if (n.Attributes.Count > 0)
                    {
                        long    id    = long.Parse(n.Attributes.GetNamedItem("id").Value);
                        OsmNode tNode = new OsmNode();
                        tNode.id  = id;
                        tNode.lat = double.Parse(n.Attributes.GetNamedItem("lat").Value, NumberFormatInfo.InvariantInfo);
                        tNode.lon = double.Parse(n.Attributes.GetNamedItem("lon").Value, NumberFormatInfo.InvariantInfo);

                        nodes[id] = tNode;
                    }
                }
                else if (n.Name == "way")
                {
                    long        id           = long.Parse(n.Attributes.GetNamedItem("id").Value);
                    bool        building     = false;
                    bool        buildingPart = false;
                    float       height       = 0;
                    float       min_height   = 0;
                    float       ele          = 0;
                    int         levels       = 0;
                    List <long> refIds       = new List <long>();
                    foreach (XmlNode cn in n)
                    {
                        if (cn.Name == "tag")
                        {
                            //<tag k="building" v="yes" />
                            //<tag k="building" v="commercial"/>
                            string k = cn.Attributes.GetNamedItem("k").Value;
                            if (k == "building")
                            {
                                building = true;
                                //cn.Attributes.GetNamedItem("v").Value;
                            }
                            //<tag k="building:part" v="yes"/>
                            else if (k == "building:part")
                            {
                                buildingPart = true;
                            }
                            //<tag k="height" v="160"/>
                            else if (k == "height")
                            {
                                height = float.Parse(cn.Attributes.GetNamedItem("v").Value, NumberFormatInfo.InvariantInfo);
                            }
                            else if (k == "min_height")
                            {
                                min_height = float.Parse(cn.Attributes.GetNamedItem("v").Value, NumberFormatInfo.InvariantInfo);
                            }
                            else if (k == "ele")
                            {
                                ele = float.Parse(cn.Attributes.GetNamedItem("v").Value, NumberFormatInfo.InvariantInfo);
                            }
                            //<tag k="building:levels" v="6"/>
                            //<tag k="building:levels" v="13C"/>
                            //<tag k="building:levels" v="1, 1M, 2M, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25"/>
                            else if (k == "building:levels")
                            {
                                try
                                {
                                    levels = int.Parse(cn.Attributes.GetNamedItem("v").Value);
                                }
                                catch { }
                            }
                            //<tag k="highway" v="secondary"/>
                            //<tag k="highway" v="pedestrian"/>
                            //<tag k="building:colour" v="#C7C2AF"/>
                            //<tag k="building:material" v="glass"/>
                            //<tag k="roof:material" v="concrete"/>
                            //<tag k="roof:shape" v="flat"/>
                        }
                        else if (cn.Name == "nd")
                        {
                            long refId = long.Parse(cn.Attributes.GetNamedItem("ref").Value);
                            refIds.Add(refId);
                        }
                    }
                    if (building || buildingPart)
                    {
                        //Console.Out.WriteLine("building: id " + id + " height " + height + " levels " + levels + " nodes count " + refIds.Count);

                        if (levels == 0)
                        {
                            levels = 1;
                        }
                        if (height == 0)
                        {
                            height = levels * levelHeight;
                        }

                        OsmBuilding bd = new OsmBuilding();
                        bd.id         = id;
                        bd.levels     = levels;
                        bd.height     = height;
                        bd.min_height = min_height;
                        bd.ele        = ele;
                        bd.nodeIds    = refIds.ToArray();
                        bd.part       = buildingPart;
                        buildings.Add(bd);
                    }
                }
                else if (n.Name == "relation")
                {
                    bool        building  = false;
                    List <long> partIds   = new List <long>();
                    long        outlineId = -1;

                    foreach (XmlNode cn in n)
                    {
                        if (cn.Name == "tag")
                        {
                            string k = cn.Attributes.GetNamedItem("k").Value;
                            if (k == "type")
                            {
                                if (cn.Attributes.GetNamedItem("v").Value == "building")
                                {
                                    building = true;
                                }
                            }
                            //else if (k == "name")
                            //{
                            //	string name = cn.Attributes.GetNamedItem("v").Value;
                            //}
                        }
                        else if (cn.Name == "member")
                        {
                            string type = cn.Attributes.GetNamedItem("type").Value;
                            if (type == "way")
                            {
                                string role  = cn.Attributes.GetNamedItem("role").Value;
                                long   refId = long.Parse(cn.Attributes.GetNamedItem("ref").Value);
                                if (role == "outline")
                                {
                                    outlineId = refId;
                                }
                                else if (role == "part")
                                {
                                    partIds.Add(refId);
                                }
                            }
                        }
                    }
                    if (building)
                    {
                        for (int i = 0; i < buildings.Count; i++)
                        {
                            if (buildings[i].id == outlineId)
                            {
                                OsmBuilding b = buildings[i];
                                if (!b.part)
                                {
                                    b.outline = true;
                                    if (b.ele > 0)
                                    {
                                        b.height = b.ele;
                                    }
                                    else
                                    {
                                        b.height = levelHeight;
                                    }
                                    buildings[i] = b;
                                }
                                break;
                            }
                        }
                    }
                }
            }
            Console.Out.WriteLine("Node count " + nodes.Count);
            Console.Out.WriteLine("Building count " + buildings.Count);

            return(false);
        }