/// <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(GISDataStorageTypes eGISStorageType) { Type aType = null; IWorkspaceFactory pWSFact = null; try { switch (eGISStorageType) { case GISDataStorageTypes.RasterFile: aType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory"); break; case GISDataStorageTypes.ShapeFile: aType = Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory"); break; case GISDataStorageTypes.FileGeodatase: aType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"); break; case GISDataStorageTypes.CAD: aType = Type.GetTypeFromProgID("esriDataSourcesFile.CadWorkspaceFactory"); break; case GISDataStorageTypes.PersonalGeodatabase: aType = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory"); break; case 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); }
/// <summary> /// Open a file-based workspace (Raster or ShapeFile) /// </summary> /// <param name="fiFullPath">Workspace directory or file path</param> /// <returns>CALLER IS RESPONSIBLE FOR RELEASING RETURNED COM OBJECT</returns> public static IWorkspace GetWorkspace(FileSystemInfo fiFullPath) { GISDataStorageTypes eType = GetWorkspaceType(fiFullPath.FullName); IWorkspaceFactory pWSFact = GetWorkspaceFactory(eType); DirectoryInfo fiWorkspace = GetWorkspacePath(fiFullPath.FullName); IWorkspace pWS = pWSFact.OpenFromFile(fiWorkspace.FullName, ArcMap.Application.hWnd); // Must release the workspace factory object int refsLeft = 0; do { refsLeft = System.Runtime.InteropServices.Marshal.ReleaseComObject(pWSFact); }while (refsLeft > 0); // CALLER IS RESPONSIBLE FOR RELEASING RETURNED COM OBJECT return(pWS); }
public static ILayer AddToMap(FileSystemInfo fiFullPath, string sLayerName, IGroupLayer pGroupLayer, 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 && string.Compare(pResultLayer.Name, sLayerName, true) == 0) && !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; } GISDataStorageTypes eStorageType = GetWorkspaceType(fiFullPath.FullName); IWorkspace pWorkspace = GetWorkspace(fiFullPath); switch (eStorageType) { case GISDataStorageTypes.RasterFile: IRasterDataset pRDS = ((IRasterWorkspace)pWorkspace).OpenRasterDataset(fiFullPath.Name); IRasterLayer pRLResult = new RasterLayer(); pRLResult.CreateFromDataset(pRDS); pResultLayer = pRLResult; break; case 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 GISDataStorageTypes.ShapeFile: IFeatureWorkspace pWS = (IFeatureWorkspace)ArcMapUtilities.GetWorkspace(fiFullPath); IFeatureClass pShapeFile = pWS.OpenFeatureClass(Path.GetFileNameWithoutExtension(fiFullPath.FullName)); pResultLayer = new FeatureLayer(); ((IFeatureLayer)pResultLayer).FeatureClass = pShapeFile; break; case 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; } ApplySymbology(pResultLayer, fiSymbologyLayerFile); if (pGroupLayer == null) { ((IMapLayers)ArcMap.Document.FocusMap).InsertLayer(pResultLayer, true, 0); } else { ((IMapLayers)ArcMap.Document.FocusMap).InsertLayerInGroup(pGroupLayer, pResultLayer, true, 0); } ArcMap.Document.UpdateContents(); ArcMap.Document.ActiveView.Refresh(); ArcMap.Document.CurrentContentsView.Refresh(null); return(pResultLayer); }