public void AddAllFeatureClass() { string txtPath = System.Windows.Forms.Application.StartupPath + "\\..\\Res\\path\\ExtractInfo.txt"; SaveInfoToTxt saveInfoTxt = new SaveInfoToTxt(); string WorkSpacePath = ""; saveInfoTxt.ReadTxt(txtPath, out WorkSpacePath); WorkSpacePath = WorkSpacePath.Trim(); IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass(); IWorkspace workSpace = workspaceFactory.OpenFromFile(WorkSpacePath, 0); IDatasetName datasetName; IEnumDatasetName enumDatasetNameFeature = workSpace.get_DatasetNames(esriDatasetType.esriDTFeatureClass); datasetName = null; while ((datasetName = enumDatasetNameFeature.Next()) != null) { if (datasetName.Type == esriDatasetType.esriDTFeatureClass) { ResourceManager rm = new ResourceManager("ZJGISDataUpdating.Properties.Resources", Assembly.GetExecutingAssembly()); IName name = datasetName as IName; IFeatureClass featureClass = name.Open() as IFeatureClass; string fileName = featureClass.AliasName; DataGridViewRow dgvRow = new DataGridViewRow(); dgvRow = dataGridViewX1.Rows[dataGridViewX1.Rows.Add()]; if (featureClass.ShapeType == esriGeometryType.esriGeometryPolygon) { dgvRow.Cells[2].Value = (Bitmap)rm.GetObject("polygon"); dgvRow.Cells[2].Tag = "面"; } else if (featureClass.ShapeType == esriGeometryType.esriGeometryPolyline || featureClass.ShapeType == esriGeometryType.esriGeometryLine) { dgvRow.Cells[2].Value = (Bitmap)rm.GetObject("line"); dgvRow.Cells[2].Tag = "线"; } else if (featureClass.ShapeType == esriGeometryType.esriGeometryMultipoint || featureClass.ShapeType == esriGeometryType.esriGeometryPoint) { dgvRow.Cells[2].Value = (Bitmap)rm.GetObject("point"); dgvRow.Cells[2].Tag = "点"; } else { MessageBox.Show("请加载正确格式的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); dataGridViewX1.Rows.Remove(dgvRow); return; } DataGridViewCheckBoxCell dgvCheckBoxCell = new DataGridViewCheckBoxCell(); dgvCheckBoxCell = dgvRow.Cells[0] as DataGridViewCheckBoxCell; dgvCheckBoxCell.Value = true; dgvRow.Cells[1].Value = fileName; dgvRow.Cells[1].Tag = featureClass; dgvRow.Cells[3].Value = WorkSpacePath; } } }
private void CreateGeoDatabase() { //为了正常调用 pWorkSpaceFactory.OpenFromFile(pDBPath, 0); //先初始化License如下,否则出未知异常 ESRI.ArcGIS.esriSystem.AoInitialize pLicenseInitialize = new ESRI.ArcGIS.esriSystem.AoInitialize(); pLicenseInitialize.Initialize(ESRI.ArcGIS.esriSystem.esriLicenseProductCode.esriLicenseProductCodeEngine); string pDBDirectory = "C:\\"; string pDBName = "LG.gdb"; string pDBPath = pDBDirectory + pDBName; ESRI.ArcGIS.Geodatabase.IWorkspaceFactory pWorkSpaceFactory = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass(); //ESRI.ArcGIS.Geodatabase.IWorkspaceName pWorkspaceName = pWorkSpaceFactory.Create(pDBDirectory, pDBName, null, 0); if (pWorkSpaceFactory.IsWorkspace(pDBPath)) { pWorkSpaceFactory.OpenFromFile(pDBPath, 0); } }
public static IPolygon GetPolyGonFromFile(string path) { IPolygon pGon = null; if (path.EndsWith(".mdb")) { string errmsg = ""; IWorkspaceFactory pwf = new ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass(); IWorkspace pworkspace = pwf.OpenFromFile(path, 0); IEnumDataset pEnumdataset = pworkspace.get_Datasets(esriDatasetType.esriDTFeatureClass); pEnumdataset.Reset(); IDataset pDataset = pEnumdataset.Next(); while (pDataset != null) { IFeatureClass pFeatureclass = pDataset as IFeatureClass; if (pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolygon && pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolyline) { pDataset = pEnumdataset.Next(); continue; } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolygon) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { pGon = pFeature.Shape as IPolygon; break; } else { pDataset = pEnumdataset.Next(); continue; } } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolyline) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { IPolyline pPolyline = pFeature.Shape as IPolyline; pGon = GetPolygonFormLine(pPolyline); if (pGon.IsClosed == false) { errmsg = "选择的要素不能构成封闭多边形!"; pGon = null; pDataset = pEnumdataset.Next(); continue; } else { break; } } } } if (pGon == null) { IEnumDataset pEnumdataset1 = pworkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset); pEnumdataset1.Reset(); pDataset = pEnumdataset1.Next(); while (pDataset != null) { IFeatureDataset pFeatureDataset = pDataset as IFeatureDataset; IEnumDataset pEnumDataset2 = pFeatureDataset.Subsets; pEnumDataset2.Reset(); IDataset pDataset1 = pEnumDataset2.Next(); while (pDataset1 != null) { if (pDataset1 is IFeatureClass) { IFeatureClass pFeatureclass = pDataset1 as IFeatureClass; if (pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolygon && pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolyline) { pDataset1 = pEnumDataset2.Next(); continue; } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolygon) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { pGon = pFeature.Shape as IPolygon; break; } else { pDataset1 = pEnumDataset2.Next(); continue; } } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolyline) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { IPolyline pPolyline = pFeature.Shape as IPolyline; pGon = GetPolygonFormLine(pPolyline); if (pGon.IsClosed == false) { errmsg = "选择的要素不能构成封闭多边形!"; pGon = null; pDataset1 = pEnumDataset2.Next(); continue; } else { break; } } } } } if (pGon != null) { break; } pDataset = pEnumdataset1.Next(); } } if (pGon == null) { if (errmsg != "") { System.Windows.Forms.MessageBox.Show(errmsg, "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); } else { System.Windows.Forms.MessageBox.Show("请选择一个包含面要素和线要素的文件", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); } return(pGon); } } else if (path.EndsWith(".shp")) { IWorkspaceFactory pwf = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); string filepath = System.IO.Path.GetDirectoryName(path); string filename = path.Substring(path.LastIndexOf("\\") + 1); IFeatureWorkspace pFeatureworkspace = (IFeatureWorkspace)pwf.OpenFromFile(filepath, 0); IFeatureClass pFeatureclass = pFeatureworkspace.OpenFeatureClass(filename); if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolygon) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { pGon = pFeature.Shape as IPolygon; } } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolyline) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { IPolyline pPolyline = pFeature.Shape as IPolyline; pGon = GetPolygonFormLine(pPolyline); if (pGon.IsClosed == false) { System.Windows.Forms.MessageBox.Show("选择的线要素不能构成封闭多边形!", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); return(null); } } } else { System.Windows.Forms.MessageBox.Show("请选择一个面或者线要素文件!", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); return(null); } } else if (path.EndsWith(".txt")) { string txtpath = path; System.IO.StreamReader smRead = new System.IO.StreamReader(txtpath, System.Text.Encoding.Default); //设置路径 string line; IPointCollection pc = pGon as IPointCollection; double x, y; while ((line = smRead.ReadLine()) != null) { if (line.IndexOf(",") > 0) { try { x = double.Parse(line.Substring(0, line.IndexOf(","))); y = double.Parse(line.Substring(line.IndexOf(",") + 1)); } catch { System.Windows.Forms.MessageBox.Show("文本文件格式不正确!", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); smRead.Close(); return(null); } IPoint tmpPoint = new ESRI.ArcGIS.Geometry.Point(); tmpPoint.X = x; tmpPoint.Y = y; object ep = System.Reflection.Missing.Value; pc.AddPoint(tmpPoint, ref ep, ref ep); } } smRead.Close(); ICurve pCurve = pGon as ICurve; if (pCurve.IsClosed == false) { System.Windows.Forms.MessageBox.Show("导入点坐标不能构成封闭多边形!", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); return(null); } } else if (path.EndsWith("gdb")) { string errmsg = ""; IWorkspaceFactory pwf = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass(); IWorkspace pworkspace = pwf.OpenFromFile(path.Substring(0, path.LastIndexOf("\\")), 0); IEnumDataset pEnumdataset = pworkspace.get_Datasets(esriDatasetType.esriDTFeatureClass); pEnumdataset.Reset(); IDataset pDataset = pEnumdataset.Next(); while (pDataset != null) { IFeatureClass pFeatureclass = pDataset as IFeatureClass; if (pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolygon && pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolyline) { pDataset = pEnumdataset.Next(); continue; } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolygon) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { pGon = pFeature.Shape as IPolygon; break; } else { pDataset = pEnumdataset.Next(); continue; } } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolyline) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { IPolyline pPolyline = pFeature.Shape as IPolyline; pGon = GetPolygonFormLine(pPolyline); if (pGon.IsClosed == false) { errmsg = "选择的要素不能构成封闭多边形!"; pGon = null; pDataset = pEnumdataset.Next(); continue; } else { break; } } } } if (pGon == null) { IEnumDataset pEnumdataset1 = pworkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset); pEnumdataset1.Reset(); pDataset = pEnumdataset1.Next(); while (pDataset != null) { IFeatureDataset pFeatureDataset = pDataset as IFeatureDataset; IEnumDataset pEnumDataset2 = pFeatureDataset.Subsets; pEnumDataset2.Reset(); IDataset pDataset1 = pEnumDataset2.Next(); while (pDataset1 != null) { if (pDataset1 is IFeatureClass) { IFeatureClass pFeatureclass = pDataset1 as IFeatureClass; if (pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolygon && pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolyline) { pDataset1 = pEnumDataset2.Next(); continue; } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolygon) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { pGon = pFeature.Shape as IPolygon; break; } else { pDataset1 = pEnumDataset2.Next(); continue; } } else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolyline) { IFeatureCursor pCursor = pFeatureclass.Search(null, false); IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { IPolyline pPolyline = pFeature.Shape as IPolyline; pGon = GetPolygonFormLine(pPolyline); if (pGon.IsClosed == false) { errmsg = "选择的要素不能构成封闭多边形!"; pGon = null; pDataset1 = pEnumDataset2.Next(); continue; } else { break; } } } } } if (pGon != null) { break; } pDataset = pEnumdataset1.Next(); } } if (pGon == null) { if (errmsg != "") { System.Windows.Forms.MessageBox.Show(errmsg, "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); } else { System.Windows.Forms.MessageBox.Show("请选择一个包含面要素和线要素的文件", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); } return(pGon); } } return(pGon); }
/// <summary> Shows dialog for saving data.</summary> /// <param name="filters"> for example: /// IGxObjectFilter ipFilter1 = new GxFilterFGDBFeatureClassesClass() ; /// List<IGxObjectFilter> gxFilterList = new List<IGxObjectFilter>(new IGxObjectFilter[] { ipFilter1 });</param> /// <param name="dialogTitle">The title of the dialog</param> /// <returns> the path to the file to save </returns> public static string ShowSaveDataDialog(List<IGxObjectFilter> filters, string dialogTitle) { try { IGxDialog pGxDialog = new GxDialogClass(); pGxDialog.Title = dialogTitle; pGxDialog.AllowMultiSelect = false; // Create a filter collection for the dialog. IGxObjectFilterCollection pFilterCol = (IGxObjectFilterCollection)pGxDialog; foreach (IGxObjectFilter filt in filters) { pFilterCol.AddFilter(filt, false); } // Open the dialog if (!pGxDialog.DoModalSave(0)) { return null; } string dirPath = pGxDialog.FinalLocation.FullName; string path = dirPath + "\\" + pGxDialog.Name; if (dirPath.ToLowerInvariant().EndsWith(".gdb")) { IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass(); IWorkspace2 workspace = (IWorkspace2)workspaceFactory.OpenFromFile(dirPath, 0); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; if (workspace.get_NameExists(esriDatasetType.esriDTFeatureClass, pGxDialog.Name)) { IDataset fc = (IDataset)featureWorkspace.OpenFeatureClass(pGxDialog.Name); DialogResult dlg = MessageBox.Show(pGxDialog.Name + " bestaat al, wilt u dit overschrijven?", "Feature Class bestaat al", MessageBoxButtons.YesNo); if (dlg == DialogResult.No) { return null; } else if (!fc.CanDelete()) { MessageBox.Show(pGxDialog.Name + " is al in gebruik, u kunt dit niet overschrijven.", "Feature Class in gebruik"); return null; } } } else if (Directory.Exists(dirPath)) { IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspace2 workspace = (IWorkspace2)workspaceFactory.OpenFromFile(dirPath, 0); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; if (workspace.get_NameExists(esriDatasetType.esriDTFeatureClass, pGxDialog.Name)) { IDataset fc = (IDataset)featureWorkspace.OpenFeatureClass(pGxDialog.Name); if (!fc.CanDelete()) { MessageBox.Show(pGxDialog.Name + " is al in gebruik, u kunt dit niet overschrijven.", "Feature Class in gebruik"); return null; } } } return path; } catch (Exception ex) { MessageBox.Show(ex.Message + " " + ex.StackTrace); return null; } }
/// <summary>Delete a existing shapefile</summary> /// <param name="path">the full path to the shapefile (*.shp)</param> /// <returns>return if succesfull otherwise false</returns> public static bool deleteFeatureClass( string path ) { FileInfo featureClassPath = new FileInfo(path); if ( path.ToLowerInvariant().EndsWith( ".shp")) { if (!featureClassPath.Exists) return true; //create factory IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile( featureClassPath.DirectoryName, 0); //create featureclass IFeatureClass featureClass = featureWorkspace.OpenFeatureClass( featureClassPath.Name.Replace(featureClassPath.Extension, "")); //cast tot dataset and delete IDataset ds = featureClass as IDataset; if (ds.CanDelete()) { ds.Delete(); return true; } else { return false; } } else if ( featureClassPath.DirectoryName.ToLowerInvariant().EndsWith(".gdb") ) { IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass(); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile( featureClassPath.DirectoryName, 0); IWorkspace2 ws = (IWorkspace2)featureWorkspace; if (!ws.get_NameExists(esriDatasetType.esriDTFeatureClass, featureClassPath.Name)) { return true; } IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(featureClassPath.Name); IDataset ds = (IDataset) featureClass; if (ds.CanDelete()) { ds.Delete(); return true; } else { return false; } } else { throw new Exception("File is not a shapefile or Filegeodatabase Feature Class"); } }
/// <summary>Create a feature class in a existing FGDB </summary> /// <param name="FGDBPath">Path to the existing FGDB</param> /// <param name="FCname">Name of the new FeatureClass</param> /// <param name="field2add">List of Ifields</param> /// <param name="srs">the srs of the shapefile</param> /// <param name="geomType">the type geometry: point, polyline, polygon, ...</param> /// <param name="deleteIfExists">Overwrite existing FeatureClass</param> /// <returns>the Feuture Class loaded in a IFeatureClass</returns> public static IFeatureClass createFeatureClass(string FGDBPath, string FCname, List<IField> field2add, ISpatialReference srs, esriGeometryType geomType, bool deleteIfExists = true) { DirectoryInfo fgbInfo = new DirectoryInfo(FGDBPath); if (FGDBPath == "" || FCname == "") return null; // name was not passed in IFeatureClass featureClass; // Instantiate a feature class description to get the required fields. IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass(); IObjectClassDescription ocDescription = fcDescription as IObjectClassDescription; IFields fields = ocDescription.RequiredFields; IFieldsEdit fieldsEdit = (IFieldsEdit) fields ; // Find the shape field in the required fields and modify its GeometryDef to // use wanted geometry and to set the spatial reference. IField shapeField = null; for (int j = 0; j < fields.FieldCount; j++) { if (fields.get_Field(j).Type == ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry) { shapeField = fields.get_Field(j); break; } } if (shapeField == null) return null; int shapeFieldIndex = fields.FindField(shapeField.Name); IField geofield = fields.get_Field(shapeFieldIndex); IGeometryDef geometryDef = geofield.GeometryDef; IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; geometryDefEdit.GeometryType_2 = geomType; geometryDefEdit.SpatialReference_2 = srs; foreach (IField field in field2add) { fieldsEdit.AddField(field); } IWorkspaceFactory2 workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass(); IWorkspace2 workspace = (IWorkspace2)workspaceFactory.OpenFromFile(FGDBPath, 0); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; string validatedName = ValidateTableName((IWorkspace)workspace, FCname); if (workspace.get_NameExists(esriDatasetType.esriDTFeatureClass, validatedName)) { if (deleteIfExists) { if (!deleteFeatureClass(fgbInfo.FullName + "\\" + validatedName)) { throw new Exception(validatedName + " exists and cannot be deleted"); } } else { throw new Exception(validatedName + " exists"); } } IFields validatedFields = validateFields((IWorkspace) workspace, fields); //create the feature class featureClass = featureWorkspace.CreateFeatureClass(validatedName, validatedFields, ocDescription.InstanceCLSID, ocDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, shapeField.Name, ""); return featureClass; }