属性表的相关开发

属性表的相关开发

三峡大学土木水电学院 肖泽云

1、显示栅格图层和矢量图层的属性表

如果采用C#来开发,首先定义一个函数showAttributeTable()用于显示栅格图层或矢量数据图层的属性表,其参数pLyr为要显示属性的图层,在程序中添加一个dataGridView控件用于显示属性表。该函数的代码如下:

private void showAttributeTable(ILayer pLyr)

{

if (pLyr is IFeatureLayer)

{

DataTable pTable = new DataTable();

IFeatureLayer pFealyr = pLyr as IFeatureLayer;

IFeatureClass pFCls = pFealyr.FeatureClass;

string shape = "";

if (pFCls.ShapeType == esriGeometryType.esriGeometryPoint)

shape = "Point";

else if (pFCls.ShapeType == esriGeometryType.esriGeometryPolyline)

shape = "Polyline";

else if (pFCls.ShapeType == esriGeometryType.esriGeometryPolygon)

shape = "Polygon";

for (int i = 0; i < pFCls.Fields.FieldCount; i++)

{

pTable.Columns.Add(pFCls.Fields.get_Field(i).Name);

}

IFeatureCursor pCursor = pFCls.Search(null, false);

int ishape = pFCls.Fields.FindField("Shape");

IFeature pFea = pCursor.NextFeature();

while (pFea != null)

{

DataRow pRow = pTable.NewRow();

for (int i = 0; i < pFCls.Fields.FieldCount; i++)

{

if (i == ishape)

{

pRow[i] = shape;

continue;

}

pRow[i] = pFea.get_Value(i).ToString();

}

pTable.Rows.Add(pRow);

pFea = pCursor.NextFeature();

}

dataGridView1.DataSource = pTable;

}

else if (pLyr is IRasterLayer)

{

IRasterLayer pRlyr = pLyr as IRasterLayer;

IRaster pRaster = pRlyr.Raster;

IRasterProps pProp = pRaster as IRasterProps;

pProp.PixelType = rstPixelType.PT_LONG;

if (pProp.PixelType == rstPixelType.PT_LONG)

{

IRasterBandCollection pBcol = pRaster as IRasterBandCollection;

IRasterBand pBand = pBcol.Item(0);

ITable pRTable = pBand.AttributeTable;

DataTable pTable = new DataTable();

for (int i = 0; i < pRTable.Fields.FieldCount; i++)

pTable.Columns.Add(pRTable.Fields.get_Field(i).Name);

ICursor pCursor = pRTable.Search(null, false);

IRow pRrow = pCursor.NextRow();

while (pRrow != null)

{

DataRow pRow = pTable.NewRow();

for (int i = 0; i < pRrow.Fields.FieldCount; i++)

{

pRow[i] = pRrow.get_Value(i).ToString();

}

pTable.Rows.Add(pRow);

pRrow = pCursor.NextRow();

}

dataGridView1.DataSource = pTable;

}

}

}

VB.net中,首先添加一个窗体,设置其名称为AttributeForm,在该窗体中添加一个DataGridView控件,如下图所示:

首先,在该窗体的代码中定义全局变量showAttributeLayer用于传递数据图层,如下:

Public showAttributeLayer As ESRI.ArcGIS.Carto.ILayer

然后定义一个显示属性表格的函数showAttributeTable(),其代码如下:

Private Sub showAttributeTable(ByVal pLayer As ESRI.ArcGIS.Carto.ILayer)

If TypeOf pLayer Is ESRI.ArcGIS.Carto.IFeatureLayer Then

Dim pTable As DataTable = New DataTable()

Dim pFealyr As ESRI.ArcGIS.Carto.IFeatureLayer = pLayer

Dim pFCls As ESRI.ArcGIS.Geodatabase.IFeatureClass = pFealyr.FeatureClass

Dim shape As String = ""

Select Case pFCls.ShapeType

Case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint

shape = "Point"

Case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline

shape = "Polyline"

Case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon

shape = "Polygon"

End Select

Dim i As Integer

For i = 0 To pFCls.Fields.FieldCount - 1

pTable.Columns.Add(pFCls.Fields.Field(i).Name)

Next

Dim pCursor As ESRI.ArcGIS.Geodatabase.IFeatureCursor = pFCls.Search(Nothing, False)

Dim ishape As Integer = pFCls.Fields.FindField("Shape")

Dim pFea As ESRI.ArcGIS.Geodatabase.IFeature = pCursor.NextFeature()

While pFea IsNot Nothing

Dim pRow As DataRow = pTable.NewRow()

For i = 0 To pFCls.Fields.FieldCount - 1

If i = ishape Then

pRow(i) = shape

Continue For

End If

pRow(i) = pFea.Value(i).ToString()

Next

pTable.Rows.Add(pRow)

pFea = pCursor.NextFeature()

End While

DataGridView1.DataSource = pTable

ElseIf TypeOf pLayer Is ESRI.ArcGIS.Carto.IRasterLayer Then

Dim pRlyr As ESRI.ArcGIS.Carto.IRasterLayer = pLayer

