Exemplo n.º 1
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string strLayerName = cboLayer.Text;
                string strFieldType = cboType.Text;
                string strFieldName = txtName.Text;

                if (strLayerName == "" || strFieldType == "" || strFieldName == "")
                {
                    MessageBox.Show("Please select variables");
                }

                clsBlockNames pBlockNames = new clsBlockNames();
                if (pBlockNames.BlockPreDeterminedName(strFieldName))
                {
                    return;
                }

                int    intLIndex = pSnippet.GetIndexNumberFromLayerName(pActiveView, strLayerName);
                ILayer pLayer    = mForm.axMapControl1.get_Layer(intLIndex);

                IFeatureLayer pFLayer   = pLayer as IFeatureLayer;
                IFeatureClass pFClass   = pFLayer.FeatureClass;
                int           intFldIdx = pFClass.FindField(strFieldName);
                if (intFldIdx != -1)
                {
                    MessageBox.Show("The field name is already assigned.");
                    return;
                }

                AddField(pFClass, strFieldName, strFieldType);
                MessageBox.Show("Done");

                if (intHandle != IntPtr.Zero)
                {
                    frmAttributeTable pfrmAttributeTable = pSnippet.returnAttTable(intHandle);
                    if (pfrmAttributeTable == null)
                    {
                        return;
                    }
                    pSnippet.LoadingAttributeTable(pLayer, pfrmAttributeTable);
                }

                this.Close();
            }
            catch (Exception ex)
            {
                frmErrorLog pfrmErrorLog = new frmErrorLog(); pfrmErrorLog.ex = ex; pfrmErrorLog.ShowDialog();
                return;
            }
        }
Exemplo n.º 2
0
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string strLayerName = cboLayer.Text;

                int    intLIndex = pSnippet.GetIndexNumberFromLayerName(pActiveView, strLayerName);
                ILayer pLayer    = mForm.axMapControl1.get_Layer(intLIndex);

                IFeatureLayer pFLayer = pLayer as IFeatureLayer;
                IFeatureClass pFClass = pFLayer.FeatureClass;
                if (clistFields.CheckedItems.Count > 0)
                {
                    for (int i = 0; i < clistFields.CheckedItems.Count; i++)
                    {
                        pSnippet.DeleteField(pFClass, (string)clistFields.CheckedItems[i]);
                    }
                }
                else
                {
                    MessageBox.Show("Select Fields to delete");
                }

                MessageBox.Show("Done");
                if (intHandle != IntPtr.Zero)
                {
                    frmAttributeTable pfrmAttributeTable = pSnippet.returnAttTable(intHandle);
                    if (pfrmAttributeTable == null)
                    {
                        return;
                    }
                    pSnippet.LoadingAttributeTable(pLayer, pfrmAttributeTable);
                }

                this.Close();
            }
            catch (Exception ex)
            {
                frmErrorLog pfrmErrorLog = new frmErrorLog(); pfrmErrorLog.ex = ex; pfrmErrorLog.ShowDialog();
                return;
            }
        }
