基本功能:加载正确shp文件,实现鹰眼,打开属性表,进行属性查询和右键框选要素
界面截图:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using System.Collections;
namespace test1
{
public partial class Form1 : Form
{
private IActiveViewEvents_Event pMapActiveViewEvents;
bool selectState = false;
public Form1()
{
InitializeComponent();
pMapActiveViewEvents = axMapControl1.Map as IActiveViewEvents_Event;
pMapActiveViewEvents.ItemAdded += new IActiveViewEvents_ItemAddedEventHandler(pMapActiveViewEvents_ItemAdded);
pMapActiveViewEvents.ItemDeleted += new IActiveViewEvents_ItemDeletedEventHandler(pMapActiveViewEvents_ItemDeleted);
}
private void pMapActiveViewEvents_ItemAdded(object Item)
{
MapReLoad();
ComboBoxReLoad();
}
private void pMapActiveViewEvents_ItemDeleted(object Item)
{
MapReLoad();
ComboBoxReLoad();
}
private void MapReLoad()
{
axMapControl2.ClearLayers();
for (int i = 0; i < axMapControl1.LayerCount; i++)
{
axMapControl2.AddLayer(axMapControl1.get_Layer(i));
}
axMapControl2.Extent = axMapControl1.FullExtent;
axMapControl2.ActiveView.Refresh();
}
private void ComboBoxReLoad()
{
comboBox1.Items.Clear();
for (int i = 0; i < axMapControl1.LayerCount; i++)
{
comboBox1.Items.Add(axMapControl1.get_Layer(i).Name);
}
comboBox1.Text = comboBox1.Items[0].ToString();
}
private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
{
MapReLoad();
axMapControl2.SpatialReference = axMapControl1.SpatialReference;
ComboBoxReLoad();
}
private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
{
IEnvelope pEnvelope = e.newEnvelope as IEnvelope;
IGraphicsContainer pGraphicsContainer = axMapControl2.Map as IGraphicsContainer;
IActiveView pActiveView = pGraphicsContainer as IActiveView;
pGraphicsContainer.DeleteAllElements();
IRectangleElement pRectangleElement = new RectangleElementClass();
IElement pElement = pRectangleElement as IElement;
pElement.Geometry = pEnvelope;
IRgbColor pColor = new RgbColorClass();
pColor.Red = 255;
pColor.Green = 0;
pColor.Blue = 0;
pColor.Transparency = 255;
ILineSymbol pLine = new SimpleLineSymbolClass();
pLine.Color = pColor;
pColor.Transparency = 0;
IFillSymbol pFill = new SimpleFillSymbolClass();
pFill.Color = pColor;
pFill.Outline = pLine;
IFillShapeElement pFillShapeElement = pElement as IFillShapeElement;
pFillShapeElement.Symbol = pFill;
pGraphicsContainer.AddElement((IElement)pFillShapeElement, 0);
pActiveView.Refresh();
}
private void axMapControl2_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
{
if (e.button == 1)
{
IPoint point = new PointClass();
point.PutCoords(e.mapX, e.mapY);
axMapControl1.CenterAt(point);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
private void axMapControl2_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
if (e.button == 1)
{
IPoint point = new PointClass();
point.PutCoords(e.mapX, e.mapY);
axMapControl1.CenterAt(point);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
else if (e.button == 2)
{
IEnvelope pEnv = axMapControl2.TrackRectangle();
axMapControl1.Extent = pEnv;
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
if (selectState == true && e.button == 2)
{
IEnvelope pEnv = axMapControl1.TrackRectangle();
axMapControl1.Map.SelectByShape(pEnv, null, false);
axMapControl1.Refresh();
}
}
public string[] OpenShapeFile()
{
string[] ShpFile = new string[2];
OpenFileDialog OpenShpFile = new OpenFileDialog();
OpenShpFile.Title = "Open Shape File";
OpenShpFile.InitialDirectory = "G:";
OpenShpFile.Filter = "Shape File(*.shp)|*.shp";
if (OpenShpFile.ShowDialog() == DialogResult.OK)
{
string ShapPath = OpenShpFile.FileName;
int Position = ShapPath.LastIndexOf("\\");
ShpFile[0] = ShapPath.Substring(0, Position);
ShpFile[1] = ShapPath.Substring(Position + 1);
}
return ShpFile;
}
private void ShpAddBtn_Click(object sender, EventArgs e)
{
string[] ShpFile = OpenShapeFile();
axMapControl1.AddShapeFile(ShpFile[0], ShpFile[1]);
}
private void selectClearBtn_Click(object sender, EventArgs e)
{
axMapControl1.Map.ClearSelection();
axMapControl1.Refresh();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(comboBox1.SelectedIndex) as IFeatureLayer;
IFeatureCursor pFeatureCursor = pFeatureLayer.Search(null, true);
IFeature pFeature = pFeatureCursor.NextFeature();
for (int i = 0; i < pFeature.Fields.FieldCount; i++)
{
if (pFeature.Fields.get_Field(i).Type != esriFieldType.esriFieldTypeGeometry)
{
listBox1.Items.Add(pFeature.Fields.get_Field(i).Name);
}
}
}
private string[] getValue(IFeatureClass pFeatureClass, string strField)
{
IFeatureCursor lFeatureCursor = pFeatureClass.Search(null, false);
IDataStatistics lData = new DataStatisticsClass();
lData.Field = strField;
lData.Cursor = lFeatureCursor as ICursor;
IEnumerator pEnumVar = lData.UniqueValues;
string[] strValue = new string[lData.UniqueValueCount];
pEnumVar.Reset();
int i = 0;
while (pEnumVar.MoveNext())
{
strValue[i++] = pEnumVar.Current.ToString();
}
return strValue;
}
private void getValueBtn_Click(object sender, EventArgs e)
{
IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(comboBox1.SelectedIndex) as IFeatureLayer;
IFeatureCursor pFeatureCursor = pFeatureLayer.Search(null, true);
IFeature pFeature = pFeatureCursor.NextFeature();
string[] values = getValue(pFeatureLayer.FeatureClass, listBox1.Text);
IField pField = new FieldClass();
for (int i = 0; i < pFeature.Fields.FieldCount; i++)
{
if (listBox1.Text == pFeature.Fields.get_Field(i).Name)
{
pField = pFeature.Fields.get_Field(i);
}
}
listBox2.Items.Clear();
for (int i = 0; i < values.Length; i++)
{
if (pField.Type == esriFieldType.esriFieldTypeString)
{
listBox2.Items.Add("\'" + values[i] + "\'");
}
else
{
listBox2.Items.Add(values[i]);
}
}
}
private void queryBtn_Click(object sender, EventArgs e)
{
IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(comboBox1.SelectedIndex) as IFeatureLayer;
if (pFeatureLayer == null)
{
MessageBox.Show("wrong");
return;
}
IQueryFilter pQueryFilter = new QueryFilterClass();
pQueryFilter.WhereClause = textBox1.Text;
try
{
IFeatureCursor pFeatureCursor = pFeatureLayer.Search(pQueryFilter, false);
IFeature pFeature;
while ((pFeature = pFeatureCursor.NextFeature()) != null)
{
axMapControl1.Map.SelectFeature(pFeatureLayer, pFeature);
}
axMapControl1.ActiveView.Refresh();
}
catch (Exception pException)
{
MessageBox.Show(pException.Message);
return;
}
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox1.Text += listBox1.Text;
}
private void listBox2_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox1.Text += listBox2.Text;
}
private void btn1_Click(object sender, EventArgs e)
{
textBox1.Text += " = ";
}
private void btn2_Click(object sender, EventArgs e)
{
textBox1.Text += " > ";
}
private void btn3_Click(object sender, EventArgs e)
{
textBox1.Text += " < ";
}
private void selectRB_CheckedChanged(object sender, EventArgs e)
{
selectState = true;
}
private void FTBtn_Click(object sender, EventArgs e)
{
FromTable ft = new FromTable(axMapControl1);
ft.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
namespace test1
{
public partial class FromTable : Form
{
private AxMapControl axMapControl;
public FromTable(AxMapControl axMapControl)
{
InitializeComponent();
this.axMapControl = axMapControl;
ComboBoxReLoad();
dataUpdate();
}
private void ComboBoxReLoad()
{
comboBox1.Items.Clear();
for (int i = 0; i < axMapControl.LayerCount; i++)
{
comboBox1.Items.Add(axMapControl.get_Layer(i).Name);
}
comboBox1.Text = comboBox1.Items[0].ToString();
}
private void dataUpdate()
{
IFeatureLayer pFeatureLayer = axMapControl.get_Layer(comboBox1.SelectedIndex) as IFeatureLayer;
IFeatureCursor featureCursor = pFeatureLayer.Search(null, false);
IFeature pFeature = featureCursor.NextFeature();
DataTable dt = new DataTable();
DataColumn column = null;
for (int i = 0; i < pFeature.Fields.FieldCount; i++)
{
column = new DataColumn(pFeature.Fields.get_Field(i).Name);
dt.Columns.Add(column);
}
DataRow row = null;
while (pFeature != null)
{
row = dt.NewRow();
for (int i = 0; i < pFeature.Fields.FieldCount; i++)
{
row[i] = pFeature.get_Value(i).ToString();
}
dt.Rows.Add(row);
pFeature = featureCursor.NextFeature();
}
dataGridView1.DataSource = dt;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dataUpdate();
}
}
}
完整工程文件——>https://download.csdn.net/download/nominior/10828134