1. 对FeatureLayer进行Symbol样式描述。
项目需要,对Geometry类型为Polyline的要素图层进行曲线样式描述,一个Symbol类型,只能赋给一个Layer级别的对象,即该图层上所有的Features都具有相同的样式属性。
Code
Dim pLineSym As ISimpleLineSymbol
pLineSym = New SimpleLineSymbol
Dim pColor As IRgbColor
pColor = New RgbColor
pColor.Red = 220
pColor.Blue = 123
pColor.Green = 21
pLineSym.Color = pColor
pLineSym.Style = esriSimpleLineStyle.esriSLSSolid
pLineSym.Width = 30
'省略部分代码
Dim pSR As ISimpleRenderer = New SimpleRenderer
pSR.Symbol = pLineSym
CType(pFeatureLayer, IGeoFeatureLayer).Renderer = pSR
'省略部分代码
而对于Element级别的元素,可以单独设置每个元素的样式,例如:
Code
Dim pLineSym As ISimpleLineSymbol
pLineSym = New SimpleLineSymbol
Dim pColor As IRgbColor
pColor = New RgbColor
pColor.Red = 220
pColor.Blue = 123
pColor.Green = 21
pLineSym.Color = pColor
pLineSym.Style = esriSimpleLineStyle.esriSLSSolid
pLineSym.Width = 30
Dim plineEle As ILineElement
plineEle = New LineElement
plineEle.Symbol = pLineSym
Dim pEles As IElement
pEles = plineEle
pEles.Geometry = pGeom
pGraphicsContainer.AddElement(pEles, 0)
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)
2.缩放到选中要素
Code
/// <summary>
/// 缩放到选中要素
/// </summary>
/// <param name="sLayerName">图层名</param>
/// <param name="sWhere">条件语句</param>
private void ZoomToSelect(string sLayerName,string sWhere)
{
try
{
ILayer pLayer = null;
for (int i = 0; i < m_pMapControl.LayerCount; i++)
{
pLayer = m_pMapControl.get_Layer(i);
if (pLayer.Name == sLayerName)
break;
}
if (pLayer.Name != sLayerName)
return;
IQueryFilter pQueryFilter = new QueryFilterClass();
pQueryFilter.WhereClause = sWhere;
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
int iSelectionFeaturesCount = pFeatureSelection.SelectionSet.Count;
if (iSelectionFeaturesCount == 0)
{
XtraMessageBox.Show("选中要素失败!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//
ISelectionSet pSelectionSet = pFeatureSelection.SelectionSet;
ICursor pCursor = null;
pSelectionSet.Search(null, false, out pCursor);
IFeatureCursor pFeatureCursor = pCursor as IFeatureCursor;
IFeature pFeature = pFeatureCursor.NextFeature();
IEnvelope pEnvelope = new EnvelopeClass();
int index = 0;
bool b = false;
IFeature pFea = null;
while (pFeature != null)
{
index++;
if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPoint)
b = true;
pFea = pFeature;
pEnvelope.Union(pFeature.Extent);
pFeature = pFeatureCursor.NextFeature();
}
if (index == 1 && b)
{
IEnvelope pEnv = new EnvelopeClass();
IPoint pPoint = pFea.Shape as IPoint;
pEnv.XMax = pPoint.X + 200;
pEnv.XMin = pPoint.X - 200;
pEnv.YMax = pPoint.Y + 200;
pEnv.YMin = pPoint.Y - 200;
//m_pMapControl.MapScale = 2000;
m_pMapControl.ActiveView.Extent = pEnv;
m_pMapControl.ActiveView.Refresh();
}
else
{
pEnvelope.Expand(1.1, 1.1, true);
m_pMapControl.ActiveView.Extent = pEnvelope;
m_pMapControl.ActiveView.Refresh();
}
if (pFeatureCursor != null)
{
pFeatureCursor = null;
ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(pFeatureCursor);
}
if (pCursor != null)
{
pCursor = null;
ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(pCursor);
}
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message);
}
}
3. 绘制多层次表达的Polyline
Code
Dim pLineSym1 As ISimpleLineSymbol
Dim pLineSym2 As ISimpleLineSymbol
pLineSym1 = New SimpleLineSymbol
pLineSym2 = New SimpleLineSymbol
Dim pLineColor1 As IRgbColor
pLineColor1 = New RgbColor
pLineColor1.Red = 0
pLineColor1.Green = 0
pLineColor1.Blue = 255
Dim pLineColor2 As IRgbColor
pLineColor2 = New RgbColor
pLineColor2.Red = 255
pLineColor2.Green = 255
pLineColor2.Blue = 255
pLineSym1.Color = pLineColor1
pLineSym1.Width = 10
pLineSym1.Style = esriSimpleLineStyle.esriSLSSolid
pLineSym2.Color = pLineColor2
pLineSym2.Width = 1
pLineSym2.Style = esriSimpleLineStyle.esriSLSDashDot
Dim pMultiLayerLineSym As IMultiLayerLineSymbol
pMultiLayerLineSym = New MultiLayerLineSymbol
pMultiLayerLineSym.AddLayer(pLineSym1)
pMultiLayerLineSym.AddLayer(pLineSym2)
'设置FeatureLayer的样式
Dim pSR As ISimpleRenderer = New SimpleRenderer
pSR.Symbol = pMultiLayerLineSym
CType(pFeatureLayer, IGeoFeatureLayer).Renderer = pSR
m_pHookHelper.FocusMap.AddLayer(pFeatureLayer)
4. 关于多图层叠加不能正常显示的情况分析
在进行跑道绘制时,需要手动绘制一个跑道状线图层,将其绘制与某特定地形影像文件图层上(img栅格图像),或与其他矢量图层进行叠加显示。由于我的程序在一开始创建shp文件时,需要对该图层设置空间参考(SpatialReference),我采用了如下方式:
Code
'原先我采用了这种空间投影参考
'spatialReference = spatialReferenceFactory.CreateProjectedCoordinateSystem(CType(esriSRProjCSType.esriSRProjCS_NAD1983UTM_20N, Integer))
'后查看其它叠加的图层的空间参考(通过ArcMap图层属性查看),原来其它图层都采用了WGS84投影坐标系,所以我必须保持与这些图层投影坐标系一致,问题得到解决
spatialReference = spatialReferenceFactory.CreateProjectedCoordinateSystem(CType(esriSRProjCSType.esriSRProjCS_WGS1984UTM_47N, Integer))
因此,无论绘制矢量图层之间叠加还是与栅格图层叠加,都需要保证它们之间的空间投影坐标参考一致,这样才能正常显示。
5. 对于图层名称和要素名称的理解
在机场选址项目中,在二维模块中,需要对跑道图层要素提供编辑功能,在实现对跑道名称进行窗口输入名称(用户自己想定义的跑道名称),提交编辑时,需要弄清楚两个概念:shp图层名称和要素Name字段名称(在定义要素类时,由我自己定义的字段)。
shp图层名称:
featureClass = featureWorkspace.CreateFeatureClass(shapefileName, validatedFields, Nothing, Nothing, esriFeatureType.esriFTSimple, "Shape", "") 通过该语句创建的要素类,其中shapefileName为要素类名称,即为要素表的表名和要素类对象集的别名,也是生成的*.shp及其他几个同名的相关文件的前缀名称。
要素(Feature)Name字段名称:
由于在定义要素类时,我一共定义了三个字段:OID、SHAPE、NAME,其中,前面两个字段为FeatureClass要素类内部字段,后一个NAME字段用于记录跑道的名称,在我提供的跑到属性编辑窗口内,用户输入名称值并提交修改的内容,应该是Feature的NAME字段值,而不是FeatureClass的名称。
对于OID、SHAPE两个字段,这两个字段为要素类内部字段,我们可以为其设置别名,在书写pFeature.*时,IDE编译器会提供提示选项,但OID属性为自动赋值,只有SHAPE属性可以人工赋值。而对于NAME自定义字段,则可通过pNewFeature.Value(index) = "跑道" 来设置跑道要素Name属性。其参数索引为从OID、SHAPE字段开始以0为起始索引。