1

I am trying to figure out how to create a buffer around selected points and add them to an new shapefile.

My idea is to have a loop that loops through the selected points and perform a buffer and then adds them to a new shapefile and saves it.

So here is my code to create points and feature data and saves it as a shapefile.

Here is my code for the shapefile creation:

public void addDataToMap(string folder, string shapefilename) {
        pMap = mDoc.FocusMap;

        string strFolder = folder;
        string strName = shapefilename;
        string strShapeFileName = "Shape";
        string strLatitude = "Latitude";
        string strLongitude = "Longitude";
        string strLocationName = "Name";

        IFeatureWorkspace pFWS;
        IWorkspaceFactory pWorkspaceFactory;
        pWorkspaceFactory = new ShapefileWorkspaceFactory();
        pFWS = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(strFolder, 0);

        //FIELDS SETUP
        IFields pFields;
        IFieldsEdit pFieldsEdit;
        pFields = new Fields();
        pFieldsEdit = (IFieldsEdit)pFields;

        //FIELD1 SETUP
        IField pField;
        IFieldEdit pFieldEdit;
        pField = new Field();
        pFieldEdit = (IFieldEdit)pField;

        pFieldEdit.Name_2 = strShapeFileName;
        pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
        pFieldsEdit.AddField(pField);

        //FIELD2 SETUP
        IField pField1;
        IFieldEdit pFieldEdit1;
        pField1 = new Field();
        pFieldEdit1 = (IFieldEdit)pField1;

        pFieldEdit1.Name_2 = strLatitude;
        pFieldEdit1.Type_2 = esriFieldType.esriFieldTypeDouble;
        pFieldsEdit.AddField(pField1);

        //FIELD3 SETUP
        IField pField2;
        IFieldEdit pFieldEdit2;
        pField2 = new Field();
        pFieldEdit2 = (IFieldEdit)pField2;

        pFieldEdit2.Name_2 = strLongitude;
        pFieldEdit2.Type_2 = esriFieldType.esriFieldTypeDouble;
        pFieldsEdit.AddField(pField2);

        //FIELD4 SETUP
        IField pField3;
        IFieldEdit pFieldEdit3;
        pField3 = new Field();
        pFieldEdit3 = (IFieldEdit)pField3;

        pFieldEdit3.Name_2 = strLocationName;
        pFieldEdit3.Type_2 = esriFieldType.esriFieldTypeString;
        pFieldsEdit.AddField(pField3);

        IGeometryDef pGeomDef;
        IGeometryDefEdit pGeomDefEdit;
        pGeomDef = new GeometryDefClass();
        pGeomDefEdit = (IGeometryDefEdit)pGeomDef;
        pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;

        //Spatialreference setup
        ISpatialReferenceFactory SRF = new SpatialReferenceEnvironmentClass();
        ISpatialReference SR = SRF.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_World_Mercator);
        SR = SRF.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
        ISpatialReferenceResolution SRR = (ISpatialReferenceResolution)SR;
        SRR.ConstructFromHorizon();
        ISpatialReferenceTolerance SRT = (ISpatialReferenceTolerance)SR;
        SRT.SetDefaultXYTolerance();
        pGeomDefEdit.SpatialReference_2 = SR;

        pFieldEdit.GeometryDef_2 = pGeomDef;

        IFeatureClass pFeatClass;
        pFeatClass = pFWS.CreateFeatureClass(strName, pFields, null, null, esriFeatureType.esriFTSimple, strShapeFileName, "");

        IFeatureLayer pFeaturelayer = new FeatureLayerClass();
        pFeaturelayer.FeatureClass = pFeatClass;
        pFeaturelayer.Name = pFeatClass.AliasName;

        IPoint point = new Point();
        for (int i = 1; i < data_.Count; i++)
        {

            point.PutCoords(data_[i].getLngAsDouble(), data_[i].getLatgAsDouble());

            IFeature feature = pFeatClass.CreateFeature();
            feature.Value[2] = data_[i].getLatgAsDouble();
            feature.Value[3] = data_[i].getLngAsDouble();
            feature.Value[4] = data_[i].getName();
            feature.Shape = point;
            feature.Store();
        }
        pMap.AddLayer(pFeaturelayer);
        mDoc.UpdateContents();
        mDoc.ActiveView.Refresh();
    }

