C# ArcEngine开发

whatdoyousay 2014-08-06 12:16:05
大家好,我才开始学习AE没多久,在开发中遇到点麻烦,希望大家帮帮忙,共同进步。
以下是我做的主界面,思路是这样,我先载入一个shp文件,然后再点击查询,按照规则查询并选择出符合条件的要素然后再输出查询出来的要素为shp文件,我在最后输出的部分碰到问题,输出的是整个图层,并不是当前选中的要素。


以下为查询按钮的代码

private void button2_Click(object sender, EventArgs e)
{ //用IFeatureSelection接口实现查询高亮显示
IMap pMap = axMapControl1.Map;
IFeatureLayer pFeaturelayer = GetLayer(pMap, "11") as IFeatureLayer;
IFeatureSelection pFeatureSelection = pFeaturelayer as IFeatureSelection;
IQueryFilter pQuery = new QueryFilterClass();
pQuery.WhereClause = "OBJECTID=" + "304920";
pFeatureSelection.SelectFeatures(pQuery, esriSelectionResultEnum.esriSelectionResultNew, false);
axMapControl1.ActiveView.Refresh();
}

private ILayer GetLayer(IMap pMap, string LayerName)
{ //根据图层名获取图层
IEnumLayer pEnunLayer;
pEnunLayer = pMap.get_Layers(null, false);
pEnunLayer.Reset();
ILayer pRetureLayer;
pRetureLayer = pEnunLayer.Next();
while (pRetureLayer != null)
{
if (pRetureLayer.Name == LayerName)
{
break;
}
pRetureLayer = pEnunLayer.Next();
}
return pRetureLayer;
}


以下为输出按钮的代码

