⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapservice.cs

📁 This is a document about routing [a spatial analysis - network]
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace MapService
{
    public class MapService
    {
        
        Graphics g;
        double minLon = 360;
        double minLat = 90;
        double maxLon = -360;
        double maxLat = -90;
        double zoom;
        string coordSys;
        Graph graph = new Graph();
        Hashtable roads = new Hashtable();

        public string CoordSys
        {
            get { return coordSys; }
        }
        public double Zoom
        {
            get { return zoom; }
        }
        public int NodeCount
        {
            get { return graph.Nodes.Count; }
        }
        public void Load(string filename)
        {
            graph.Clear();

            string line;            
            StreamReader mifReader = new StreamReader(filename+".MIF");
            StreamReader midReader = new StreamReader(filename + ".MID");

            while ((line = mifReader.ReadLine()) != null)
            {
                if (line.StartsWith("CoordSys"))
                {
                    coordSys = line.Substring(9);
                }               
                else if (line.StartsWith("Pline"))
                {                                        
                    //mid
                    string midLine = midReader.ReadLine().ToUpper();
                    string data = "";
                    bool check=false;
                    for (int i = 0; i < midLine.Length; i++)
                    {
                        if (midLine[i] == '"') check = !check;
                        else
                            if (check & midLine[i] == ',')
                                data += '-';
                            else
                                data += midLine[i];
                    }
                    string[] columns = data.Split(',');
                    int direction = Int32.Parse(columns[12]);
                    Road road;
                    if (roads.ContainsKey(columns[0]))
                        road = (Road)roads[columns[0]];
                    else
                    {
                        road = new Road(columns[0]);
                        roads.Add(columns[0], road);
                    }
                    Segment segment = new Segment(Int32.Parse(columns[1]), Int32.Parse(columns[2]),
                        Int32.Parse(columns[3]), Int32.Parse(columns[4]));
                    road.Segments.Add(segment);
                    segment.Len = 0;
                    //mif
                    Node u = null;
                    Node v;
                    int len = Int32.Parse(line.Substring(6));
                    for (int i = 0; i < len; i++)
                    {
                        line = mifReader.ReadLine();
                        double lon = Double.Parse(line.Substring(0, line.IndexOf(' ')));
                        double lat = Double.Parse(line.Substring(line.IndexOf(' ') + 1));
                        if (lon < minLon) minLon = lon;
                        if (lat < minLat) minLat = lat;
                        if (lon > maxLon) maxLon = lon;
                        if (lat > maxLat) maxLat = lat;

                        string key = String.Format("{0:n6},{1:n6}", lon, lat);
                        if (!graph.Contains(key))
                        {
                            v = new Node(key, lon, lat);
                            graph.AddNode(v);
                        }
                        else
                            v = graph.Nodes[key];
                        segment.Nodes.Add(v);
                        if (u != null)
                        {
                            switch (direction)
                            {
                                case 0:
                                    graph.AddEdge(u, v, true);
                                    break;
                                case 1:
                                    graph.AddEdge(u, v, false);
                                    break;
                                case -1:
                                    graph.AddEdge(v, u, false);
                                    break;
                            }
                            segment.Len += Common.Len(u, v);
                        }
                        u = v;
                    }
                }
            }
            mifReader.Close();
            midReader.Close();
        }

        #region map
        public void Draw(Graphics g,int w, int h)
        {
            this.g = g;
            zoom = Math.Min(w / (maxLon - minLon), h / (maxLat - minLat));
            g.Clear(Color.White);
            Pen pen1 = new Pen(Color.Black);
            Pen pen2 = new Pen(Color.Gray);

            foreach (Node n1 in graph.Nodes)
            {
                foreach (EdgeToNeighbor edge in n1.Neighbors)
                {
                    if(edge.IsTwoWay)
                        Draw(edge.Source, edge.Neighbor,pen1);
                    else
                        Draw(edge.Source, edge.Neighbor, pen2);
                }
            }
        }

        private void Draw(Node p, Pen pen)
        {
            g.DrawRectangle(pen,(int)(zoom * (p.Lon - minLon)) - 2, 
                (int)(zoom * (maxLat - p.Lat)) - 2, 4, 4);
        }
        private void Draw(Node p1, Node p2, Pen pen)
        {
            g.DrawLine(pen, (int)(zoom * (p1.Lon - minLon)),
                (int)(zoom * (maxLat - p1.Lat)),
                (int)(zoom * (p2.Lon - minLon)),
                (int)(zoom * (maxLat - p2.Lat)));
        }
        public Node XY2LonLat(int x, int y)
        {
            double lon = x / zoom + minLon;
            double lat = maxLat - y / zoom;
            return new Node(String.Format("{0:n6},{1:n6}", lon, lat), lon, lat);
        }
        public Node SnapNode(Node p)
        {
            if(graph.Contains(p)) 
            {
                return p;
            }
            else
            {
                Node t = null;
                int len = Int32.MaxValue;
                foreach (Node n in graph.Nodes)
                {
                    int l=Common.Len(p,n);
                    if (l < len)
                    {
                        t = n;
                        len = l;
                    }
                }
                return t;
            }
        }
        public Node SnapEdge(Node p)
        {
            if (graph.Contains(p))
            {
                return p;
            }
            else
            {
                Node t = null;
                int len = Int32.MaxValue;
                foreach (Node n1 in graph.Nodes)
                    foreach (EdgeToNeighbor egde in n1.Neighbors)
                    {
                        Node n2 = egde.Neighbor;
                        int l1 = Common.Len(p,n1);                        
                        int l2 = Common.Len(p, n2);
                        Node n3 = Common.GetNode(n1, n2, l1, l2);
                        if (graph.Contains(n3.Key))
                            n3 = graph.Nodes[n3.Key];
                        else
                            n3.Data = egde;
                        int l3 = Common.Len(p, n3);
                        if (l1 < len)
                        {
                            t = n1; 
                            len = l1;
                        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -