1 高程值
地图与比例尺:
1.2 思路分析
各类等高线与等高距的关系:
判断条件设计:
首曲线:国标码为710101,不能被等高距整除,如果能被等高距整除时不能被5倍等高距整除,为错误首曲线
计曲线:国标码为710102,不能被5倍等高距整除,为错误计曲线
间曲线:国标码为710103,不能被0.5倍等高距整除,为错误等高线
助曲线:国标码为710104,能被0.25倍等高距整除,但不能被0.5倍登高距和等高距整除
2 代码设计
2.1 获取图层要素的框架
/// <summary> /// 检查点图层 /// </summary> /// <param name="wsPath">数据库名称</param> /// <param name="layerName">图层名称</param> public void checkContour(string wsPath, string layerName, double contourInterval) { //1. 打开工作空间 //ESRI.ArcGIS.DataSourcesGDB IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactory(); IWorkspace workspace = workspaceFactory.OpenFromFile(wsPath, 0); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; //2. 打开要素层 IFeatureClass pFeaterClass = featureWorkspace.OpenFeatureClass(layerName); //4. 查询所有要素 string where = ""; IQueryFilter filter = new QueryFilterClass(); filter.WhereClause = where; //5.定义查询游标并循环迭代 IFeatureCursor pFeatcursor = pFeaterClass.Search(filter, false); IFeature pFeature = pFeatcursor.NextFeature(); //6. 运算结果存储 List<IFeature> errLists = new List<IFeature>(); while (pFeature != null) { //要对该要素进行处理的个性化代码 pFeature = pFeatcursor.NextFeature(); } ExportPtsToShapefile(wsPath, errLists, layerName, esriGeometryType.esriGeometryPoint); }
:
获取mdb文件需要权限为:IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactory();
读取gdb文件需要的权限为:IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
本段代码思路为:
创建工作工厂,再在工厂以打开文件的方式中得到工作空间
在工作空间中,打开要素类
使用查询语句,结合光标循环,得到所有要素
2.2 获取字段名称的代码框架
IFields fields = pFeature.Fields; int elevIdx = fields.FindFieldByAliasName("ELEV"); double elevValue = double.Parse(pFeature.get_Value(elevIdx).ToString()); int gbIdx = fields.FindFieldByAliasName("GB"); string gbValue = pFeature.get_Value(gbIdx).ToString();
说明:
使用IFields接口获取要素的所有字段;pFeature.Fields
根据字段别名,从所有字段中,获取指定字段的索引;FindFiledByAliasName(fieldName)
根据指定字段的说因,获取该索引值对应的要素的具体取值;pFeature.get_value(fieldIndex)
2.3 输出shp文件的代码及分析
2.3.1 在工作空间中创建要素类
IWorkspace2 workspace2 = (IWorkspace2)workspace; if (workspace2.get_NameExists(esriDatasetType.esriDTFeatureClass, checkLayerName + "_检查结果")) { IFeatureClass pFC = featureWorkspace.OpenFeatureClass(checkLayerName + "_检查结果"); IDataset pDS = pFC as IDataset; pDS.Delete(); } IFeatureClass shpFeatureClass = featureWorkspace.CreateFeatureClass(checkLayerName + "_检查结果", pFields, null, null, esriFeatureType.esriFTSimple, "shape", "");
分析:
- 接口转换,在IWorkspace2接口中,调用get_NameExists方法,判断要创建的文件是否在工作空间中已存在
- 如果存在,则在工作空间中打开该要素类,并将其删除
- 之后,在工作空间中再创建要素类
2.3.2 为shape文件定义新字段
定义空间参考和几何类型
IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "shape"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeoDef = new GeometryDefClass(); //The geometry definition for the field if IsGeometry is TRUE. IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef; //设置要素的类型及投影 pGeoDefEdit.GeometryType_2 = geoType; IGeoDataset geoDataset = pFeaterClass as IGeoDataset; pGeoDefEdit.SpatialReference_2 = geoDataset.SpatialReference; pGeoDefEdit.GeometryType_2 = geoType;//要素的几何类型 pFieldEdit.GeometryDef_2 = pGeoDef; pFieldsEdit.AddField(pField);
自定义字段:
//添加其他的字段 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "X"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; //pFieldEdit.Precision_2 = 7;//数值精度 //pFieldEdit.Scale_2 = 6;//小数点位数 pFieldsEdit.AddField(pField);
自定义字段函数:
/// <summary> /// 向gdb图层中添加字段 /// </summary> /// <param name="gdbName">gdb文件夹名称</param> /// <param name="layerName">涂层名称</param> /// <param name="fieldName">字段名称</param> /// <param name="type">字段类型</param> public void AddFields(string gdbName, string layerName, string fieldName, esriFieldType type) { IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass(); IWorkspace workspace = workspaceFactory.OpenFromFile(gdbName, 0); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; IFeatureClass pFeaterClass = featureWorkspace.OpenFeatureClass(layerName); if (pFeaterClass.FindField(fieldName) >= 0) { return; } else { IFieldsEdit pFieldsEdit = pFeaterClass.Fields as IFieldsEdit; //添加其他的字段 IField pField = new FieldClass(); IFieldEdit2 pEdit = pField as IFieldEdit2; pEdit.Name_2 = fieldName; pEdit.Type_2 = type; pFieldsEdit.AddField(pField); } if (((IWorkspaceFactoryLockControl)workspaceFactory).SchemaLockingEnabled) { ((IWorkspaceFactoryLockControl)workspaceFactory).DisableSchemaLocking(); } }
2.3.4 要素间字段值交换
for(IFeature fea in liFeatures) { IFeature pFeature = shapeFeatureClass.CreateFeature(); IFields fields = fea.Fields; int elevIdx = fields.FindFieldByAliasName("ELEV"); double elevValue = double.Parse(fea.get_Value(elevIdx).ToString()); pFeature.set_value(pFeature.Fields.FindFieldByAliasName("ELEV"), elevValue); }