private void Export_Click(object sender, EventArgs e)
{

SaveFileDialog saveDia = new SaveFileDialog();
saveDia.Title = "Export Shp.......";
saveDia.CreatePrompt = false;
saveDia.OverwritePrompt = false;//提示用户是否覆盖
saveDia.Filter = "ShapeFile (*.shp)|*.shp";
saveDia.RestoreDirectory = true;
saveDia.AddExtension = true;//若用户没有添加扩展名,将自行添加
if (saveDia.ShowDialog() == DialogResult.OK)
{

string strFullPath = "";
string filePath = "";
string fileName = "";
string shapeName = "";
strFullPath = saveDia.FileName;
int Index = strFullPath.LastIndexOf("\\");
filePath = strFullPath.Substring(0, Index);
fileName = strFullPath.Substring(Index + 1);
shapeName = fileName.Remove(fileName.ToLower().LastIndexOf(".shp"), 4);

IMap map = axMapControl1.Map;
IEnumLayer enumLayer = map.get_Layers(null, true);
ILayer layer = enumLayer.Next();

IFeatureLayer pFeatureLayer = layer as IFeatureLayer;
IFeatureClass pFeatureClass;
IFeatureSelection pFSelection = pFeatureLayer as IFeatureSelection;
IFeature yFeature = null;
IFeatureClass yFeatureClass;
IEnumIDs pEnumID = pFSelection.SelectionSet.IDs;
int a = pEnumID.Next();
while (pEnumID != null & a != -1)
{
yFeature = pFeatureLayer.FeatureClass.GetFeature(a);
a = pEnumID.Next();
}
pFeatureClass = yFeature.Class as IFeatureClass;
ExportToShp yy = new ExportToShp();
yy.ExportFeatureClassToShp(pFeatureClass, strFullPath, filePath);
}



public void ExportFeatureClassToShp(IFeatureClass apFeatureClass, string ExportShapeFileName,string ExportFilePath)
{
if (apFeatureClass == null)
{
MessageBox.Show("请选择", "系统提示");
return;
}
if (ExportShapeFileName == "")
return;


string ExportFileShortName = System.IO.Path.GetFileNameWithoutExtension(ExportShapeFileName);
ExportFilePath = System.IO.Path.GetDirectoryName(ExportShapeFileName);
shpPath = ExportFilePath + "\\" + ExportFileShortName + "\\" + ExportFileShortName + ".shp";
//设置导出要素类的参数
IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();
IDataset pOutDataset = (IDataset)apFeatureClass;
pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName;
//创建一个输出shp文件的工作空间
IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
IWorkspaceName pInWorkspaceName = new WorkspaceNameClass();
pInWorkspaceName = pShpWorkspaceFactory.Create(ExportFilePath, ExportFileShortName, null, 0);

//创建一个要素集合
IFeatureDatasetName pInFeatureDatasetName = null;
//创建一个要素类
IFeatureClassName pInFeatureClassName = new FeatureClassNameClass();
IDatasetName pInDatasetClassName;
pInDatasetClassName = (IDatasetName)pInFeatureClassName;
pInDatasetClassName.Name = ExportFileShortName;//作为输出参数
pInDatasetClassName.WorkspaceName = pInWorkspaceName;
//通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合
long iCounter;
IFields pOutFields, pInFields;
IFieldChecker pFieldChecker;
IField pGeoField;
IEnumFieldError pEnumFieldError = null;
pInFields = apFeatureClass.Fields;
pFieldChecker = new FieldChecker();
pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);
//通过循环查找几何字段
pGeoField = null;
for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)
{
if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)
{
pGeoField = pOutFields.get_Field((int)iCounter);
break;
}
}
//得到几何字段的几何定义
IGeometryDef pOutGeometryDef;
IGeometryDefEdit pOutGeometryDefEdit;
pOutGeometryDef = pGeoField.GeometryDef;
//设置几何字段的空间参考和网格
pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef;
pOutGeometryDefEdit.GridCount_2 = 1;
pOutGeometryDefEdit.set_GridSize(0, 1500000);
try
{
//开始导入
IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass();
pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);
IGeometryDef , IFields , string configKey, int FlushInterval, int parentHWND);
MessageBox.Show("导出成功", "系统提示");
}
catch (Exception ex)
{
MessageBox.Show("the following exception occurred:" + ex.ToString());
}
}
...全文
739 点赞 收藏 18
写回复
18 条回复
w350836053 2016年10月24日
楼主有arcengine10.3么
回复 点赞
Taylor521314 2016年05月11日
有没有大神能指导我一下,初学者,如何遍历图层的所有要素?
回复 点赞
笨啦啦 2016年03月15日
楼主,多多关照。我刚开始学。
回复 点赞
whatdoyousay 2014年09月02日
引用 14 楼 GIS520025 的回复:
如果字段shape是如图的也不成功,不知道什么原因
FID和Shape是默认字段,如果字段没错,应该是赋值的时候出现了偏移,你调整一下AddFeatureToFeatureClass里的初始值试试
回复 点赞
GIS520025 2014年09月01日
引用 13 楼 liuhuibing12 的回复:
引用 11 楼 GIS520025 的回复:
[/code]
谢谢,成功生成了,不过字段少了几个,不知道哪里出问题了

我调试的时候也是有这样的问题,你可以看下addFields函数里面的i变量,初始值为3,这个函数你需要自己根据需求改一下[/quote]

如果字段shape是如图的也不成功,不知道什么原因
回复 点赞
GIS520025 2014年08月22日
引用 4 楼 liuhuibing12 的回复:
为了方便后来人,我把代码分享出来,用了两种方法实现 1,直接调用Geoprocessor工具集,里面有个FeatureClassToFeatureClass接口,直接实现你想要的功能

