创建与编辑数据
要求:
编写程序,导入文本文件到空间数据库中。
1、创建一个File Geodatabase
2、创建一个FeatureClass,geometry类型为esriGeometryType.esriGeometryPoint、采用esriSRGeoCSType.esriSRGeoCS_NAD1983坐标系统,该FeatureClass的属性字段包含经度、纬度以及State_Name字段。
3、使用CreateFeature/Store将文本文件中的数据信息导入到FearureClass(使用IFeatureCursor/IFeatureBuffer也可以进行同样的操作)。
4、使用usa.mxd中的图层更新“State_Name”字段。
作答:
1.在自定义的CreateFields函数里创建字段集
//在CreateFields函数里创建字段集
public IFieldsEdit CreateFields()
{
//使用编辑接口IFieldEdit(IFieldsEdit)创建新的Field对象或新的Fields集
//创建字段集合. 使用IFieldsEdit接口,要将Field对象加入到Fields集中
//IFields pFields = new FieldsClass();
//IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
IFieldsEdit pFields = new FieldsClass(); //等同上面两句
//创建字段Object ID;必要字段 使用IFieldEdit接口可为新的字段设定名字、数据类型、长度等属性
IFieldEdit pField = new FieldClass();
pField.Name_2 = "ObjectID";
pField.Type_2 = esriFieldType.esriFieldTypeOID;
pFields.AddField(pField);//创建完数据集需要的每个pField后,要把它们加入到pFields集中
//创建几何定义
//IGeometryDef pGD = new GeometryDefClass();
//IGeometryDefEdit pGDE = pGD as IGeometryDefEdit;
IGeometryDefEdit pGDE = new GeometryDefClass(); //等同于上面两句
pGDE.GeometryType_2 = esriGeometryType.esriGeometryPoint;//☆geometry类型也可以是Point,Polyline,Polygon
//创建地理坐标系 使用esriSRGeoCSType,esriSRGeoCS2Type或esriSRGeoCS3Type枚举中的元素作为gcsType来创建地理坐标系
ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass();
esriSRGeoCSType geoSystem = esriSRGeoCSType.esriSRGeoCS_NAD1983;//☆选择坐标系类型
ISpatialReferenceResolution pSRR = pSRF.CreateGeographicCoordinateSystem(Convert.ToInt32(geoSystem)) as ISpatialReferenceResolution;
pSRR.ConstructFromHorizon(); //定义此空间参照的XY分辨率和域范围(基于其地平线的范围)
ISpatialReferenceTolerance pSRT = pSRR as ISpatialReferenceTolerance;//访问空间信息和M偏差
pSRT.SetDefaultXYTolerance(); //设置用于控制X和Y尺寸中的点合并的默认簇公差 (相当于当前空间参考单位中的1mm)
ISpatialReference pSR = pSRR as ISpatialReference;
pGDE.SpatialReference_2 = pSR;
//创建字段Shape
pField = new FieldClass();
pField.Name_2 = "Shape";
pField.Type_2 = esriFieldType.esriFieldTypeGeometry;
pField.GeometryDef_2 = pGDE;
pFields.AddField(pField);
//创建字段经度
pField = new FieldClass();
pField.Name_2 = "Longtitude";
pField.Type_2 = esriFieldType.esriFieldTypeDouble;
pFields.AddField(pField);
//创建字段纬度
pField = new FieldClass();
pField.Name_2 = "Latitude";
pField.Type_2 = esriFieldType.esriFieldTypeDouble;
pFields.AddField(pField);
//创建字段 小光光
pField = new FieldClass();
pField.Name_2 = "小光光";
pField.Type_2 = esriFieldType.esriFieldTypeString;
pField.Length_2 = 25;
pFields.AddField(pField);
return pFields;
}
2.创建File Geodatabase、FeatureClass
//创建File Geodatabase
//file geodatabase,需要引用Geodatabase、DataSourcesGDB,DataSourcesFile
IWorkspaceFactory pWF = new FileGDBWorkspaceFactoryClass();
//IWorkspaceFactory接口使用Create方法生成WorkspaceName对象
IWorkspaceName pWN = pWF.Create("文件路径","文件名称.gdb",null,0);
//使用IName接口上的Open方法,将WorkspaceName实例化成Workspace
IName pName = pWN as IName;
IWorkspace pWS = pName.Open() as IWorkspace;
//创建要素集FeatureClass 在FileGDBWorkspace上,使用IFeatureWorkspace接口下的CreateFeatureClass方法创建新的要素集,生成FileGDB
IFeatureWorkspace pFW = pWS as IFeatureWorkspace;
IFeatureClass pFC = pFW as IFeatureClass;
IFieldsEdit pFields = CreateFields();//在创建新的FeatureClass前,需要为新的要素集创建字段集; 在CreateFields函数里创建了字段集
pFC = pFW.CreateFeatureClass("FClassName",pFields,null,null,esriFeatureType.esriFTSimple,"Shape",null); //创建要素集
创建完成的File Geodatabase 和 要素集FeatureClass 图示(帮助理解):
3.使用 CreateFeature/Store或IFeatureCursor/IFeatureBuffer 将文本文件中的经纬度坐标导入到FeatureClass中
/---------------------------- --------------------------------
//使用CreateFeature/Store将文本文件中的经纬度坐标导入到FeatureClass中
//读取Txt文件
System.IO.FileStream FS = new System.IO.FileStream("文本文件地址",FileMode.Open);
StreamReader SR = new StreamReader(FS);
while (!SR.EndOfStream)
{
string[] Fields = SR.ReadLine().Split(new char[] {','});
//生成点状要素
IPoint pPoint = new PointClass();
pPoint.X = double.Parse(Fields[0]);//☆文本字段索引值根据情况更改
pPoint.Y = double.Parse(Fields[1]);
//使用用CreateFeature/Store将坐标Feature加入到FeatureClass里(这个坐标点可以显示)
IFeature pFea = pFC.CreateFeature();
pFea.Shape = pPoint; //让点显示出来
//pFea.set_Value(2,Fields[0]); //2是字段经度的索引值 Fields[0]是文本中的第一个值
//pFea.set_Value(3,Fields[1]); //3是字段纬度的索引值 Fields[1]是文本中的第二个值
pFea.set_Value(pFea.Fields.FindField("Longtitude"),Fields[0]);//FindField,返回经度Longtitude在表中的索引值
pFea.set_Value(pFea.Fields.FindField("Latitude"),Fields[1]);
pFea.Store();
/* 使用IFeatureCursor/IFeatureBuffer向要素集FeatureClass中插入数据 (这个坐标点没显示)
IFeatureCursor pFCur = pFC.Insert(true);
IFeatureBuffer pFB = pFC.CreateFeatureBuffer();
pFB.set_Value(2, Fields[0]);
pFB.set_Value(3, Fields[1]);
pFCur.InsertFeature(pFB); //FeatureCursor的InsertFeature方法 */
}
SR.Close();
FS.Close();
将经纬度坐标信息导入到FeatureClass中 图示:
4.根据mxd中的图层表的字段值 来更新FeatureClass表中的"小光光"字段属性值
-------------------------------------------------------///
//根据mxd中的图层更新FeatureClass表中的"小光光"字段属性值
IFeatureLayer pFL1 = axMapControl1.get_Layer(0) as IFeatureLayer;
IFeatureClass pFC1 = pFL1.FeatureClass; //要素图层States的FeatureClass1表
IQueryFilter pQF = new QueryFilterClass();
IFeatureCursor pFCur1 = pFC.Update(pQF,false); //更新FeatureClass表的字段
IFeature pFea1 = pFCur1.NextFeature(); //表FeatureClass中的Feature1
while (pFea1 != null)
{
ISpatialFilter pSF = new SpatialFilterClass();
pSF.Geometry = pFea1.Shape;
pSF.SpatialRel = esriSpatialRelEnum.esriSpatialRelWithin; //空间查询类型位于
pSF.WhereClause = "";
IFeatureCursor pFCur2 = pFC1.Search(pSF,true); //在要素图层States的FeatureClass1表中查询
IFeature pFea2 = pFCur2.NextFeature();
//.set_Value(attribureIndex,attributeValue)
//获取要素图层States的第4个字段State_Name的各个Feature值 添加到 FeatureClass表字段是"小光光"的索引位置
pFea1.set_Value(pFea1.Fields.FindField("小光光"),pFea2.get_Value(3).ToString());
pFea1.Store();
pFea1 = pFCur1.NextFeature();
}
axMapControl1.ActiveView.Refresh();
MessageBox.Show("Create file geodatabase successfully!");
FeatureClass表中的"小光光"字段 更新 图示:
参考文章:https://blog.csdn.net/Domen_Dragon/article/details/86515494