Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

i generate a tool to return values using area of polygons. but when i execute the tool, visual studio show an error :"Error HRESULT E_FAIL has been returned from a call to a COM component" in gp.Execute("Intersect_analysis", parameters, null);. this tool use area of polygons . first use GRIDCODE field to calculate area of each GRIDCODE . for example in slope layer we have 5 GRIDCODE then this tool calculate polygon Area for each GRIDCODE then intersect slope layer with layer2 such as lanslide layer and calculate area of layer2 in each gridcode .for example gridcode 1 = 2000 KM2 and layer2 in gridcode1 = 500 KM2 finally layer2 in gridcode1 area divide to gridcode1 area : 500/ 2000 the code is : what's my problem in writing this code?

namespace class_library
{
    public partial class form_model : Form
    {
       public IHookHelper phookhelper;
        // jahate moarefie polygon be combobox
       IFeatureLayer pfeaturelayer;

       string path, name;

        public form_model()
        {
            InitializeComponent();
        }

        private void form_model_Load(object sender, EventArgs e)
        {
            pfeaturelayer = new FeatureLayerClass();

            if (phookhelper.FocusMap.LayerCount < 2)
            {


               MessageBox.Show( "at least two layer must be import in ArcMap ","error");
                this.Size = new Size(0, 0);
                this.ShowInTaskbar = false;
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.Close();
                this.Hide();

            }
            else
            {
                for (int i = 0; i < phookhelper.FocusMap.LayerCount; i++)
                {
                    pfeaturelayer = (IFeatureLayer)phookhelper.FocusMap.get_Layer(i);
                    if (pfeaturelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
                    {
                        comboBox1.Items.Add(phookhelper.FocusMap.get_Layer(i).Name);


                    }
                }
                for (int i = 0; i < phookhelper.FocusMap.LayerCount; i++)
                {
                    pfeaturelayer = (IFeatureLayer)phookhelper.FocusMap.get_Layer(i);
                    if (pfeaturelayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
                    {
                        comboBox2.Items.Add(phookhelper.FocusMap.get_Layer(i).Name);


                    }
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IFeatureCursor pFeatureCursor;
            IFeature pFeature;
            if (comboBox1.SelectedIndex == -1)
            {
                MessageBox.Show("select watershed layer");
                return;
            }
            if (comboBox2.SelectedIndex == -1)
            {
                MessageBox.Show(" select landslide layer");
                return;
            }
            if (String.IsNullOrEmpty(path) || String.IsNullOrEmpty(name))
            {
                MessageBox.Show("select save rout");
                return;

            }

            // computing area of each gridcode in aspect layer
            int layer1 = comboBox1.SelectedIndex;
            int layer2 = comboBox2.SelectedIndex;
            IFeatureLayer fl1 = (IFeatureLayer)phookhelper.FocusMap.get_Layer(layer1);
            IFeatureLayer fl2 = (IFeatureLayer)phookhelper.FocusMap.get_Layer(layer2);

            pFeatureCursor = fl2.Search(null, false);
            pFeature = pFeatureCursor.NextFeature();
            double val;
            int fieldIndex;
            fieldIndex = fl2.FeatureClass.FindField("GRIDCODE");
            if (fieldIndex == -1)
            {
                MessageBox.Show( + "there is no GRIDCODE field in selected layer " );
                return;
            }
            // getting unique values
            List<double> UVs= new List<double>();
            while (pFeature != null)
            {
                try
                {
                    val = (int)pFeature.get_Value(fieldIndex);
                    if (!UVs.Contains(val))
                    {
                        UVs.Add(val);
                    }
                }
                catch { }
                pFeature = pFeatureCursor.NextFeature();


            }



            IQueryFilter2 pQFL = new QueryFilterClass();
            Dictionary<double,double> Areas = new Dictionary<double,double>();
            foreach (double i in UVs)
            {

                pQFL.WhereClause = String.Concat("\"GRIDCODE\" = ", (int) i);
                pFeatureCursor = fl2.Search(pQFL, false);
                pFeature = pFeatureCursor.NextFeature();
                double TotalArea1 = 0;
                IArea pArea;
                while (pFeature != null)
                {
                    pArea = (IArea)pFeature.Shape;
                    //MessageBox.Show(pArea.Area.ToString());
                    TotalArea1 += pArea.Area; 
                    pFeature = pFeatureCursor.NextFeature();
                }

                Areas.Add(i,TotalArea1);
            }

            string layer1path, layer2path;
            IDataset ds1 = (IDataset)fl1;
            IDataset ds2 = (IDataset)fl2;
            layer1path = String.Concat(ds1.Workspace.PathName, "\\", fl1.Name,".shp");
            layer2path = String.Concat(ds2.Workspace.PathName, "\\", fl2.Name, ".shp");

            try
            {


                IGeoProcessor gp = new GeoProcessorClass();
                IVariantArray parameters = new VarArrayClass();

                parameters.Add(String.Concat('"', layer1path, '"', ";", '"', layer2path, '"'));
                parameters.Add("c:\\intersect.shp");
                parameters.Add("ALL");
                gp.OverwriteOutput = true;
                gp.AddOutputsToMap = false;

                gp.Execute("Intersect_analysis", parameters, null);


                }
             catch
        {
            MessageBox.Show("error in intersect");
            return;
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.