SaveFileDialog saveDia = new SaveFileDialog();
            saveDia.Title = "Export Shp.......";
            saveDia.CreatePrompt = false;
            saveDia.OverwritePrompt = false;//提示用户是否覆盖  
            saveDia.Filter = "ShapeFile  (*.shp)|*.shp";
            saveDia.RestoreDirectory = true;
            saveDia.AddExtension = true;//若用户没有添加扩展名,将自行添加
            if (saveDia.ShowDialog() == DialogResult.OK)
            {

                string strFullPath = "";
                string filePath = "";
                string fileName = "";
                string shapeName = "";
                strFullPath = saveDia.FileName;
                int Index = strFullPath.LastIndexOf("\\");
                filePath = strFullPath.Substring(0, Index);
                fileName = strFullPath.Substring(Index + 1);
                shapeName = fileName.Remove(fileName.ToLower().LastIndexOf(".shp"), 4);

                IQueryFilter pQuryFilter = new QueryFilterClass();
                pQuryFilter.WhereClause = Divesity.GlobleVariety.SQLLine;
                FeatureClassToFeatureClass pFeattoFeat = new FeatureClassToFeatureClass();
                Geoprocessor GP = new Geoprocessor();
                object sev = null;
                //GP.AddToolbox(@"C:\Program Files\ArcGIS\Desktop10.0\ArcToolbox\Toolboxes\Coverage Tools.tbx");
                IVariantArray parameters = new VarArrayClass();
                GP.OverwriteOutput = true;
                try
                {
                    pFeattoFeat.in_features = Divesity.GlobleVariety. LoadFilePath;
                    pFeattoFeat.where_clause = pQuryFilter.WhereClause;
                    pFeattoFeat.out_name = fileName;
                    pFeattoFeat.out_path = filePath;
                    pFeattoFeat.out_feature_class = strFullPath;
                    GP.Execute(pFeattoFeat, null);
                    MessageBox.Show("导出成功!");
                }
                catch (Exception)
                {
                    // Print geoprocessing execution error messages.
                    Console.WriteLine(GP.GetMessages(ref sev));
                }
2,另外一种方法就是先新建一个空白的shp文件,然后再添加你指定图层的字段,最后再添加要素和各字段的值。 1),新建shp文件,用createfeatureclass方法

