创建FILE GEODATABASE 和栅格目录及向栅格目录中添加影像
using System;
using System.IO;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Carto;
namespace RasterOperation
{
public class RasterCatalogOperation
{
/// <summary>
/// fileGDB路径
/// </summary>
public string FileGDBPath
{
get ;
set ;
}
/// <summary>
/// 栅格数据目录
/// </summary>
public string SourceRasterDic
{
get;
set;
}
/// <summary>
/// 栅格目录名称
/// </summary>
public string RasterDSName
{
get;
set;
}
/// <summary>
/// 栅格坐标系
/// </summary>
public ISpatialReference RasterSpRf
{
get;
set;
}
/// <summary>
/// 矢量坐标系
/// </summary>
public ISpatialReference GeometrySpRf
{
get;
set;
}
/// <summary>
/// 栅格目录转换为图层
/// </summary>
/// <param name="folderName">目录名</param>
/// <param name="datasetName">数据集名</param>
/// <returns>要素图层</returns>
public ILayer AddRasterCatalogLayer(string folderName, string datasetName)
{
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
try
{
IWorkspace workspc = workspaceFactory.OpenFromFile(folderName, 0);
IRasterWorkspaceEx pRaterWs = (IRasterWorkspaceEx)workspc;
IRasterCatalog rasterCatalog = pRaterWs.OpenRasterCatalog(datasetName);
ESRI.ArcGIS.Carto.IGdbRasterCatalogLayer rastercatalogLayer = new
GdbRasterCatalogLayerClass();
rastercatalogLayer.Setup((ITable)rasterCatalog);
//Add it to map if the layer is valid.
if (!(rastercatalogLayer == null))
{
return rastercatalogLayer as ILayer;
}
else
return null;
}
catch
{
return null;
}
//Create a raster catalog layer.
}
/// <summary>
/// 创建FileGDB
/// </summary>
/// <param name="fullPath">路径名</param>
public void CreateFileGDB(string fullPath)
{
if (!Directory.Exists(fullPath))
{
IWorkspaceFactory2 wsFctry = new FileGDBWorkspaceFactoryClass();
wsFctry.Create(System.IO.Path.GetDirectoryName(fullPath),
System.IO.Path.GetFileName(fullPath), null, 0);
FileGDBPath = fullPath;
wsFctry = null;
}
}
/// <summary>
/// 创建RasterCatalog
/// </summary>
/// <param name="path">路径</param>
/// <param name="catalogName">名称</param>
/// <param name="rasterCoordSys">栅格坐标系</param>
/// <param name="geometryCoordsys">矢量坐标系</param>
public void CreateRasterCatalog_GP(string path, string catalogName, ISpatialReference rasterCoordSys, ISpatialReference geometryCoordsys)
{
//坐标转换
//Coordinate system for raster column
IGPCoordinateSystem rSR = new GPCoordinateSystemClass();
rSR.SpatialReference = rasterCoordSys;
//Coordinate system for geometry column
IGPSpatialReference gSR = new GPSpatialReferenceClass();
gSR.SpatialReference = geometryCoordsys;
//初始化GeoProcessor
ESRI.ArcGIS.Geoprocessor.Geoprocessor geoProcessor = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
RasterDSName = catalogName;
//创建工具
CreateRasterCatalog createRasterCatalog = new CreateRasterCatalog();
//设置参数
createRasterCatalog.out_path = path;
createRasterCatalog.out_name = catalogName;
createRasterCatalog.raster_spatial_reference = rSR;
createRasterCatalog.spatial_reference = gSR;
//利用工具创建RasterCatalog
geoProcessor.Execute(createRasterCatalog, null);
//ReturnMessages(geoProcessor);
}
//GP message handling
public void ReturnMessages(Geoprocessor gp)
{
if (gp.MessageCount > 0)
{
for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
{
Console.WriteLine(gp.GetMessage(Count));
}
}
}
//static string rasterFolder = @"c:\tempdata";
//static string outRC = @"Database Connections\connection to raster.sde\rc_203";
//LoadDirtoRasterCatalog(outRC, rasterFolder);
/// <summary>
/// 输入整个目录到库中
/// </summary>
/// <param name="outRasterCatalog">目标栅格库全路径</param>
/// <param name="inputDir">输入目录</param>
public void LoadDirtoRasterCatalog(string outRasterCatalog, string inputDir)
{
if (!Directory.Exists(inputDir))
{
System.Windows.Forms.MessageBox.Show("路径不正确,请重新输入", "提示");
return;
}
//初始化 GeoProcessor
ESRI.ArcGIS.Geoprocessor.Geoprocessor geoProcessor = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
//设置参数
IVariantArray parameters = new VarArrayClass();
//输入的文件夹目录
parameters.Add(inputDir);
//目标栅格库路径
parameters.Add(outRasterCatalog);
//Execute the tool to load rasters in the directory to raster catalog
geoProcessor.Execute("WorkspaceToRasterCatalog", parameters, null);
// ReturnMessages(geoProcessor);
}
/// <summary>
/// 选择要素的坐标系
/// </summary>
/// <param name="refFileName"></param>
/// <returns></returns>
public ISpatialReference InputReferece()
{
System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
if (SourceRasterDic != string.Empty || SourceRasterDic != " ")
{
openFileDialog.InitialDirectory = SourceRasterDic;
}
openFileDialog.Title = "加载图层数据";
openFileDialog.Filter = "Tif文件(*.tif)|*.tif|Shp文件(*.shp)|*.shp|Jpg文件(*.jpg)|*.jpg|Bmp文件(*.bmp)|*.bmp|Gif文件(*.gif)|*.gif|Img文件(*.img)|*.img|Png文件(*.png)|*.png|Tiff文件(*.tiff)|*.tiff";
openFileDialog.Multiselect = false;
openFileDialog.DefaultExt = "*.tif|*.shp";
openFileDialog.SupportMultiDottedExtensions = true;
string refFileName = string.Empty;
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
refFileName = openFileDialog.FileName;
}
else
{
return null;
}
IWorkspaceFactory pWorkspaceFactory = null;
string p = System.IO.Path.GetExtension(refFileName);
string fileNameWithoutExtentsion = System.IO.Path.GetFileNameWithoutExtension(refFileName);
string pathName = System.IO.Path.GetDirectoryName(refFileName);
string fileName = System.IO.Path.GetFileName(refFileName);
if (p == ".shp" || p == ".SHP")
{
IFeatureWorkspace ws;
IFeatureClass pFeatureClass;
pWorkspaceFactory = new ShapefileWorkspaceFactory();
ws = pWorkspaceFactory.OpenFromFile(pathName, 0) as IFeatureWorkspace;
pFeatureClass = ws.OpenFeatureClass(fileName);
ws = null;
return (pFeatureClass as IGeoDataset).SpatialReference;
}
if (p == ".jpg" || p == ".bmp" || p == ".gif" || p == ".img" || p == ".png" || p == ".tif" || p == ".tiff"
|| p == ".JPG" || p == ".BMP" || p == ".GIF" || p == ".IMG" || p == ".PNG" || p == ".TIF" || p == ".TIFF")
{
IRasterLayer pRasterLayer = new RasterLayerClass();
IRasterWorkspace pRasterWorkspace;
IRaster pRaster;
IRasterDataset pRasterDataset;
IWorkspace pWorkspace;
pWorkspaceFactory = new RasterWorkspaceFactoryClass();
pWorkspace = pWorkspaceFactory.OpenFromFile(pathName, 0);//0
pRasterWorkspace = pWorkspace as IRasterWorkspace;
pRasterDataset = pRasterWorkspace.OpenRasterDataset(fileName);
pRaster = pRasterDataset.CreateDefaultRaster();
pRasterLayer.CreateFromRaster(pRaster);
IRasterProps pRasterProps = pRasterLayer.Raster as IRasterProps;
return pRasterProps.SpatialReference;
}
return null;
}
/// <summary>
/// 获取要素数据集
/// </summary>
/// <param name="workspace">工作空间</param>
/// <returns>要素数据集</returns>
public IFeatureClass GetFirstFClass(IWorkspace workspace)
{
IFeatureClass FClass = null;
IEnumDataset enumDataset = workspace.get_Datasets(esriDatasetType.esriDTAny);
enumDataset.Reset();
IDataset perFeatSet = enumDataset.Next();
while (perFeatSet != null)
{
esriDatasetType getDatasetType = perFeatSet.Type;
switch (getDatasetType)
{
case esriDatasetType.esriDTFeatureClass:
ILayerFactoryHelper layerFactoryHelper = new LayerFactoryHelperClass();
IEnumLayer enumLayer = layerFactoryHelper.CreateLayersFromName(perFeatSet.FullName);
enumLayer.Reset();
ILayer layer = enumLayer.Next();
while (layer != null)
{
//筛选出第一个面状要素数据集
if (layer is IFeatureLayer &&
(layer as IFeatureLayer).FeatureClass.ShapeType
== ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)
{
return (layer as IFeatureLayer).FeatureClass;
}
layer = enumLayer.Next();
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(enumLayer);
break;
case esriDatasetType.esriDTFeatureDataset:
case esriDatasetType.esriDTTable:
case esriDatasetType.esriDTRasterDataset:
break;
default:
break;
}
perFeatSet = enumDataset.Next();
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(enumDataset);
return FClass;
}
}
}
来自:http://www.cnblogs.com/wylaok/archive/2012/07/20/2600660.html