private void btnRun_Click(object sender, EventArgs e) { try { if (cboFieldName.Text == "") { MessageBox.Show("Please select an ID field to be used as region ID.", "Please choose an ID field"); return; } if (txtOutput.Text == "") { MessageBox.Show("Please specify path and file name.", "Please specify a path"); return; } frmProgress pfrmProgress = new frmProgress(); pfrmProgress.lblStatus.Text = "Processing:"; pfrmProgress.pgbProgress.Style = ProgressBarStyle.Marquee; pfrmProgress.Show(); //Create Row ID vectors int nFeature = m_pFClass.FeatureCount(null); //Get index for independent and dependent variables string strIDfield = cboFieldName.Text; int intIDIdx = m_pFClass.Fields.FindField(strIDfield); double[] arrRowID = new double[nFeature]; int i = 0; IFeatureCursor pFCursor = m_pFClass.Search(null, true); IFeature pFeature = pFCursor.NextFeature(); while (pFeature != null) { arrRowID[i] = Convert.ToDouble(pFeature.get_Value(intIDIdx)); i++; pFeature = pFCursor.NextFeature(); } NumericVector vecRowID = m_pEngine.CreateNumericVector(arrRowID); m_pEngine.SetSymbol("sample.ids", vecRowID); //Get the file path and name to create spatial weight matrix //Input string strNameR = m_pSnippet.FilePathinRfromLayer(m_pFLayer); if (strNameR == null) { return; } //Shp name IDataset dataset = (IDataset)(m_pFLayer); string strShpname = dataset.BrowseName; if (dataset.Category == "Shapefile Feature Class") { strShpname = strShpname + ".shp"; } //Output string strOutput = m_pSnippet.FilePathinRfromText(txtOutput.Text); int intSuccess = 0; //Create spatial weight matrix in R if (m_pFClass.ShapeType == esriGeometryType.esriGeometryPolygon) { m_pEngine.Evaluate("sample.shp <- readShapePoly('" + strNameR + "')"); intSuccess = m_pSnippet.CreateSpatialWeightMatrixPolywithID(m_pEngine, m_pFClass, txtSWM.Text, pfrmProgress, Convert.ToDouble(nudAdvanced.Value), chkCumulate.Checked); } else if (m_pFClass.ShapeType == esriGeometryType.esriGeometryPoint) { m_pEngine.Evaluate("sample.shp <- readShapePoints('" + strNameR + "')"); //intSuccess = m_pSnippet.ExploreSpatialWeightMatrix1(m_pEngine, m_pFClass, txtSWM.Text, pfrmProgress, Convert.ToDouble(nudAdvanced.Value), chkCumulate.Checked); intSuccess = m_pSnippet.CreateSpatialWeightMatrixPtswithID(m_pEngine, m_pFClass, txtSWM.Text, pfrmProgress, Convert.ToDouble(nudAdvanced.Value), chkCumulate.Checked, m_pClippedPolygon); //chkCumulate.Visible = false; } else { MessageBox.Show("This geometry type is not supported"); pfrmProgress.Close(); this.Close(); } if (intSuccess == 0) { MessageBox.Show("Fail to create spatial weights.", "Fail"); return; } else { m_pEngine.Evaluate("write.nb.gal(sample.nb, '" + strOutput + "', oldstyle = F, shpfile = '" + strShpname + "', ind = '" + strIDfield + "')"); MessageBox.Show("A spatial weights file is successfuly created in " + strOutput, "Success"); } pfrmProgress.Close(); } catch (Exception ex) { frmErrorLog pfrmErrorLog = new frmErrorLog(); pfrmErrorLog.ex = ex; pfrmErrorLog.ShowDialog(); return; } }