《ArcGIS Engine 地理信息系统开发从入门到精通(第二版)》——6.6 Geodatabase的使用与开发

简介:

本节书摘来自异步社区《ArcGIS Engine 地理信息系统开发从入门到精通(第二版)》一书中的第6章,第6.6节,作者: 邱洪钢 , 张青莲 , 熊友谊 更多章节内容可以访问云栖社区“异步社区”公众号查看。

6.6 Geodatabase的使用与开发

ArcGIS Engine 地理信息系统开发从入门到精通(第二版)
6.6.1 空间数据库连接
空间数据库企业级Geodatabase的连接主要是使用IworkspaceFactory、Iworkspace和IpropertySet等3个接口,通过IPropertySet 设置数据库的连接属性,由IworkspaceFactory创建一个IWorkspace的接口指针并返回该指针。6.6.3小节的代码演示了如何进行空间数据库的连接。

6.6.2 创建新的数据集
数据集是由一组具有相同空间参考的要素类组成,用于存放数据。创建新的数据集主要使用IWorkspace、IFeatureWorkspace、IfeatureDataset和IspatialReference等4个接口,其中,IWorkspace用于生成IFeatureWorkspace,ISpatialReference用于设置要素数据集的空间参考属性(该属性为可选属性),整个过程返回IFeatureDataset接口指针,即新生成的要素数据集。

6.6.3 空间数据的入库
下面代码使用SDE空间数据库,因此ArcEngine的Lisence应使用企业数据库类型,在Form1_Load事件中初始化lisence,不能使用lisence控件设置,否则提示没有许可lisence。

