arcengine镶嵌数据集的基本操作(C#)

1.创建新的镶嵌数据集。

/// /// 创建镶嵌数据集 /// /// 工作空间 /// 名称 /// 空间参考 /// 镶嵌数据集 public static IMosaicDataset CreateMosaicDataset(IWorkspace pFgdbWorkspace, string pMDame, ISpatialReference pSrs) { try { IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactory(); ICreateMosaicDatasetParameters pCreationPars = new CreateMosaicDatasetParametersClass(); pCreationPars.BandCount = 3; pCreationPars.PixelType = rstPixelType.PT_UCHAR; IMosaicWorkspaceExtensionHelper pMosaicExentionHelper = new MosaicWorkspaceExtensionHelperClass(); IMosaicWorkspaceExtension pMosaicExtention = pMosaicExentionHelper.FindExtension(pFgdbWorkspace); return pMosaicExtention.CreateMosaicDataset(pMDame, pSrs, pCreationPars, "DOM"); } catch (Exception) { return null; } }

2.获取已有镶嵌数据集
/// /// 获取镶嵌数据集 /// /// 数据集名称 /// 数据集所在工作空间 /// 镶嵌数据集 public static IMosaicDataset GetMosaicDataset(string MosaicName, IWorkspace workspace) { IMosaicDataset pMosicDataset = null; IMosaicWorkspaceExtensionHelper pMosaicWsExHelper = new MosaicWorkspaceExtensionHelperClass(); IMosaicWorkspaceExtension pMosaicWsExt = pMosaicWsExHelper.FindExtension(workspace); if (pMosaicWsExt != null) { try { pMosicDataset = pMosaicWsExt.OpenMosaicDataset(MosaicName); } catch (Exception ex) { return pMosicDataset; } } return pMosicDataset; }

3.获取已有镶嵌数据表
/// /// 获取镶嵌数据表 /// /// 镶嵌数据集 /// 镶嵌数据表 public static ITable GetMosaicDatasetTable(IMosaicDataset pMosaicDataset) { ITable pTable = null; IEnumName pEnumName = pMosaicDataset.Children; pEnumName.Reset(); ESRI.ArcGIS.esriSystem.IName pName; while ((pName = pEnumName.Next()) != null) { pTable = pName.Open() as ITable; int i = pTable.Fields.FieldCount; if (i >= 21) break; } return pTable; }

4.删除某个镶嵌数据集
/// /// 删除镶嵌数据集 /// /// 名称 /// 工作空间 /// 删除成功时返回true,否则返回false public static bool DeleteMosaic(string rasterName, IWorkspace workspace) { try { IMosaicWorkspaceExtensionHelper pMosaicWsExHelper = new MosaicWorkspaceExtensionHelperClass(); IMosaicWorkspaceExtension pMosaicWsExt = pMosaicWsExHelper.FindExtension(workspace); pMosaicWsExt.DeleteMosaicDataset(rasterName); return true; } catch (Exception ex) { return false; } }

5.在镶嵌数据集中添加删格数据
/// /// 导入栅格数据 /// /// 导入文件路径 /// 【arcengine镶嵌数据集的基本操作(C#)】镶嵌数据集 public static bool ImportRasterToMosaic(string filePath, IMosaicDataset mosaicDataSet) { try { IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); IRasterWorkspace rasterWorkspace = workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(filePath), 0) as IRasterWorkspace; IMosaicDatasetOperation mOp = (IMosaicDatasetOperation)mosaicDataSet; IAddRastersParameters addRs = new AddRastersParametersClass(); IRasterDatasetCrawler rsDsetCrawl = new RasterDatasetCrawlerClass(); rsDsetCrawl.RasterDataset = rasterWorkspace.OpenRasterDataset(System.IO.Path.GetFileName(filePath)); IRasterTypeFactory rsFact = new RasterTypeFactoryClass(); IRasterType rsType = rsFact.CreateRasterType("Raster dataset"); rsType.FullName = rsDsetCrawl.DatasetName; addRs.Crawler = (IDataSourceCrawler)rsDsetCrawl; addRs.RasterType = rsType; mOp.AddRasters(addRs, null); //计算cellSize 和边界 // Create a calculate cellsize ranges parameters object. ICalculateCellSizeRangesParameters computeArgs = new CalculateCellSizeRangesParametersClass(); // Use the mosaic dataset operation interface to calculate cellsize ranges. mOp.CalculateCellSizeRanges(computeArgs, null); // Create a build boundary parameters object. IBuildBoundaryParameters boundaryArgs = new BuildBoundaryParametersClass(); // Set flags that control boundary generation. boundaryArgs.AppendToExistingBoundary = true; // Use the mosaic dataset operation interface to build boundary. mOp.BuildBoundary(boundaryArgs, null); return true; } catch (Exception) {return false; } }

6.导出镶嵌数据集
/// /// 导出镶嵌数据集为删格数据 /// /// 数据名称 /// 工作空间 /// 保存路径 /// 成功返回true,失败返回false public static bool DownLoadMosaic( string RasterName,IWorkspace workspaceDB,string DownLoadLocation) { try { IWorkspace wsGDB = null; IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass(); //判断是GDB文件还是普通文件夹 string locationForm =DownLoadLocation.Substring(DownLoadLocation.Length - 4, 4).ToUpper(); if (locationForm == ".GDB") { wsGDB = workspaceFactory.OpenFromFile(@"" + DownLoadLocation, 0); } else { IRasterWorkspace rasterWorkspace = SetRasterWorkspace(DownLoadLocation); wsGDB = (IWorkspace)rasterWorkspace; } IMosaicWorkspaceExtensionHelper mosaicHelper = newMosaicWorkspaceExtensionHelperClass(); IMosaicWorkspaceExtension mosaicWs = mosaicHelper.FindExtension(workspaceDB); IMosaicDataset mosaic = mosaicWs.OpenMosaicDataset(RasterName); IFunctionRasterDataset functionDS = (IFunctionRasterDataset)mosaic; ISaveAs rasterSaveAs = (ISaveAs)functionDS; if (locationForm == ".GDB") { rasterSaveAs.SaveAs(RasterName, wsGDB, "GDB"); } else { rasterSaveAs.SaveAs(RasterName+".tif", wsGDB, "TIFF"); }return true; } catch { return false; } }

    推荐阅读