首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# + ArcGIS10.0创设shapefile,为什么汉字不能写入到.dbf属性文件里(请看代码)

2012-12-15 
C# + ArcGIS10.0创建shapefile,为什么汉字不能写入到.dbf属性文件里(请看代码)本帖最后由 zhenggary 于 20

C# + ArcGIS10.0创建shapefile,为什么汉字不能写入到.dbf属性文件里(请看代码)
本帖最后由 zhenggary 于 2012-11-29 09:42:41 编辑 各位大虾,请帮帮我,这个问题困扰我好多天了。
问题:在程序中,生成shapefile,但是在生成的.dbf属性文件里,汉字(pInsertFeatureBuffer.Value[3] = "中国";)没有被写入。怎样才能把汉字写入到.dbf文件里。
环境:VS2010 + ESRI AirGIS10.0

代码如下:
using System;
using ESRI.ArcGIS;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

namespace CSV2ShapeFile
{
    class Program
    {
        static void Main(string[] args)
        {
// License验证
            if (CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcInfo) != ESRI.ArcGIS.esriSystem.esriLicenseStatus.esriLicenseCheckedOut)
            {
                // don't get the license
                Console.WriteLine("Don't get the license,failed!");
            }

            const string strFolder = @"D:\ZG\Mapwork";   //目标文件夹
            const string strName = "myshapefile";  //文件名
            const string strShapeFieldName = "Shape";

            //Open the folder to contain the shapefile as a workspace
            IFeatureWorkspace pFWS;
            IWorkspaceFactory2 pWorkspaceFactory = new ShapefileWorkspaceFactory() as IWorkspaceFactory2;
            pFWS = pWorkspaceFactory.OpenFromFile(strFolder, 0) as IFeatureWorkspace;

            //Set up a simple fields collection
            IFields pFields = new FieldsClass();  //字段
            IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;

            //Make the shape field
            //it will need a geometry definition, with a spatial reference
            IField pField = new FieldClass();
            IFieldEdit pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Name_2 = strShapeFieldName;
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;

            IGeometryDef pGeometryDef = new GeometryDef();


            IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
            pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;  
            pGeometryDefEdit.SpatialReference_2 = new UnknownCoordinateSystem() as ISpatialReference;  
            pFieldEdit.GeometryDef_2 = pGeometryDef;
            pFieldsEdit.AddField(pField);

            //Add No field
            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 30;
            pFieldEdit.Name_2 = "No";
            pFieldEdit.AliasName_2 = "AliasNo";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            //Add Address field
            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 30;
            pFieldEdit.Name_2 = "Address";
            pFieldEdit.AliasName_2 = "AliasAddress";
            pFieldEdit.Editable_2 = false;
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            //Add LONGITUDE field
            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Length_2 = 30;
            pFieldEdit.Name_2 = "X";
            pFieldEdit.AliasName_2 = "AliasX";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);


            //Add LATITUDE field
            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;


            pFieldEdit.Length_2 = 30;
            pFieldEdit.Name_2 = "Y";
            pFieldEdit.AliasName_2 = "AliasY";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            //Create the shapefile
            Console.WriteLine("Start Create shapefile...");
            IFeatureClass featureClass = pFWS.CreateFeatureClass(strName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); 
           
            // 
            IFeatureCursor pInsertFeatureCursor;
            pInsertFeatureCursor = featureClass.Insert(true);

            IFeatureBuffer pInsertFeatureBuffer;
            pInsertFeatureBuffer = featureClass.CreateFeatureBuffer();

            pInsertFeatureBuffer.Value[2] = "116023";
            pInsertFeatureBuffer.Value[3] = "中国";
            pInsertFeatureBuffer.Value[4] = "316023";
            pInsertFeatureBuffer.Value[5] = "416023";

