基于Arcengine实现栅格数据处理功能(获取栅格目录、创建栅格数据集、 添加栅格数据、 格式转换、影像镶嵌、栅格数据计算器(由用户定义计算表达式))
时间: 2023-07-10 13:14:59 浏览: 196
可以通过以下步骤基于ArcEngine实现栅格数据处理功能:
1. 获取栅格目录
获取栅格目录需要使用ESRI.ArcGIS.DataSourcesFile.IWorkspaceFactory接口和ESRI.ArcGIS.Geodatabase.IWorkspace接口,示例代码如下:
```csharp
string folderPath = @"C:\rasterFolder";
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesRaster.RasterWorkspaceFactoryClass();
ESRI.ArcGIS.Geodatabase.IWorkspace workspace = workspaceFactory.OpenFromFile(folderPath, 0);
ESRI.ArcGIS.Geodatabase.IEnumDataset enumDataset = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTRasterDataset);
ESRI.ArcGIS.Geodatabase.IDataset dataset = enumDataset.Next();
while (dataset != null)
{
Console.WriteLine(dataset.Name);
dataset = enumDataset.Next();
}
```
2. 创建栅格数据集
创建栅格数据集需要使用ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace接口和ESRI.ArcGIS.DataSourcesRaster.IRasterDataset接口,示例代码如下:
```csharp
string folderPath = @"C:\rasterFolder";
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesRaster.RasterWorkspaceFactoryClass();
ESRI.ArcGIS.Geodatabase.IWorkspace workspace = workspaceFactory.OpenFromFile(folderPath, 0);
ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace rasterWorkspace = (ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace)workspace;
ESRI.ArcGIS.DataSourcesRaster.IRasterDataset rasterDataset = rasterWorkspace.CreateRasterDataset("rasterName", "TIFF", new ESRI.ArcGIS.Geometry.IEnvelopeClass(), 1, 1, 1, null);
```
3. 添加栅格数据
添加栅格数据需要使用ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace接口和ESRI.ArcGIS.DataSourcesRaster.IRasterDataset接口,示例代码如下:
```csharp
string folderPath = @"C:\rasterFolder";
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesRaster.RasterWorkspaceFactoryClass();
ESRI.ArcGIS.Geodatabase.IWorkspace workspace = workspaceFactory.OpenFromFile(folderPath, 0);
ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace rasterWorkspace = (ESRI.ArcGIS.DataSourcesRaster.IRasterWorkspace)workspace;
ESRI.ArcGIS.DataSourcesRaster.IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset("rasterName");
ESRI.ArcGIS.DataSourcesRaster.IRasterBandCollection rasterBandCollection = (ESRI.ArcGIS.DataSourcesRaster.IRasterBandCollection)rasterDataset;
ESRI.ArcGIS.DataSourcesRaster.IRasterBand rasterBand = rasterBandCollection.Item(0);
ESRI.ArcGIS.DataSourcesRaster.IPixelBlock pixelBlock = rasterBand.CreatePixelBlock(new ESRI.ArcGIS.Geometry.IEnvelopeClass());
ESRI.ArcGIS.DataSourcesRaster.IRasterEdit rasterEdit = (ESRI.ArcGIS.DataSourcesRaster.IRasterEdit)rasterBand;
rasterEdit.StartEditing();
rasterEdit.Write((ESRI.ArcGIS.DataSourcesRaster.IPixelBlock)pixelBlock);
rasterEdit.StopEditing(true);
```
4. 格式转换
格式转换需要使用ESRI.ArcGIS.DataManagementTools.RasterConversionOpClass类,示例代码如下:
```csharp
string inputRasterPath = @"C:\rasterFolder\rasterName1";
string outputRasterPath = @"C:\rasterFolder\rasterName2";
ESRI.ArcGIS.ConversionTools.RasterConversionOp rasterConversionOp = new ESRI.ArcGIS.ConversionTools.RasterConversionOp();
rasterConversionOp.Convert(inputRasterPath, outputRasterPath, "TIFF");
```
5. 影像镶嵌
影像镶嵌需要使用ESRI.ArcGIS.DataManagementTools.MosaicToNewRaster类,示例代码如下:
```csharp
string inputRasterPath1 = @"C:\rasterFolder\rasterName1";
string inputRasterPath2 = @"C:\rasterFolder\rasterName2";
string outputRasterPath = @"C:\rasterFolder\rasterName3";
ESRI.ArcGIS.DataManagementTools.MosaicToNewRaster mosaicToNewRaster = new ESRI.ArcGIS.DataManagementTools.MosaicToNewRaster();
mosaicToNewRaster.in_rasters = inputRasterPath1 + ";" + inputRasterPath2;
mosaicToNewRaster.out_rasterdataset = outputRasterPath;
mosaicToNewRaster.pixel_type = "32_BIT_FLOAT";
mosaicToNewRaster.number_of_bands = 1;
mosaicToNewRaster.mosaic_method = "LAST";
mosaicToNewRaster.Execute();
```
6. 栅格数据计算器
栅格数据计算器需要使用ESRI.ArcGIS.SpatialAnalystTools.RasterCalculatorTool类,示例代码如下:
```csharp
string inputRasterPath1 = @"C:\rasterFolder\rasterName1";
string inputRasterPath2 = @"C:\rasterFolder\rasterName2";
string outputRasterPath = @"C:\rasterFolder\rasterName3";
ESRI.ArcGIS.SpatialAnalystTools.RasterCalculatorTool rasterCalculatorTool = new ESRI.ArcGIS.SpatialAnalystTools.RasterCalculatorTool();
rasterCalculatorTool.expression = "([rasterName1] + [rasterName2]) / 2"; //计算表达式
rasterCalculatorTool.out_raster = outputRasterPath;
rasterCalculatorTool.Execute();
```
注意,需要将计算表达式中的栅格数据集名称用方括号括起来。
阅读全文