Beispiel #1
0
        public IWorkspace CreateOrOpenLoaclGeoDataBase(string path, EnumLoaclGeoDatabaseType type)
        {
            try
            {
                IWorkspaceFactory pTargetWsf = null;
                switch (type)
                {
                case EnumLoaclGeoDatabaseType.MDB:
                    pTargetWsf = new AccessWorkspaceFactory();
                    break;

                case EnumLoaclGeoDatabaseType.GDB:
                    pTargetWsf = new FileGDBWorkspaceFactory();
                    break;

                case EnumLoaclGeoDatabaseType.SHP:
                    pTargetWsf = new ShapefileWorkspaceFactory();
                    break;
                }
                if (System.IO.Directory.Exists(path) || System.IO.File.Exists(path))
                {
                    if (pTargetWsf.IsWorkspace(path))
                    {
                        return(pTargetWsf.OpenFromFile(path, 0));
                    }
                    else
                    {
                        ErrorMessage = "当前工作空间已经损坏";
                        return(null);
                    }
                }
                else
                {
                    string         sPath          = System.IO.Path.GetDirectoryName(path);
                    string         sName          = System.IO.Path.GetFileNameWithoutExtension(path);
                    IWorkspaceName pWorkspaceName = pTargetWsf.Create(sPath, sName, null, 0);
                    return(pTargetWsf.Open(pWorkspaceName.ConnectionProperties, 0));
                }
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message;
                return(null);
            }
        }
Beispiel #2
0
 /// <summary>
 /// 加载Shape文件
 /// </summary>
 /// <param name="axMapControl">地图控件引用</param>
 /// <param name="strFilePath">文件路径</param>
 /// <returns>正常:“”,异常:异常字符;</returns>
 private SystemErrorType LoadShapeFile(ref AxMapControl axMapControl, string strFilePath)
 {
     if (strFilePath == "")
     {
         return(SystemErrorType.enumFilePathIsNull);
     }
     try
     {
         IWorkspaceFactory pWorkSpaceFactory = null;
         IFeatureWorkspace pFeatureWorkspace = null;
         IFeatureClass     pFeatureClass     = null;
         IFeatureLayer     pFeatureLayer     = null;
         String            WorkSpacePath     = strFilePath.Substring(0, strFilePath.LastIndexOf('\\'));  //取文件夹路径
         string            Layers            = strFilePath.Substring(strFilePath.LastIndexOf('\\') + 1); //取文件名
         //创建shape文件workspace,加载shape文件到地图控件
         pWorkSpaceFactory = new ShapefileWorkspaceFactory()  as IWorkspaceFactory;
         if (pWorkSpaceFactory.IsWorkspace(WorkSpacePath))
         {
             pFeatureWorkspace          = pWorkSpaceFactory.OpenFromFile(WorkSpacePath, 0) as IFeatureWorkspace;
             pFeatureClass              = pFeatureWorkspace.OpenFeatureClass(Layers);
             pFeatureLayer              = new FeatureLayer() as IFeatureLayer;
             pFeatureLayer.Name         = Layers.Substring(0, Layers.Length - 4);
             pFeatureLayer.FeatureClass = pFeatureClass;
             axMapControl.ClearLayers();
             axMapControl.AddLayer(pFeatureLayer);
             axMapControl.ActiveView.Refresh();
         }
         else
         {
             return(SystemErrorType.enumDataIsIllegal);
         }
     }
     catch (Exception)
     {
         return(SystemErrorType.enumArcObjectHandleError);
     }
     return(SystemErrorType.enumOK);
 }
