ArcEngine Mosaic Dataset 镶嵌数据集总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yh0503/article/details/51762723

本文大部分内容参考AO帮助文档和ArcMap帮助文档,大家实际使用中遇到的问题也可以在本帖下方留言交流,谢谢!

欢迎浏览,拒绝转载!


镶嵌数据集基础知识

关于镶嵌数据集的基础知识可以参考幕晓燕大神的博客:传送门
ArcObjects 镶嵌数据集 官方教程:传送门

类图

在线路径:传送门
本地路径:如 C:\Program Files (x86)\ArcGIS\DeveloperKit10.2\Diagrams\DataSourcesRasterObjectModel.pdf

和镶嵌数据集有关的GP工具

这里写图片描述

镶嵌数据集代码示例

1.如何创建镶嵌数据集

    //1.获取GDB工作空间
    IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
    IWorkspace fgdbWorkspace = workspaceFactory.OpenFromFile(@"C:\testGdb.gdb", 0);

    //2.获取坐标系
    ISpatialReferenceFactory spatialrefFactory = new SpatialReferenceEnvironmentClass();
    ISpatialReference mosaicSrs = spatialrefFactory.CreateProjectedCoordinateSystem((int)(esriSRProjCSType.esriSRProjCS_World_Mercator));

    // 3.获取创建镶嵌数据集的参数
    ICreateMosaicDatasetParameters creationPars = new CreateMosaicDatasetParametersClass();
    //3.1设置镶嵌数据集的波段数量
    creationPars.BandCount = 3;
    //3.2设置镶嵌数据集的像素类型
    creationPars.PixelType = rstPixelType.PT_UCHAR; 
    //4.创建IMosaicWorkspaceExtensionHelper对象
    IMosaicWorkspaceExtensionHelper mosaicExtHelper = new MosaicWorkspaceExtensionHelperClass();
    //4.1 根据GDB工作空间对象获取IMosaicWorkspaceExtension对象
    IMosaicWorkspaceExtension mosaicExt = mosaicExtHelper.FindExtension(fgdbWorkspace);
    //用上面的参数创建镶嵌数据集
    IMosaicDataset theMosaicDataset = mosaicExt.CreateMosaicDataset("testMD", mosaicSrs,creationPars,"");

2.如何创建引用镶嵌数据集

private void CreateReferencedMosaic(IWorkspace ws, String MosaicDatasetName, ISpatialReference SR, IRasterCatalog catalog)
{
    //创建IMosaicWorkspaceExtensionHelper对象
    IMosaicWorkspaceExtensionHelper MosaicWsHelper = new MosaicWorkspaceExtensionHelperClass();
    //利用IMosaicWorkspaceExtensionHelper对象,传入一个Workspace对象获取IMosaicWorkspaceExtension对象
    IMosaicWorkspaceExtension mosaicWsExtension = MosaicWsHelper.FindExtension(ws);
    //定义创建参考镶嵌数据集的参数
    ICreateMosaicDatasetParameters mdParameters = new CreateMosaicDatasetParametersClass();
    mdParameters.BandCount = 3; 
    //可选参数
    mdParameters.PixelType = rstPixelType.PT_UCHAR; 
    //可选参数
    //If the raster catalog is a regular raster catalog, define this parameter.
    mdParameters.MaximumVisibleCellsize = 10; 
    //This is the threshold above which a rasterized wireframe will be displayed.
    //Create the referenced mosaic dataset. 
    //Name represents the name of the mosaic dataset, SR represents the spatial reference, 
    //and catalog can be the input raster catalog or mosaic dataset.
    IMosaicDataset md = mosaicWsExtension.CreateReferencedMosaicDataset(MosaicDatasetName, SR, catalog, null, mdParameters);
}   