public static void createwhatShapefile(esriGeometryType type ,IMap map, string filePath, string fileName)
          {
              //建立shape字段
             IFields pFields = new FieldsClass();
             IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
             IField pField = new FieldClass();
             IFieldEdit pFieldEdit = pField as IFieldEdit;
             pFieldEdit.Name_2 = "Shape";
             pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
 
             //设置geometry definition
             IGeometryDef pGeometryDef = new GeometryDefClass();
             IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
             pGeometryDefEdit.GeometryType_2 = type;   //根据当前图层的类型来确定新建图层的类型
             pGeometryDefEdit.SpatialReference_2 = map.SpatialReference;
             pFieldEdit.GeometryDef_2 = pGeometryDef;
             pFieldsEdit.AddField(pField);
 
             //新建字段
             pField = new FieldClass();
             pFieldEdit = pField as IFieldEdit;
             pFieldEdit.Length_2 = 10;
             pFieldEdit.Name_2 = "OBJECTID";
             pFieldEdit.AliasName_2 = "ID";
             pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
             pFieldsEdit.AddField(pField);
             //其他字段用循环新建
             
 
             IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
             IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace;
 
             //IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
             //IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace;

             int i = fileName.IndexOf(".shp");
             if (i == -1)
                 pFeatureWorkspace.CreateFeatureClass(fileName + ".shp", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
             else
                 pFeatureWorkspace.CreateFeatureClass(fileName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
 
            //MessageBox.Show("OK");4 
         }

2),添加字段

public void addFields(IFeature pFeature,IFeatureClass pFeatureClass)  //传入待修改要素类及样本要素
        {

            IFields ppFileds = pFeatureClass.Fields;
            IFieldsEdit ppFieldsEdit = (IFieldsEdit)ppFileds;
            IFields pFields = pFeature.Fields;
            while (pFields.FieldCount > pFeatureClass.Fields.FieldCount)
            {
                
                for (int i = 3; i < pFeature.Fields.FieldCount; i++)
                {
                    IField ppField = new FieldClass();
                    IField pField = pFields.get_Field(i);
                    IFieldEdit pFieldEdit = (IFieldEdit)ppField;
              
                    pFieldEdit.Name_2 = pFields.get_Field(i).Name;
                    pFieldEdit.Type_2 = pField.Type;
                    ppFieldsEdit.AddField(ppField);
                    
                }

            }
        }
3),导出为shp文件

 private void 导出为ShpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDia = new SaveFileDialog();
            saveDia.Title = "Export Shp.......";
            saveDia.CreatePrompt = false;
            saveDia.OverwritePrompt = false;//提示用户是否覆盖  
            saveDia.Filter = "ShapeFile  (*.shp)|*.shp";
            saveDia.RestoreDirectory = true;
            saveDia.AddExtension = true;//若用户没有添加扩展名,将自行添加
            if (saveDia.ShowDialog() == DialogResult.OK)
            {

                string strFullPath = "";
                string filePath = "";
                string fileName = "";
                string shapeName = "";
                strFullPath = saveDia.FileName;
                int Index = strFullPath.LastIndexOf("\\");
                filePath = strFullPath.Substring(0, Index);
                fileName = strFullPath.Substring(Index + 1);
                shapeName = fileName.Remove(fileName.ToLower().LastIndexOf(".shp"), 4);
                ILayer yLayer = axMapControl1.get_Layer(0);
                IFeatureLayer yFeatureLayer = yLayer as IFeatureLayer;          //获取esriGeometryType,作为参数传入新建shp文件的函数来确定新建类型
                IFeatureClass yFeatureClass = yFeatureLayer.FeatureClass;
                string fieldname =yFeatureClass.ShapeFieldName;
                IFields yFields= yFeatureClass.Fields;
                int ind=yFields.FindField(fieldname);
                IField yField = yFields.get_Field(ind);
                
                 IGeometryDef geometryDef = yField.GeometryDef;
                 esriGeometryType type = geometryDef.GeometryType;   //获取esriGeometryType,作为参数传入新建shp文件的

                 createwhatShapefile(type, axMapControl1.Map, filePath, fileName);        //新建空白shp文件
                axMapControl1.AddShapeFile(filePath, fileName);

                ILayer pLayer = axMapControl1.get_Layer(0);
                IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;          //定义被复制图层和空白shp文件要素和要素类
                IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;

                ILayer pLayer1 = axMapControl1.get_Layer(1);
                IFeatureLayer pFeatureLayer1 = pLayer1 as IFeatureLayer;
                IFeatureClass pFeatureClass1 = pFeatureLayer1.FeatureClass;
                
                IQueryFilter pQueryFilter = new QueryFilterClass();             //SQL Filter 
                pQueryFilter.WhereClause = Divesity.GlobleVariety.SQLLine; //这里是我定义的一个全球变量,存储的是查询的SQL语句
  
                IFeatureCursor pFeatureCursor = pFeatureClass1.Search(pQueryFilter, false);     // 添加要素至要素类并作为一个新涂层展现出来
               
                IFeature pFeature = pFeatureCursor.NextFeature();
                
                if (pFeatureClass.Fields.FieldCount != pFeature.Fields.FieldCount)
                {
                    addFields(pFeature, pFeatureClass);
                }
                
                while (pFeature != null)
                {
                   
                    AddFeatureToFeatureClass(pFeatureClass, pFeature);
                    pFeature = pFeatureCursor.NextFeature();
                }
                MessageBox.Show("导出成功!");
                
能详细点吗,Divesity是什么,如果可以求分享805444842@qq.com
回复 点赞
whatdoyousay 2014年08月22日
引用 11 楼 GIS520025 的回复:
[/code]
谢谢,成功生成了,不过字段少了几个,不知道哪里出问题了[/quote] 我调试的时候也是有这样的问题,你可以看下addFields函数里面的i变量,初始值为3,这个函数你需要自己根据需求改一下
回复 点赞
whatdoyousay 2014年08月22日
我上传了文档,链接 http://download.csdn.net/detail/liuhuibing12/7802885
回复 点赞
GIS520025 2014年08月22日
引用 8 楼 liuhuibing12 的回复:
[quote=引用 6 楼 GIS520025 的回复:] 少了个方法AddFeatureToFeatureClass(pFeatureClass, pFeature);,求求
对,把这个忘了

private IFeatureClass AddFeatureToFeatureClass(IFeatureClass pFeatureClass, IFeature pFeature)
         {
             IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true);
             IFeatureBuffer pFeatureBuffer = pFeatureClass.CreateFeatureBuffer();
             IFields pFields = pFeatureClass.Fields;
             for (int i = 1; i <= pFeature.Fields.FieldCount-1; i++)
             {
                 IField pField = pFields.get_Field(i);
                 if (pField.Type == esriFieldType.esriFieldTypeGeometry)
                 {
                     pFeatureBuffer.set_Value(i, pFeature.ShapeCopy);                
                 }
                 else
                 {
                         switch (pField.Type)
                         {
                             case esriFieldType.esriFieldTypeInteger:
                                 pFeatureBuffer.set_Value(i, Convert.ToInt32(pFeature.get_Value(i)));

                                 break;
                             case esriFieldType.esriFieldTypeDouble:
                                 pFeatureBuffer.set_Value(i, Convert.ToDouble(pFeature.get_Value(i)));
                                 break;
                             case esriFieldType.esriFieldTypeString:
                                 pFeatureBuffer.set_Value(i, Convert.ToString(pFeature.get_Value(i)));
                                 break;
                             default:
                                 break;
                     }
                 }                   
             }
             pFeatureCursor.InsertFeature(pFeatureBuffer);
             return pFeatureClass;
         }

