对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类代替。