3.如何打开镶嵌数据集

    // Create a gdb workspace.
    IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
    IWorkspace fgdbWorkspace = workspaceFactory.OpenFromFile(@"C:\testGdb.gdb", 0);
    // Create the mosaic workspace extension helper class.
    IMosaicWorkspaceExtensionHelper mosaicExtHelper = new MosaicWorkspaceExtensionHelperClass();
    // Find the right extension from the workspace.
    IMosaicWorkspaceExtension mosaicExt = mosaicExtHelper.FindExtension(fgdbWorkspace);
    // Use the extension to open the mosaic dataset.
    IMosaicDataset theMosaicDataset = mosaicExt.OpenMosaicDataset("testMD");

4.获取已有镶嵌数据表

如果想获取Attribute Table,用IMosaicDataset.Catalog属性即可,该属性的类型为IFeatureClass。
这里写图片描述

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;
}   

5.删除某个镶嵌数据集

方法一:(缺点:删除不是很彻底,会遗留矢量文件)
这里写图片描述

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;
    }
}   

方法二:(推荐)
调用IMosaicDatasetOperation3 接口中的DeleteMosaicDataset方法,可以设置是否连带删除OverviewImages和ItemCache。(具体代码略)

DeleteMosaicDataset (IDeleteMosaicDatasetParameters pMDDeleteParams, ITrackCancelpTrackCancel);

这里写图片描述
方法三:(缺点:创建GP比较慢 优点:比较稳定,异常信息比较清楚)
调用GP工具,位置:Data Management tools/Raster/Mosaic dataset/Delete Mosaic dataset
这里写图片描述

6.在镶嵌数据集中添加数据

示例1:
添加栅格数据到镶嵌数据集的步骤:
1.Create a file crawler.
2.Specify the source path.
3.Specify whether to search subdirectories.
4.Specify a file filter.
5.Create an AddRastersParameters object.
6.Specify the data crawler to use to crawl the data.
7.Specify the raster type to use to add the data.(raster type参考官方ArcObjects教程即可)
8.Use the Mosaic dataset operation interface to add rasters to the Mosaic dataset.

public void AddRastersToMD(IMosaicDataset theMosaicDataset, IRasterType
    theRasterType)
{
    // Create a file crawler.
    IDataSourceCrawler myCrawler = new FileCrawlerClass();
    // Specify the source path.
    ((IFileCrawler)myCrawler).Path = @"C:\TestData";
    // Specify whether to search subdirectories.
    ((IFileCrawler)myCrawler).Recurse = true;
    // Specify a file filter.
    ((IFileCrawler)myCrawler).Filters = ".TIF";

    // The mosaic dataset operation interface is used to perform operations on 
    // a mosaic dataset.
    IMosaicDatasetOperation theMosaicDatasetOperation = (IMosaicDatasetOperation)
        (theMosaicDataset);
    // Create an AddRaster parameters object.
    IAddRastersParameters AddRastersArgs = new AddRastersParametersClass();
    // Specify the data crawler to use to crawl the data.
    AddRastersArgs.Crawler = myCrawler;
    // Specify the raster type to use to add the data.
    AddRastersArgs.RasterType = theRasterType;
    // Use the mosaic dataset operation interface to add 
    // rasters to the mosaic dataset.
    theMosaicDatasetOperation.AddRasters(AddRastersArgs, null);
} 

示例2:

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);
        //计算像素大小范围
        ICalculateCellSizeRangesParameters computeArgs = new CalculateCellSizeRangesParametersClass();
        mOp.CalculateCellSizeRanges(computeArgs, null);
        //创建镶嵌数据集的边界
        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;
    }
}

7.导出镶嵌数据集

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 = new  MosaicWorkspaceExtensionHelperClass();
        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;
    }
}

8.替换镶嵌数据集中数据的路径