[/quote]谢谢,成功生成了,不过字段少了几个,不知道哪里出问题了
回复 点赞
GIS520025 2014年08月22日
太感谢了+++++
回复 点赞
GIS520025 2014年08月22日
引用 7 楼 liuhuibing12 的回复:
[quote=引用 5 楼 GIS520025 的回复:] [quote=引用 4 楼 liuhuibing12 的回复:] 为了方便后来人,我把代码分享出来,用了两种方法实现 ……………… MessageBox.Show("导出成功!"); [/code]
能详细点吗,Divesity是什么,如果可以求分享805444842@qq.com[/quote] pQueryFilter.WhereClause = Divesity.GlobleVariety.SQLLine; //这里是我定义的一个全球变量 Divesity是我新建的命名空间名称[/quote]你说的第二个方法,AddFeatureToFeatureClass(pFeatureClass, pFeature);这个方法可以分享吗。。。好苦逼,求求
回复 点赞
whatdoyousay 2014年08月22日
引用 6 楼 GIS520025 的回复:
少了个方法AddFeatureToFeatureClass(pFeatureClass, pFeature);,求求
对,把这个忘了

private IFeatureClass AddFeatureToFeatureClass(IFeatureClass pFeatureClass, IFeature pFeature)
         {
             IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true);
             IFeatureBuffer pFeatureBuffer = pFeatureClass.CreateFeatureBuffer();
             IFields pFields = pFeatureClass.Fields;
             for (int i = 1; i <= pFeature.Fields.FieldCount-1; i++)
             {
                 IField pField = pFields.get_Field(i);
                 if (pField.Type == esriFieldType.esriFieldTypeGeometry)
                 {
                     pFeatureBuffer.set_Value(i, pFeature.ShapeCopy);                
                 }
                 else
                 {
                         switch (pField.Type)
                         {
                             case esriFieldType.esriFieldTypeInteger:
                                 pFeatureBuffer.set_Value(i, Convert.ToInt32(pFeature.get_Value(i)));

                                 break;
                             case esriFieldType.esriFieldTypeDouble:
                                 pFeatureBuffer.set_Value(i, Convert.ToDouble(pFeature.get_Value(i)));
                                 break;
                             case esriFieldType.esriFieldTypeString:
                                 pFeatureBuffer.set_Value(i, Convert.ToString(pFeature.get_Value(i)));
                                 break;
                             default:
                                 break;
                     }
                 }                   
             }
             pFeatureCursor.InsertFeature(pFeatureBuffer);
             return pFeatureClass;
         }

回复 点赞
whatdoyousay 2014年08月22日
引用 5 楼 GIS520025 的回复:
[quote=引用 4 楼 liuhuibing12 的回复:] 为了方便后来人,我把代码分享出来,用了两种方法实现 ……………… MessageBox.Show("导出成功!"); [/code]
能详细点吗,Divesity是什么,如果可以求分享805444842@qq.com[/quote] pQueryFilter.WhereClause = Divesity.GlobleVariety.SQLLine; //这里是我定义的一个全球变量 Divesity是我新建的命名空间名称
回复 点赞
GIS520025 2014年08月22日
少了个方法AddFeatureToFeatureClass(pFeatureClass, pFeature);,求求
回复 点赞
whatdoyousay 2014年08月14日
为了方便后来人,我把代码分享出来,用了两种方法实现 1,直接调用Geoprocessor工具集,里面有个FeatureClassToFeatureClass接口,直接实现你想要的功能

