随机数创建点shp文件

很简单,存个档,以后可以直接用,只有一个属性字段——高程。

 1 ISpatialReference pSpatialReference = this.axMapControl1.ActiveView.FocusMap.SpatialReference;
 2             string strShapeFolder="C:/";
 3             string strShapeFile = "test.shp";
 4             string shapeFileFullName = strShapeFolder + strShapeFile;
 5             IWorkspaceFactory pWorkspaceFactory= new ShapefileWorkspaceFactory();
 6             IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWorkspaceFactory.OpenFromFile(strShapeFolder, 0);
 7             IFeatureClass pFeatureClass;
 8             if (File.Exists(shapeFileFullName))
 9             {
10                 pFeatureClass = pFeatureWorkspace.OpenFeatureClass(strShapeFile);
11                 IDataset pDataset = (IDataset)pFeatureClass;
12                 pDataset.Delete();
13             }
14             IFields pFields = new FieldsClass();
15             IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;
16             
17             IField pField = new FieldClass();
18             IFieldEdit pFieldEdit = (IFieldEdit)pField;
19             pFieldEdit.Name_2 = "SHAPE";
20             pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
21             IGeometryDefEdit pGeoDef = new GeometryDefClass();
22             IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;
23             pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
24             pGeoDefEdit.SpatialReference_2 = pSpatialReference; //new UnknownCoordinateSystemClass();
25             pFieldEdit.GeometryDef_2 = pGeoDef;
26             pFieldsEdit.AddField(pField);
27 
28             pField = new FieldClass();
29             pFieldEdit = (IFieldEdit)pField;
30             pFieldEdit.Name_2 = "elevation";
31             pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
32             pFieldsEdit.AddField(pField);
33 
34             pFeatureClass = pFeatureWorkspace.CreateFeatureClass(strShapeFile, pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
35 
36             Random rd = new Random();
37             for (int i = 0; i < 20;i++ )
38             {
39                 IPoint pPoint = new PointClass();
40                 pPoint.X = rd.Next(0, 1000);
41                 pPoint.Y = rd.Next(0, 1000);
42                 pPoint.Z = rd.Next(0, 100);
43                 IFeature pFeature = pFeatureClass.CreateFeature();
44                 pFeature.Shape = pPoint;
45                 pFeature.set_Value(pFeature.Fields.FindField("elevation"), pPoint.Z);
46                 pFeature.Store();
47             }
48             IFeatureLayer pFeaturelayer = new FeatureLayerClass();
49             pFeaturelayer.FeatureClass = pFeatureClass;
50             pFeaturelayer.Name = "layer";
51             this.axMapControl1.AddLayer(pFeaturelayer);
52         }

 

posted on 2013-01-15 14:57  小猪同学  阅读(351)  评论(0编辑  收藏  举报

导航