C#针对ArcSDE的操作2(矢量数据、栅格数据导入到数据集)

1.数据集与数据的概念
  ArcGIS Help关于数据与数据集的概念:
  要素类:具有一组公用属性字段的同一类型要素(例如,井、宗地和街道)的集合。
  要素数据集:主题相关的要素类的集合(通常用于管理拓扑、网络或地形)。
  栅格数据集:涉及一定范围的单个栅格或影像数据图层。
  栅格目录:一个排列有序的影像集合,例如涉及某一区域的一系列影像切片。
  总结一下:要素数据集(Feature Dataset)由一组相同空间参考(Spatial Reference)的要素类(Feature Class)组成,一个要素数据集中可以有多个要素类。而一个栅格数据集(Raster dataset)只能保存单张影像,可以管理多张影像的集合称之为栅格目录(Raster Catalog)。

  
2.矢量数据导入要素数据集

C#针对ArcSDE的操作2(矢量数据、栅格数据导入到数据集)_第1张图片 图1.矢量数据导入界面

if(((DataRowView)comboBoxEx1.SelectedItem).Row["DatasetID"].ToString() == "要素数据集")
{
    FileInfo fileInfo = new FileInfo(fileName);
    string filePath = fileInfo.DirectoryName;
    string file = fileInfo.Name;
    IWorkspaceFactory shpwpf = new ShapefileWorkspaceFactory();
    IWorkspace shpwp = shpwpf.OpenFromFile(filePath, 0);
    IFeatureWorkspace shpfwp = shpwp as IFeatureWorkspace;
    IFeatureClass shpfc = shpfwp.OpenFeatureClass(file);
    //导入SDE数据库
    IFeatureClass sdeFeatureClass = null;
    IFeatureClassDescription featureClassDescription = new FeatureClassDescriptionClass();
    IObjectClassDescription objectClassDescription = featureClassDescription as IObjectClassDescription;
    IFields fields = shpfc.Fields;
    IFieldChecker fieldChecker = new FieldCheckerClass();
    IEnumFieldError enumFieldError = null;
    IFields vFields = null;
    fieldChecker.ValidateWorkspace = featureWorkspace as IWorkspace;
    fieldChecker.Validate(fields, out enumFieldError, out vFields);
    featureDataset = featureWorkspace.OpenFeatureDataset(this.comboBoxEx2.Text);
    try
    {
        sdeFeatureClass = featureWorkspace.OpenFeatureClass(shpfc.AliasName);
    }
    catch
    {

    }
    if (sdeFeatureClass == null)
    {
        sdeFeatureClass = featureDataset.CreateFeatureClass(shpfc.AliasName, vFields,
        objectClassDescription.InstanceCLSID, objectClassDescription.ClassExtensionCLSID,
        shpfc.FeatureType, shpfc.ShapeFieldName, "");
        IFeatureCursor featureCursor = shpfc.Search(null, true);
        IFeature feature = featureCursor.NextFeature();
        IFeatureCursor sdeFeatureCursor = sdeFeatureClass.Insert(true);
        IFeatureBuffer sdeFeatureBuffer;
        while (feature != null)
        {
            sdeFeatureBuffer = sdeFeatureClass.CreateFeatureBuffer();
            IField shpField = new FieldClass();
            IFields shpFields = feature.Fields;
            for (int i = 0; i < shpFields.FieldCount; i++)
            {
                shpField = shpFields.get_Field(i);
                int index = sdeFeatureBuffer.Fields.FindField(shpField.Name);
                if (index != -1)
                {
                    sdeFeatureBuffer.set_Value(index, feature.get_Value(i));
                }
            }
            sdeFeatureCursor.InsertFeature(sdeFeatureBuffer);
            sdeFeatureCursor.Flush();
            feature = featureCursor.NextFeature();
        }
    }
    MessageBox.Show("导入成功", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

3.栅格数据导入栅格数据集
  在对已经构建的栅格数据中导入栅格数据遇到了点问题,。。。暂时的解决方案是删除已经构建的栅格数据集,并将要导入的栅格数据命名为原栅格数据名并导入。缺点是代码量提升以及让之前构建的栅格数据集有些多此一举。

C#针对ArcSDE的操作2(矢量数据、栅格数据导入到数据集)_第2张图片
图2.将遥感影像导入栅格数据集

else if (((DataRowView)comboBoxEx1.SelectedItem).Row["DatasetID"].ToString() == "栅格数据集") 
{
    //导入SDE数据库
    type = "栅格数据";
    FileInfo fileInfo = new FileInfo(fileName);
    string filePath = fileInfo.DirectoryName;
    string file = fileInfo.Name;
    string file1;
    int lastIndex;
    rasterWorkspaceEx = workspace as IRasterWorkspaceEx;
    IWorkspaceFactory tifwpf = new RasterWorkspaceFactoryClass();
    IWorkspace tifwp = tifwpf.OpenFromFile(filePath, 0);
    IRasterWorkspace tifrwp = tifwp as IRasterWorkspace;
    IRasterDataset rasterDataset =      tifrwp.OpenRasterDataset(file);
    lastIndex = file.LastIndexOf(@".");
    file1 = file.Substring(0, lastIndex);
    //存储栅格辅助函数
    imortRasterData(filePath, file, this.comboBoxEx2.Text);
    MessageBox.Show("导入成功");
    sql();
}
//存储栅格辅助函数
private void imortRasterData(string strRasterFileDir, string strRasterFileName, string strOutName)
{
    try
    {
        IWorkspace2 pWS2 = workspace as IWorkspace2;
        IRasterWorkspaceEx pRWs = workspace as IRasterWorkspaceEx;
        IDataset pDataset = pRWs.OpenRasterDataset(strOutName) as IDataset;
        //删掉之前创建的栅格数据集
        pDataset.Delete();
        pDataset = null;
        IWorkspaceFactory pRasterWsFac = new RasterWorkspaceFactoryClass();
        IWorkspace pWs = pRasterWsFac.OpenFromFile(strRasterFileDir, 0);
        IRasterDataset pRasterDs = null;
        IRasterWorkspace pRasterWs;
        if (!(pWs is IRasterWorkspace))
        {
            return;
        }
        pRasterWs = pWs as IRasterWorkspace;
        pRasterDs = pRasterWs.OpenRasterDataset(strRasterFileName);
        ISaveAs2 saveAs2 = (ISaveAs2)pRasterDs;
        IRasterStorageDef rasterStorageDef = new RasterStorageDefClass();
        IRasterStorageDef2 rasterStorageDef2 = (IRasterStorageDef2)rasterStorageDef;
        rasterStorageDef2.CompressionType = esriRasterCompressionType.esriRasterCompressionUncompressed;
        rasterStorageDef2.CompressionQuality = 100;
        rasterStorageDef2.Tiled = true;
        rasterStorageDef2.TileHeight = 128;
        rasterStorageDef2.TileWidth = 128;
        saveAs2.SaveAsRasterDataset(strOutName, workspace, "gdb", rasterStorageDef2);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
        return;
    }
}

  如果栅格数据较大,存储需要较长时间,可以创建一个等待界面,参考解决C#窗口假死问题
  同样的,数据导入数据集后,可以在ArcCatelog中查看:

C#针对ArcSDE的操作2(矢量数据、栅格数据导入到数据集)_第3张图片
图3.在地理数据库中查看数据集中的数据

你可能感兴趣的:(C#,ArcEngine开发笔记)