SaveFileDialog saveDia = new SaveFileDialog();
            saveDia.Title = "Export Shp.......";
            saveDia.CreatePrompt = false;
            saveDia.OverwritePrompt = false;//提示用户是否覆盖  
            saveDia.Filter = "ShapeFile  (*.shp)|*.shp";
            saveDia.RestoreDirectory = true;
            saveDia.AddExtension = true;//若用户没有添加扩展名,将自行添加
            if (saveDia.ShowDialog() == DialogResult.OK)
            {

                string strFullPath = "";
                string filePath = "";
                string fileName = "";
                string shapeName = "";
                strFullPath = saveDia.FileName;
                int Index = strFullPath.LastIndexOf("\\");
                filePath = strFullPath.Substring(0, Index);
                fileName = strFullPath.Substring(Index + 1);
                shapeName = fileName.Remove(fileName.ToLower().LastIndexOf(".shp"), 4);

                IQueryFilter pQuryFilter = new QueryFilterClass();
                pQuryFilter.WhereClause = Divesity.GlobleVariety.SQLLine;
                FeatureClassToFeatureClass pFeattoFeat = new FeatureClassToFeatureClass();
                Geoprocessor GP = new Geoprocessor();
                object sev = null;
                //GP.AddToolbox(@"C:\Program Files\ArcGIS\Desktop10.0\ArcToolbox\Toolboxes\Coverage Tools.tbx");
                IVariantArray parameters = new VarArrayClass();
                GP.OverwriteOutput = true;
                try
                {
                    pFeattoFeat.in_features = Divesity.GlobleVariety. LoadFilePath;
                    pFeattoFeat.where_clause = pQuryFilter.WhereClause;
                    pFeattoFeat.out_name = fileName;
                    pFeattoFeat.out_path = filePath;
                    pFeattoFeat.out_feature_class = strFullPath;
                    GP.Execute(pFeattoFeat, null);
                    MessageBox.Show("导出成功!");
                }
                catch (Exception)
                {
                    // Print geoprocessing execution error messages.
                    Console.WriteLine(GP.GetMessages(ref sev));
                }
2,另外一种方法就是先新建一个空白的shp文件,然后再添加你指定图层的字段,最后再添加要素和各字段的值。 1),新建shp文件,用createfeatureclass方法

