110,537
社区成员
发帖
与我相关
我的任务
分享
int bz = 0;
private IWorkspace2 pWorkspace2;
private IWorkspaceFactory wf;
private IFeatureWorkspace fws;
private IFields pFields;
private IFieldsEdit pFieldsEdit;
private IFeatureClass pFeatClass;
private void button9_Click(object sender, EventArgs e)
{
object text = this.comboBox1.Text;
//IWorkspaceFactory wf = new ShapefileWorkspaceFactoryClass();
//IWorkspaceName wname = wf.Create("C:\\temp\\", "biaozhu", null, 0);
//IName name_ = wname as IName;
//IWorkspace workspace = (IWorkspace)name_.Open();
if (bz == 0)
{
wf = new ShapefileWorkspaceFactoryClass();
IWorkspaceName wName = wf.Create(Application.StartupPath, "bz", null, 0);
fws = (IFeatureWorkspace)wf.OpenFromFile(Application.StartupPath + "\\bz", 0);
pWorkspace2 = fws as IWorkspace2;
//设置字段集
pFields = new FieldsClass();
pFieldsEdit = (IFieldsEdit)pFields;
//设置字段
IField pField = new FieldClass();
IFieldEdit pFieldEdit = (IFieldEdit)pField;
//创建类型为几何类型的字段
pFieldEdit.Name_2 = "shape";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
////设置投影坐标系为51N
//ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
//IProjectedCoordinateSystem pcs = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_51N);
//设置地理坐标系为WGS84
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
IGeographicCoordinateSystem pcs = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
//设置几何类型为点类型, 坐标系为上述
IGeometryDef pGeoDef = new GeometryDefClass();
IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;
pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
pGeoDefEdit.SpatialReference_2 = pcs;
//pGeoDefEdit.SpatialReference_2 = geographicCoordinateSystem;
pFieldEdit.GeometryDef_2 = pGeoDef;
pFieldsEdit.AddField(pField);
//添加其他的字段
pField = new FieldClass();
pFieldEdit = (IFieldEdit)pField;
pFieldEdit.Name_2 = "biaozhu";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldsEdit.AddField(pField);
//创建shapefile
pFeatClass = fws.CreateFeatureClass("bz", pFields, null, null, esriFeatureType.esriFTSimple, "shape", "");
bz = 1;
}
IWorkspaceEdit wedit;
//IFeatureCursor featurecursor;
wedit = pWorkspace2 as IWorkspaceEdit;
wedit.StartEditing(false);
wedit.StartEditOperation();
//IFeature pFeature = pFeatClass.CreateFeature();
IFeatureBuffer featureBuffer = pFeatClass.CreateFeatureBuffer();
IFeatureCursor featurecursor = pFeatClass.Insert(true);
IPoint pt = new PointClass();
pt.X = Convert.ToDouble(this.textBox2.Text);
pt.Y = Convert.ToDouble(this.textBox1.Text);
pt.PutCoords(pt.X, pt.Y);
int fieldIndex = pFields.FindField("biaozhu");
object featureOID;
featureBuffer.Shape = pt;
featureBuffer.set_Value(fieldIndex, text);
featureOID = featurecursor.InsertFeature(featureBuffer);
featurecursor.Flush();
wedit.StopEditOperation();
wedit.StopEditing(true);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFields);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFieldsEdit);
System.Runtime.InteropServices.Marshal.ReleaseComObject(fws);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wf);
}