Dim pRaster As ESRI.ArcGIS.Geodatabase.IRaster = pRlyr.Raster

Dim pProp As ESRI.ArcGIS.DataSourcesRaster.IRasterProps = pRaster

pProp.PixelType = ESRI.ArcGIS.Geodatabase.rstPixelType.PT_LONG

If pProp.PixelType = ESRI.ArcGIS.Geodatabase.rstPixelType.PT_LONG Then

Dim pBcol As ESRI.ArcGIS.DataSourcesRaster.IRasterBandCollection = pRaster

Dim pBand As ESRI.ArcGIS.DataSourcesRaster.IRasterBand = pBcol.Item(0)

Dim pRTable As ESRI.ArcGIS.Geodatabase.ITable = pBand.AttributeTable

Dim pTable As DataTable = New DataTable()

Dim i As Integer

For i = 0 To pRTable.Fields.FieldCount - 1

pTable.Columns.Add(pRTable.Fields.Field(i).Name)

Next

Dim pCursor As ESRI.ArcGIS.Geodatabase.ICursor = pRTable.Search(Nothing, False)

Dim pRrow As ESRI.ArcGIS.Geodatabase.IRow = pCursor.NextRow()

While pRrow IsNot Nothing

Dim pRow As DataRow = pTable.NewRow()

For i = 0 To pRrow.Fields.FieldCount - 1

pRow(i) = pRrow.Value(i).ToString()

Next

pTable.Rows.Add(pRow)

pRrow = pCursor.NextRow()

End While

DataGridView1.DataSource = pTable

End If

End If

End Sub

再在AttributeForm窗体的Load事件中添加调用显示属性表格的代码,如下:

Private Sub AttributeForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

showAttributeTable(showAttributeLayer)

Me.Text = showAttributeLayer.Name + "的属性表"

End Sub

在主程序中若要显示某个图层的属性表,直接定义一个AttributeForm对象即可,如下代码:

Private Sub 显示图层属性表格_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 显示图层属性表格.Click

'获得选择图层的序号

Dim layerIndex As Integer = FindLayerIndex(ComboBox1.Text)

'定义显示属性表格窗体

Dim attributeForm As New AttributeForm()

attributeForm.showAttributeLayer = AxMapControl1.Map.Layer(layerIndex)

attributeForm.ShowDialog()

End Sub

运行程序,其结果如下图所示:

2、在属性表中点击行显示该对象

通过点击属性表中的行来显示该对象,主要通过查询该对象的空间位置,并通过闪烁显示来表示该位置,首先定义一个函数QueryGeoObject(),该函数实现的功能即通过点击的属性表行序号来查询该数据的位置,其代码如下:

Private Sub QueryGeoObject(ByVal pFeatureLayer As ESRI.ArcGIS.Carto.IFeatureLayer,index as Integer)

Dim queryFilter As ESRI.ArcGIS.Geodatabase.IQueryFilter = New ESRI.ArcGIS.Geodatabase.QueryFilterClass()

Dim queryString As String

queryString = DataGridView1.Columns(0).HeaderText + " = " + DataGridView1.Rows(index).Cells(0).Value

queryFilter.WhereClause = queryString

Dim featureCursor As ESRI.ArcGIS.Geodatabase.IFeatureCursor = pFeatureLayer.Search(queryFilter, False)

Dim pFeature As ESRI.ArcGIS.Geodatabase.IFeature = featureCursor.NextFeature()

If pFeature IsNot Nothing Then

Select Case pFeature.Shape.GeometryType

Case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint

AxMapControl.FlashShape(pFeature.Shape)

Case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline

Dim lineSymbol As ESRI.ArcGIS.Display.ILineSymbol = New ESRI.ArcGIS.Display.SimpleLineSymbol()

AxMapControl.FlashShape(pFeature.Shape, 10, 100, lineSymbol)

Case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon

Dim fillSymbol As ESRI.ArcGIS.Display.IFillSymbol = New ESRI.ArcGIS.Display.SimpleFillSymbol()

AxMapControl.FlashShape(pFeature.Shape, 10, 100, fillSymbol)

End Select

End If

End Sub

其中,Select Case中的代码表示判断数据的几何类型(点、线或面)来指定不同的闪烁显示方式,如果还需要将场景缩放至数据,只需要添加如下代码即可(对于点类型数据不适用):

Dim mapExtent As ESRI.ArcGIS.Geometry.IEnvelope = pFeature.Shape.Envelope

AxMapControl.Extent = mapExtent

在属性表的行标题上双击事件中添加调用点击对象查询几何对象的函数QueryGeoObject(),其代码如下:

Private Sub DataGridView1_RowHeaderMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseDoubleClick

If TypeOf showAttributeLayer Is ESRI.ArcGIS.Carto.IFeatureLayer Then

QueryGeoObject(showAttributeLayer, e.RowIndex)

End If

End Sub

文档下载地址(网络硬盘,请勿直接复制链接):
http://www.brsbox.com/filebox/up ... 79f1/dirids/1501532

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值