自己看,不解释!
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
/*
*******************************************************
*
* SXW 2010 08 14
* 本类部分功能接口依附于_workSpaceInstance
* 必须首先使用CreateWorkspace或者OpenWorkspaceFromFile
* 实例化_workSpaceInstance,才能使用
*
******************************************************* */
using System;
using System.Windows.Forms;
using System.IO;
using System.Data;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.CatalogUI;
namespace ControlLibrary
{
class RuleDBCreater
{
private static IWorkspace _workSpaceInstance;
public RuleDBCreater()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static void setWorkSpaceInstance(IWorkspace ws)
{
_workSpaceInstance = ws;
}
public static IWorkspace getWorkSpaceInstance()
{
return _workSpaceInstance;
}
/// <summary>
/// 创建规则数据库workspace
/// </summary>
/// <param name="workspaceType"></param>
/// <param name="WorkspaceDirectory"></param>
/// <returns> 返回workspace实例 </returns>
public static IWorkspace CreateWorkspace( string pName, string pPath)
{
IWorkspaceFactory workspaceFactory = null ;
Type factoryType = Type.GetTypeFromProgID( " esriDataSourcesGDB.AccessWorkspaceFactory " );
workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
Directory.CreateDirectory(pPath);
IWorkspaceName workspaceName = workspaceFactory.Create(pPath, pName, null , 0 );
IName Name = (IName)workspaceName;
_workSpaceInstance = (IWorkspace)(Name.Open());
return _workSpaceInstance;
}
/// <summary>
/// 从文件创建规则数据库workspace
/// </summary>
/// <param name="WorkspaceDirectory"></param>
/// <returns> 返回workspace实例 </returns>
public static IWorkspace OpenWorkspaceFromFile( string WorkspaceDirectory)
{
IWorkspaceFactory workspaceFactory = null ;
Type factoryType = Type.GetTypeFromProgID( " esriDataSourcesGDB.AccessWorkspaceFactory " );
workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
_workSpaceInstance = workspaceFactory.OpenFromFile(WorkspaceDirectory, 0 );
return _workSpaceInstance;
}
/// <summary>
/// 创建要素数据集
/// </summary>
/// <param name="name"></param>
/// <returns> IFeatureDataset </returns>
public static IFeatureDataset CreateFeatureDataSet( /* string name,ISpatialReference srf */ )
{
IFeatureDatasetDialog fdlg = new FeatureDatasetDefDialog();
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)_workSpaceInstance;
return fdlg.DoModalCreate(featureWorkspace, 0 );
// if (_workSpaceInstance == null) return null;
// IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)_workSpaceInstance;
// IFeatureDataset tmp = featureWorkspace.CreateFeatureDataset(name, srf);
// return tmp;
}
/// <summary>
/// 删除要素数据集
/// </summary>
/// <param name="name"></param>
/// <param name="srf"></param>
/// <returns></returns>
public static void DeleteFeatureDataSet( string name)
{
IFeatureDataset fds = GetFeatureDataSet(name);
if (fds != null )
fds.Delete();
}
/// <summary>
/// 获取数据库中IFeatureDataset
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static IFeatureDataset GetFeatureDataSet( string name)
{
if (_workSpaceInstance == null ) return null ;
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)_workSpaceInstance;
IFeatureDataset tmp = featureWorkspace.OpenFeatureDataset(name);
return tmp;
}
/// <summary>
/// 重命名DataSet
/// </summary>
/// <param name="fds"></param>
/// <param name="newname"></param>
public static void RenameFeatureDataSet(IFeatureDataset fds, string newname)
{
if (fds.CanRename())
{
fds.Rename(newname);
}
}
/// <summary>
/// 获取IFeatureDataset空间参照
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public static ISpatialReference GetFdsSpatialReference(IFeatureDataset ds)
{
IGeoDataset gds = ds as IGeoDataset;
return gds.SpatialReference;
}
/// <summary>
/// 设置IFeatureDataset空间参照
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public static void SetFdsSpatialReference(IFeatureDataset ds)
{
IGeoDataset gds = ds as IGeoDataset;
// Cast the geodataset to the IGeoDatasetSchemaEdit interface and set the Spatial Reference.
IGeoDatasetSchemaEdit geoDatasetSchemaEdit = (IGeoDatasetSchemaEdit)gds;
if (geoDatasetSchemaEdit.CanAlterSpatialReference)
{
geoDatasetSchemaEdit.AlterSpatialReference(EditSpatialReferenceDlg(gds.SpatialReference));
}
}
/// <summary>
/// 创建属性字段
/// </summary>
/// <param name="shapeType"></param>
/// <param name="dgrc"></param>
/// <param name="spr"></param>
/// <returns></returns>
public static IFields CreateFields( string shapeType, DataTable dt, ISpatialReference spr, bool hasM, bool hasZ)
{
IField oField = new FieldClass();
IFieldEdit oFieldEdit = oField as IFieldEdit;
IFields oFields = new FieldsClass();
IFieldsEdit oFieldsEdit = oFields as IFieldsEdit;
IGeometryDef geometryDef = new GeometryDefClass();
IGeometryDefEdit geometryDefEdit = geometryDef as IGeometryDefEdit;
foreach (DataRow dr in dt.Rows)
{
switch (dr[ 1 ].ToString())
{
case " Object ID " :
oFieldEdit.Name_2 = " ObjectID " ;
oFieldEdit.AliasName_2 = " FID " ;
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
oFieldsEdit.AddField(oField);
break ;
case " Multipoint Features " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = " SHAPE " ;
oFieldEdit.IsNullable_2 = true ;
oFieldEdit.Required_2 = true ;
geometryDefEdit.AvgNumPoints_2 = 1 ;
geometryDefEdit.GridCount_2 = 0 ;
geometryDefEdit.HasM_2 = hasM;
geometryDefEdit.HasZ_2 = hasZ;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryMultipoint;
geometryDefEdit.SpatialReference_2 = spr;
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
oFieldEdit.GeometryDef_2 = (GeometryDef)geometryDefEdit;
oFieldsEdit.AddField(oField);
break ;
case " MultiPatch Features " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = " SHAPE " ;
oFieldEdit.IsNullable_2 = true ;
oFieldEdit.Required_2 = true ;
geometryDef = new GeometryDefClass();
geometryDefEdit = geometryDef as IGeometryDefEdit;
geometryDefEdit.AvgNumPoints_2 = 1 ;
geometryDefEdit.GridCount_2 = 0 ;
geometryDefEdit.HasM_2 = hasM;
geometryDefEdit.HasZ_2 = hasZ;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryMultiPatch;
geometryDefEdit.SpatialReference_2 = spr;
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
oFieldEdit.GeometryDef_2 = (GeometryDef)geometryDefEdit;
oFieldsEdit.AddField(oField);
break ;
case " Point Features " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = " SHAPE " ;
oFieldEdit.IsNullable_2 = true ;
oFieldEdit.Required_2 = true ;
geometryDef = new GeometryDefClass();
geometryDefEdit = geometryDef as IGeometryDefEdit;
geometryDefEdit.AvgNumPoints_2 = 1 ;
geometryDefEdit.GridCount_2 = 0 ;
geometryDefEdit.HasM_2 = hasM;
geometryDefEdit.HasZ_2 = hasZ;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
geometryDefEdit.SpatialReference_2 = spr;
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
oFieldEdit.GeometryDef_2 = (GeometryDef)geometryDefEdit;
oFieldsEdit.AddField(oField);
break ;
case " Line Features " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = " SHAPE " ;
oFieldEdit.IsNullable_2 = true ;
oFieldEdit.Required_2 = true ;
geometryDef = new GeometryDefClass();
geometryDefEdit = geometryDef as IGeometryDefEdit;
geometryDefEdit.AvgNumPoints_2 = 1 ;
geometryDefEdit.GridCount_2 = 0 ;
geometryDefEdit.HasM_2 = hasM;
geometryDefEdit.HasZ_2 = hasZ;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryLine;
geometryDefEdit.SpatialReference_2 = spr;
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
oFieldEdit.GeometryDef_2 = (GeometryDef)geometryDefEdit;
oFieldsEdit.AddField(oField);
break ;
case " Polygon Features " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = " SHAPE " ;
oFieldEdit.IsNullable_2 = true ;
oFieldEdit.Required_2 = true ;
geometryDef = new GeometryDefClass();
geometryDefEdit = geometryDef as IGeometryDefEdit;
geometryDefEdit.AvgNumPoints_2 = 1 ;
geometryDefEdit.GridCount_2 = 0 ;
geometryDefEdit.HasM_2 = hasM;
geometryDefEdit.HasZ_2 = hasZ;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
geometryDefEdit.SpatialReference_2 = spr;
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
oFieldEdit.GeometryDef_2 = (GeometryDef)geometryDefEdit;
oFieldsEdit.AddField(oField);
break ;
case " Short Int " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = dr[ 0 ].ToString();
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger;
oFieldEdit.IsNullable_2 = true ;
oFieldsEdit.AddField(oField);
break ;
case " Long Int " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = dr[ 0 ].ToString();
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
oFieldsEdit.AddField(oField);
break ;
case " Float " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = dr[ 0 ].ToString();
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeSingle;
oFieldsEdit.AddField(oField);
break ;
case " Double " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = dr[ 0 ].ToString();
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
oFieldsEdit.AddField(oField);
break ;
case " Text " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = dr[ 0 ].ToString();
oFieldEdit.Length_2 = 30 ; // Only string fields require that you set the length.
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
oFieldsEdit.AddField(oField);
break ;
case " Date " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = dr[ 0 ].ToString();
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate;
oFieldsEdit.AddField(oField);
break ;
// case "Guid":
// IField fld12 = new FieldClass();
// IFieldEdit fldedt12 = (IFieldEdit)fld12;
// fldedt12.Name_2 = dr[0].ToString();
// fldedt12.Type_2 = esriFieldType.esriFieldTypeGUID;
// fieldsEdit.set_Field(i, fld12);
// break;
// case "Geomery":
// IField fld13 = new FieldClass();
// IFieldEdit fldedt13 = (IFieldEdit)fld13;
// fldedt13.Name_2 = dr[0].ToString();
// fldedt13.IsNullable_2 = true;
// fldedt13.Required_2 = true;
// IGeometryDef geometryDef13 = new GeometryDefClass();
// IGeometryDefEdit geometryDefEdit13 = (IGeometryDefEdit)geometryDef13;
// geometryDef13 = new GeometryDefClass();
// geometryDefEdit13 = (IGeometryDefEdit)geometryDef13;
// geometryDefEdit13.AvgNumPoints_2 = 1;
// geometryDefEdit13.GridCount_2 = 0;
// geometryDefEdit13.HasM_2 = hasM;
// geometryDefEdit13.HasZ_2 = hasZ;
// fldedt13.Type_2 = esriFieldType.esriFieldTypeGeometry;
// geometryDefEdit13.GeometryType_2 = esriGeometryType.esriGeometryAny;
// // Generate a default Spatial Reference
// geometryDefEdit13.SpatialReference_2 = spr;
// fldedt13.GeometryDef_2 = (GeometryDef)geometryDefEdit13;
// fldedt13.Type_2 = esriFieldType.esriFieldTypeGeometry;
// fieldsEdit.set_Field(i, fld13);
// break;
// case "Raster":
// IField fld14 = new FieldClass();
// IFieldEdit fldedt14 = (IFieldEdit)fld14;
// fldedt14.Name_2 = dr[0].ToString();
// fldedt14.Type_2 = esriFieldType.esriFieldTypeRaster;
// fieldsEdit.set_Field(i, fld14);
// break;
}
}
return oFields;
}
/// <summary>
/// 创建要素类
/// </summary>
/// <param name="workspace"></param>
/// <param name="featureDataset"></param>
/// <param name="featureClassName"></param>
/// <param name="fields"></param>
/// <param name="CLSID"></param>
/// <param name="CLSEXT"></param>
/// <param name="strConfigKeyword"></param>
/// <returns></returns>
public static IFeatureClass CreateFeatureClass(IFeatureDataset featureDataset, String featureClassName, IFields fields)
{
if (_workSpaceInstance == null ) return null ;
IFeatureClass featureClass = null ;
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)_workSpaceInstance;
// assign the class id value if not assigned
ESRI.ArcGIS.esriSystem.UID CLSID = null ;
ESRI.ArcGIS.esriSystem.UID CLSEXT = null ;
string strConfigKeyword = "" ;
if (CLSID == null )
{
CLSID = new ESRI.ArcGIS.esriSystem.UIDClass();
CLSID.Value = " esriGeoDatabase.Feature " ;
}
System.String strShapeField = "" ;
// locate the shape field
for (Int32 j = 0 ; j < fields.FieldCount; j ++ )
{
esriFieldType dd = fields.get_Field(j).Type;
if (dd == esriFieldType.esriFieldTypeGeometry)
{
strShapeField = fields.get_Field(j).Name;
}
}
// finally create and return the feature class
if (featureDataset != null )
{
featureClass = featureDataset.CreateFeatureClass(featureClassName, fields, CLSID, CLSEXT, esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword);
}
return featureClass;
}
/// <summary>
/// 获取FeatureClass从IFeatureDataset
/// </summary>
/// <param name="featDs"></param>
/// <param name="className"></param>
/// <returns></returns>
public static IFeatureClass GetFeatureClassFromFeatureDataset(IFeatureDataset featDs, string className)
{
IFeatureClass featClass;
IFeatureClassContainer fcContainer = featDs as IFeatureClassContainer;
for ( int i = 0 ; i < fcContainer.ClassCount; i ++ )
{
featClass = fcContainer.get_Class(i);
if (RuleDBCreater.GetFeatureClassName(featClass) == className)
{
return featClass;
}
}
return null ;
}
/// <summary>
/// 重命名FeatureClass
/// </summary>
/// <param name="fds"></param>
/// <param name="newname"></param>
public static void RenameFeatureClass(IFeatureClass fc, string newname)
{
IDataset ds = fc as IDataset;
if (ds.CanRename())
{
ds.Rename(newname);
}
}
/// <summary>
/// 从数据集删除要素类
/// </summary>
/// <param name="featureDataset"></param>
/// <param name="featureClassName"></param>
/// <param name="fields"></param>
/// <returns></returns>
public static void DeleteFeatureClass(IFeatureDataset featureDataset, String featureClassName)
{
IFeatureClass fc = GetFeatureClassFromFeatureDataset(featureDataset, featureClassName);
if (fc != null )
{
IDataset ds = fc as IDataset;
if (ds.CanDelete())
{
ds.Delete();
}
}
}
/// <summary>
/// 编辑空间参照
/// </summary>
/// <param name="inputSpr"></param>
/// <returns></returns>
public static ISpatialReference EditSpatialReferenceDlg(ISpatialReference inputSpr)
{
ESRI.ArcGIS.CatalogUI.ISpatialReferenceDialog2 spatialReferenceDialog2 = new ESRI.ArcGIS.CatalogUI.SpatialReferenceDialogClass();
ISpatialReference spf = spatialReferenceDialog2.DoModalEdit(inputSpr, false , false , false , false , false , false , false , 0 );
return spf;
}
/// <summary>
/// 创建空间参照
/// </summary>
/// <param name="coordinateSystem"></param>
/// <returns></returns>
public static ISpatialReference CreateSpatialReferenceDlg() //
{
ESRI.ArcGIS.CatalogUI.ISpatialReferenceDialog2 spatialReferenceDialog2 = new ESRI.ArcGIS.CatalogUI.SpatialReferenceDialogClass();
ISpatialReference spatialReference = spatialReferenceDialog2.DoModalCreate( true , false , false , 0 );
// ISpatialReferenceTolerance spatialReferenceTolerance = spatialReference as ISpatialReferenceTolerance;
return spatialReference;
}
public static ISpatialReference SetXYZMTolerance(ISpatialReference srf, double xytolerance, double ztolerance, double mtolerance)
{
ISpatialReferenceTolerance spatialReferenceTolerance = srf as ISpatialReferenceTolerance;
// spatialReferenceTolerance.XYTolerance = xytolerance;
// spatialReferenceTolerance.ZTolerance = ztolerance;
// spatialReferenceTolerance.MTolerance = mtolerance;
return srf;
}
public static void GetXYZMTolerance(ISpatialReference srf, out double xytolerance, out double ztolerance, out double mtolerance)
{
ISpatialReferenceTolerance spatialReferenceTolerance = srf as ISpatialReferenceTolerance;
xytolerance = spatialReferenceTolerance.XYTolerance;
ztolerance = spatialReferenceTolerance.ZTolerance;
mtolerance = spatialReferenceTolerance.MTolerance;
}
/// <summary>
/// 获取工作区内所有数据集名称
/// </summary>
/// <param name="ws"></param>
/// <returns></returns>
public static IEnumDatasetName GetFeatureDataSetNames(IWorkspace ws)
{
return ws.get_DatasetNames(esriDatasetType.esriDTFeatureDataset);
}
public static bool IsArcDbFile( string path)
{
IWorkspaceFactory workspaceFactory = null ;
Type factoryType = Type.GetTypeFromProgID( " esriDataSourcesGDB.AccessWorkspaceFactory " );
workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
return workspaceFactory.IsWorkspace(path);
}
public static string GetFeatureClassName(IFeatureClass fc)
{
IDataset ds = fc as IDataset;
return ds.Name;
}
public static IEnumFeatureClass GetFeatureClasses(IFeatureDataset fds)
{
IFeatureClassContainer pFeatureclassContainer = (IFeatureClassContainer)fds;
return (IEnumFeatureClass)pFeatureclassContainer.Classes;
}
public static bool IsFeatureDataSetExist( string name)
{
// if (_workSpaceInstance == null) return true;
IEnumDatasetName iEdn = GetFeatureDataSetNames(_workSpaceInstance);
iEdn.Reset();
IDatasetName iDsn = iEdn.Next();
while (iDsn is IFeatureDatasetName)
{
if (iDsn.Name == name)
return true ;
iDsn = iEdn.Next();
}
return false ;
}
public static bool IsFeatureClassExist( string dsname, string fcname)
{
IEnumFeatureClass iEfc = GetFeatureClasses(GetFeatureDataSet(dsname));
iEfc.Reset();
IFeatureClass fcn = iEfc.Next();
while (fcn is IFeatureClass)
{
if (fcname == fcn.AliasName)
return true ;
fcn = iEfc.Next();
}
return false ;
}
/// <summary>
/// 设置VerticalCoordinateSystem
/// </summary>
public static ISpatialReference3 SetVerticalCoordinateSystem(ISpatialReference3 spatialReference3)
{
ISpatialReferenceFactory3 spatialReferenceFactory3 = new SpatialReferenceEnvironmentClass();
IVerticalCoordinateSystem verticalCoordinateSystem = spatialReferenceFactory3.CreateVerticalCoordinateSystem(( int )esriSRVerticalCSType.esriSRVertCS_NAVD1988);
spatialReference3.VerticalCoordinateSystem = verticalCoordinateSystem;
return spatialReference3;
}
}
}
// public static bool IsNameValid(string str)
// {
// System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z]\w{0,17}$");
// if (reg.IsMatch(s))
// {
// return true;
// }
// else
// {
// return false;
// }
// }
*
* SXW 2010 08 14
* 本类部分功能接口依附于_workSpaceInstance
* 必须首先使用CreateWorkspace或者OpenWorkspaceFromFile
* 实例化_workSpaceInstance,才能使用
*
******************************************************* */
using System;
using System.Windows.Forms;
using System.IO;
using System.Data;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.CatalogUI;
namespace ControlLibrary
{
class RuleDBCreater
{
private static IWorkspace _workSpaceInstance;
public RuleDBCreater()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static void setWorkSpaceInstance(IWorkspace ws)
{
_workSpaceInstance = ws;
}
public static IWorkspace getWorkSpaceInstance()
{
return _workSpaceInstance;
}
/// <summary>
/// 创建规则数据库workspace
/// </summary>
/// <param name="workspaceType"></param>
/// <param name="WorkspaceDirectory"></param>
/// <returns> 返回workspace实例 </returns>
public static IWorkspace CreateWorkspace( string pName, string pPath)
{
IWorkspaceFactory workspaceFactory = null ;
Type factoryType = Type.GetTypeFromProgID( " esriDataSourcesGDB.AccessWorkspaceFactory " );
workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
Directory.CreateDirectory(pPath);
IWorkspaceName workspaceName = workspaceFactory.Create(pPath, pName, null , 0 );
IName Name = (IName)workspaceName;
_workSpaceInstance = (IWorkspace)(Name.Open());
return _workSpaceInstance;
}
/// <summary>
/// 从文件创建规则数据库workspace
/// </summary>
/// <param name="WorkspaceDirectory"></param>
/// <returns> 返回workspace实例 </returns>
public static IWorkspace OpenWorkspaceFromFile( string WorkspaceDirectory)
{
IWorkspaceFactory workspaceFactory = null ;
Type factoryType = Type.GetTypeFromProgID( " esriDataSourcesGDB.AccessWorkspaceFactory " );
workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
_workSpaceInstance = workspaceFactory.OpenFromFile(WorkspaceDirectory, 0 );
return _workSpaceInstance;
}
/// <summary>
/// 创建要素数据集
/// </summary>
/// <param name="name"></param>
/// <returns> IFeatureDataset </returns>
public static IFeatureDataset CreateFeatureDataSet( /* string name,ISpatialReference srf */ )
{
IFeatureDatasetDialog fdlg = new FeatureDatasetDefDialog();
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)_workSpaceInstance;
return fdlg.DoModalCreate(featureWorkspace, 0 );
// if (_workSpaceInstance == null) return null;
// IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)_workSpaceInstance;
// IFeatureDataset tmp = featureWorkspace.CreateFeatureDataset(name, srf);
// return tmp;
}
/// <summary>
/// 删除要素数据集
/// </summary>
/// <param name="name"></param>
/// <param name="srf"></param>
/// <returns></returns>
public static void DeleteFeatureDataSet( string name)
{
IFeatureDataset fds = GetFeatureDataSet(name);
if (fds != null )
fds.Delete();
}
/// <summary>
/// 获取数据库中IFeatureDataset
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static IFeatureDataset GetFeatureDataSet( string name)
{
if (_workSpaceInstance == null ) return null ;
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)_workSpaceInstance;
IFeatureDataset tmp = featureWorkspace.OpenFeatureDataset(name);
return tmp;
}
/// <summary>
/// 重命名DataSet
/// </summary>
/// <param name="fds"></param>
/// <param name="newname"></param>
public static void RenameFeatureDataSet(IFeatureDataset fds, string newname)
{
if (fds.CanRename())
{
fds.Rename(newname);
}
}
/// <summary>
/// 获取IFeatureDataset空间参照
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public static ISpatialReference GetFdsSpatialReference(IFeatureDataset ds)
{
IGeoDataset gds = ds as IGeoDataset;
return gds.SpatialReference;
}
/// <summary>
/// 设置IFeatureDataset空间参照
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public static void SetFdsSpatialReference(IFeatureDataset ds)
{
IGeoDataset gds = ds as IGeoDataset;
// Cast the geodataset to the IGeoDatasetSchemaEdit interface and set the Spatial Reference.
IGeoDatasetSchemaEdit geoDatasetSchemaEdit = (IGeoDatasetSchemaEdit)gds;
if (geoDatasetSchemaEdit.CanAlterSpatialReference)
{
geoDatasetSchemaEdit.AlterSpatialReference(EditSpatialReferenceDlg(gds.SpatialReference));
}
}
/// <summary>
/// 创建属性字段
/// </summary>
/// <param name="shapeType"></param>
/// <param name="dgrc"></param>
/// <param name="spr"></param>
/// <returns></returns>
public static IFields CreateFields( string shapeType, DataTable dt, ISpatialReference spr, bool hasM, bool hasZ)
{
IField oField = new FieldClass();
IFieldEdit oFieldEdit = oField as IFieldEdit;
IFields oFields = new FieldsClass();
IFieldsEdit oFieldsEdit = oFields as IFieldsEdit;
IGeometryDef geometryDef = new GeometryDefClass();
IGeometryDefEdit geometryDefEdit = geometryDef as IGeometryDefEdit;
foreach (DataRow dr in dt.Rows)
{
switch (dr[ 1 ].ToString())
{
case " Object ID " :
oFieldEdit.Name_2 = " ObjectID " ;
oFieldEdit.AliasName_2 = " FID " ;
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
oFieldsEdit.AddField(oField);
break ;
case " Multipoint Features " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = " SHAPE " ;
oFieldEdit.IsNullable_2 = true ;
oFieldEdit.Required_2 = true ;
geometryDefEdit.AvgNumPoints_2 = 1 ;
geometryDefEdit.GridCount_2 = 0 ;
geometryDefEdit.HasM_2 = hasM;
geometryDefEdit.HasZ_2 = hasZ;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryMultipoint;
geometryDefEdit.SpatialReference_2 = spr;
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
oFieldEdit.GeometryDef_2 = (GeometryDef)geometryDefEdit;
oFieldsEdit.AddField(oField);
break ;
case " MultiPatch Features " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = " SHAPE " ;
oFieldEdit.IsNullable_2 = true ;
oFieldEdit.Required_2 = true ;
geometryDef = new GeometryDefClass();
geometryDefEdit = geometryDef as IGeometryDefEdit;
geometryDefEdit.AvgNumPoints_2 = 1 ;
geometryDefEdit.GridCount_2 = 0 ;
geometryDefEdit.HasM_2 = hasM;
geometryDefEdit.HasZ_2 = hasZ;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryMultiPatch;
geometryDefEdit.SpatialReference_2 = spr;
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
oFieldEdit.GeometryDef_2 = (GeometryDef)geometryDefEdit;
oFieldsEdit.AddField(oField);
break ;
case " Point Features " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = " SHAPE " ;
oFieldEdit.IsNullable_2 = true ;
oFieldEdit.Required_2 = true ;
geometryDef = new GeometryDefClass();
geometryDefEdit = geometryDef as IGeometryDefEdit;
geometryDefEdit.AvgNumPoints_2 = 1 ;
geometryDefEdit.GridCount_2 = 0 ;
geometryDefEdit.HasM_2 = hasM;
geometryDefEdit.HasZ_2 = hasZ;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
geometryDefEdit.SpatialReference_2 = spr;
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
oFieldEdit.GeometryDef_2 = (GeometryDef)geometryDefEdit;
oFieldsEdit.AddField(oField);
break ;
case " Line Features " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = " SHAPE " ;
oFieldEdit.IsNullable_2 = true ;
oFieldEdit.Required_2 = true ;
geometryDef = new GeometryDefClass();
geometryDefEdit = geometryDef as IGeometryDefEdit;
geometryDefEdit.AvgNumPoints_2 = 1 ;
geometryDefEdit.GridCount_2 = 0 ;
geometryDefEdit.HasM_2 = hasM;
geometryDefEdit.HasZ_2 = hasZ;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryLine;
geometryDefEdit.SpatialReference_2 = spr;
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
oFieldEdit.GeometryDef_2 = (GeometryDef)geometryDefEdit;
oFieldsEdit.AddField(oField);
break ;
case " Polygon Features " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = " SHAPE " ;
oFieldEdit.IsNullable_2 = true ;
oFieldEdit.Required_2 = true ;
geometryDef = new GeometryDefClass();
geometryDefEdit = geometryDef as IGeometryDefEdit;
geometryDefEdit.AvgNumPoints_2 = 1 ;
geometryDefEdit.GridCount_2 = 0 ;
geometryDefEdit.HasM_2 = hasM;
geometryDefEdit.HasZ_2 = hasZ;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
geometryDefEdit.SpatialReference_2 = spr;
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
oFieldEdit.GeometryDef_2 = (GeometryDef)geometryDefEdit;
oFieldsEdit.AddField(oField);
break ;
case " Short Int " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = dr[ 0 ].ToString();
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger;
oFieldEdit.IsNullable_2 = true ;
oFieldsEdit.AddField(oField);
break ;
case " Long Int " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = dr[ 0 ].ToString();
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
oFieldsEdit.AddField(oField);
break ;
case " Float " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = dr[ 0 ].ToString();
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeSingle;
oFieldsEdit.AddField(oField);
break ;
case " Double " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = dr[ 0 ].ToString();
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
oFieldsEdit.AddField(oField);
break ;
case " Text " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = dr[ 0 ].ToString();
oFieldEdit.Length_2 = 30 ; // Only string fields require that you set the length.
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
oFieldsEdit.AddField(oField);
break ;
case " Date " :
oField = new FieldClass();
oFieldEdit = oField as IFieldEdit;
oFieldEdit.Name_2 = dr[ 0 ].ToString();
oFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate;
oFieldsEdit.AddField(oField);
break ;
// case "Guid":
// IField fld12 = new FieldClass();
// IFieldEdit fldedt12 = (IFieldEdit)fld12;
// fldedt12.Name_2 = dr[0].ToString();
// fldedt12.Type_2 = esriFieldType.esriFieldTypeGUID;
// fieldsEdit.set_Field(i, fld12);
// break;
// case "Geomery":
// IField fld13 = new FieldClass();
// IFieldEdit fldedt13 = (IFieldEdit)fld13;
// fldedt13.Name_2 = dr[0].ToString();
// fldedt13.IsNullable_2 = true;
// fldedt13.Required_2 = true;
// IGeometryDef geometryDef13 = new GeometryDefClass();
// IGeometryDefEdit geometryDefEdit13 = (IGeometryDefEdit)geometryDef13;
// geometryDef13 = new GeometryDefClass();
// geometryDefEdit13 = (IGeometryDefEdit)geometryDef13;
// geometryDefEdit13.AvgNumPoints_2 = 1;
// geometryDefEdit13.GridCount_2 = 0;
// geometryDefEdit13.HasM_2 = hasM;
// geometryDefEdit13.HasZ_2 = hasZ;
// fldedt13.Type_2 = esriFieldType.esriFieldTypeGeometry;
// geometryDefEdit13.GeometryType_2 = esriGeometryType.esriGeometryAny;
// // Generate a default Spatial Reference
// geometryDefEdit13.SpatialReference_2 = spr;
// fldedt13.GeometryDef_2 = (GeometryDef)geometryDefEdit13;
// fldedt13.Type_2 = esriFieldType.esriFieldTypeGeometry;
// fieldsEdit.set_Field(i, fld13);
// break;
// case "Raster":
// IField fld14 = new FieldClass();
// IFieldEdit fldedt14 = (IFieldEdit)fld14;
// fldedt14.Name_2 = dr[0].ToString();
// fldedt14.Type_2 = esriFieldType.esriFieldTypeRaster;
// fieldsEdit.set_Field(i, fld14);
// break;
}
}
return oFields;
}
/// <summary>
/// 创建要素类
/// </summary>
/// <param name="workspace"></param>
/// <param name="featureDataset"></param>
/// <param name="featureClassName"></param>
/// <param name="fields"></param>
/// <param name="CLSID"></param>
/// <param name="CLSEXT"></param>
/// <param name="strConfigKeyword"></param>
/// <returns></returns>
public static IFeatureClass CreateFeatureClass(IFeatureDataset featureDataset, String featureClassName, IFields fields)
{
if (_workSpaceInstance == null ) return null ;
IFeatureClass featureClass = null ;
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)_workSpaceInstance;
// assign the class id value if not assigned
ESRI.ArcGIS.esriSystem.UID CLSID = null ;
ESRI.ArcGIS.esriSystem.UID CLSEXT = null ;
string strConfigKeyword = "" ;
if (CLSID == null )
{
CLSID = new ESRI.ArcGIS.esriSystem.UIDClass();
CLSID.Value = " esriGeoDatabase.Feature " ;
}
System.String strShapeField = "" ;
// locate the shape field
for (Int32 j = 0 ; j < fields.FieldCount; j ++ )
{
esriFieldType dd = fields.get_Field(j).Type;
if (dd == esriFieldType.esriFieldTypeGeometry)
{
strShapeField = fields.get_Field(j).Name;
}
}
// finally create and return the feature class
if (featureDataset != null )
{
featureClass = featureDataset.CreateFeatureClass(featureClassName, fields, CLSID, CLSEXT, esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword);
}
return featureClass;
}
/// <summary>
/// 获取FeatureClass从IFeatureDataset
/// </summary>
/// <param name="featDs"></param>
/// <param name="className"></param>
/// <returns></returns>
public static IFeatureClass GetFeatureClassFromFeatureDataset(IFeatureDataset featDs, string className)
{
IFeatureClass featClass;
IFeatureClassContainer fcContainer = featDs as IFeatureClassContainer;
for ( int i = 0 ; i < fcContainer.ClassCount; i ++ )
{
featClass = fcContainer.get_Class(i);
if (RuleDBCreater.GetFeatureClassName(featClass) == className)
{
return featClass;
}
}
return null ;
}
/// <summary>
/// 重命名FeatureClass
/// </summary>
/// <param name="fds"></param>
/// <param name="newname"></param>
public static void RenameFeatureClass(IFeatureClass fc, string newname)
{
IDataset ds = fc as IDataset;
if (ds.CanRename())
{
ds.Rename(newname);
}
}
/// <summary>
/// 从数据集删除要素类
/// </summary>
/// <param name="featureDataset"></param>
/// <param name="featureClassName"></param>
/// <param name="fields"></param>
/// <returns></returns>
public static void DeleteFeatureClass(IFeatureDataset featureDataset, String featureClassName)
{
IFeatureClass fc = GetFeatureClassFromFeatureDataset(featureDataset, featureClassName);
if (fc != null )
{
IDataset ds = fc as IDataset;
if (ds.CanDelete())
{
ds.Delete();
}
}
}
/// <summary>
/// 编辑空间参照
/// </summary>
/// <param name="inputSpr"></param>
/// <returns></returns>
public static ISpatialReference EditSpatialReferenceDlg(ISpatialReference inputSpr)
{
ESRI.ArcGIS.CatalogUI.ISpatialReferenceDialog2 spatialReferenceDialog2 = new ESRI.ArcGIS.CatalogUI.SpatialReferenceDialogClass();
ISpatialReference spf = spatialReferenceDialog2.DoModalEdit(inputSpr, false , false , false , false , false , false , false , 0 );
return spf;
}
/// <summary>
/// 创建空间参照
/// </summary>
/// <param name="coordinateSystem"></param>
/// <returns></returns>
public static ISpatialReference CreateSpatialReferenceDlg() //
{
ESRI.ArcGIS.CatalogUI.ISpatialReferenceDialog2 spatialReferenceDialog2 = new ESRI.ArcGIS.CatalogUI.SpatialReferenceDialogClass();
ISpatialReference spatialReference = spatialReferenceDialog2.DoModalCreate( true , false , false , 0 );
// ISpatialReferenceTolerance spatialReferenceTolerance = spatialReference as ISpatialReferenceTolerance;
return spatialReference;
}
public static ISpatialReference SetXYZMTolerance(ISpatialReference srf, double xytolerance, double ztolerance, double mtolerance)
{
ISpatialReferenceTolerance spatialReferenceTolerance = srf as ISpatialReferenceTolerance;
// spatialReferenceTolerance.XYTolerance = xytolerance;
// spatialReferenceTolerance.ZTolerance = ztolerance;
// spatialReferenceTolerance.MTolerance = mtolerance;
return srf;
}
public static void GetXYZMTolerance(ISpatialReference srf, out double xytolerance, out double ztolerance, out double mtolerance)
{
ISpatialReferenceTolerance spatialReferenceTolerance = srf as ISpatialReferenceTolerance;
xytolerance = spatialReferenceTolerance.XYTolerance;
ztolerance = spatialReferenceTolerance.ZTolerance;
mtolerance = spatialReferenceTolerance.MTolerance;
}
/// <summary>
/// 获取工作区内所有数据集名称
/// </summary>
/// <param name="ws"></param>
/// <returns></returns>
public static IEnumDatasetName GetFeatureDataSetNames(IWorkspace ws)
{
return ws.get_DatasetNames(esriDatasetType.esriDTFeatureDataset);
}
public static bool IsArcDbFile( string path)
{
IWorkspaceFactory workspaceFactory = null ;
Type factoryType = Type.GetTypeFromProgID( " esriDataSourcesGDB.AccessWorkspaceFactory " );
workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
return workspaceFactory.IsWorkspace(path);
}
public static string GetFeatureClassName(IFeatureClass fc)
{
IDataset ds = fc as IDataset;
return ds.Name;
}
public static IEnumFeatureClass GetFeatureClasses(IFeatureDataset fds)
{
IFeatureClassContainer pFeatureclassContainer = (IFeatureClassContainer)fds;
return (IEnumFeatureClass)pFeatureclassContainer.Classes;
}
public static bool IsFeatureDataSetExist( string name)
{
// if (_workSpaceInstance == null) return true;
IEnumDatasetName iEdn = GetFeatureDataSetNames(_workSpaceInstance);
iEdn.Reset();
IDatasetName iDsn = iEdn.Next();
while (iDsn is IFeatureDatasetName)
{
if (iDsn.Name == name)
return true ;
iDsn = iEdn.Next();
}
return false ;
}
public static bool IsFeatureClassExist( string dsname, string fcname)
{
IEnumFeatureClass iEfc = GetFeatureClasses(GetFeatureDataSet(dsname));
iEfc.Reset();
IFeatureClass fcn = iEfc.Next();
while (fcn is IFeatureClass)
{
if (fcname == fcn.AliasName)
return true ;
fcn = iEfc.Next();
}
return false ;
}
/// <summary>
/// 设置VerticalCoordinateSystem
/// </summary>
public static ISpatialReference3 SetVerticalCoordinateSystem(ISpatialReference3 spatialReference3)
{
ISpatialReferenceFactory3 spatialReferenceFactory3 = new SpatialReferenceEnvironmentClass();
IVerticalCoordinateSystem verticalCoordinateSystem = spatialReferenceFactory3.CreateVerticalCoordinateSystem(( int )esriSRVerticalCSType.esriSRVertCS_NAVD1988);
spatialReference3.VerticalCoordinateSystem = verticalCoordinateSystem;
return spatialReference3;
}
}
}
// public static bool IsNameValid(string str)
// {
// System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z]\w{0,17}$");
// if (reg.IsMatch(s))
// {
// return true;
// }
// else
// {
// return false;
// }
// }