IMosaicDatasetOperation pMosaicDatasetOperation = pOutputMosaicDataset as IMosaicDatasetOperation;
IReplacePathsParameters pReplacePathsParameters = new ReplacePathsParametersClass();
ESRI.ArcGIS.esriSystem.IStringArray pNewStringArray = new ESRI.ArcGIS.esriSystem.StrArrayClass();
ESRI.ArcGIS.esriSystem.IStringArray pOldStringArray = new ESRI.ArcGIS.esriSystem.StrArrayClass();
\\源路径:\\localhost\repository1\DOM\ZHENGJING\
pOldStringArray.Add(sSrcPath);
\\目标路径:E:\TempTest\新建文件夹 (2)\DOM\ZHENGJING  
pNewStringArray.Add(sTarPath);
pReplacePathsParameters.NewPaths = pNewStringArray; 
pReplacePathsParameters.OldPaths = pOldStringArray;
pReplacePathsParameters.ReplaceItemURIPaths = true;
pMosaicDatasetOperation.ReplacePaths(pReplacePathsParameters, null);

9.获取镶嵌数据集的影像物理路径

方法一:(缺点:如果路径不对或者损坏,则读取不出来)

ITable pTable = pMosaicDataset.Catalog as ITable;
ICursor pCursor = pTable.Search(null, false);
IRow pRow = pCursor.NextRow();
//循环存储在镶嵌数据集里面的影像信息  
while (pRow != null)
{
    IRasterCatalogItem pRasterCatalogItem = (IRasterCatalogItem)pRow;
    IRasterDataset pRasterdataset = pRasterCatalogItem.RasterDataset;
    //用这个接口,镶嵌数据集中的每一条记录是  
    IFunctionRasterDataset pFunctionRD = pRasterdataset as IFunctionRasterDataset;
    ESRI.ArcGIS.esriSystem.IArray pArray = pFunctionRD.MemberRasterDatasets;
    for (int n = 0; n < pArray.Count; n++)
    {
        IRasterDataset pFunRst = pArray.get_Element(n) as IRasterDataset;
        //获得存储在镶嵌数据集里面每一个影像的物理路径  
        string aName = pFunRst.CompleteName;
    }
    pRow = pCursor.NextRow();
}

参考链接:http://blog.csdn.net/linghe301/article/details/8133418

方法二:

IItemPathsQuery2 pItemPathsQuery = pMosaicDataset as IItemPathsQuery2;
IQueryPathsParameters pQueryParas = new QueryPathsParametersClass();
pQueryParas.BrokenPathsOnly = false;
pQueryParas.FoldersOnly = true;
pQueryParas.QueryDatasetPaths = true;
pQueryParas.QueryItemURIPaths = false;
pItemPathsQuery.QueryPathsParameters = pQueryParas;
IWorkspace pTempWs = EngineAPI.CreateInMemoryWorkspace();//创建临时工作空间
pComReleaser.ManageLifetime(pTempWs);
pItemPathsQuery.ExportPaths(true, true, "PathList", pTempWs, "", null);
ITable pTable = (pTempWs as IFeatureWorkspace).OpenTable("PathList");
pComReleaser.ManageLifetime(pTable);
mosaicItemPathList = EngineAPI.GetFieldUniqueValue(pTable, "Path");//获取唯一值列表

方法三:(缺点:需要生成临时的GDB文件)
调用GP工具ExportMosaicDatasetPaths导出

10.提取指定范围的镶嵌数据集

第一步:复制镶嵌数据集
第二步:使用镶嵌数据集的Catalog获取对应的FeaClass,然后进行对应的范围查询,获取不符合查询条件的列表
第三步:根据查询出的结果设置查询条件,对镶嵌数据集执行删除操作。
PS:代码略,如有需求请留言。

11.提取指定条件的镶嵌数据集

第一步:复制镶嵌数据集
第二步:使用镶嵌数据集的Catalog获取对应的FeaClass,然后进行对应的属性查询,获取不符合查询条件的列表
第三步:根据查询出的结果设置查询条件,对镶嵌数据集执行删除操作。
PS:代码略,如有需求请留言。

猜你喜欢

转载自blog.csdn.net/yh0503/article/details/51762723