Esempio n. 1
0
        private void btnRun_Click(object sender, EventArgs e)
        {
            try
            {
                if (cboFieldName.Text == "")
                {
                    MessageBox.Show("Please select target field");
                    return;
                }
                frmProgress pfrmProgress = new frmProgress();
                pfrmProgress.lblStatus.Text    = "Processing:";
                pfrmProgress.pgbProgress.Style = ProgressBarStyle.Marquee;
                pfrmProgress.Show();

                //Decimal places
                int intDeciPlaces = 5;

                // Creates the input and output matrices from the shapefile//
                //string strLayerName = cboTargetLayer.Text;

                //int intLIndex = m_pSnippet.GetIndexNumberFromLayerName(m_pActiveView, strLayerName);
                //ILayer pLayer = m_pForm.axMapControl1.get_Layer(intLIndex);

                //pFLayer = pLayer as IFeatureLayer;
                //pFClass = pFLayer.FeatureClass;
                int nFeature = m_pFClass.FeatureCount(null);

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

                //Get index for independent and dependent variables
                //Get variable index
                string strVarNM  = cboFieldName.Text;
                int    intVarIdx = m_pFClass.FindField(strVarNM);

                //Store Variable at Array
                double[] arrVar = new double[nFeature];

                int i = 0;

                while (pFeature != null)
                {
                    arrVar[i] = Convert.ToDouble(pFeature.get_Value(intVarIdx));
                    i++;
                    pFeature = pFCursor.NextFeature();
                }

                //Plot command for R
                StringBuilder plotCommmand = new StringBuilder();

                if (!m_blnCreateSWM)
                {
                    //Get the file path and name to create spatial weight matrix
                    string strNameR = m_pSnippet.FilePathinRfromLayer(m_pFLayer);

                    if (strNameR == null)
                    {
                        return;
                    }

                    //Create spatial weight matrix in R
                    if (m_pFClass.ShapeType == esriGeometryType.esriGeometryPolygon)
                    {
                        m_pEngine.Evaluate("sample.shp <- readShapePoly('" + strNameR + "')");
                    }
                    else if (m_pFClass.ShapeType == esriGeometryType.esriGeometryPoint)
                    {
                        m_pEngine.Evaluate("sample.shp <- readShapePoints('" + strNameR + "')");
                    }
                    else
                    {
                        MessageBox.Show("This geometry type is not supported");
                        pfrmProgress.Close();
                        this.Close();
                    }


                    int intSuccess = m_pSnippet.CreateSpatialWeightMatrix(m_pEngine, m_pFClass, txtSWM.Text, pfrmProgress);
                    if (intSuccess == 0)
                    {
                        return;
                    }
                }

                //pEngine.Evaluate("sample.nb <- poly2nb(sample.shp);sample.listw <- nb2listw(sample.nb, style='W')");
                NumericVector vecVar = m_pEngine.CreateNumericVector(arrVar);
                m_pEngine.SetSymbol(strVarNM, vecVar);
                plotCommmand.Append("sam.result <- ");

                //Select method
                if (cboSAM.Text == "Moran Coefficient")
                {
                    plotCommmand.Append("moran.test");
                }
                else if (cboSAM.Text == "Geary Ratio")
                {
                    plotCommmand.Append("geary.test");
                }

                plotCommmand.Append("(" + strVarNM + ", sample.listw, ");

                //select assumption
                if (cboAssumption.Text == "Normality")
                {
                    plotCommmand.Append("randomisation=FALSE, , zero.policy=TRUE)");
                }
                else if (cboAssumption.Text == "Randomization")
                {
                    plotCommmand.Append("randomisation=TRUE, , zero.policy=TRUE)");
                }

                m_pEngine.Evaluate(plotCommmand.ToString());
                //Print Output
                string   strDecimalPlaces = "N" + intDeciPlaces.ToString();
                string[] strResults       = new string[7];
                strResults[0] = cboSAM.Text + " under " + cboAssumption.Text;
                strResults[1] = "";
                NumericVector vecResults = m_pEngine.Evaluate("sam.result$estimate").AsNumeric();
                strResults[2] = "Statistic: " + vecResults[0].ToString(strDecimalPlaces);
                strResults[3] = "Expectation: " + vecResults[1].ToString(strDecimalPlaces);
                strResults[4] = "Variance: " + vecResults[2].ToString(strDecimalPlaces);
                double dblStd  = m_pEngine.Evaluate("sam.result$statistic").AsNumeric().First();
                double dblPval = m_pEngine.Evaluate("sam.result$p.value").AsNumeric().First();
                strResults[5] = "Standard deviate: " + dblStd.ToString(strDecimalPlaces);
                if (dblPval < Math.Pow(0.1, intDeciPlaces))
                {
                    strResults[6] = "p-value: < 0.001";
                }
                else
                {
                    strResults[6] = "p-value: " + dblPval.ToString(strDecimalPlaces);
                }

                frmGenResult pfrmResult = new frmGenResult();
                pfrmResult.Text                = "Summary";
                pfrmResult.txtField.Text       = strVarNM;
                pfrmResult.txtStatistics.Lines = strResults;
                pfrmProgress.Close();
                pfrmResult.Show();
            }
            catch (Exception ex)
            {
                frmErrorLog pfrmErrorLog = new frmErrorLog(); pfrmErrorLog.ex = ex; pfrmErrorLog.ShowDialog();

                return;
            }
        }