private void Form1_Load(object sender, EventArgs e)
    {
      IAoInitialize pao = new AoInitializeClass();
      pao.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);     
    }
    // 工作空间
    IWorkspace workspace;
    //矢量数据工作空间
    IFeatureWorkspace featureWorkspace;
    //影像数据工作空间
    IRasterWorkspaceEx rasterWorkspace;
    //矢量数据集
    IFeatureDataset featureDataset;
    //影像数据集
    IRasterDataset rasterDataset;
    private void button1_Click(object sender, EventArgs e)
    {
      // SDE空间连接属性
      IPropertySet propertySet = new PropertySetClass();
      propertySet.SetProperty("server", this.textBox1.Text );
      propertySet.SetProperty("instance",this.textBox2.Text );
      propertySet.SetProperty ("database",this.textBox3.Text );
      propertySet.SetProperty("user", this.textBox4.Text );
      propertySet.SetProperty ("password",this.textBox5.Text );
      propertySet.SetProperty("version", "SDE.DEFAULT");
      IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactory();
      //打开SDE工作空间
      workspace = workspaceFactory.Open(propertySet, 0);
      MessageBox.Show("连接SDE空间数据库成功");
    }
    //创建数据集(矢量数据集和影像数据集)
    private void button2_Click(object sender, EventArgs e)
    {
      featureWorkspace =workspace as IFeatureWorkspace ;
      rasterWorkspace = workspace as IRasterWorkspaceEx;
      //定义空间参考
      ISpatialReferenceFactory spatialReferenceFactory = new SpatialReference   
      EnvironmentClass();
      ISpatialReference spatialReference = spatialReferenceFactory.   
      CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_Beijing1954);
      spatialReference.SetDomain(-1000, -1000, 1000, 1000);
      IEnumDatasetName enumDatasetName;
      IDatasetName  datasetName;
      string dsName = "";
      enumDatasetName = workspace.get_DatasetNames(esriDatasetType.esriDTFeature   
      Dataset);
      datasetName = enumDatasetName.Next();
      bool isExist = false;
      //创建矢量数据集
      dsName = "SDE."+this.textBox6.Text;
      while (datasetName != null)
      {
        if (datasetName.Name == dsName)
        {
          isExist = true;
        }
        datasetName = enumDatasetName.Next();
      }
      if (isExist ==false )
      {
        featureDataset = featureWorkspace.CreateFeatureDataset(this. textBox6.Text,   
        spatialReference);
      }
      //创建影像数据集
      isExist = false;
      enumDatasetName = workspace.get_DatasetNames(esriDatasetType.esriDTRaster   
      Dataset );
      datasetName = enumDatasetName.Next();

      dsName = "SDE." + this.textBox6.Text;
      while (datasetName != null)
      {
        if (datasetName.Name == dsName)
        {
          isExist = true;
        }
        datasetName = enumDatasetName.Next();
      }
      if (isExist == false)
      {
        //设置存储参数
        IRasterStorageDef rasterStorageDef = new RasterStorageDefClass();
        rasterStorageDef.CompressionType = esriRasterCompressionType.   
        esriRasterCompressionUncompressed;
        rasterStorageDef.PyramidLevel = 1;
        rasterStorageDef.PyramidResampleType = rstResamplingTypes.RSP_   
        BilinearInterpolation;
        rasterStorageDef.TileHeight = 128;
        rasterStorageDef.TileWidth = 128;
        //设置坐标系统
        IRasterDef rasterDef = new RasterDefClass();
        ISpatialReference rasterDpatialRefrence = new UnknownCoordinateSystem Class();
        rasterDef.SpatialReference = rasterDpatialRefrence;

        IGeometryDef geometryDef = new GeometryDefClass();
        IGeometryDefEdit geometryDefedit = (IGeometryDefEdit)geometryDef;
        geometryDefedit.AvgNumPoints_2 = 5;
        geometryDefedit.GridCount_2 = 1;
        geometryDefedit.set_GridSize(0, 1000);
        geometryDefedit.GeometryType_2 = esriGeometryType.esriGeometry Polygon;
        ISpatialReference spatialReference2 = new UnknownCoordinateSystem Class();
        geometryDefedit.SpatialReference_2 = spatialReference2;
        rasterDataset = rasterWorkspace.CreateRasterDataset(this.textBox7. Text, 1,   
        rstPixelType.PT_LONG, rasterStorageDef, "DEFAULTS", rasterDef, geometry Def);
      }
    }
    //加载矢量数据到SDE数据库
    private void button3_Click(object sender, EventArgs e)
    {
      featureWorkspace = workspace as IFeatureWorkspace;
      this.openFileDialog1.Filter = "shp file (*.shp)|*.shp";
      this.openFileDialog1.Title = "打开矢量数据";
      this.openFileDialog1.Multiselect = false;
      string fileName = "";
      if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
      {
        fileName = this.openFileDialog1.FileName;
        string filepath;
        string file;
        int lastIndex;
        lastIndex = fileName.LastIndexOf(@"\");
        filepath = fileName.Substring(0, lastIndex );
        file = fileName.Substring(lastIndex+1);
        //读取SHP数据
        IWorkspaceFactory shpwpf = new ShapefileWorkspaceFactoryClass();
        IWorkspace shpwp = shpwpf.OpenFromFile(filepath, 0);
        IFeatureWorkspace shpfwp = shpwp as IFeatureWorkspace;
        IFeatureClass shpfc = shpfwp.OpenFeatureClass(file);
        //导入SDE数据库 
        IFeatureClass sdeFeatureClass=null;
        IFeatureClassDescription featureClassDescription = new FeatureClass   
        DescriptionClass();
        IObjectClassDescription objectClassDescription = featureClassDescription   
        as IObjectClassDescription;
        IFields fields = shpfc.Fields;
        IFieldChecker fieldChecker = new FieldCheckerClass();
        IEnumFieldError enumFieldError = null;
        IFields validateFields = null;
        fieldChecker.ValidateWorkspace = featureWorkspace as IWorkspace;
        fieldChecker.Validate(fields, out enumFieldError, out validateFields);
        featureDataset = featureWorkspace.OpenFeatureDataset(this.textBox6.Text);
        try
        {
          sdeFeatureClass = featureWorkspace.OpenFeatureClass(shpfc.Alias Name);
        }
        catch (Exception ex)
        {
        }
        //在SDE数据库中创建矢量数据集
        if (sdeFeatureClass == null)
        {
          sdeFeatureClass = featureDataset.CreateFeatureClass(shpfc. AliasName,   
          validateFields,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();
        }
        //加载数据到Mapcontrol
        IFeatureLayer sdeFeatureLayer = new FeatureLayerClass();
        sdeFeatureLayer.FeatureClass = sdeFeatureClass;
        this.axMapControl1.Map.AddLayer(sdeFeatureLayer as ILayer);
        this.axMapControl1.Extent = this.axMapControl1.FullExtent;
        this.axMapControl1.Refresh();
      }

    }
    //加载影像数据到SDE数据库
    private void button4_Click(object sender, EventArgs e)
    {     
      this.openFileDialog1.Filter = "TIFF file (*.tif)|*.tif";
      this.openFileDialog1.Title = "打开影像数据";
      this.openFileDialog1.Multiselect = false;
      string fileName = "";
      if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
      {
        fileName = this.openFileDialog1.FileName;
        string filepath;
        string file;
        int lastIndex;
        lastIndex = fileName.LastIndexOf(@"\");
        filepath = fileName.Substring(0, lastIndex);
        file = fileName.Substring(lastIndex + 1);

        //导入SDE数据库 
        rasterWorkspace = workspace as IRasterWorkspaceEx;
        IWorkspaceFactory tifwpf = new RasterWorkspaceFactoryClass();
        IWorkspace tifwp = tifwpf.OpenFromFile(filepath, 0);
        IRasterWorkspace tifrwp = tifwp as IRasterWorkspace;
        IRasterDataset rasterDataset = tifrwp.OpenRasterDataset(file);   
        IRasterDataset sdeRasterDataset = null;        
        lastIndex = file.LastIndexOf(@".");
        file = file.Substring(0, lastIndex);
        try
        {
          sdeRasterDataset = rasterWorkspace.OpenRasterDataset(file);
        }
        catch (Exception Ex)
        {
        }
        if (sdeRasterDataset == null)
        {
          IGeoDataset geoDataset =rasterDataset as IGeoDataset ;
          IRasterSdeServerOperation rasterSdeServeroperation ;
          IBasicRasterSdeConnection sdeCon = new BasicRasterSdeLoader();
          IPropertySet propertySet = new PropertySetClass();
          propertySet = workspace.ConnectionProperties;
          //建立与SDE数据库的连接
          sdeCon.ServerName = propertySet.GetProperty("server").ToString();
          sdeCon.Instance = propertySet.GetProperty("instance").ToString();
          sdeCon.UserName = propertySet.GetProperty("user").ToString();
          sdeCon.Password = "sde";
          sdeCon.Database = propertySet.GetProperty("database").ToString();
          sdeCon.SdeRasterName = file;
          sdeCon.InputRasterName = fileName;
          rasterSdeServeroperation = sdeCon as IRasterSdeServerOperation;
          //保存影像数据到SDE数据库中
          rasterSdeServeroperation.Create ();
          rasterSdeServeroperation.Update ();
          rasterSdeServeroperation.ComputeStatistics ();
          IRasterLayer rasterLayer=new RasterLayerClass ();
          sdeRasterDataset = rasterWorkspace.OpenRasterDataset(file);
          rasterLayer.CreateFromDataset(sdeRasterDataset);
          this.axMapControl1.Map.AddLayer(rasterLayer as ILayer);
          this.axMapControl1.Extent = this.axMapControl1.FullExtent;
          this.axMapControl1.Refresh();
        }
      }
    }
  }
}
相关文章
|
定位技术 数据格式
GIS开发:arcgis server发布CGCS2000切片
GIS开发:arcgis server发布CGCS2000切片
235 0
|
SQL 开发框架 数据可视化
ArcGIS Engine学习系列1 AE基础介绍
市面上AE教程大致到ArcGIS10.2,ESRI宣布从ArcGIS10.5开始便停止AE的更新,使用AO做开发,初学者可以在学习C#语言后,从AE入门,逐渐过度到AO。不同版本下AE数据类型数量Enums:枚举类型,用于实现一些定义的内容Structs:结构体Interfaces:接口Classes:类AE开发中,为了更好地管理COM对象,ESRI将这些COM对象放到不同的组件库中。
300 0
|
缓存 定位技术
GIS开发:arcgis server切片数据和wmts
GIS开发:arcgis server切片数据和wmts
224 0
|
XML 存储 定位技术
GIS开发:Arcgis的切片格式
GIS开发:Arcgis的切片格式
214 0
ArcGIS开发手动添加License类
版权声明:欢迎评论和转载,转载请注明来源。 https://blog.csdn.net/zy332719794/article/details/43699511 第一步: 在工程中添加类: 类文件名“LicenseInitializer.
1101 0
|
定位技术
ArcGIS Engine中的Symbols详解
本文由本人翻译ESRI官方帮助文档。尊重劳动成果,转载请注明来源。 Symbols     ArcObjects用了三种类型的Symbol(符号样式)来绘制图形特征:marker symbols(标记符号),line symbols(线符号),和fill symbols(填充符号)。
910 0
|
API 定位技术 开发工具
ArcGIS API for Silverlight开发入门准备
         微软的Silverlight提供了跨浏览器和跨平台开发环境,在Web中可用于创建和展现富互联网应用(RIA,Rich  Internet Application)。
1068 0
|
API
收集的一些关于ArcGIS API for Silverlight开发的连接
1、ESRI官方的帮助文档是必不可不看的: http://help.arcgis.com/en/webapi/silverlight/help/index.
677 0
|
人工智能 数据可视化 API
ArcGIS API for Python
ArcGIS API for Python
71 0
|
JavaScript 前端开发 应用服务中间件
Arcgis api for javascript 详细部署
Arcgis api for javascript 详细部署