參考:ArcGIS Engine效率探究——要素的添加和刪除、屬性的讀取和更新
刪除要素
//添加圖層,顯示在最上面
axMapControl1.AddShapeFile(@"D:\01-業務工作\08-綜合業務平台\綜合業務平台數據\船載", "cjsj.shp");
//刪除 shapefile 中的要素
//獲取第一個圖層
ILayer pLayer = axMapControl1.get_Layer(0);
//以矢量圖層的形式獲取
IFeatureLayer pFLayer = pLayer as IFeatureLayer;
//獲取要素集
IFeatureClass pFC = pFLayer.FeatureClass;
//查詢全部要素
IFeatureCursor pFCursor = pFC.Search(null, false);
IFeature pfeature = pFCursor.NextFeature();
//遍歷所有要素,並刪除
while (pfeature != null)
{
pfeature.Delete();
pfeature = pFCursor.NextFeature();
}
添加要素,dtzh 為 DataTable 對象
for (int i = 0; i < dtzh.Rows.Count; i++)
{
//定義一個點,並加入經緯度
IPoint pPoint = new PointClass();
double lon = Double.Parse(dtzh.Rows[i]["經度"].ToString());
double lat = Double.Parse(dtzh.Rows[i]["緯度"].ToString());
pPoint.PutCoords(lon, lat);
//將點添加到要素集中,並存儲
IFeature pFeature = pFC.CreateFeature();
pFeature.Shape = pPoint;
pFeature.Store();
//獲取索引值
int i_id = pFC.FindField("Id");
int i_time = pFC.FindField("Time");
int i_cjsj = pFC.FindField("采集時間");
int i_lon = pFC.FindField("經度");
int i_lat = pFC.FindField("緯度");
int i_fo = pFC.FindField("Fo");
int i_fm = pFC.FindField("Fm");
int i_fv = pFC.FindField("Fv");
int i_bv = pFC.FindField("BV");
int i_yield = pFC.FindField("Yield");
//為要素添加屬性值
pFeature.set_Value(i_id, i);
pFeature.set_Value(i_time, dtzh.Rows[i]["Time"].ToString());
pFeature.set_Value(i_cjsj, dtzh.Rows[i]["采集時間"].ToString());
pFeature.set_Value(i_lon, Double.Parse(dtzh.Rows[i]["經度"].ToString()));
pFeature.set_Value(i_lat, Double.Parse(dtzh.Rows[i]["緯度"].ToString()));
pFeature.set_Value(i_fo, Int32.Parse(dtzh.Rows[i]["Fo"].ToString()));
pFeature.set_Value(i_fm, Int32.Parse(dtzh.Rows[i]["Fm"].ToString()));
pFeature.set_Value(i_fv, Int32.Parse(dtzh.Rows[i]["Fv"].ToString()));
pFeature.set_Value(i_bv, Int32.Parse(dtzh.Rows[i]["Blank Value"].ToString()));
pFeature.set_Value(i_yield, dtzh.Rows[i]["Yield"].ToString());
pFeature.Store();
}
顯示地圖經緯度坐標
private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
tssl_lonlat.Text = String.Format("經緯度:東經{0:0.000}° 北緯{1:0.000}°", e.mapX, e.mapY);
tssl_lonlat.Visible = true;
}
