Sunday, 2 August 2015

arcgis desktop - Selecting features by location based on selected feature using ArcObjects?


I am very new to ArcGIS and ArcObjects.


I am writing a code for a button that will detect a selected feature and then select other features based on the feature that I had selected please take a look at my code


public class LandmarkBtn : ESRI.ArcGIS.Desktop.AddIns.Button {

public LandmarkBtn() {
}


protected override void OnClick() {
var doc = ArcMap.Document;
var focusMap = doc.FocusMap;
IEnumLayer layer = focusMap.get_Layers(null,true);
ILayer lyr = null;
IFeatureClass Lmarks = null;

while ((lyr = layer.Next()) != null) {


var featureLayer = lyr as IFeatureLayer;
if (featureLayer == null)
continue;



var name = featureLayer.FeatureClass.AliasName;

if ("landmarks".Equals(name.ToLowerInvariant())) {



Lmarks = featureLayer.FeatureClass;
break;
}
}

if (Lmarks == null) {
MessageBox.Show("Sorry, No LM Layer Found");
return;
}



var selection = (IEnumFeature)focusMap.FeatureSelection;
IFeature ftr = null;
ftr = selection.Next();

ftr now holds the selected landmark(feature), I want to perform a query that will select features from another layer that are within 100 meters of the landmark feature, can you please direct me onto where I should look or provide me with a sample code that might help?



Answer



This will take multiple steps:





  1. Get a cursor from you selection set


        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;
    }


  2. Union and buffer the items in the cursor


    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)
    {
    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;
    }



  3. Select the spatial filter on the new shape


       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;
    }


  4. Search on the feature class with the filter.


        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;
    }
    }



  5. Then if you want to select the new features:


        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;
    }


Good Luck



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...