Exemplo n.º 3
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                string strLayerName  = cboLayer.Text;
                string strExpression = txtNExpression.Text;
                string strTargetFld  = txtTargetField.Text;

                //Load Feature Class
                int    intLIndex = pSnippet.GetIndexNumberFromLayerName(pActiveView, strLayerName);
                ILayer pLayer    = mForm.axMapControl1.get_Layer(intLIndex);

                IFeatureLayer pFLayer = pLayer as IFeatureLayer;
                IFeatureClass pFClass = pFLayer.FeatureClass;


                if (strTargetFld == "")
                {
                    MessageBox.Show("Please select the target field");
                    return;
                }
                else if (strExpression == "")
                {
                    MessageBox.Show("Please insert expression to calculate field");
                    return;
                }

                if (radR.Checked)
                {
                    try
                    {
                        REngine pEngine = mForm.pEngine;
                        // Split the text into an array of words
                        string[] words = strExpression.Split(new char[] { ',', '$', '(', ')', '/', '*', '+', '-', '^', '=', ' ' }, StringSplitOptions.RemoveEmptyEntries); //Any other splitters?

                        //Get number and names of Expressed fields
                        int intNField        = lstFields.Items.Count;
                        int intNExpressedFld = 0;

                        string[] fieldNames = new string[intNField];

                        for (int j = 0; j < intNField; j++)
                        {
                            string strTemp = (string)lstFields.Items[j];
                            if (words.Contains(strTemp))
                            {
                                fieldNames[intNExpressedFld] = (string)lstFields.Items[j];
                                intNExpressedFld++;
                            }
                        }

                        //Get Features
                        int nFeature = pFClass.FeatureCount(null);

                        IFeatureCursor pFCursor = pFLayer.Search(null, true);
                        IFeature       pFeature = pFCursor.NextFeature();

                        //Get index for target and source fields
                        int    intTargetIdx = pFLayer.FeatureClass.Fields.FindField(strTargetFld);
                        IField pField       = pFLayer.FeatureClass.Fields.Field[intTargetIdx];

                        ////Allow calculation for other field types (v.1.0.6)
                        //if (pField.Type != esriFieldType.esriFieldTypeDouble)
                        //{
                        //    MessageBox.Show("R sytanx is only applicable to Double field type");
                        //    return;
                        //}

                        NumericVector vecResult = null;
                        bool          blnINF    = false;
                        bool          blnNAN    = false;
                        LogicalVector vecINF    = null;
                        LogicalVector vecNAN    = null;

                        //Get values from selected fields
                        if (intNExpressedFld > 0)
                        {
                            int[] idxes = new int[intNExpressedFld];
                            for (int j = 0; j < intNExpressedFld; j++)
                            {
                                idxes[j] = pFLayer.FeatureClass.Fields.FindField(fieldNames[j]);
                            }

                            //Store values in source fields into Array
                            //double[,] arrSource = new double[nFeature, intNExpressedFld];
                            double[][] arrSource = new double[intNExpressedFld][];

                            for (int j = 0; j < intNExpressedFld; j++)
                            {
                                arrSource[j] = new double[nFeature];
                            }

                            int i = 0;
                            while (pFeature != null)
                            {
                                for (int j = 0; j < intNExpressedFld; j++)
                                {
                                    arrSource[j][i] = Convert.ToDouble(pFeature.get_Value(idxes[j]));
                                }

                                i++;
                                pFeature = pFCursor.NextFeature();
                            }

                            //Source value to R vector
                            for (int j = 0; j < intNExpressedFld; j++)
                            {
                                //double[] arrVector = arrSource.GetColumn<double>(j);
                                NumericVector vecSource = pEngine.CreateNumericVector(arrSource[j]);
                                pEngine.SetSymbol(fieldNames[j], vecSource);
                            }

                            //Verifying before evaluation
                            vecINF = pEngine.Evaluate("is.infinite(" + strExpression + ")").AsLogical();
                            vecNAN = pEngine.Evaluate("is.nan(" + strExpression + ")").AsLogical();

                            for (int k = 0; k < vecINF.Length; k++)
                            {
                                blnINF = vecINF[k];
                                blnNAN = vecNAN[k];
                                if (blnINF)
                                {
                                    MessageBox.Show("INF");
                                    return;
                                }
                                else if (blnNAN)
                                {
                                    MessageBox.Show("NAN");
                                    return;
                                }
                            }

                            //Calculate
                            vecResult = pEngine.Evaluate(strExpression).AsNumeric();
                        }
                        else if (intNExpressedFld == 0) //Constant
                        {
                            //Verifying before evaluation
                            blnINF = pEngine.Evaluate("is.infinite(" + strExpression + ")").AsLogical().First();
                            blnNAN = pEngine.Evaluate("is.nan(" + strExpression + ")").AsLogical().First();
                            if (blnINF)
                            {
                                MessageBox.Show("INF");
                                return;
                            }
                            else if (blnNAN)
                            {
                                MessageBox.Show("NAN");
                                return;
                            }

                            vecResult = pEngine.Evaluate("rep(" + strExpression + ", " + nFeature.ToString() + ")").AsNumeric();
                        }

                        //Update Field
                        pFCursor = pFLayer.FeatureClass.Update(null, false);
                        pFeature = pFCursor.NextFeature();

                        int featureIdx = 0;

                        try
                        {
                            while (pFeature != null)
                            {
                                pFeature.set_Value(intTargetIdx, (object)vecResult[featureIdx]);
                                pFCursor.UpdateFeature(pFeature);

                                pFeature = pFCursor.NextFeature();
                                featureIdx++;
                            }
                        }
                        catch
                        {
                            MessageBox.Show("Fail to update field values. The field type might not support this calculation.", "Field Calcualation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("R Syntax Error: " + ex.ToString());
                        return;
                    }
                }
                else
                {
                    try
                    {
                        //Not working in 10.5
                        ICalculator pCal = null;
                        try
                        {
                            pCal = new Calculator();
                        }
                        catch
                        {
                            MessageBox.Show("Fail to load the ArcGIS Field Calculator due to a license issue. Please use a R sytanx", "Field Calcualation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        IFeatureCursor pFCursor = pFClass.Update(null, false);
                        pCal.Cursor        = (ICursor)pFCursor;
                        pCal.Expression    = strExpression;
                        pCal.PreExpression = "";
                        pCal.Field         = strTargetFld;
                        pCal.Calculate();

                        ////Not working
                        //ITable pTable = (ITable)pFClass;
                        //Geoprocessor gp = new Geoprocessor();
                        //CalculateField pCalFld = new CalculateField();
                        //pCalFld.in_table = pTable;
                        //pCalFld.field = strTargetFld;
                        //pCalFld.field = pFClass.Fields.Field[pFClass.FindField(strTargetFld)];
                        //pCalFld.expression = strExpression;
                        //pCalFld.expression = "5";
                        //pCalFld.expression_type = "VB";

                        //gp.Execute(pCalFld, null);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("ArcGIS Syntax Error: " + ex.ToString());
                        return;
                    }
                }

                MessageBox.Show("Done");
                if (intHandle != IntPtr.Zero)
                {
                    frmAttributeTable pfrmAttributeTable = pSnippet.returnAttTable(intHandle);
                    if (pfrmAttributeTable == null)
                    {
                        return;
                    }
                    pSnippet.LoadingAttributeTable(pLayer, pfrmAttributeTable);
                }
            }
            catch (Exception ex)
            {
                frmErrorLog pfrmErrorLog = new frmErrorLog(); pfrmErrorLog.ex = ex; pfrmErrorLog.ShowDialog();
                return;
            }
        }