            IPoint pPoint = new Point();
            pPoint.X = Convert.ToInt32(pInsertFeatureBuffer.Value[4].ToString()) / 3600000;
            pPoint.Y = Convert.ToInt32(pInsertFeatureBuffer.Value[5].ToString()) / 3600000;
            pInsertFeatureBuffer.Shape = pPoint;

            //InsertFeature
            pInsertFeatureCursor.InsertFeature(pInsertFeatureBuffer);
           //
            pInsertFeatureCursor.Flush();

            Console.WriteLine("End Create shapefile");   
            Console.ReadLine();
        }
// License验证
        public static esriLicenseStatus CheckOutLicenses(esriLicenseProductCode productCode)
        {
            RuntimeManager.Bind(ProductCode.EngineOrDesktop);
            esriLicenseStatus licenseStatus;


            licenseStatus = esriLicenseStatus.esriLicenseUnavailable;

            AoInitialize m_pAoInitialize = new AoInitialize();
            if (m_pAoInitialize == null)
                return licenseStatus;

            //Determine if the product is available
            licenseStatus = m_pAoInitialize.IsProductCodeAvailable(productCode);
            if (licenseStatus == esriLicenseStatus.esriLicenseAvailable)
                licenseStatus = m_pAoInitialize.Initialize(productCode);
    
            return licenseStatus;
        }
    }
}

[最优解释]
首先 :开始编辑前 要启动编辑 (如arcmap)StartEdit() 结束编辑后 调用 StopEdit()
字段赋值 使用  pTarFeatureBuffer.set_Value(int 字段索,引object 字段值 )

      

  private static bool StartEdit(IFeatureClass trgFC)
        {
            IDataset pDataset = (IDataset)trgFC;
            IWorkspace pWorkspace = pDataset.Workspace;

            if (pWorkspace == null) return false;

            IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IWorkspaceEdit pWE = pFeatureWorkspace as IWorkspaceEdit;
            if (!pWE.IsBeingEdited())
            {
                IVersionedObject pVersioned = pDataset as IVersionedObject;

                if (pVersioned == null 
[其他解释]
加分等待高人指点。。。
[其他解释]
 !pVersioned.IsRegisteredAsVersioned)
                {
                    IMultiuserWorkspaceEdit pMulti = pFeatureWorkspace as IMultiuserWorkspaceEdit;
                    if (pMulti != null)
                    {


                        if (pMulti.SupportsMultiuserEditSessionMode(esriMultiuserEditSessionMode.esriMESMNonVersioned))
                        {
                            try
                            {
                                pMulti.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMNonVersioned);
                            }
                            catch (Exception)
                            {
                                pWE.StartEditing(false);
                            }
                        }
                        else
                        {
                            pWE.StartEditing(false);
                        }
                    }
                    else
                    {
                        pWE.StartEditing(false);
                    }
                }
                else
                {
                    pWE.StartEditing(false);
                }

                if (pWE.IsBeingEdited() == false) return false;


            } 
            pWE.StartEditOperation();
            return true;
        }

        private static void StopEdit(IFeatureClass trgFC, bool isSave)
        {
            IDataset pDataset = (IDataset)trgFC;
            IWorkspace pWorkspace = pDataset.Workspace;
            if (pWorkspace == null) return;
            IWorkspaceEdit pWE = null;
            IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            pWE = pFeatureWorkspace as IWorkspaceEdit;
            if (pWE.IsBeingEdited())
            {
                pWE.StopEditOperation();
                pWE.StopEditing(isSave); //保存慢呀!
            }
        }


[其他解释]
追加说明:
我提供的代码会生成三个文件(.dbf, .shx, .shp)。同样的代码逻辑,几年以前是VB6+ArcGIS9.2,插入的汉字和字母等都可以正常显示在.dbf属性文件里,现在升级到C#+ArcGIS10.0后,字母可以正常显示,汉字却不能显示了。
请各位高手结合我的代码,给诊断病情。等待各位高手出诊。。。

热点排行