コード例 #1
0
        public int CreateSpatialWeightMatrixPoly(REngine pEngine, IMapLayer pMapLayer, string strSWMtype, frmProgress pfrmProgress, double dblAdvancedValue, bool blnCumul)
        {
            try
            {
                //Return 0, means fails to create spatial weight matrix, 1 means success.

                SpatialWeightMatrixType pSWMType = new SpatialWeightMatrixType();

                if (pMapLayer is MapPolygonLayer)
                {
                    if (strSWMtype == pSWMType.strPolyDefs[0])
                    {
                        pEngine.Evaluate("sample.nb <- poly2nb(sample.shp, queen=T)");
                    }
                    else if (strSWMtype == pSWMType.strPolyDefs[1])
                    {
                        pEngine.Evaluate("sample.nb <- poly2nb(sample.shp, queen=F)");
                    }

                    if (dblAdvancedValue > 1)
                    {
                        try
                        {
                            pEngine.Evaluate("sample.nblags <- nblag(sample.nb, maxlag = " + dblAdvancedValue.ToString() + ")");
                        }
                        catch
                        {
                            MessageBox.Show("Please reduce the maximum lag order");
                        }

                        if (blnCumul)
                        {
                            pEngine.Evaluate("sample.nb <- nblag_cumul(sample.nblags)");
                        }
                        else
                        {
                            pEngine.Evaluate("sample.nb <- sample.nblags[[" + dblAdvancedValue.ToString() + "]]");
                        }
                    }

                    //For dealing empty neighbors
                    try
                    {
                        pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W');sample.listb <- nb2listw(sample.nb, style='B')");
                    }
                    catch
                    {
                        DialogResult dialogResult = MessageBox.Show("Empty neighbor sets are founded. Do you want to continue?", "Empty neighbor", MessageBoxButtons.YesNo);


                        if (dialogResult == DialogResult.Yes)
                        {
                            pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W', zero.policy=TRUE);sample.listb <- nb2listw(sample.nb, style='B', zero.policy=TRUE)");
                        }
                        else if (dialogResult == DialogResult.No)
                        {
                            pfrmProgress.Close();
                            return(0);
                        }
                    }
                }
                else
                {
                    int intResult = SWMusingGAL(pEngine, pMapLayer, strSWMtype);
                    if (intResult == -1)
                    {
                        pfrmProgress.Close();
                        return(0);
                    }
                }
                return(1);
            }
            catch (Exception ex)
            {
                frmErrorLog pfrmErrorLog = new frmErrorLog(); pfrmErrorLog.ex = ex; pfrmErrorLog.ShowDialog();
                return(0);
            }
        }//For only polygons
