博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《ArcGIS Engine 地理信息系统开发从入门到精通(第二版)》——6.6 Geodatabase的使用与开发...
阅读量:6219 次
发布时间:2019-06-21

本文共 10721 字,大约阅读时间需要 35 分钟。

本节书摘来自异步社区《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();        }      }    }  }}

转载地址:http://ozmja.baihongyu.com/

你可能感兴趣的文章
从 Everything 到 Listary,自 Launch 归 Wox
查看>>
[Guava源码日报](2)Strings分析
查看>>
TKoa 1.0.1 发布,TypeScript 版的 Node.js Koa 框架
查看>>
使用Free Spire.Presentation生成PowerPoint文件
查看>>
PureScript 0.12.3 发布,静态类型语言
查看>>
Derek解读Bytom源码-Api Server接口服务
查看>>
如何优雅地实现Redis命令setbits与getbits
查看>>
Round Robin 轮询调度算法
查看>>
网络测速插件speedtest
查看>>
使用ABAP编程实现对微软Office Word文档的操作
查看>>
Kafka 分布式消息系统
查看>>
spring源码-开篇
查看>>
Python中的魔法方法
查看>>
MIT识物机器人:“秒懂”物体,过目不忘,不用标记数据!
查看>>
【Web API系列教程】3.10 — 实战:处理数据(发布App到Azure App Service)
查看>>
脑残式网络编程入门(一):跟着动画来学TCP三次握手和四次挥手
查看>>
【Web Audio API】 — 那些年的 web audio
查看>>
深入理解CAS算法原理
查看>>
构建多页的前后分离web项目(alpaca-spa的视图用法)
查看>>
Ubuntu笔记--文件关联软件
查看>>