private void sure_Click(object sender, EventArgs e) { string layer_name = layers1.Text; ILayer choose = null; double @base = double.Parse(basenumber.Text); double interval = double.Parse(dis.Text); ILayer temp_lay; for (int i = 0; i < m_hookHelper.FocusMap.LayerCount; i++) { temp_lay = m_hookHelper.FocusMap.get_Layer(i); if (temp_lay.Name == layer_name) { choose = temp_lay; } } IRasterLayer chooseras = choose as IRasterLayer; ISurfaceOp2 pSurfaceOp = default(ISurfaceOp2); pSurfaceOp = new RasterSurfaceOp() as ISurfaceOp2; IGeoDataset pRasterDataset = chooseras as IGeoDataset; IWorkspace pShpWS = default(IWorkspace); IWorkspaceFactory pShpWorkspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory(); pShpWS = pShpWorkspaceFactory.OpenFromFile(FilePath, 0); pSurfaceOp = new RasterSurfaceOp() as ISurfaceOp2; IRasterAnalysisEnvironment pRasterAEnv = (IRasterAnalysisEnvironment)pSurfaceOp; pRasterAEnv.OutWorkspace = pShpWS; IGeoDataset pOutput = default(IGeoDataset); IFeatureClass pFeatureClass = default(IFeatureClass); IFeatureLayer pFLayer = default(IFeatureLayer); object tmpbase; tmpbase = (object)@base; object tmpmy = 1; pOutput = pSurfaceOp.Contour(pRasterDataset, interval, ref tmpbase, ref tmpmy); pFeatureClass = (IFeatureClass)pOutput; pFLayer = new FeatureLayer(); pFLayer.FeatureClass = pFeatureClass; IGeoFeatureLayer pGeoFL = default(IGeoFeatureLayer); pGeoFL = (IGeoFeatureLayer)pFLayer; pGeoFL.DisplayAnnotation = false; pGeoFL.DisplayField = "CONTOUR"; pGeoFL.Name = "CONTOUR"; m_hookHelper.FocusMap.AddLayer(pGeoFL); }
//加载矢量图层 public static void addShapfileLayer(IMapControl3 map, string shapefilePath) { string fullPath = shapefilePath; //利用"\\"将文件路径分成两部分 int Position = fullPath.LastIndexOf("\\"); //文件目录 string FilePath = fullPath.Substring(0, Position); // string ShpName = fullPath.Substring(Position + 1); IWorkspaceFactory pWF; pWF = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory(); IFeatureWorkspace pFWS; pFWS = (IFeatureWorkspace)pWF.OpenFromFile(FilePath, 0); IFeatureClass pFClass; pFClass = pFWS.OpenFeatureClass(ShpName); IFeatureLayer pFLayer; pFLayer = new FeatureLayer(); pFLayer.FeatureClass = pFClass; pFLayer.Name = pFClass.AliasName; map.AddLayer(pFLayer, 0); // MainFrom.m_mapControl.Refresh(esriViewDrawPhase.esriViewGeography, null, null); //选择数据源 // MainFrom.m_pTocControl.Update(); }
public ESRI.ArcGIS.Geodatabase.IFeatureClass GetFeatureClassFromShapefileOnDisk(System.String string_ShapefileName, out ESRI.ArcGIS.Geodatabase.IWorkspace workspace) { //We have a valid directory, proceed System.IO.FileInfo fileInfo_check = new System.IO.FileInfo(string_ShapefileName); if (fileInfo_check.Exists) { //We have a valid shapefile, proceed ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory(); workspace = workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(string_ShapefileName), 0); ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileName(string_ShapefileName)); return(featureClass); } else { workspace = null; //Not valid shapefile return(null); } }
//#### ArcGIS Snippets #### #region "Add Shapefile Using OpenFileDialog" ///<summary>Add a shapefile to the ActiveView using the Windows.Forms.OpenFileDialog control.</summary> /// ///<param name="activeView">An IActiveView interface</param> /// ///<remarks></remarks> public void AddShapefileUsingOpenFileDialog(ESRI.ArcGIS.Carto.IActiveView activeView) { //parameter check if (activeView == null) { return; } // Use the OpenFileDialog Class to choose which shapefile to load. 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) { // The user chose a particular shapefile. // The returned string will be the full path, filename and file-extension for the chosen shapefile. Example: "C:\test\cities.shp" string shapefileLocation = openFileDialog.FileName; if (shapefileLocation != "") { // Ensure the user chooses a shapefile // Create a new ShapefileWorkspaceFactory CoClass to create a new workspace ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory(); // System.IO.Path.GetDirectoryName(shapefileLocation) returns the directory part of the string. Example: "C:\test\" ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(shapefileLocation), 0); // Explicit Cast // System.IO.Path.GetFileNameWithoutExtension(shapefileLocation) returns the base filename (without extension). Example: "cities" ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation)); ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = new ESRI.ArcGIS.Carto.FeatureLayer(); featureLayer.FeatureClass = featureClass; featureLayer.Name = featureClass.AliasName; featureLayer.Visible = true; activeView.FocusMap.AddLayer(featureLayer); // Zoom the display to the full extent of all layers in the map activeView.Extent = activeView.FullExtent; activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeography, null, null); } else { // The user did not choose a shapefile. // Do whatever remedial actions as necessary // System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #1", // System.Windows.Forms.MessageBoxButtons.OK, // System.Windows.Forms.MessageBoxIcon.Exclamation); } } else { // The user did not choose a shapefile. They clicked Cancel or closed the dialog by the "X" button. // Do whatever remedial actions as necessary. // System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #2", // System.Windows.Forms.MessageBoxButtons.OK, // System.Windows.Forms.MessageBoxIcon.Exclamation); } }
private void browseButton_Click(object sender, EventArgs e) { ESRI.ArcGIS.CatalogUI.IGxDialog browser = new ESRI.ArcGIS.CatalogUI.GxDialog(); browser.ObjectFilter = new GxFilterFeatureClassesClass(); browser.Title = "Name of Feature Class to Create"; if (!browser.DoModalSave((int)Handle)) return; if (browser.ReplacingObject) { MessageBox.Show(@"Cannot overwrite an existing feature class.", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { // shapefile folder if (browser.FinalLocation is IGxFolder) { Dataset = null; ESRI.ArcGIS.Geodatabase.IWorkspaceFactory wsf = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory(); Workspace = wsf.OpenFromFile(browser.FinalLocation.FullName, 0) as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace; } // geodatabase (root level) var database = browser.FinalLocation as IGxDatabase; if (database != null) { Dataset = null; Workspace = database.Workspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace; } // feature dataset in a geodatabase var dataset = browser.FinalLocation as IGxDataset; if (dataset != null) { Dataset = dataset.Dataset as ESRI.ArcGIS.Geodatabase.IFeatureDataset; Workspace = dataset.Dataset.Workspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace; var geoDataset = (ESRI.ArcGIS.Geodatabase.IGeoDataset)Dataset; if (geoDataset != null) spatialReferenceTextBox.Text = geoDataset.SpatialReference.Name; } if (Workspace == null) { MessageBox.Show(@"Location is not a valid feature workspace.", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error); outputPathTextBox.Text = ""; saveButton.Enabled = false; } else { FeatureClassName = browser.Name; outputPathTextBox.Text = browser.FinalLocation.FullName + @"\" + FeatureClassName; saveButton.Enabled = true; } spatialReferenceTextBox.Text = (Dataset == null) ? @"Unknown" : ((ESRI.ArcGIS.Geodatabase.IGeoDataset)Dataset).SpatialReference. Name; } }