镶嵌数据集(Mosaic Dataset)是ArcGIS 10.0中推出的一种全新的空间数据模型,用于管理海量影像数据。我们经常需要访问影像数据的快视图,本文将为您介绍如何批量地为镶嵌数据集创建快视图文件。
环境说明:本例采用的是File Geodatabase,即文件地理数据库来存储镶嵌数据集;使用ArcObjects开发,以ArcGIS Add-In的方式部署在ArcMap中,作为一个命令工具使用。下图是工具的界面设计:
可以看到该工具实现功能需要经过几个关键步骤:
1.用户点击选择按钮,打开文件夹浏览对话框,选择File Geodatabase;
2.程序自动访问File Geodatabase,并获取其中所有的Mosaic Dataset,放到下拉框列表中供用户选择;
3.用户设置其他输出参数:快视图存储路径、DPI、图片压缩比等;
4.用户点击“开始生成”按钮,执行批量创建快视图任务;
5.程序提示当前创建内容,显示进度,完成后提示用户。
其中最为关键的是用户点击开始生成按钮后的操作,包括访问镶嵌数据集,遍历其中每一景影像数据,并为其创建快视图。
以下是点击按钮执行的代码:
private void btnCreate_Click(object sender, EventArgs e) { if (txtGdbPath.Text.Trim() == "" || Directory.Exists(txtGdbPath.Text) == false || txtPreviewPath.Text.Trim() == "" || Directory.Exists(txtPreviewPath.Text) == false) { MessageBox.Show("请选择正确的空间数据库和快视图目录!"); return; } txtLog.Text += "开始:详细进度信息将写入日志文件。\r\n"; long start = DateTime.Now.Ticks; LogHelper.AddMessage(string.Format("{0} 开始执行快视图生成操作...", DateTime.Now.ToLongTimeString())); //打开镶嵌数据集 IMosaicDataset md = GdbHelper.OpenMosaicDataset(_workspace, cmbDatasets.SelectedItem.ToString()); if (md == null) { txtLog.Text += "错误:未获取到镶嵌数据集,请查看日志了解详细信息。\r\n"; return; } short quality = (short)numQuality.Value; int dpi = (int)numDpi.Value; //批量创建快视图 GdbHelper.CreatePreview(md, _activeView, cmbDatasets.SelectedItem.ToString(), txtPreviewPath.Text, quality, dpi); long end = DateTime.Now.Ticks; int totalTime = (int)((end - start) / TimeSpan.TicksPerSecond); LogHelper.AddMessage(string.Format("{0} 快视图生成操作完成,共计用时{1}秒。", DateTime.Now.ToLongTimeString(), totalTime)); LogHelper.WriteToLogFile(); txtLog.Text += "完成:请查看日志了解详细信息。"; }
public static void CreatePreview(IMosaicDataset mosaicDataset, IActiveView view, string mdName, string dirPreview, short quality, int outRes) { ITable pTable = GetMosaicDatasetTable(mosaicDataset); if (pTable == null) return; int indexName = pTable.FindField("NAME"); int indexOID = pTable.FindField("OBJECTID"); int indexRaster = pTable.FindField("RASTER"); ICursor pCursor = pTable.Search(null, false); IRow pRow = pCursor.NextRow(); int count = 1; while (pRow != null) { string rasterName = Convert.ToString(pRow.get_Value(indexName)); string oid = Convert.ToString(pRow.get_Value(indexOID)); string picName = string.Format("{0}_{1}.jpg", mdName, oid); picName = System.IO.Path.Combine(dirPreview, picName); LogHelper.AddMessage(string.Format("正在创建第 {0} 个快视图:{1}...", count, picName)); if (File.Exists(picName)) { LogHelper.AddMessage(string.Format("{0} 已存在,跳过...", picName)); } else { try { IRasterCatalogItem pRasterCatalogItem = (IRasterCatalogItem)pRow; IRasterDataset pRasterdataset = pRasterCatalogItem.RasterDataset; IRasterLayer pRasterlayer = new RasterLayerClass(); pRasterlayer.CreateFromDataset(pRasterdataset); IRaster pRaster = pRasterlayer.Raster; IRasterProps pRasterpro = pRaster as IRasterProps; if (view.FocusMap.LayerCount > 0) { view.FocusMap.ClearLayers(); } view.FocusMap.AddLayer(pRasterlayer as ILayer); IArea pArea = pRasterpro.Extent.Envelope as IArea; view.FullExtent = pRasterpro.Extent; view.FullExtent.CenterAt(pArea.Centroid); view.Refresh(); view.FocusMap.RecalcFullExtent(); ExportActiveView(view, picName, pRasterpro.Extent, quality, outRes); } catch (Exception ex) { LogHelper.AddMessage(string.Format("为 {0} 创建快视图失败:", ex.Message)); } } pRow = pCursor.NextRow(); count++; if (count % 100 == 0) { LogHelper.WriteToLogFile(); GC.Collect();//如不清理内存,将导致内存占用过大,x64系统达到3.8G左右程序无响应 } } Marshal.ReleaseComObject(pCursor); view.Clear(); }
private static void ExportActiveView(ESRI.ArcGIS.Carto.IActiveView pActiveView, System.String picName, IEnvelope pEn, short quality, int outRes) { if (pActiveView == null || !(picName.EndsWith(".jpg"))) return; ESRI.ArcGIS.Output.IExport export = new ESRI.ArcGIS.Output.ExportJPEGClass(); export.ExportFileName = picName; export.Resolution = outRes;//输出分辨率,即DPI,默认96 IExportJPEG pEXG = export as IExportJPEG; pEXG.Quality = quality;//图片质量,0~100,100表示不压缩 tagRECT exportRECT; // This is a structure exportRECT.right = pActiveView.ExportFrame.right; exportRECT.bottom = pActiveView.ExportFrame.bottom; exportRECT = pActiveView.ExportFrame; IDisplayTransformation pDisplayTransformation = pActiveView.ScreenDisplay.DisplayTransformation; pDisplayTransformation.TransformRect(pEn, ref exportRECT, 8); exportRECT.left = 0; exportRECT.top = 0; //isc //Set up the PixelBounds envelope to match the exportRECT ESRI.ArcGIS.Geometry.IEnvelope envelope = new ESRI.ArcGIS.Geometry.EnvelopeClass(); envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom); export.PixelBounds = envelope; System.Int32 hDC = export.StartExporting(); pActiveView.Output(hDC, (System.Int16)export.Resolution, ref exportRECT, pEn, null); // Explicit Cast and 'ref' keyword needed export.FinishExporting(); export.Cleanup(); pActiveView.Clear(); Marshal.ReleaseComObject(pEXG); Marshal.ReleaseComObject(export); pEXG = null; export = null; }