Example #1
0
 private void clearResultMenuItem_Click(object sender, EventArgs e)
 {
     axMapControl1.CurrentTool = null;//将地图空间的当前工具注销
     try
     {
         string name = NetWorkAnalysClass.getPath(path) + "\\data\\HuanbaoGeodatabase.gdb";
         //打开工作空间
         IFeatureWorkspace  pFWorkspace = NetWorkAnalysClass.OpenWorkspace(name) as IFeatureWorkspace;
         IGraphicsContainer pGrap       = this.axMapControl1.ActiveView as IGraphicsContainer;
         pGrap.DeleteAllElements();                                             //删除所添加的图片要素
         IFeatureClass inputFClass   = pFWorkspace.OpenFeatureClass("Stops");   //站点要素
         IFeatureClass barriesFClass = pFWorkspace.OpenFeatureClass("barries"); //障碍点要素
         if (inputFClass.FeatureCount(null) > 0)
         {
             ITable pTable = barriesFClass as ITable;
             pTable.DeleteSearchedRows(null);
         }
         for (int i = 0; i < axMapControl1.LayerCount; i++) //删除分析结果
         {
             ILayer pLayer = axMapControl1.get_Layer(i);
             if (pLayer.Name == ShortPathSolveCommand.m_NAContext.Solver.DisplayName)
             {
                 axMapControl1.DeleteLayer(i);
                 break;
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
     this.axMapControl1.Refresh();
 }
Example #2
0
 public override void OnMouseDown(int Button, int Shift, int X, int Y)
 {
     // TODO:  Add AddNetStopsTool.OnMouseDown implementation
     try
     {
         //根据人为用鼠标在地图上单击的点的坐标新建站点要素
         IPoint pStopsPoint = new PointClass();
         //将鼠标在屏幕上点击的一点的坐标转换为地图上的坐标,并赋给pStopsPoint
         pStopsPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
         IFeature newPointFeature = inputFClass.CreateFeature();//创建新的障碍点要素
         try
         {
             pStopsPoint.Z         = 0;
             newPointFeature.Shape = pStopsPoint;      //将pStopsPoint赋给新要素的Shape属性
             IZAware pZAware = pStopsPoint as IZAware; //Z值为高程
             IMAware pMAware = pStopsPoint as IMAware; //M值是用来储存其他属性信息,如温度、浓度
             pZAware.ZAware = true;
             pMAware.MAware = true;
         }
         catch
         {
             IGeometry pGeo    = pStopsPoint;
             IZAware   pZAware = pGeo as IZAware;
             pZAware.ZAware        = false;
             newPointFeature.Shape = pGeo;
         }
         newPointFeature.Store();//储存新建的站点要素
         //将站点图标添加到图层
         IGraphicsContainer pGrap = m_hookHelper.ActiveView as IGraphicsContainer;
         IColor             pColor;
         IRgbColor          pRgbColor = new RgbColorClass();
         pRgbColor.Red   = 255;
         pRgbColor.Green = 255;
         pRgbColor.Blue  = 255;
         pColor          = pRgbColor as IColor;
         IPictureMarkerSymbol pms = new PictureMarkerSymbolClass();
         pms.BitmapTransparencyColor = pColor;
         string picturePath = NetWorkAnalysClass.getPath(path) + "\\data\\Img\\stops.bmp";
         //添加自定义站点图片
         pms.CreateMarkerSymbolFromFile(esriIPictureType.esriIPictureBitmap, picturePath);
         pms.Size = 18;
         IMarkerElement pMarkerEle = new MarkerElementClass();
         pMarkerEle.Symbol = pms as IMarkerSymbol;
         //将站点位置数据的空间参考设为与地图空间参考一致
         pStopsPoint.SpatialReference = m_hookHelper.ActiveView.FocusMap.SpatialReference;
         IElement pEle = pMarkerEle as IElement;
         pEle.Geometry = pStopsPoint;
         pGrap.AddElement(pEle, 1);
         m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
     }
     catch
     {
         MessageBox.Show("添加站点失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         return;
     }
 }
Example #3
0
        /// <summary>
        /// Occurs when this tool is clicked
        /// </summary>
        public override void OnClick()
        {
            string name = NetWorkAnalysClass.getPath(path) + "\\data\\HuanbaoGeodatabase.gdb";

            //根据路径打开要素工作空间
            pFWorkspace = NetWorkAnalysClass.OpenWorkspace(name) as IFeatureWorkspace;
            //从工作空间里打开“Barries”要素类
            barriesFClass = pFWorkspace.OpenFeatureClass("Barries");
            //判断该要素类里是否含有要素,若有,则删除
            if (barriesFClass.FeatureCount(null) > 0)
            {
                ITable pTable = barriesFClass as ITable;
                pTable.DeleteSearchedRows(null);
            }
        }
Example #4
0
        /// <summary>
        /// Occurs when this tool is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add AddNetStopsTool.OnClick implementation
            string name = NetWorkAnalysClass.getPath(path) + "\\data\\HuanbaoGeodatabase.gdb";

            //根据路径打开要素工作空间
            pFWorkspace = NetWorkAnalysClass.OpenWorkspace(name) as IFeatureWorkspace;
            //从工作空间里打开“Stops”要素类
            inputFClass = pFWorkspace.OpenFeatureClass("Stops");
            //判断该要素类里是否含有要素,若有,则删除
            if (inputFClass.FeatureCount(null) > 0)
            {
                ITable pTable = inputFClass as ITable;
                pTable.DeleteSearchedRows(null);
            }
        }
Example #5
0
        //最短路径分析
        private void routeSolverMenuItem_Click(object sender, EventArgs e)
        {
            ICommand pCommand;

            pCommand = new ShortPathSolveCommand();  //新建自定义的“执行分析”命令
            pCommand.OnCreate(axMapControl1.Object); //创建该命令
            pCommand.OnClick();                      //启动该命令
            pCommand = null;
            #region 道路详细信息
            //获取最短路径的长度
            string        totalLengthOfRoute = "";
            IFeatureLayer shortRouteLayer    = NetWorkAnalysClass.GetLayerByName(
                m_mapControl.Map, "Routes") as IFeatureLayer;
            IFeatureCursor featureCursor = shortRouteLayer.Search(null, false);
            IFeature       featue        = featureCursor.NextFeature();
            for (int i = 0; i < featue.Fields.FieldCount; i++)
            {
                if (featue.Fields.get_Field(i).Name == "Total_Shape_Length")
                {
                    totalLengthOfRoute = featue.get_Value(i).ToString();
                }
            }
            //获取与最短路径有关的道路的名字
            FormQueryBySpatial formQueryBySpatial   = new FormQueryBySpatial();
            ArrayList          containRouteNameList = formQueryBySpatial.selectRouteNameBySpatial(
                esriSpatialRelEnum.esriSpatialRelContains, m_mapControl.Map);
            ArrayList overlapRouteNameList = formQueryBySpatial.selectRouteNameBySpatial(
                esriSpatialRelEnum.esriSpatialRelOverlaps, m_mapControl.Map);
            m_mapControl.Map.ClearSelection();
            string output = "经过道路有:";

            for (int i = 0; i < containRouteNameList.Count; i++)
            {
                output += containRouteNameList[i].ToString() + ",";
            }
            output  = output.Substring(0, output.Length - 1);
            output += "\n" + "站点所在道路为:";
            for (int i = 0; i < overlapRouteNameList.Count; i++)
            {
                output += overlapRouteNameList[i].ToString() + ",";
            }
            output = output.Substring(0, output.Length - 1);
            output = output + "\n" + "路径长度:" + totalLengthOfRoute + "米";
            MessageBox.Show(output);
            #endregion
        }
Example #6
0
        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            string            name        = NetWorkAnalysClass.getPath(path) + "\\data\\HuanbaoGeodatabase.gdb"; //得到数据库文件路径
            IFeatureWorkspace pFWorkspace = NetWorkAnalysClass.OpenWorkspace(name) as IFeatureWorkspace;         //打开要素工作空间

            //"RouteNetwork","BaseData"参数不可更改
            networkDataset = NetWorkAnalysClass.OpenPathNetworkDataset(pFWorkspace as IWorkspace,
                                                                       "RouteNetwork", "BaseData"); //打开网络数据集
            m_NAContext = NetWorkAnalysClass.CreatePathSolverContext(networkDataset);               //通过网络数据集创建网络分析上下文


            inputFClass   = pFWorkspace.OpenFeatureClass("Stops");   //根据名字打开站点要素类
            barriesFClass = pFWorkspace.OpenFeatureClass("Barries"); //根据名字打开障碍点要素类
            if (IFLayerExist("NetworkDataset") == false)             //若不存在NetworkDataset图层就执行
            {
                //创建名为“NetworkDataset”的图层
                ILayer        layer;
                INetworkLayer networkLayer;
                networkLayer = new NetworkLayerClass();
                networkLayer.NetworkDataset = networkDataset;
                layer      = networkLayer as ILayer;
                layer.Name = "NetworkDataset";
                m_hookHelper.ActiveView.FocusMap.AddLayer(layer);
                layer.Visible = false;
            }
            //检查是否存在名为“Route”的图层。若存在则删除
            if (IFLayerExist(m_NAContext.Solver.DisplayName) == true)
            {
                for (int i = 0; i < m_hookHelper.FocusMap.LayerCount; i++)
                {
                    if (m_hookHelper.FocusMap.get_Layer(i).Name == m_NAContext.Solver.DisplayName)
                    {
                        m_hookHelper.FocusMap.DeleteLayer(m_hookHelper.FocusMap.get_Layer(i));
                    }
                }
            }
            INALayer naLayer = m_NAContext.Solver.CreateLayer(m_NAContext);
            ILayer   pLayer  = naLayer as ILayer;

            pLayer.Name = m_NAContext.Solver.DisplayName;
            m_hookHelper.ActiveView.FocusMap.AddLayer(pLayer);//往当前地图中添加该图层


            int featureCount = inputFClass.FeatureCount(null); //得到站点要素类中要素的个数

            if (featureCount < 2)                              //若少于两个则不能进行分析
            {
                MessageBox.Show("只有一个站点,不能进行路径分析!");
                return;
            }
            IGPMessages gpMessages = new GPMessagesClass();//定义一个地理处理结果信息返回对象

            //加载站点要素,并设置容差
            NetWorkAnalysClass.LoadNANetworkLocations("Stops", inputFClass, m_NAContext, 80);
            //加载障碍点要素,并设置容差
            NetWorkAnalysClass.LoadNANetworkLocations("Barriers", barriesFClass, m_NAContext, 50);
            INASolver naSolver = m_NAContext.Solver;//创建网络分析对象

            try
            {
                naSolver.Solve(m_NAContext, gpMessages, null);//执行最短路径分析
            }
            catch (Exception ex)
            {
                MessageBox.Show("未能找到有效路径" + ex.Message, "提示",
                                MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                return;
            }
            //将“Routes”图层组下的“Stops”、“Point Barriers”图层设为不可见
            for (int i = 0; i < m_hookHelper.FocusMap.LayerCount; i++)
            {
                if (m_hookHelper.FocusMap.get_Layer(i).Name == m_NAContext.Solver.DisplayName)
                {
                    //ICompositeLayer Interface Provides access to members that work with a collection of layers that behaves like a single layer.
                    //CompositeLayer为图层组类型
                    ICompositeLayer pCompositeLayer = m_hookHelper.FocusMap.get_Layer(i) as ICompositeLayer;
                    for (int t = 0; t < pCompositeLayer.Count; t++)
                    {
                        ILayer pResultLayer = pCompositeLayer.get_Layer(t);
                        if (pResultLayer.Name == "Stops" || pResultLayer.Name == "Point Barriers")
                        {
                            pResultLayer.Visible = false;
                            continue;
                        }
                    }
                }
            }

            //接下来将地图的视图范围缩放至最短路径的显示范围
            IGeoDataset geoDataset; //地理数据集
            IEnvelope   envelope;   //最小边界矩形

            geoDataset = m_NAContext.NAClasses.get_ItemByName("Routes") as IGeoDataset;
            //The IGeoDataset::Extent property returns an envelope representing the maximum extent of data which has been stored in the dataset.
            envelope = geoDataset.Extent;
            if (!envelope.IsEmpty)
            {
                envelope.Expand(1.1, 1.1, true);
            }
            //将地图的显示的范围设置为“Routes”图层的数据范围
            m_hookHelper.ActiveView.Extent = envelope;
            //刷新视图
            m_hookHelper.ActiveView.Refresh();
            //获取最短路径“Routes”要素
            ITable  table  = m_NAContext.NAClasses.get_ItemByName("Routes") as ITable;
            ICursor cursor = table.Search(null, false);
            IRow    row    = cursor.NextRow();

            for (int i = 0; i < table.Fields.FieldCount; i++)
            {
                if (table.Fields.get_Field(i).AliasName == "Total_Shape_Length")
                {
                    //获取最短路径的总长度
                    total_Length_str = row.get_Value(i).ToString();
                }
            }
        }