1. 程式人生 > >ARCEngine中新增Shp資料,柵格資料,GDB資料

ARCEngine中新增Shp資料,柵格資料,GDB資料

目錄

新增shp資料:

新增柵格資料:

新增GDB資料:


新增shp資料:

 private void 新增要素資料集ToolStripMenuItem_Click(object sender, EventArgs e)
       {
           System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
           openFileDialog.InitialDirectory = "c:\\";
           openFileDialog.Filter = "Shapefiles (*.shp)|*.shp";
           openFileDialog.FilterIndex = 2;
           openFileDialog.RestoreDirectory = true;
           openFileDialog.Multiselect = false;

           if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
           {
               string shapefileLocation = openFileDialog.FileName;
               if (shapefileLocation != "")
               {
                   IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
                   IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(shapefileLocation), 0);
                   IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation));
                   IFeatureLayer featureLayer = new FeatureLayerClass();
                   featureLayer.FeatureClass = featureClass;
                   featureLayer.Name = featureClass.AliasName;
                   featureLayer.Visible = true;
                   IActiveView activeView = m_mapControl4.ActiveView;
                   activeView.FocusMap.AddLayer(featureLayer);
                   activeView.Extent = activeView.FullExtent;
                   activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
               }
               else
               {
                   System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #1",
                                                       System.Windows.Forms.MessageBoxButtons.OK,
                                                       System.Windows.Forms.MessageBoxIcon.Exclamation);
               }
           }
       }

新增柵格資料:

 private void 新增柵格資料ToolStripMenuItem_Click(object sender, EventArgs e)
       {
           string file, sfilepath, sfilename;
           object missing = Type.Missing;
           try
           {
               OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
               OpenFileDialog1.Title = "選擇要新增的資料檔案";
               OpenFileDialog1.Filter = "影象資料(*.jpg,*.bmp,*.gif)|*.jpg;*.bmp;*.gif";               
               if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
               {
                   file = OpenFileDialog1.FileName;
                   sfilepath = file.Substring(0, file.LastIndexOf("\\"));
                   sfilename = file.Substring(file.LastIndexOf("\\") + 1, file.Length - file.LastIndexOf("\\") - 1);
                   IWorkspaceFactory WorkspaceFactory = new RasterWorkspaceFactoryClass();
                   IWorkspace Workspace = WorkspaceFactory.OpenFromFile(sfilepath, 0);
                   IRasterWorkspace RasterWorkspace = Workspace as IRasterWorkspace;
                   IRasterDataset RasterDataset = RasterWorkspace.OpenRasterDataset(sfilename);
                   IRaster Raster = RasterDataset.CreateDefaultRaster();
                   IRasterLayer RasterLayer = new RasterLayerClass();
                   RasterLayer.CreateFromRaster(Raster);
                   m_mapControl4.AddLayer(RasterLayer as ILayer, 0);
                   m_mapControl4.Refresh();
               }
           }
           catch (Exception ex) { MessageBox.Show(ex.ToString()); }
       }

新增GDB資料:

  private void 新增GDB資料ToolStripMenuItem_Click(object sender, EventArgs e)
       {
           IFeatureWorkspace pFeatureWorkspace;
           IFeatureLayer pFeatureLayer;
           IFeatureDataset pFeatureDataset;
           FolderBrowserDialog addgdb = new FolderBrowserDialog();
           addgdb.SelectedPath = "";
           addgdb.ShowDialog();
           IWorkspaceFactory pFileGDBWorkspaceFactory = new FileGDBWorkspaceFactory();
           IWorkspace pWorkspace = pFileGDBWorkspaceFactory.OpenFromFile(addgdb.SelectedPath, 0);
           IFeatureWorkspace pFeatWorkspace = pWorkspace as IFeatureWorkspace;
           IEnumDataset pEnumDataset = pWorkspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
           pEnumDataset.Reset();
           IDataset pDataset = pEnumDataset.Next();
           if (pDataset is IFeatureDataset)
           {
               pFeatureWorkspace = (IFeatureWorkspace)pFileGDBWorkspaceFactory.OpenFromFile(addgdb.SelectedPath, 0);
               pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(pDataset.Name);
               IEnumDataset pEnumDataset1 = pFeatureDataset.Subsets;
               pEnumDataset1.Reset();
               IDataset pDataset1 = pEnumDataset1.Next();
               try
               {
                   if (pDataset1 is IFeatureClass)
                   {
                       pFeatureLayer = new FeatureLayerClass();
                       pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pDataset1.Name);
                       pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
                       IActiveView activeView = m_mapControl4.ActiveView;
                       activeView.FocusMap.AddLayer(pFeatureLayer);
                       activeView.Extent = activeView.FullExtent;
                       activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);

                   }
               }
               catch (Exception ex) { return; }
           }
           addgdb.Dispose();
       }