Arcengine-点图层的创建

对Arcengine不太熟悉,在一次实验中,根据点集创建点图层,总共200左右个点,耗时56秒左右。原来是我的方法用错了。

我只是循环数组,挨个添加点,所以慢,如下

 //    Parallel.For(0, pointsList.Count, i =>
                //    {
                //        pPoint.X = pointsList[i].getLon();
                //        pPoint.Y = pointsList[i].getLat();
                //        IFeature pFeature = pFeatureClass.CreateFeature();
                //        pFeature.Shape = pPoint;
                //        pFeature.set_Value(pFeature.Fields.FindField("ID"), i);
                //        pFeature.set_Value(pFeature.Fields.FindField("Temputre"), pointsList[i].getData());
                //        pFeature.Store();

                //    });

在csdn论坛上请教了老师,发现,多点创建时,应该使用IFeatureBuffer和IFeatureCursor,具体方法如下:

//创建矢量点图层
        private IFeatureClass CreateShpFromPoint(string fullfilepath)
        {
                ISpatialReference pSpatialReference = m_MapCtl2.ActiveView.FocusMap.SpatialReference;

                string strShapeFolder = imgDirPath;
                string strShapeFile = "selectPoints.shp";
                string shapeFileFullName = strShapeFolder + strShapeFile;

                IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
                IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(strShapeFolder, 0);
                IFeatureClass pFeatureClass;
                if (File.Exists(shapeFileFullName))
                {
                    pFeatureClass = pFeatureWorkspace.OpenFeatureClass(strShapeFile);
                    IDataset pDataset = (IDataset)pFeatureClass;
                    pDataset.Delete();
                }

                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;

                IGeometryDefEdit pGeoDef = new GeometryDefClass();
                IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;
                pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
                pGeoDefEdit.SpatialReference_2 = pSpatialReference; //new UnknownCoordinateSystemClass();
                pFieldEdit.GeometryDef_2 = pGeoDef;
                pFieldsEdit.AddField(pField);

                pField = new FieldClass();
                pFieldEdit = (IFieldEdit)pField;
                pFieldEdit.Name_2 = "ID";
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                pFieldsEdit.AddField(pField);


                pField = new FieldClass();
                pFieldEdit = (IFieldEdit)pField;
                pFieldEdit.Name_2 = "Temputre";
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
                pFieldsEdit.AddField(pField);

                pFeatureClass = pFeatureWorkspace.CreateFeatureClass(strShapeFile, pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");

            return pFeatureClass;
}

然后再插入点

private void InsertFeatures(IFeatureClass pfeatureClass, List<PointData> pointsList)
        {
            IFeatureBuffer pfeatureBuffer = pfeatureClass.CreateFeatureBuffer();
            IFeatureCursor pfeatureCursor = pfeatureClass.Insert(true);

            //字段索引
            int filedIndex_ID = pfeatureClass.FindField("ID");
            int fileIndex_Tem = pfeatureClass.FindField("Temputre");
            IPoint pPoint = new PointClass();
            for (int i = 0; i < pointsList.Count; i++)
            {
                pPoint.X = pointsList[i].getLon();
                pPoint.Y = pointsList[i].getLat();
                pfeatureBuffer.Shape = pPoint;
                pfeatureBuffer.set_Value(filedIndex_ID,i);
                pfeatureBuffer.set_Value(fileIndex_Tem,pointsList[i].getData());
                pfeatureCursor.InsertFeature(pfeatureBuffer);
            }
        }

PointData是我自定义的一个类,可以用IPoint类代替。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值