比方说我想获取河南省的数据只需传入行政区图层中河南省图元的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
/// <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;
}
}
}