public static void createwhatShapefile(esriGeometryType type ,IMap map, string filePath, string fileName)
          {
              //建立shape字段
             IFields pFields = new FieldsClass();
             IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
             IField pField = new FieldClass();
             IFieldEdit pFieldEdit = pField as IFieldEdit;
             pFieldEdit.Name_2 = "Shape";
             pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
 
             //设置geometry definition
             IGeometryDef pGeometryDef = new GeometryDefClass();
             IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
             pGeometryDefEdit.GeometryType_2 = type;   //根据当前图层的类型来确定新建图层的类型
             pGeometryDefEdit.SpatialReference_2 = map.SpatialReference;
             pFieldEdit.GeometryDef_2 = pGeometryDef;
             pFieldsEdit.AddField(pField);
 
             //新建字段
             pField = new FieldClass();
             pFieldEdit = pField as IFieldEdit;
             pFieldEdit.Length_2 = 10;
             pFieldEdit.Name_2 = "OBJECTID";
             pFieldEdit.AliasName_2 = "ID";
             pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
             pFieldsEdit.AddField(pField);
             //其他字段用循环新建
             
 
             IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
             IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace;
 
             //IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
             //IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace;

             int i = fileName.IndexOf(".shp");
             if (i == -1)
                 pFeatureWorkspace.CreateFeatureClass(fileName + ".shp", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
             else
                 pFeatureWorkspace.CreateFeatureClass(fileName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
 
            //MessageBox.Show("OK");4 
         }

2),添加字段

public void addFields(IFeature pFeature,IFeatureClass pFeatureClass)  //传入待修改要素类及样本要素
        {

            IFields ppFileds = pFeatureClass.Fields;
            IFieldsEdit ppFieldsEdit = (IFieldsEdit)ppFileds;
            IFields pFields = pFeature.Fields;
            while (pFields.FieldCount > pFeatureClass.Fields.FieldCount)
            {
                
                for (int i = 3; i < pFeature.Fields.FieldCount; i++)
                {
                    IField ppField = new FieldClass();
                    IField pField = pFields.get_Field(i);
                    IFieldEdit pFieldEdit = (IFieldEdit)ppField;
              
                    pFieldEdit.Name_2 = pFields.get_Field(i).Name;
                    pFieldEdit.Type_2 = pField.Type;
                    ppFieldsEdit.AddField(ppField);
                    
                }

            }
        }
3),导出为shp文件

 private void 导出为ShpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDia = new SaveFileDialog();
            saveDia.Title = "Export Shp.......";
            saveDia.CreatePrompt = false;
            saveDia.OverwritePrompt = false;//提示用户是否覆盖  
            saveDia.Filter = "ShapeFile  (*.shp)|*.shp";
            saveDia.RestoreDirectory = true;
            saveDia.AddExtension = true;//若用户没有添加扩展名,将自行添加
            if (saveDia.ShowDialog() == DialogResult.OK)
            {

                string strFullPath = "";
                string filePath = "";
                string fileName = "";
                string shapeName = "";
                strFullPath = saveDia.FileName;
                int Index = strFullPath.LastIndexOf("\\");
                filePath = strFullPath.Substring(0, Index);
                fileName = strFullPath.Substring(Index + 1);
                shapeName = fileName.Remove(fileName.ToLower().LastIndexOf(".shp"), 4);
                ILayer yLayer = axMapControl1.get_Layer(0);
                IFeatureLayer yFeatureLayer = yLayer as IFeatureLayer;          //获取esriGeometryType,作为参数传入新建shp文件的函数来确定新建类型
                IFeatureClass yFeatureClass = yFeatureLayer.FeatureClass;
                string fieldname =yFeatureClass.ShapeFieldName;
                IFields yFields= yFeatureClass.Fields;
                int ind=yFields.FindField(fieldname);
                IField yField = yFields.get_Field(ind);
                
                 IGeometryDef geometryDef = yField.GeometryDef;
                 esriGeometryType type = geometryDef.GeometryType;   //获取esriGeometryType,作为参数传入新建shp文件的

                 createwhatShapefile(type, axMapControl1.Map, filePath, fileName);        //新建空白shp文件
                axMapControl1.AddShapeFile(filePath, fileName);

                ILayer pLayer = axMapControl1.get_Layer(0);
                IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;          //定义被复制图层和空白shp文件要素和要素类
                IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;

                ILayer pLayer1 = axMapControl1.get_Layer(1);
                IFeatureLayer pFeatureLayer1 = pLayer1 as IFeatureLayer;
                IFeatureClass pFeatureClass1 = pFeatureLayer1.FeatureClass;
                
                IQueryFilter pQueryFilter = new QueryFilterClass();             //SQL Filter 
                pQueryFilter.WhereClause = Divesity.GlobleVariety.SQLLine; //这里是我定义的一个全球变量,存储的是查询的SQL语句
  
                IFeatureCursor pFeatureCursor = pFeatureClass1.Search(pQueryFilter, false);     // 添加要素至要素类并作为一个新涂层展现出来
               
                IFeature pFeature = pFeatureCursor.NextFeature();
                
                if (pFeatureClass.Fields.FieldCount != pFeature.Fields.FieldCount)
                {
                    addFields(pFeature, pFeatureClass);
                }
                
                while (pFeature != null)
                {
                   
                    AddFeatureToFeatureClass(pFeatureClass, pFeature);
                    pFeature = pFeatureCursor.NextFeature();
                }
                MessageBox.Show("导出成功!");
                
回复 点赞
xian_wwq 2014年08月14日
顶下,
回复 点赞
whatdoyousay 2014年08月14日
终于解决了……不容易。 来人啊,白送分啊
回复 点赞
whatdoyousay 2014年08月06日
头疼很久了,求高手来解救一下
回复 点赞
发动态
发帖子
C#
创建于2007-09-28

8.5w+

社区成员

64.0w+

社区内容

.NET技术 C#
社区公告
暂无公告