1

I'd like to develop an application with ArcEngine that can calculate the area of a polygon.

I wrote the below code to select a polygon in a combobox:

for (int i = 0; i < pmapcontrol2.LayerCount; i++)
{

    Pfeaturelayer = (IFeatureLayer)pmapcontrol2.get_Layer(i);
    if (Pfeaturelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
    {


        cmbfeaturelayer.Items.Add(pmapcontrol2.get_Layer(i).Name);
    }
}

But I couldn't write code to calculate pfeaturelayer area. I think I should use Ilayer interface and Ilayer use Ifeature to calculate area not ifeaturelayer.

Is there any way to use Ifeature for this application?

1 Answer 1

3

Here is a link for some sample code that calculates area of selected polygon feature.

http://edndoc.esri.com/arcobjects/9.2/ComponentHelp/esriGeometry/Area_Example.htm

// The example shows how to get the properties for a selected polygon in
// an ArcMap edit session.
public void showPolygonProperties()
{
    //get editor extension
    UID editorUID = new UIDClass();
    editorUID.Value = "esriEditor.Editor";
    IEditor editor = m_application.FindExtensionByCLSID(editorUID) as IEditor;
    if (editor.SelectionCount != 1)
    {
        System.Windows.Forms.MessageBox.Show("Start Editor and select one polygon");
        return;
    }
    IEnumFeature selectedFeatures = editor.EditSelection;
    IPoint centerPoint = new ESRI.ArcGIS.Geometry.Point();
    IPoint labelPoint = new ESRI.ArcGIS.Geometry.Point();
    IFeature feature = selectedFeatures.Next();
    while (feature != null)
    {
        if (feature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
        {
            IArea area = feature.Shape as IArea;
            String message = "+++Polygon.IArea properties...\n" +
            "Area = " + area.Area + "\n" +
            "Center.X = " + area.Centroid.X + "\n" +
            "Center.Y = " + area.Centroid.Y + "\n" +
            "LabelPoint.X = " + area.LabelPoint.X + "\n" +
            "LabelPoint.Y = " + area.LabelPoint.Y;
            area.QueryCentroid(centerPoint);
            area.QueryLabelPoint(labelPoint);
            message = message + "\n" + "+++Polygon.IArea Queries..." + "\n" +
            "Center = " + centerPoint.X + "," + centerPoint.Y + "\n" +
            "Label = " + labelPoint.X + "," + labelPoint.Y;
            System.Windows.Forms.MessageBox.Show(message);
        }
        feature = selectedFeatures.Next();
    }
}
4
  • thank you . but this example used editor to calculate area .i'd like to calculate the selected layer in combobox .
    – BBG_GIS
    Feb 9, 2012 at 19:56
  • @majid moosavi, Can you elaborate more what you mean by 'calculate the selected layer in combobox'?
    – artwork21
    Feb 9, 2012 at 20:22
  • ,i have a combobox and a button .when i click to the button ,the program calculate area of the selected layer in combobox and show the area in messagebox. thanks
    – BBG_GIS
    Feb 9, 2012 at 21:19
  • 1
    IArea.Area property gives you the area size. If you'd like to compute the combines area of all selected polygons you will need to iterate through a cursor of features and add the areas then display in your control. Feb 10, 2012 at 14:32

Your Answer

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

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