Пример #1
0
        /// <summary>
        /// Create a singleton for a workspace factory
        /// </summary>
        /// <param name="eGISStorageType"></param>
        /// <returns>CALLER IS RESPONSIBLE FOR RELEASING RETURNED COM OBJECT</returns>
        /// <remarks>This is the only correct method for creating a workspace factory. Do not call "New" to create this singleton classes.
        /// http://forums.esri.com/Thread.asp?c=93&f=993&t=178686
        /// </remarks>
        private static IWorkspaceFactory GetWorkspaceFactory(ArcMapBrowse.GISDataStorageTypes eGISStorageType)
        {
            Type aType = null;
            IWorkspaceFactory pWSFact = null;

            try
            {
                switch (eGISStorageType)
                {
                case ArcMapBrowse.GISDataStorageTypes.RasterFile:
                    aType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory");
                    break;

                case ArcMapBrowse.GISDataStorageTypes.ShapeFile:
                    aType = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
                    break;

                case ArcMapBrowse.GISDataStorageTypes.FileGeodatase:
                    aType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");
                    break;

                case ArcMapBrowse.GISDataStorageTypes.CAD:
                    aType = Type.GetTypeFromProgID("esriDataSourcesFile.CadWorkspaceFactory");
                    break;

                case ArcMapBrowse.GISDataStorageTypes.PersonalGeodatabase:
                    aType = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory");
                    break;

                case ArcMapBrowse.GISDataStorageTypes.TIN:
                    aType = Type.GetTypeFromProgID("esriDataSourcesFile.TinWorkspaceFactory");
                    break;

                default:
                    throw new Exception("Unhandled GIS storage type");
                }

                pWSFact = (IWorkspaceFactory)Activator.CreateInstance(aType);
            }
            catch (Exception ex)
            {
                Exception ex2 = new Exception("Error getting workspace factory", ex);
                ex2.Data["Workspace Type"] = eGISStorageType.ToString();
                throw ex2;
            }

            // CALLER IS RESPONSIBLE FOR RELEASING RETURNED COM OBJECT
            return(pWSFact);
        }
Пример #2
0
        public static ILayer AddToMap(FileSystemInfo fiFullPath, string sLayerName = "", string sGroupLayer = "", FileInfo fiSymbologyLayerFile = null, bool bAddToMapIfPresent = false)
        {
            if (!fiFullPath.Exists)
            {
                return(null);
            }

            // Only add if it doesn't exist already
            ILayer pResultLayer = GetLayerBySource(fiFullPath);

            if (pResultLayer is ILayer && !bAddToMapIfPresent)
            {
                return(pResultLayer);
            }

            // Confirm that the symbology layer file exists
            if (fiSymbologyLayerFile != null && !fiSymbologyLayerFile.Exists)
            {
                Exception ex = new Exception("A symbology layer file was provided, but the file does not exist");
                ex.Data["Data Source"] = fiFullPath.FullName;
                ex.Data["Layer file"]  = fiSymbologyLayerFile.FullName;
                throw ex;
            }

            ArcMapBrowse.GISDataStorageTypes eStorageType = GetWorkspaceType(fiFullPath.FullName);
            IWorkspace pWorkspace = GetWorkspace(fiFullPath);

            switch (eStorageType)
            {
            case ArcMapBrowse.GISDataStorageTypes.RasterFile:
                IRasterDataset pRDS      = ((IRasterWorkspace)pWorkspace).OpenRasterDataset(fiFullPath.Name);
                IRasterLayer   pRLResult = new RasterLayer();
                pRLResult.CreateFromDataset(pRDS);
                pResultLayer = pRLResult;
                break;

            case ArcMapBrowse.GISDataStorageTypes.CAD:
                string        sFile = Path.GetFileName(Path.GetDirectoryName(fiFullPath.FullName));
                string        sFC   = sFile + ":" + Path.GetFileName(fiFullPath.FullName);
                IFeatureClass pFC   = ((IFeatureWorkspace)pWorkspace).OpenFeatureClass(sFC);
                pResultLayer = new FeatureLayer();
                ((IFeatureLayer)pResultLayer).FeatureClass = pFC;
                break;

            case ArcMapBrowse.GISDataStorageTypes.ShapeFile:
                IFeatureClass pShapeFile = ((IFeatureWorkspace)pWorkspace).OpenFeatureClass(fiFullPath.FullName);
                pResultLayer = new FeatureLayer();
                ((IFeatureLayer)pResultLayer).FeatureClass = pShapeFile;
                break;

            case ArcMapBrowse.GISDataStorageTypes.TIN:
                ITin pTIN = ((ITinWorkspace)pWorkspace).OpenTin(fiFullPath.FullName);
                pResultLayer = new TinLayer();
                ((ITinLayer)pResultLayer).Dataset = pTIN;
                pResultLayer.Name = fiFullPath.Name;
                break;

            default:
                Exception ex = new Exception("Unhandled GIS dataset type");
                ex.Data["FullPath Path"] = fiFullPath.FullName;
                ex.Data["Storage Type"]  = eStorageType.ToString();
                throw ex;
            }

            if (!string.IsNullOrEmpty(sLayerName))
            {
                pResultLayer.Name = sLayerName;
            }

            if (string.IsNullOrEmpty(sGroupLayer))
            {
                ((IMapLayers)ArcMap.Document.FocusMap).InsertLayer(pResultLayer, true, 0);
            }
            else
            {
                IGroupLayer pGrpLayer = GetGroupLayer(sGroupLayer);
                ((IMapLayers)ArcMap.Document.FocusMap).InsertLayerInGroup(pGrpLayer, pResultLayer, true, 0);
            }

            ArcMap.Document.UpdateContents();
            ArcMap.Document.ActiveView.Refresh();
            ArcMap.Document.CurrentContentsView.Refresh(null);

            return(pResultLayer);
        }