[GIS] Spatial Query between polyline features and point features within specified Buffer

add-inarcgis-desktoparcmaparcobjectsc

I'm new to GIS (so I'm lost on this) and looking for a C# code sample to help me do 2 types of spatial queries. I have 2 Layers only. First layer is pipelines, which are polyline shapes so I can select multiple pipeline segments to make up an entire pipeline. Second Layer is facilities, which are point shapes.

Query 1: Select Pipeline Segments in the Pipeline Layer (Polylines) and find the facilities (in the Facility Layer) that are within a certain distance of selected Pipeline Segments.

Query 2: Select a single Facility in the Facility Layer (Point) and find the Pipeline Segments (Polylines) that are within a certain distance of the Facility Point.

I can do these queries via the ArcMap interface. I select the Source Features in the Attribute Table, then Selection Menu, Select by Location, select the "other" Layer as the Target Layer and for Spatial Selection Method I select "Target layer(s) features are within a distance of the Source layer feature". But now I need to do this in C# code for my ArcMap AddIn.

Best Answer

Assuming you have a handle of your query featureclass and selected feature, you can use the following code snippet:

double searchDistance = 10; //this is your buffer distance
IFeatureClass fc = ...; //Query1: this is your facility feature class
IFeature feat = ...;// Query1: this is your selected pipeline feature

ISpatialFilter spatialFilter = new SpatialFilterClass();
if (searchDistance == 0)
    spatialFilter.Geometry = feat.Shape; // can be IGeometry
else
{
    ITopologicalOperator topoOperator = (ITopologicalOperator)feat.ShapeCopy;
    spatialFilter.Geometry = topoOperator.Buffer(searchDistance);
}

spatialFilter.GeometryField = fc.ShapeFieldName;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

IFeatureCursor featureCursor = fc.Search(spatialFilter, true);

//loop through features in the cursor
Related Question