文件图层创建(可用于创建临时图层)

public void CreateNewLayer(AxMapControl ax, string layerName, esriGeometryType layerType)

{

   // 空间参考系 得到当前地图的空间参考系

  ISpatialReference spatialReference = ax.ActiveView.FocusMap.SpatialReference;

  // 设定保持的路径

  string strShapeFolder = @"F:\fuzhou\";

  // 设定图层文件名

  string strShapeFile = layerName + ".shp";

  string shapeFileFullName = strShapeFolder + strShapeFile;

  // 定义工作空间

  IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactory();

  // 矢量工组空间

     IFeatureWorkspace featureWorkspace =(IFeatureWorkspace) workspaceFactory.OpenFromFile(strShapeFolder, 0);

  IFeatureClass featureClass;

  // 如何已存在当前文件

  if (File.Exists(shapeFileFullName))

  {

    featureClass = featureWorkspace.OpenFeatureClass(strShapeFile);

    // 数据集对象的删除

    IDataset dataset = (IDataset)featureClass;

    dataset.Delete();

  }

  // 定义矢量数据的属性字段

  IFields fields = new FieldsClass();

  IFieldsEdit fieldsEdit = (IFieldsEdit)fields;

  IField field = new FieldClass();

  IFieldEdit fieldEdit = (IFieldEdit)field;

  fieldEdit.Name_2 = "SHAPE";

  fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;

  IGeometryDefEdit geoDef = new GeometryDefClass();

  IGeometryDefEdit geoDefEdit = (IGeometryDefEdit)geoDef;

  geoDefEdit.GeometryType_2 = layerType;//设置图层类型

  geoDefEdit.SpatialReference_2 = spatialReference;

  fieldEdit.GeometryDef_2 = geoDef;

  fieldsEdit.AddField(field);

 

  field = new FieldClass();

  fieldEdit = (IFieldEdit)field;

  fieldEdit.Name_2 = "data";

  fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;

  fieldsEdit.AddField(field);

      // 创建对象

  featureClass = featureWorkspace.CreateFeatureClass(strShapeFile, fields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");

  ax.AddShapeFile(strShapeFolder, strShapeFile);

}

posted @ 2012-09-05 15:22  oftenlin  阅读(291)  评论(0编辑  收藏  举报