Beispiel #3
0
        //// 加载选中图层
        ////blnAddData是否加载数据到Map

        public static bool AddSelectedLayer(ref IBasicMap pMap, string pSuffix, List <IFeatureLayer> pListCol, List <IDataset> m_DatasetCol, string pCurrentFilePath, bool blnAddData, int intPyrmd)
        {
            //intPyrmd=0;

            bool functionReturnValue = false;

            functionReturnValue = false;
            //WHFErrorHandle.clsErrorHandle pfrmError = new WHFErrorHandle.clsErrorHandle();
            System.Windows.Forms.Cursor pCursor = Cursors.Default;
            int i = 0;

            //Dim pFileName As IFileName
            IWorkspaceFactory pWorkspaceFactory = default(IWorkspaceFactory);
            //Dim pRasterWorkspace As IRasterWorkspace2
            //Dim pRasterDataset As IRasterDataset
            //Dim pRasterLayer As IRasterLayer

            IFeatureWorkspace pFeatWorkspace = default(IFeatureWorkspace);
            //Dim pFeatDS As IFeatureDataset
            IDataset pDataset = default(IDataset);
            //Dim pFeatCls As IFeatureClass
            IFeatureLayer pFeatLayer = default(IFeatureLayer);
            //Dim pLayer As ILayer
            ////以下三个变量是用来获得工作区的路径和文件的名称
            //Dim sPath() As String
            string sWorkSpacePath = pCurrentFilePath;
            string sFileName      = null;

            if (pListCol.Count == 0)
            {
                return(functionReturnValue);
            }

            //要进行空间参考的比较
            blnEditMapSpatial = true;
            try
            {
                switch (pSuffix)
                {
                case ".SHP":
                    //shp文件的处理
                    //获得工作区,并通过shp文件的名称打开文件
                    pWorkspaceFactory = new ShapefileWorkspaceFactory();
                    if (pWorkspaceFactory.IsWorkspace(sWorkSpacePath))
                    {
                        pFeatWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(sWorkSpacePath, 0);
                        for (i = 1; i <= pListCol.Count; i++)
                        {
                            sFileName = (string)pListCol[i].Name;
                            pDataset  = (IDataset)pFeatWorkspace.OpenFeatureClass(sFileName);
                            m_DatasetCol.Add(pDataset);
                            pFeatLayer = new FeatureLayer();
                            //设置图层的名字和要素类
                            pFeatLayer.FeatureClass = (IFeatureClass)pDataset;
                            pFeatLayer.Name         = sFileName.Substring(0, sFileName.Length - 4);
                            //pFeatLayer.Name = Strings.Left(sFileName, Strings.Len(sFileName) - 4);
                        }
                        functionReturnValue = true;
                    }
                    break;
                //Case ".IMG", ".TIF", ".BMP", ".JPG"
                //    pWorkspaceFactory = New RasterWorkspaceFactory
                //    '创建并添加栅格图层
                //    If pWorkspaceFactory.IsWorkspace(sWorkSpacePath) Then
                //        pRasterWorkspace = pWorkspaceFactory.OpenFromFile(sWorkSpacePath, 0)
                //        Dim pRasPyrmd As IRasterPyramid
                //        Dim intRslt As Integer
                //        Dim intSameDo As Integer
                //        For i = 1 To pListCol.Count
                //            sFileName = pListCol.Item(i)
                //            pRasterDataset = pRasterWorkspace.OpenRasterDataset(sFileName)
                //            m_DatasetCol.Add(pRasterDataset)
                //            '//判断栅格金字塔是否存在,询问是否构建
                //            pRasPyrmd = pRasterDataset

                //            '//如果金字塔存在则直接添加到地图,如果不存在则提示
                //            If Not pRasPyrmd.Present Then
                //                Select Case intPyrmd
                //                    Case 0
                //                        '//intSamedo决定是否选择了"采用相同设置".0代表否,1代表是
                //                        '//intResult 表示用户点击的按钮0,1,2分别代表"否","是","退出"
                //                        intRslt = pfrmError.DisplayInformationEx("添加的栅格数据没有构建金字塔,是否构建?", sFileName & "金字塔构建", "采用相同设置,不再询问", 4, intSameDo, "是", "否", "退出")

                //                        '// 0则不创建,不做任何处理
                //                        If intRslt = 1 Then      '//创建
                //                            Windows.Forms.Cursor.Current = Cursors.WaitCursor
                //                            DoEvents()
                //                            pRasPyrmd.Create()
                //                            If intSameDo = 1 Then
                //                                intPyrmd = 1
                //                            End If
                //                            Windows.Forms.Cursor.Current = Cursors.Default
                //                        ElseIf intRslt = 0 Then
                //                            '//接下来的图幅不创建金字塔,直接加载到地图,设置intPyrmd为3不做判断
                //                            If intSameDo = 1 Then
                //                                intPyrmd = 3
                //                            End If
                //                        End If
                //                    Case 1
                //                        Windows.Forms.Cursor.Current = Cursors.WaitCursor
                //                        DoEvents()
                //                        pRasPyrmd.Create()
                //                        Windows.Forms.Cursor.Current = Cursors.Default
                //                    Case 2      '//退出创建,不加载该栅格数据
                //                        If intSameDo = 1 Then
                //                            intPyrmd = 2
                //                            Exit Function
                //                        End If
                //                End Select
                //            End If
                //            pRasterLayer = New RasterLayer
                //            pRasterLayer.CreateFromDataset(pRasterDataset)
                //        Next
                //        AddSelectedLayer = True
                //    End If

                case ".DWG":
                case ".DXF":
                case ".DGN":
                    functionReturnValue = AddCADLayer(ref pMap, pCurrentFilePath, pListCol, blnAddData);
                    break;

                case ".MXD":
                    string       strMxdName = null;
                    IMapDocument pMapDoc    = default(IMapDocument);
                    IMap         pTmpMap    = default(IMap);

                    sFileName  = (string)pListCol[1].Name;
                    strMxdName = sWorkSpacePath + "\\" + sFileName;
                    pMapDoc    = new MapDocument();
                    pMapDoc.Open(strMxdName, null);
                    pTmpMap = pMapDoc.get_Map(0);

                    break;

                case "IDatasetName":

                    IDatasetName pDSN = default(IDatasetName);
                    for (i = 1; i <= pListCol.Count; i++)
                    {
                        pDSN.Name           = pListCol[i].Name;
                        functionReturnValue = AddDataset(ref pMap, pDSN, m_DatasetCol, blnAddData);
                    }

                    break;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }
            return(functionReturnValue);
        }
Beispiel #4
0
        /// <summary>
        /// 添加选中的图层
        /// </summary>
        /// <param name="pMap"></param>
        /// <param name="pSuffix"></param>
        /// <param name="pListCol"></param>
        /// <param name="m_DatasetCol"></param>
        /// <param name="pCurrentFilePath"></param>
        /// <param name="blnAddData"></param>
        /// <returns></returns>
        public static bool AddSelectedLayer(IBasicMap pMap, string pSuffix, Collection <object> pListCol, Collection <object> m_DatasetCol, string pCurrentFilePath, bool blnAddData)
        {
            bool functionReturnValue = false;

            System.Windows.Forms.Cursor pCursor = Cursors.Default;

            //Dim pFileName As IFileName
            IWorkspaceFactory pWorkspaceFactory = null;

            IFeatureWorkspace pFeatWorkspace = null;
            //Dim pFeatDS As IFeatureDataset
            IDataset pDataset = null;
            //Dim pFeatCls As IFeatureClass
            IFeatureLayer pFeatLayer = null;
            //Dim pLayer As ILayer
            ////以下三个变量是用来获得工作区的路径和文件的名称
            //Dim sPath() As String
            string sWorkSpacePath = pCurrentFilePath;
            string sFileName      = null;

            if (pListCol.Count == 0)
            {
                return(false);
            }

            //要进行空间参考的比较
            blnEditMapSpatial = true;
            try
            {
                switch (pSuffix)
                {
                case ".shp":
                {
                    //shp文件的处理
                    //获得工作区,并通过shp文件的名称打开文件
                    pWorkspaceFactory = new ShapefileWorkspaceFactory() as IWorkspaceFactory;
                    if (pWorkspaceFactory.IsWorkspace(sWorkSpacePath))
                    {
                        pFeatWorkspace = pWorkspaceFactory.OpenFromFile(sWorkSpacePath, 0) as IFeatureWorkspace;
                        for (int i = 0; i < pListCol.Count; i++)
                        {
                            sFileName = pListCol[i].ToString();
                            pDataset  = pFeatWorkspace.OpenFeatureClass(sFileName) as IDataset;
                            m_DatasetCol.Add(pDataset);
                            pFeatLayer = new FeatureLayer();
                            //设置图层的名字和要素类
                            pFeatLayer.FeatureClass = pDataset as IFeatureClass;
                            pFeatLayer.Name         = sFileName.Substring(0, sFileName.Length - 4);
                            if (blnAddData == true)
                            {
                                //pMap.AddLayer pFeatLayer
                                ClsMapLayer.AddLyrToBasicMap(pMap, pFeatLayer);
                                ClsMapLayer.SortLayer(pMap, pFeatLayer);
                            }
                            //pSelectedCln.Add(pFeatLayer)
                        }
                        functionReturnValue = true;
                    }

                    break;
                }

                case "IDatasetName":

                    IDatasetName pDSN = null;
                    for (int i = 0; i < pListCol.Count; i++)
                    {
                        pDSN = pListCol[i] as IDatasetName;
                        functionReturnValue = ClsMapLayer.AddDataset(pMap, pDSN, m_DatasetCol, blnAddData);
                    }

                    break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return(functionReturnValue);
        }