您的位置:首页 > 编程语言 > C#

C#+AE实现类似Identify功能

2017-12-31 22:46 357 查看
转眼已经到2017年年末,再不发博文就要到下一年了^_^

这次做的是老师布置的作业,用C#+AE实现类似ArcMap中Identify的功能

最后做出来是用矩形框选择要素,点击treeview中的要素,该要素会在地图上闪烁,但只能对一个图层进行identify。

首先,建立显示属性信息的窗体(identifyform):



窗体identifyform中代码:

public partial class identifyform : Form
{

public IMapControlDefault m;
public identifyform()
{
InitializeComponent();
}

private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
IFeatureLayer pfeaturelayer = new FeatureLayerClass();
string ss = treeView1.SelectedNode.Parent.Text;
for (int i = 0; i < m.LayerCount; i++)
{
ILayer play = m.get_Layer(i);
if (play.Name == ss)
{
pfeaturelayer = m.get_Layer(i) as IFeatureLayer;
break;
}
}
IQueryFilter pqueryfilter = new QueryFilter();
string s = treeView1.SelectedNode.Text.ToString();

pqueryfilter.SubFields = "*";
pqueryfilter.WhereClause = "NAME='" + s + "'";

IFeatureCursor pfeaturecursor = pfeaturelayer.Search(pqueryfilter, true);
IFeature pfeature = pfeaturecursor.NextFeature();
IFields pfields = pfeaturelayer.FeatureClass.Fields;

listView1.Items.Clear();

while (pfeature != null)
{
for (int i = 0; i < pfields.FieldCount; i++)
{
ListViewItem list1 = new ListViewItem();
list1.Text = pfields.get_Field(i).Name;
int pindex2 = pfeaturecursor.FindField(pfields.get_Field(i).Name);
list1.SubItems.Add(pfeature.get_Value(pindex2).ToString());
listView1.Items.Add(list1);
}
m.FlashShape(pfeature.Shape, 3, 500, null);
pfeature = pfeaturecursor.NextFeature();
}
}

}
新建类(identifyicom)中代码:

class identifyicom:ICommand,ITool
{
IMapControlDefault mapp;
identifyform identform = new identifyform();
public string s1;
public string s2;
public int Bitmap
{
get { throw new NotImplementedException(); }
}

public string Caption
{
get { throw new NotImplementedException(); }
}

public string Category
{
get { throw new NotImplementedException(); }
}

public bool Checked
{
get { throw new NotImplementedException(); }
}

public bool Enabled
{
get { throw new NotImplementedException(); }
}

public int HelpContextID
{
get { throw new NotImplementedException(); }
}

public string HelpFile
{
get { throw new NotImplementedException(); }
}

public string Message
{
get { throw new NotImplementedException(); }
}

public string Name
{
get { throw new NotImplementedException(); }
}

public void OnClick()
{

}

public void OnCreate(object Hook)
{
mapp = Hook as IMapControlDefault;
}

public string Tooltip
{
get { throw new NotImplementedException(); }
}

public int Cursor
{
get;
set;
}

public bool Deactivate()
{
return true;
}

public bool OnContextMenu(int x, int y)
{
return false;
}

public void OnDblClick()
{
throw new NotImplementedException();
}

public void OnKeyDown(int keyCode, int shift)
{
throw new NotImplementedException();
}

public void OnKeyUp(int keyCode, int shift)
{
throw new NotImplementedException();
}

public void OnMouseDown(int button, int shift, int x, int y)
{
bool b1 = false;
foreach (Form f2 in System.Windows.Forms.Application.OpenForms)
{
if ("identifyform" == f2.Name)
{
f2.Activate();
b1 = true;
}
}
if (!b1)
{
identform = new identifyform();
identform.Show();
}

IGeometry pgeometry = mapp.TrackRectangle();
IEnvelope pEnvelope = pgeometry as IEnvelope;
ISelectionEnvironment pSelectionEnvironment = new SelectionEnvironmentClass();
IRgbColor prgbcolor = new RgbColor();
prgbcolor.Red = 0;
prgbcolor.Green = 0;
prgbcolor.Blue = 255;
pSelectionEnvironment.DefaultColor = prgbcolor;
mapp.ActiveView.FocusMap.SelectByShape(pEnvelope, pSelectionEnvironment, false);
mapp.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);

s1 = pgeometry.Envelope.XMax.ToString();
s2 = pgeometry.Envelope.YMax.ToString();
identform.toolStripStatusLabel2.Text = "X:" + s1 + " " + "Y:" + s2;

ISpatialFilter psf = new SpatialFilter();
psf.SubFields = "*";
psf.Geometry = pgeometry;
psf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

for (int j = 0; j < mapp.LayerCount; j++)
{
IFeatureLayer pfl = mapp.get_Layer(j) as IFeatureLayer;
if (pfl == null) return;

IFeatureCursor pfc = pfl.Search((ISpatialFilter)psf, true);
IFeature pfeature = pfc.NextFeature();

identform.treeView1.Nodes.Clear();
TreeNode pnode1 = new TreeNode(pfl.Name);
identform.treeView1.Nodes.Add(pnode1);

identform.listView1.Clear();
identform.listView1.Columns.Add("Field", 180, HorizontalAlignment.Left);
identform.listView1.Columns.Add("Value", 180, HorizontalAlignment.Left);
IFields pfields = pfl.FeatureClass.Fields;
int pind = pfc.FindField("NAME");

if (pfeature != null)
{
for (int i = 0; i < pfields.FieldCount; i++)
{
ListViewItem list1 = new ListViewItem();
list1.Text = pfields.get_Field(i).Name;
int pindex = pfc.FindField(pfields.get_Field(i).Name);
list1.SubItems.Add(pfeature.get_Value(pindex).ToString());
identform.listView1.Items.Add(list1);
}
}

while (pfeature != null)
{
pnode1.Nodes.Add(pfeature.get_Value(pind).ToString());
pfeature = pfc.NextFeature();
}
identform.m = mapp;
mapp.ActiveView.Refresh();
}
}

public void OnMouseMove(int button, int shift, int x, int y)
{

}

public void OnMouseUp(int button, int shift, int x, int y)
{

}

public void Refresh(int hdc)
{

}

}

调用:
private void toolStripButton18_Click(object sender, EventArgs e)
{
ICommand pCmd = new identifyicom();
pCmd.OnCreate(axMapControl1.Object);
ITool ptool = pCmd as ITool;
axMapControl1.CurrentTool = ptool;
}

效果图如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# ArcGIS ArcEngine