大家可能在平时开发中可能会遇到按行政区加载图层数据的需求,比方说整个国土资源部有某一类型专题数据有若干图层,每个图层数据的范围是整个中国,这时国土资源部开发一套系统可能有这个需求,每个省只能加载本省的数据。以下函数即为按按行政区范围创建行政区范围的图层。
比方说我想获取河南省的数据只需传入行政区图层中河南省图元的Geometry即可。另外如果如果遇到行政区层必须用 esriSpatialRelEnum.esriSpatialRelContains这个参数,否则行政区这个图层就不能只获取河南这个省份,它会把河南省的周围省份,河北,湖北,安徽,陕西,山东,山西等省份的图元也添加创建到新图层。
Code
/// <summary>
/// 按行政区范围创建行政区范围的图层
/// </summary>
/// <param name="pFeatureLayer">源数据图层</param>
/// <param name="pGeometry">行政区范围</param>
/// <param name="bXZQ">图层是否为行政区</param>
/// <returns>新创建的图层</returns>
private IFeatureLayer GetSelectionLayer(IFeatureLayer pFeatureLayer, IGeometry pGeometry,bool bXZQ)
{
try
{
if (pFeatureLayer != null && pGeometry != null)
{
IQueryFilter pQueryFilter;
ISpatialFilter pSpatialFilter = new SpatialFilterClass();
IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
pSpatialFilter.GeometryField = pFeatureLayer.FeatureClass.ShapeFieldName;
pFeatureSelection.Clear();
if (!bXZQ)
{
pSpatialFilter.Geometry = pGeometry;
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
pQueryFilter = pSpatialFilter;
pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
}
else
{
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
pQueryFilter = pSpatialFilter;
if(pGeometry is IGeometryCollection)
{
for (int i = 0; i < (pGeometry as IGeometryCollection).GeometryCount; i++)
{
pSpatialFilter.Geometry = (pGeometry as IGeometryCollection).get_Geometry(i);
pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultAdd, false);
}
}
}
IFeatureLayerDefinition pFLDefinition = pFeatureLayer as IFeatureLayerDefinition;
IFeatureLayer pNewFeatureLayer = pFLDefinition.CreateSelectionLayer(pFeatureLayer.Name ,true, null, null);
pNewFeatureLayer.MaximumScale = pFeatureLayer.MaximumScale;
pNewFeatureLayer.MinimumScale = pFeatureLayer.MinimumScale;
pNewFeatureLayer.Selectable = pFeatureLayer.Selectable;
pNewFeatureLayer.Visible = pFeatureLayer.Visible;
pNewFeatureLayer.ScaleSymbols = pFeatureLayer.ScaleSymbols;
return pNewFeatureLayer;
}
else
{
return null;
}
}
catch (Exception Err)
{
MessageBox.Show(Err.Message, "获取SelectionLayer", MessageBoxButtons.OK, MessageBoxIcon.Information);
return null;
}
}
}
原文地址:
http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=34165&extra=&page=1