Esempio n. 2
0
        private void pChart_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                //Export the chart to an image file
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    conMenu.Show(pChart, e.X, e.Y);
                    return;
                }

                if (m_intTotalNSeries == 0)
                {
                    return;
                }
                //Clear previous selection
                while (pChart.Series.Count != m_intTotalNSeries)
                {
                    pChart.Series.RemoveAt(pChart.Series.Count - 1);
                }

                int intPtsSeriesIdx = m_intTotalNSeries - 1;
                _canDraw = false;

                HitTestResult result = pChart.HitTest(e.X, e.Y);

                int dblOriPtsSize = pChart.Series[intPtsSeriesIdx].MarkerSize;

                System.Drawing.Color pMarkerColor = System.Drawing.Color.Cyan;
                var seriesLines = new System.Windows.Forms.DataVisualization.Charting.Series
                {
                    Name              = "SelPts",
                    Color             = pMarkerColor,
                    BorderColor       = pMarkerColor,
                    IsVisibleInLegend = false,
                    IsXValueIndexed   = false,
                    //ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line,
                    ChartType   = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point,
                    MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle,
                    MarkerSize  = dblOriPtsSize * 2
                };

                pChart.Series.Add(seriesLines);

                int intSelPtsCnt = 0;
                for (int i = 0; i < pChart.Series[intPtsSeriesIdx].Points.Count; i++)
                {
                    int intX = (int)pChart.ChartAreas[0].AxisX.ValueToPixelPosition(pChart.Series[intPtsSeriesIdx].Points[i].XValue);
                    int intY = (int)pChart.ChartAreas[0].AxisY.ValueToPixelPosition(pChart.Series[intPtsSeriesIdx].Points[i].YValues[0]);

                    System.Drawing.Point SelPts = new System.Drawing.Point(intX, intY);

                    if (_rect.Contains(SelPts))
                    {
                        if (intSelPtsCnt == 0)
                        {
                            seriesLines.Points.AddXY(pChart.Series[intPtsSeriesIdx].Points[i].XValue, pChart.Series[intPtsSeriesIdx].Points[i].YValues[0]);

                            //int index = result.PointIndex;
                            string   strDecimalPlaces = "N" + m_intDeciPlaces.ToString();
                            string[] strResults       = new string[8];
                            strResults[0] = cboSAM.Text + " under " + cboAssumption.Text;
                            strResults[1] = (i + 1).ToString() + " order result";
                            strResults[2] = "Number of none-zero links: " + m_arrResults[i][0].ToString("N0");
                            strResults[3] = "Average number of links: " + (m_arrResults[i][0] / m_intNFeature).ToString(strDecimalPlaces);
                            strResults[4] = "Statistic: " + m_arrResults[i][1].ToString(strDecimalPlaces);
                            strResults[5] = "Expectation: " + m_arrResults[i][2].ToString(strDecimalPlaces);
                            strResults[6] = "Variance: " + m_arrResults[i][3].ToString(strDecimalPlaces);
                            strResults[7] = "p-value: " + m_arrResults[i][4].ToString(strDecimalPlaces);

                            CloseOpendResultForm(m_pHandle);
                            frmGenResult pfrmResult = new frmGenResult();
                            pfrmResult.Text                = "Summary";
                            pfrmResult.txtField.Text       = cboFieldName.Text;
                            pfrmResult.txtStatistics.Lines = strResults;
                            pfrmResult.Show();

                            m_pHandle = pfrmResult.Handle;

                            //Brushing on Map Control is under reviewing. 080316 HK
                            //DrawLineOnActiveView(m_arrMaxFromLinks[i], m_arrMaxToLinks[i], m_arrXYCoord, m_pActiveView);
                        }

                        intSelPtsCnt++;
                    }
                }
                if (intSelPtsCnt == 0)
                {
                    m_pActiveView.GraphicsContainer.DeleteAllElements();
                    m_pActiveView.Refresh();
                }
                if (intSelPtsCnt > 1)
                {
                    MessageBox.Show("Can select only one point");
                }
            }
            catch (Exception ex)
            {
                frmErrorLog pfrmErrorLog = new frmErrorLog(); pfrmErrorLog.ex = ex; pfrmErrorLog.ShowDialog();
                return;
            }
        }
