This was what I did to solve the problem. Some of it might need to be explained but I think it's very clear. Thanks to brenth. https://gis.stackexchange.com/users/19213/brenth
This helped a lot!
Selecting features by location based on selected feature using ArcObjects?
public void GetClosestFeatures(IEnumerable selectedFeatures)
{
try
{
var mxDocument = ArcMap.Application.Document as IMxDocument;
if (mxDocument != null)
{
var map = mxDocument.FocusMap;
var searchDistance = 1000.0;
if(UI.LayerSelection.Equals("Pipeline"))
{
if (UI.txtRadiusToFacilities.Text != "")
{
searchDistance = Convert.ToDouble(UI.txtRadiusToFacilities.Text);
}
else
{
UI.txtRadiusToFacilities.Text = searchDistance.ToString(CultureInfo.InvariantCulture);
}
// This not 100% Accurate due to the Lattitude changing as you move north/south so if we know the lattite we can use
// the MetersToDecimalDegrees function but this will do!
searchDistance = Convert.ToDouble((UI.txtRadiusToFacilities.Text)) / (111.32 * 1000);
}
else
{
if (UI.txtRadiusToPipelines.Text != "")
{
searchDistance = Convert.ToDouble(UI.txtRadiusToPipelines.Text);
}
else
{
UI.txtRadiusToPipelines.Text = searchDistance.ToString(CultureInfo.InvariantCulture);
}
// This not 100% Accurate due to the Lattitude changing as you move north/south so if we know the lattite we can use
// the MetersToDecimalDegrees function but this will do!
searchDistance = Convert.ToDouble((UI.txtRadiusToPipelines.Text)) / (111.32 * 1000);
}
var cursor = GetSelectedFeatures(CurrentLayer);
// Default buffer is Decimal Degrees
var polygon = UnionShapes(cursor, searchDistance);
// Maybe use the name in place of the CurrentLayer.Name as this is to find Facilities for the Selected Pipelines
var featureLayer = Utilities.GetOppositeFeatureLayerByFeatureClassName(map, CurrentLayer.Name);
var filter = CreateSpatialFilter(featureLayer.FeatureClass, polygon);
var cursor2 = GetSelectedItemsByShape(featureLayer.FeatureClass, filter);
// Iterate through the features, viewing (as in this case) or editing them.
IFeatureClass fc = Utilities.GetOppositeFeatureLayerByFeatureClassName(map, CurrentLayer.Name).FeatureClass;
IFeature feature = null;
if (UI.LayerSelection.Equals("Pipeline"))
{
var nameFacilityNameIndex = fc.Fields.FindField(GeodatabaseFieldNames.FacilityNameFieldName);
var nameFacilityTypeIndex = fc.Fields.FindField(GeodatabaseFieldNames.FacilityTypeFieldFieldName);
var counter = 0;
UI.lvwFacilitiesSpatialResult.Items.Clear();
try
{
while ((feature = cursor2.NextFeature()) != null)
{
//Console.WriteLine("Facility Found: {0}", feature.get_Value(nameFieldIndex));
counter++;
string[] rowData =
{
(counter).ToString(CultureInfo.InvariantCulture),
feature.get_Value(nameFacilityNameIndex).ToString(),
feature.get_Value(nameFacilityTypeIndex).ToString()
};
var lvItem = new ListViewItem(rowData);
UI.lvwFacilitiesSpatialResult.Items.Add(lvItem);
}
}
catch (Exception ex)
{
// Handle any errors that might occur on NextFeature().
MessageBox.Show(ex.Message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
else // Facility
{
var namePipelinenameIndex = fc.Fields.FindField(GeodatabaseFieldNames.PipelineNameFieldName);
var companyNameFieldIndex = fc.Fields.FindField(GeodatabaseFieldNames.OperatorNameFieldName);
var counter = 0;
UI.lvwPipelinesSpatialResult.Items.Clear();
try
{
while ((feature = cursor2.NextFeature()) != null)
{
//Console.WriteLine("Facility Found: {0}", feature.get_Value(nameFieldIndex));
counter++;
string[] rowData =
{
(counter).ToString(CultureInfo.InvariantCulture),
feature.get_Value(namePipelinenameIndex).ToString(),
feature.get_Value(companyNameFieldIndex).ToString()
};
var lvItem = new ListViewItem(rowData);
UI.lvwPipelinesSpatialResult.Items.Add(lvItem);
}
}
catch (Exception ex)
{
// Handle any errors that might occur on NextFeature().
MessageBox.Show(ex.Message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
public static IFeatureCursor GetSelectedFeatures(IFeatureLayer featureLayer)
{
if (featureLayer == null) return null;
IFeatureSelection fSel = (IFeatureSelection)featureLayer;
ISelectionSet selSet = (ISelectionSet)fSel.SelectionSet;
ICursor cursor = null;
selSet.Search(null, false, out cursor);
IFeatureCursor fCursor = cursor as IFeatureCursor;
return fCursor;
}
public static IPolygon UnionShapes(IFeatureCursor cursor, double bufferDist)
{
if (cursor == null) return null;
IFeature pFeat = cursor.NextFeature();
if (pFeat != null)
{
if (pFeat.Shape is IPoint || pFeat.Shape is IPolyline)
{
ITopologicalOperator ptopBuffer = (ITopologicalOperator)pFeat.Shape;
IPolygon pTempPoly = (IPolygon)ptopBuffer.Buffer(bufferDist);
ITopologicalOperator ptopUnion = (ITopologicalOperator)pTempPoly;
pFeat = cursor.NextFeature();
while (pFeat != null)
{
ptopBuffer = (ITopologicalOperator)pFeat.Shape;
pTempPoly = (IPolygon)ptopBuffer.Buffer(bufferDist);
ptopUnion = (ITopologicalOperator)ptopUnion.Union(pTempPoly);
pFeat = cursor.NextFeature();
}
return (IPolygon)ptopUnion;
}
return null;
}
return null;
}
public static ISpatialFilter CreateSpatialFilter(IFeatureClass fc, IGeometry shape)
{
if (fc == null) return null;
ISpatialFilter sf = new SpatialFilterClass();
sf.GeometryField = fc.ShapeFieldName;
sf.Geometry = shape;
sf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
return sf;
}
public static IFeatureCursor GetSelectedItemsByShape(IFeatureClass fc, ISpatialFilter filter)
{
try
{
if (filter == null) return null;
if (fc == null) return null;
IFeatureCursor fcursor = fc.Search(filter, true);
return fcursor;
}
catch
{
return null;
}
}
public static ISelectionSet CursorToSelectionSet(IFeatureLayer layer, IFeatureCursor cursor)
{
IFeatureSelection fSel = (IFeatureSelection)layer;
if (cursor != null)
{
IFeature feat = cursor.NextFeature();
while (feat != null)
{
fSel.Add(feat);
feat = cursor.NextFeature();
}
}
return fSel.SelectionSet;
}
Since I only have 2 Layers then I can do this.
public static IFeatureLayer GetOppositeFeatureLayerByFeatureClassName(IMap map, string featureClassName)
{
IFeatureLayer result = null;
for (var i = 0; i