コード例 #2
0
        }//For only polygons

        public int CreateSpatialWeightMatrixPts(REngine pEngine, IMapLayer pMapLayer, string strSWMtype, frmProgress pfrmProgress, double dblAdvancedValue, bool blnCumul)//For only point dataset
        {
            try
            {
                //Return 0, means fails to create spatial weight matrix, 1 means success.

                SpatialWeightMatrixType pSWMType = new SpatialWeightMatrixType();

                if (pMapLayer is MapPointLayer)
                {
                    #region Delaunay
                    if (strSWMtype == pSWMType.strPointDef[0])
                    {
                        if (blnCumul == false)
                        {
                            pEngine.Evaluate("sample.nb <- tri2nb(coordinates(sample.shp))");
                            //For dealing empty neighbors

                            try
                            {
                                pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W'); sample.listb <- nb2listw(sample.nb, style='B')");
                            }
                            catch
                            {
                                DialogResult dialogResult = MessageBox.Show("Empty neighbor sets are founded. Do you want to continue?", "Empty neighbor", MessageBoxButtons.YesNo);


                                if (dialogResult == DialogResult.Yes)
                                {
                                    pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W', zero.policy=TRUE);sample.listb <- nb2listw(sample.nb, style='B', zero.policy=TRUE)");
                                }
                                else if (dialogResult == DialogResult.No)
                                {
                                    pfrmProgress.Close();
                                    return(0);
                                }
                            }
                        }
                        else
                        {
                            if (pMapLayer == null)
                            {
                                return(0);
                            }

                            MainForm pForm = System.Windows.Forms.Application.OpenForms["MainForm"] as MainForm;

                            string strStartPath = pForm.strPath;
                            string pathr        = strStartPath.Replace(@"\", @"/");
                            pEngine.Evaluate("source('" + pathr + "/del.subset.R')");

                            try
                            {
                                pEngine.Evaluate("library(deldir); library(rgeos)");
                            }
                            catch
                            {
                                MessageBox.Show("Please checked R packages installed in your local computer.");
                                return(0);
                            }


                            string strPolypathR = FilePathinRfromLayer(pMapLayer);

                            pEngine.Evaluate("sample.sub.shp <- readShapePoly('" + strPolypathR + "')");

                            pEngine.Evaluate("sample.nb <- del.subset(sample.shp, sample.sub.shp)");
                            bool blnError = pEngine.Evaluate("nrow(sample.shp) == length(sample.nb)").AsLogical().First();

                            if (blnError == false)
                            {
                                MessageBox.Show("The number of features in points and the rows of neighbors is not matched.", "Error");
                                return(0);
                            }
                            else
                            {
                                try
                                {
                                    pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W'); sample.listb <- nb2listw(sample.nb, style='B')");
                                }
                                catch
                                {
                                    DialogResult dialogResult = MessageBox.Show("Empty neighbor sets are founded. Do you want to continue?", "Empty neighbor", MessageBoxButtons.YesNo);


                                    if (dialogResult == DialogResult.Yes)
                                    {
                                        pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W', zero.policy=TRUE);sample.listb <- nb2listw(sample.nb, style='B', zero.policy=TRUE)");
                                    }
                                    else if (dialogResult == DialogResult.No)
                                    {
                                        return(0);
                                    }
                                }
                            }
                        }
                    }
                    #endregion
                    else if (strSWMtype == pSWMType.strPointDef[1])
                    {
                        pEngine.Evaluate("sample.nb <- dnearneigh(coordinates(sample.shp), 0, " + dblAdvancedValue.ToString() + ")");
                        //For dealing empty neighbors
                        if (pEngine.Evaluate("sum(card(sample.nb)) < 1").AsLogical().First())
                        {
                            MessageBox.Show("There are too many empty neighbors");
                            pfrmProgress.Close();
                            return(0);
                        }

                        try
                        {
                            pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W'); sample.listb <- nb2listw(sample.nb, style='B')");
                        }
                        catch
                        {
                            DialogResult dialogResult = MessageBox.Show("Empty neighbor sets are founded. Do you want to continue?", "Empty neighbor", MessageBoxButtons.YesNo);


                            if (dialogResult == DialogResult.Yes)
                            {
                                pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W', zero.policy=TRUE);sample.listb <- nb2listw(sample.nb, style='B', zero.policy=TRUE)");
                            }
                            else if (dialogResult == DialogResult.No)
                            {
                                pfrmProgress.Close();
                                return(0);
                            }
                        }
                        finally
                        {
                            pfrmProgress.Close();
                            MessageBox.Show("Fail to create spatial weights matrix");
                        }
                    }
                    else if (strSWMtype == pSWMType.strPointDef[2])
                    {
                        pEngine.Evaluate("col.knn <- knearneigh(coordinates(sample.shp), k=" + dblAdvancedValue.ToString() + ")");
                        pEngine.Evaluate("sample.nb <- knn2nb(col.knn)");
                        //For dealing empty neighbors

                        bool blnSymmetric = pEngine.Evaluate("is.symmetric.nb(sample.nb)").AsLogical().First();
                        if (blnSymmetric == false)
                        {
                            DialogResult dialogResult = MessageBox.Show("This spatial weight matrix is asymmetric. Some functions are restricted in an asymmetric matrix. Do you want to continue?", "Asymmetric spatial weight matrix", MessageBoxButtons.YesNo);
                            if (dialogResult == DialogResult.No)
                            {
                                pfrmProgress.Close();
                                return(0);
                            }
                        }

                        try
                        {
                            pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W'); sample.listb <- nb2listw(sample.nb, style='B')");
                        }
                        catch
                        {
                            DialogResult dialogResult = MessageBox.Show("Empty neighbor sets are founded. Do you want to continue?", "Empty neighbor", MessageBoxButtons.YesNo);


                            if (dialogResult == DialogResult.Yes)
                            {
                                pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W', zero.policy=TRUE);sample.listb <- nb2listw(sample.nb, style='B', zero.policy=TRUE)");
                            }
                            else if (dialogResult == DialogResult.No)
                            {
                                pfrmProgress.Close();
                                return(0);
                            }
                        }
                    }
                }
                else
                {
                    int intResult = SWMusingGAL(pEngine, pMapLayer, strSWMtype);
                    if (intResult == -1)
                    {
                        pfrmProgress.Close();
                        return(0);
                    }
                }
                return(1);
            }
            catch (Exception ex)
            {
                frmErrorLog pfrmErrorLog = new frmErrorLog(); pfrmErrorLog.ex = ex; pfrmErrorLog.ShowDialog();
                return(0);
            }
        }
コード例 #3
0
        //Creating Spatial Weight Matrix
        public int CreateSpatialWeightMatrix(REngine pEngine, IMapLayer pMapLayer, string strSWMtype, frmProgress pfrmProgress)
        {
            try
            {
                //Return 0, means fails to create spatial weight matrix, 1 means success.

                SpatialWeightMatrixType pSWMType = new SpatialWeightMatrixType();

                if (pMapLayer is MapPolygonLayer)
                {
                    if (strSWMtype == pSWMType.strPolyDefs[0])
                    {
                        pEngine.Evaluate("sample.nb <- poly2nb(sample.shp, queen=T)");
                    }
                    else if (strSWMtype == pSWMType.strPolyDefs[1])
                    {
                        pEngine.Evaluate("sample.nb <- poly2nb(sample.shp, queen=F)");
                    }
                    else
                    {
                        pEngine.Evaluate("sample.nb <- poly2nb(sample.shp, queen=T)");
                    }
                    //For dealing empty neighbors
                    try
                    {
                        pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W');sample.listb <- nb2listw(sample.nb, style='B')");
                    }
                    catch
                    {
                        DialogResult dialogResult = MessageBox.Show("Empty neighbor sets are founded. Do you want to continue?", "Empty neighbor", MessageBoxButtons.YesNo);


                        if (dialogResult == DialogResult.Yes)
                        {
                            pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W', zero.policy=TRUE);sample.listb <- nb2listw(sample.nb, style='B', zero.policy=TRUE)");
                        }
                        else if (dialogResult == DialogResult.No)
                        {
                            pfrmProgress.Close();
                            return(0);
                        }
                    }
                }
                else if (pMapLayer is MapPointLayer)
                {
                    FormCollection pFormCollection = System.Windows.Forms.Application.OpenForms;
                    bool           blnOpen         = false;
                    int            intIdx          = 0;

                    for (int j = 0; j < pFormCollection.Count; j++)
                    {
                        if (pFormCollection[j].Name == "frmSubsetPoly")//Brushing to Histogram
                        {
                            intIdx = j;

                            blnOpen = true;
                        }
                    }

                    if (blnOpen) //Delaunay with clipping
                    {
                        frmSubsetPoly pfrmSubsetPoly1 = pFormCollection[intIdx] as frmSubsetPoly;
                        if (pfrmSubsetPoly1.m_blnSubset)
                        {
                            string strPolypathR = FilePathinRfromLayer(pfrmSubsetPoly1.m_pMaplayer);

                            pEngine.Evaluate("sample.sub.shp <- readShapePoly('" + strPolypathR + "')");

                            pEngine.Evaluate("sample.nb <- del.subset(sample.shp, sample.sub.shp)");
                            bool blnError = pEngine.Evaluate("nrow(sample.shp) == length(sample.nb)").AsLogical().First();

                            if (blnError == false)
                            {
                                MessageBox.Show("The number of features in points and the rows of neighbors is not matched.", "Error");
                                return(0);
                            }
                            else
                            {
                                try
                                {
                                    pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W'); sample.listb <- nb2listw(sample.nb, style='B')");
                                }
                                catch
                                {
                                    DialogResult dialogResult = MessageBox.Show("Empty neighbor sets are founded. Do you want to continue?", "Empty neighbor", MessageBoxButtons.YesNo);


                                    if (dialogResult == DialogResult.Yes)
                                    {
                                        pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W', zero.policy=TRUE);sample.listb <- nb2listw(sample.nb, style='B', zero.policy=TRUE)");
                                    }
                                    else if (dialogResult == DialogResult.No)
                                    {
                                        pfrmProgress.Close();
                                        return(0);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        pEngine.Evaluate("sample.nb <- tri2nb(coordinates(sample.shp))");
                        //For dealing empty neighbors

                        try
                        {
                            pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W'); sample.listb <- nb2listw(sample.nb, style='B')");
                        }
                        catch
                        {
                            DialogResult dialogResult = MessageBox.Show("Empty neighbor sets are founded. Do you want to continue?", "Empty neighbor", MessageBoxButtons.YesNo);


                            if (dialogResult == DialogResult.Yes)
                            {
                                pEngine.Evaluate("sample.listw <- nb2listw(sample.nb, style='W', zero.policy=TRUE);sample.listb <- nb2listw(sample.nb, style='B', zero.policy=TRUE)");
                            }
                            else if (dialogResult == DialogResult.No)
                            {
                                pfrmProgress.Close();
                                return(0);
                            }
                        }
                    }
                }
                else
                {
                    int intResult = SWMusingGAL(pEngine, pMapLayer, strSWMtype);
                    if (intResult == -1)
                    {
                        pfrmProgress.Close();
                        return(0);
                    }
                }
                return(1);
            }
            catch (Exception ex)
            {
                frmErrorLog pfrmErrorLog = new frmErrorLog(); pfrmErrorLog.ex = ex; pfrmErrorLog.ShowDialog();
                return(0);
            }
        }