Esempio n. 3
0
        private void btnRun_Click(object sender, EventArgs e)
        {
            if (cboFldnm1.Text == "" || cboFldnm2.Text == "")
            {
                MessageBox.Show("Please select target field");
                return;
            }

            frmProgress pfrmProgress = new frmProgress();

            pfrmProgress.lblStatus.Text    = "Processing:";
            pfrmProgress.pgbProgress.Style = ProgressBarStyle.Marquee;
            pfrmProgress.Show();

            REngine pEngine = m_pForm.pEngine;

            int nFeature = m_pFClass.FeatureCount(null);

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

            //Get index for independent and dependent variables
            //Get variable index
            string strVarNM1  = (string)cboFldnm1.SelectedItem;
            string strVarNM2  = (string)cboFldnm2.SelectedItem;
            int    intVarIdx1 = m_pFClass.FindField(strVarNM1);
            int    intVarIdx2 = m_pFClass.FindField(strVarNM2);

            //Store Variable at Array
            double[] arrVar1 = new double[nFeature];
            double[] arrVar2 = new double[nFeature];

            int i = 0;

            while (pFeature != null)
            {
                arrVar1[i] = Convert.ToDouble(pFeature.get_Value(intVarIdx1));
                arrVar2[i] = Convert.ToDouble(pFeature.get_Value(intVarIdx2));
                i++;
                pFeature = pFCursor.NextFeature();
            }

            pFCursor.Flush();

            //Plot command for R
            StringBuilder plotCommmand = new StringBuilder();

            string strStartPath = m_pForm.strPath;
            string pathr        = strStartPath.Replace(@"\", @"/");

            pEngine.Evaluate("source('" + pathr + "/ESDA_LEE/AllFunctions_LARRY.R')");
            pEngine.Evaluate("source('" + pathr + "/ESDA_LEE/AllFunctions_neighbor.R')");
            pEngine.Evaluate("source('" + pathr + "/ESDA_LEE/AllFunctions_SASbi.R')");

            //Get the file path and name to create spatial weight matrix
            string strNameR = m_pSnippet.FilePathinRfromLayer(m_pFLayer);

            if (strNameR == null)
            {
                return;
            }

            //Create spatial weight matrix in R
            pEngine.Evaluate("library(spdep); library(maptools)");
            pEngine.Evaluate("sample.shp <- readShapePoly('" + strNameR + "')");
            //pEngine.Evaluate("sample.nb <- poly2nb(sample.shp, queen=FALSE)");
            pEngine.Evaluate("sample.nb <- poly2nb(sample.shp)");

            NumericVector vecVar1 = pEngine.CreateNumericVector(arrVar1);

            pEngine.SetSymbol("sample.v1", vecVar1);
            NumericVector vecVar2 = pEngine.CreateNumericVector(arrVar2);

            pEngine.SetSymbol("sample.v2", vecVar2);

            string strNonZero = null;

            if (chkDiagZero.Checked)
            {
                strNonZero = "FALSE";
            }
            else
            {
                strNonZero = "TRUE";
            }

            if (cboMeasure.Text == "Lee's L")
            {
                pEngine.Evaluate("sample.result <- test.global.lee.L(sample.v1, sample.v2, sample.nb, style = 'W', alternative='two.sided', diag.zero = " + strNonZero + ")");
            }
            else if (cboMeasure.Text == "Pearson's r")
            {
                pEngine.Evaluate("sample.result <- test.global.pearson(sample.v1, sample.v2, alternative='two.sided')");
            }
            else if (cboMeasure.Text == "Bivariate Moran")
            {
                pEngine.Evaluate("sample.result <- test.global.bMoran(sample.v1, sample.v2, sample.nb, style = 'W', alternative='two.sided', diag.zero = " + strNonZero + ")");
            }
            else if (cboMeasure.Text == "Bivariate Geary")
            {
                pEngine.Evaluate("sample.result <- test.global.bGeary(sample.v1, sample.v2, sample.nb, style = 'W', alternative='two.sided', diag.zero = " + strNonZero + ")");
            }
            int intDeciPlaces = 4;
            //Print Output
            string strDecimalPlaces = "N" + intDeciPlaces.ToString();

            string[] strResults = new string[8];
            strResults[0] = pEngine.Evaluate("sample.result$method").AsCharacter().First();
            strResults[1] = "";
            if (cboMeasure.Text != "Pearson's r")
            {
                if (chkDiagZero.Checked)
                {
                    strResults[2] = "Weights: W*";
                }
                else
                {
                    strResults[2] = "Weights: W";
                }
            }
            else
            {
                strResults[2] = string.Empty;
            }

            NumericVector vecResults = pEngine.Evaluate("sample.result$estimate").AsNumeric();

            strResults[3] = "Observed: " + vecResults[0].ToString(strDecimalPlaces);
            strResults[4] = "Expectation: " + vecResults[1].ToString(strDecimalPlaces);
            strResults[5] = "Variance: " + vecResults[2].ToString(strDecimalPlaces);
            double dblStd  = pEngine.Evaluate("sample.result$statistic").AsNumeric().First();
            double dblPval = pEngine.Evaluate("sample.result$p.value").AsNumeric().First();

            strResults[6] = "Standard deviate: " + dblStd.ToString(strDecimalPlaces);
            strResults[7] = "p-value: " + dblPval.ToString(strDecimalPlaces);

            frmGenResult pfrmResult = new frmGenResult();

            pfrmResult.Text                = "Summary";
            pfrmResult.txtField.Text       = cboFldnm1.Text + " & " + cboFldnm2.Text;
            pfrmResult.txtStatistics.Lines = strResults;
            pfrmProgress.Close();
            pfrmResult.Show();
        }
Esempio n. 4
0
        private void btnRun_Click(object sender, EventArgs e)
        {
            int intDeciPlaces = 5;

            if (cboFldnm1.Text == "" || cboFldnm2.Text == "")
            {
                MessageBox.Show("Please select target field");
                return;
            }

            frmProgress pfrmProgress = new frmProgress();

            pfrmProgress.lblStatus.Text    = "Processing:";
            pfrmProgress.pgbProgress.Style = ProgressBarStyle.Marquee;
            pfrmProgress.Show();

            REngine pEngine = m_pForm.pEngine;
            // Creates the input and output matrices from the shapefile//
            string strLayerName = cboTargetLayer.Text;

            int    intLIndex = m_pSnippet.GetIndexNumberFromLayerName(m_pActiveView, strLayerName);
            ILayer pLayer    = m_pForm.axMapControl1.get_Layer(intLIndex);

            IFeatureLayer pFLayer  = pLayer as IFeatureLayer;
            IFeatureClass pFClass  = pFLayer.FeatureClass;
            int           nFeature = pFClass.FeatureCount(null);

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

            //Get index for independent and dependent variables
            //Get variable index
            string strVarNM1  = (string)cboFldnm1.SelectedItem;
            string strVarNM2  = (string)cboFldnm2.SelectedItem;
            int    intVarIdx1 = pFClass.FindField(strVarNM1);
            int    intVarIdx2 = pFClass.FindField(strVarNM2);

            //Store Variable at Array
            double[] arrVar1 = new double[nFeature];
            double[] arrVar2 = new double[nFeature];

            int i = 0;

            while (pFeature != null)
            {
                arrVar1[i] = Convert.ToDouble(pFeature.get_Value(intVarIdx1));
                arrVar2[i] = Convert.ToDouble(pFeature.get_Value(intVarIdx2));
                i++;
                pFeature = pFCursor.NextFeature();
            }

            //Plot command for R
            StringBuilder plotCommmand = new StringBuilder();

            string strStartPath = m_pForm.strPath;
            string pathr        = strStartPath.Replace(@"\", @"/");

            pEngine.Evaluate("source('" + pathr + "/AllFunctions_LeeL.R')");

            //Get the file path and name to create spatial weight matrix
            string strNameR = m_pSnippet.FilePathinRfromLayer(pFLayer);

            if (strNameR == null)
            {
                return;
            }

            //Create spatial weight matrix in R
            pEngine.Evaluate("library(spdep); library(maptools)");
            pEngine.Evaluate("sample.shp <- readShapePoly('" + strNameR + "')");
            pEngine.Evaluate("sample.nb <- poly2nb(sample.shp)");

            NumericVector vecVar1 = pEngine.CreateNumericVector(arrVar1);

            pEngine.SetSymbol("sample.v1", vecVar1);
            NumericVector vecVar2 = pEngine.CreateNumericVector(arrVar2);

            pEngine.SetSymbol("sample.v2", vecVar2);

            string strNonZeroDiag = null;

            if (chkNonZeroDiag.Checked)
            {
                strNonZeroDiag = "FALSE";
            }
            else
            {
                strNonZeroDiag = "TRUE";
            }

            if (cboSAM.Text == "Lee's L")
            {
                pEngine.Evaluate("sample.g <- L.global.test(sample.v1, sample.v2, sample.nb, style='W', alternative='two.sided', diag.zero=" + strNonZeroDiag + ")");


                //Print Output
                string   strDecimalPlaces = "N" + intDeciPlaces.ToString();
                string[] strResults       = new string[7];
                if (chkNonZeroDiag.Checked)
                {
                    strResults[0] = cboSAM.Text + "* under " + cboAssumption.Text;
                }
                else
                {
                    strResults[0] = cboSAM.Text + "0 under " + cboAssumption.Text;
                }

                strResults[1] = "";
                NumericVector vecResults = pEngine.Evaluate("sample.g$estimate").AsNumeric();
                strResults[2] = "Statistic: " + vecResults[0].ToString(strDecimalPlaces);
                strResults[3] = "Expectation: " + vecResults[1].ToString(strDecimalPlaces);
                strResults[4] = "Variance: " + vecResults[2].ToString(strDecimalPlaces);
                double dblStd  = pEngine.Evaluate("sample.g$statistic").AsNumeric().First();
                double dblPval = pEngine.Evaluate("sample.g$p.value").AsNumeric().First();
                strResults[5] = "Standard deviate: " + dblStd.ToString(strDecimalPlaces);
                strResults[6] = "p-value: " + dblPval.ToString(strDecimalPlaces);

                frmGenResult pfrmResult = new frmGenResult();
                pfrmResult.Text                = "Summary";
                pfrmResult.txtField.Text       = strVarNM1 + " & " + strVarNM2;
                pfrmResult.txtStatistics.Lines = strResults;

                pfrmResult.Show();
            }
            pfrmProgress.Close();
        }