C#+ArcGIS Engine 中创建Shp点图层

if (lblFilePath.Text.Trim() == "")// '检查保存路径不为空
{
MessageBox.Show("请选择保存路径");
return;
}
if (TxtSaveName.Text.Trim() == "")// '检查文件名不为空
{
MessageBox.Show("请选择保存图层名");
return;
}

//指定目录,如果存在则删除
//ISpatialReference pSpatialReference = _frmMain.pCurrentMap.ActiveView.FocusMap.SpatialReference;

string strShapeFolder = lblFilePath.Text;
string strShapeFile = TxtSaveName.Text.Trim() + ".shp";


FileInfo fFile = new FileInfo(strShapeFolder + @"\" + TxtSaveName.Text.Trim() + ".shp");
//是否重命名
if (fFile.Exists)
{

DialogResult r2 = MessageBox.Show("此文件名已经存在,要覆盖吗?", "返回值 是6 否7", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (r2 == System.Windows.Forms.DialogResult.Yes) //取消关闭
{
DirectoryInfo fold = new DirectoryInfo(strShapeFolder);
FileInfo[] files = fold.GetFiles(TxtSaveName.Text.Trim() + ".*");
foreach (FileInfo f in files)
{
f.Delete();
}
}
else
{
TxtSaveName.Focus();
return;
}
}

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();
}

//设置字段
int i;
double tmpLongitude;
double tmplatitude;
IPoint pPoint;

IFields pFields = new FieldsClass();
IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;

IField pField = new FieldClass();
IFieldEdit pFieldEdit = (IFieldEdit)pField;

ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironment();
IGeographicCoordinateSystem pGCS;
pGCS = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);

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 = pGCS; //new UnknownCoordinateSystemClass();
pFieldEdit.GeometryDef_2 = pGeoDef;
pFieldsEdit.AddField(pField);


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

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

pField = new FieldClass();
pFieldEdit = (IFieldEdit)pField;
pFieldEdit.Name_2 = "地点";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldEdit.Length_2 = 30;
pFieldsEdit.AddField(pField);

pField = new FieldClass();
pFieldEdit = (IFieldEdit)pField;
pFieldEdit.Name_2 = "炮型";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldEdit.Length_2 = 30;
pFieldsEdit.AddField(pField);

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

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

//进度条
dlgReadProgress newprogress = new dlgReadProgress();
{
newprogress.Label1.Text = "正在生成shp文件,请稍候。。。";
newprogress.ProgressBar.Maximum = gvwPoint.SelectedRows.Count;
newprogress.ProgressBar.Step = 1;
}

for (i = 0; i < gvwPoint.SelectedRows.Count; i++)
{
tmpLongitude = Convert.ToDouble(gvwPoint.SelectedRows[i].Cells[3].Value.ToString().Trim());
tmpLongitude += Convert.ToDouble(gvwPoint.SelectedRows[i].Cells[4].Value.ToString().Trim()) / 60.0;
tmpLongitude += Convert.ToDouble(gvwPoint.SelectedRows[i].Cells[5].Value.ToString().Trim()) / 3600.0;

tmplatitude = Convert.ToDouble(gvwPoint.SelectedRows[i].Cells[6].Value.ToString().Trim());
tmplatitude += Convert.ToDouble(gvwPoint.SelectedRows[i].Cells[7].Value.ToString().Trim()) / 60.0;
tmplatitude += Convert.ToDouble(gvwPoint.SelectedRows[i].Cells[8].Value.ToString().Trim()) / 3600.0;

pPoint = new PointClass();
pPoint.X = tmpLongitude;
pPoint.Y = tmplatitude;
IFeature pFeature = pFeatureClass.CreateFeature();
pFeature.Shape = pPoint;
pFeature.set_Value(pFeature.Fields.FindField("经度"), tmpLongitude.ToString("F4"));
pFeature.set_Value(pFeature.Fields.FindField("纬度"), tmplatitude.ToString("F4"));
pFeature.set_Value(pFeature.Fields.FindField("地点"), gvwPoint.SelectedRows[i].Cells[2].Value);
pFeature.set_Value(pFeature.Fields.FindField("炮型"), gvwPoint.SelectedRows[i].Cells[11].Value);
pFeature.set_Value(pFeature.Fields.FindField("高程"), gvwPoint.SelectedRows[i].Cells[9].Value);
pFeature.Store();

newprogress.ProgressBar.PerformStep();
newprogress.Show();
newprogress.Refresh();
}
newprogress.Close();

DialogResult r1 = MessageBox.Show("是否加载当前图层?", "返回值 是6 否7", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (r1 == DialogResult.Yes)
{
IFeatureLayer pFeaturelayer = new FeatureLayerClass();
pFeaturelayer.FeatureClass = pFeatureClass;
pFeaturelayer.Name = "layer";

_frmMain.pCurrentMap.AddLayer(pFeaturelayer);
}

你可能感兴趣的:(ArcGis Engine)