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?