And now when the layer is created I have done some code to Count the selected features and the if it is a Point I want to create a new shapefile with a given buffer around the selected points.

And here is that code:

        private void cmdBuffer_Click(object sender, EventArgs e)
        {
        int counter=0;
        IEnumFeature enumFeature = (IEnumFeature)ArcMap.Document.FocusMap.FeatureSelection;
        IFeature feature = enumFeature.Next();
        while (feature != null) {
            IGeometry geomfeature = feature.ShapeCopy;
            if (geomfeature.GeometryType == esriGeometryType.esriGeometryPoint)
            {
                //Create some buffering magic.
            }
            feature = enumFeature.Next();
        }
        if (counter != 0)
        {

        }
        else {
            MessageBox.Show("You need to select features to make a buffer", "Try again.");
        }
    }

Does anyone have a good practice for this?

8
  • Welcome to GIS SE. As a new user, please take the Tour. Use of third-party sites for source code has several issues, the most significant being that far fewer people are willing to follow links to unknown sites (so few will view your code), and the long-term stability of these archives is suspect (meaning that even if folks are willing to follow the link, there might not be anything there). Please Edit the question to contain a concrete problem and a code snippet to reproduce the problem.
    – Vince
    Commented Jun 4, 2017 at 22:02
  • You still haven't stated what your problem is. What is the result of you code? How does it not match your intent? The "magic" part is missing, and this is the only part which is necessary
    – Vince
    Commented Jun 5, 2017 at 0:14
  • @Vince The problem is that I dont know where to start on how to make the buffer. Should I use IGeometry, I dont know. Commented Jun 5, 2017 at 6:08
  • Why don't you simply call the existing buffer geoprocessing tool? That will honour any selection and the output is a featureclass. You could turn all your code to a dozen lines. Search help for Igeoprocessor.
    – Hornbydd
    Commented Jun 5, 2017 at 7:51
  • @Hornbydd Do you mean something like this? Geoprocessor GP = new Geoprocessor(); ESRI.ArcGIS.AnalysisTools.Buffer bufferTool = new ESRI.ArcGIS.AnalysisTools.Buffer(); bufferTool.in_features = geomfeature; bufferTool.out_feature_class = @"H:\"; bufferTool.buffer_distance_or_field = 10; GP.Execute(bufferTool, null); Commented Jun 5, 2017 at 8:41

1 Answer 1

0

Here is some code using VBA showing you an approach to calling the Buffer tool:

Public Sub buff()
    ' Get featurelayer so selections are used
    Dim pmxd As IMxDocument
    Set pmxd = ThisDocument
    Dim pmap As IMap
    Set pmap = pmxd.FocusMap
    Dim pl As ILayer
    Set pl = pmap.Layer(0)
    Dim pfl As IFeatureLayer
    Set pfl = pl

    ' Load parameters into Array, IMPORTANT must be same order as tool
    Dim pA As IVariantArray
    Set pA = New VarArray
    With pA
        .Add pfl
        .Add "IN_MEMORY/test"
        .Add "100 Meters" ' 100m
        .Add "FULL"
        .Add "ROUND"
        .Add "NONE"
        .Add "#" ' Default
        .Add "#" ' Default
    End With

    ' Create geoprocessor
    Dim pGP As IGeoProcessor
    Set pGP = New GeoProcessor
    pGP.AddOutputsToMap = True
    pGP.OverwriteOutput = True

    ' Run tool
    Dim pRes As IGeoProcessorResult
    Set pRes = pGP.Execute("Buffer_analysis", pA, Nothing)
    If pRes.Status <> esriJobSucceeded Then
        Debug.Print "oops something went wrong!"
    End If
End Sub
1
  • Awesome. It works. I rewrote it to C#, thanks alot. Commented Jun 5, 2017 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.