ArcEngine Mosaic Dataset 鑲嵌資料集總結
本文大部分內容參考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:程式碼略,如有需求請留言。