/// <summary> /// 获取当前栅格图层中城市用地类型栅格的数量。区分栅格图层是否有属性表两种情况。 /// </summary> /// <param name="rasterLayer"></param> /// <param name="landUseClassificationInfo"></param> /// <returns></returns> public static int GetUrbanCount(IRasterLayer rasterLayer, LandUseClassificationInfo landUseClassificationInfo) { int urbanConuts = 0; IRasterBandCollection rbc = rasterLayer.Raster as IRasterBandCollection; IRasterBand rb = rbc.Item(0); //ITable table = rbc.Item(0).AttributeTable; //AttributeTable - OID, Value, Count ITable attributeTable = rb.AttributeTable; if (attributeTable != null) { int count = attributeTable.RowCount(null); int[] values = new int[count]; int[] counts = new int[count]; ICursor cursor = attributeTable.Search(null, false); IRow row = cursor.NextRow(); int index = 0; //数据不一定有OBJECTID字段。同时列名称及顺序不一定符合条件Value,Count。 while (row != null) { values[index] = Convert.ToInt32(row.get_Value(GetColumnIndex(attributeTable, "VALUE"))); counts[index] = Convert.ToInt32(row.get_Value(GetColumnIndex(attributeTable, "COUNT"))); row = cursor.NextRow(); index++; } for (int j = 0; j < count; j++) { for (int i = 0; i < landUseClassificationInfo.UrbanValues.Count; i++) { if (Convert.ToSingle(values[j]) == landUseClassificationInfo.UrbanValues[i].LanduseTypeValue) { urbanConuts += Convert.ToInt32(counts[j]); } } } } else { rb.ComputeStatsAndHist(); IRasterHistogram rh = rb.Histogram; object rhCount = rh.Counts; double[] counts = (double[])rhCount; IRasterUniqueValueRenderer rasterUniqueValueRenderer = (IRasterUniqueValueRenderer)rasterLayer.Renderer; IRasterRendererUniqueValues rasterRendererUniqueValues = (IRasterRendererUniqueValues)rasterUniqueValueRenderer; IUniqueValues uniqueValues = rasterRendererUniqueValues.UniqueValues; for (int j = 0; j < uniqueValues.Count; j++) { for (int i = 0; i < landUseClassificationInfo.UrbanValues.Count; i++) { if (Convert.ToSingle(uniqueValues.get_UniqueValue(j)) == landUseClassificationInfo.UrbanValues[i].LanduseTypeValue) { urbanConuts += Convert.ToInt32(counts[j + 1]); } } } } return(urbanConuts); }
/// <summary> /// 设置栅格渲染的唯一值 /// </summary> /// <param name="uniqueValueRenderer">唯一值渲染</param> private void SetRasterUniqueValue(IRasterUniqueValueRenderer uniqueValueRenderer) { //必须计算获取唯一值,添加到图层,不然只能渲染0值 IUniqueValues uniqueValues = GetFloatValueAndCount(rasterLayer); IRasterRendererUniqueValues renderUniqueValues = uniqueValueRenderer as IRasterRendererUniqueValues; if (renderUniqueValues != null) { renderUniqueValues.UniqueValues = uniqueValues; } }
/// <summary> /// 获取浮点类型栅格图层的唯一值(排好序的) /// </summary> /// <returns></returns> private List <object> GetFloatSortedUniqueValue() { IUniqueValues uniqueValues = GetFloatValueAndCount(rasterLayer); List <object> uniqueValue = new List <object>(); for (int i = 0; i < uniqueValues.Count; i++) { uniqueValue.Add(uniqueValues.UniqueValue[i]); } uniqueValue.Sort(); return(uniqueValue); }
private bool GetUniqueValues(string inFile) { IRasterLayer pRasterLayer = null; IUniqueValues pUniqueValues = null; IRasterCalcUniqueValues pCalcUniqueValues = null; try { pRasterLayer = new RasterLayerClass(); pRasterLayer.CreateFromFilePath(inFile); if (!(1 == pRasterLayer.BandCount)) { XtraMessageBox.Show("文件类型不正确,请选择分类结果文件!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); return(false); } pUniqueValues = new UniqueValuesClass(); pCalcUniqueValues = new RasterCalcUniqueValuesClass(); pCalcUniqueValues.AddFromRaster(pRasterLayer.Raster, 0, pUniqueValues); if (_dt.Rows.Count > 0) { _dt.Rows.Clear(); } for (int i = 0; i < pUniqueValues.Count; i++) { DataRow row = _dt.NewRow(); row[0] = pUniqueValues.get_UniqueValue(i); _dt.Rows.Add(row); } return(true); } catch (Exception ex) { XtraMessageBox.Show("读取唯一值失败!\r\n" + ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); Log.WriteLog(typeof(frmRecode), ex); return(false); } finally { if (pCalcUniqueValues != null) { Marshal.ReleaseComObject(pCalcUniqueValues); } if (pUniqueValues != null) { Marshal.ReleaseComObject(pUniqueValues); } if (pRasterLayer != null) { Marshal.ReleaseComObject(pRasterLayer); } } }
/// <summary> /// 获取唯一值和相应数目 /// </summary> /// <param name="fieldName">字段</param> /// <returns></returns> public UniqueValueAndCount GetUniqueValueAndCount(string fieldName) { //判断是否存在属性表 IUniqueValues uniqueValues = IsFloatRasterLayer() ? GetFloatValueAndCount(rasterLayer) : GetTableValueAndCount(rasterLayer, fieldName); //获取唯一值和相应数目 UniqueValueAndCount uniqueValueCount = new UniqueValueAndCount(); for (int i = 0; i < uniqueValues.Count; i++) { uniqueValueCount.AddValue(uniqueValues.UniqueValue[i]); uniqueValueCount.AddCount(uniqueValues.UniqueCount[i]); } uniqueValueCount.UniqueValue.Sort(); return(uniqueValueCount); }
/// <summary> /// Gets the class value from raster. /// </summary> /// <param name="inFile">The in file.</param> /// <returns>List<System.String>.</returns> /// <exception cref="System.Exception">文件类型不正确,请选择分类结果文件!</exception> public static List <string> GetClassFromRaster(string inFile) { IRasterLayer pRasterLayer = null; IUniqueValues pUniqueValues = null; IRasterCalcUniqueValues pCalcUniqueValues = null; List <string> classNames = null; try { pRasterLayer = new RasterLayerClass(); pRasterLayer.CreateFromFilePath(inFile); if (!(1 == pRasterLayer.BandCount)) { throw new Exception("文件类型不正确,请选择分类结果文件!"); } pUniqueValues = new UniqueValuesClass(); pCalcUniqueValues = new RasterCalcUniqueValuesClass(); pCalcUniqueValues.AddFromRaster(pRasterLayer.Raster, 0, pUniqueValues); classNames = new List <string>(); for (int i = 0; i < pUniqueValues.Count; i++) { classNames.Add(pUniqueValues.get_UniqueValue(i).ToString()); } return(classNames); } catch (Exception ex) { throw ex; } finally { if (pCalcUniqueValues != null) { Marshal.ReleaseComObject(pCalcUniqueValues); } if (pUniqueValues != null) { Marshal.ReleaseComObject(pUniqueValues); } if (pRasterLayer != null) { Marshal.ReleaseComObject(pRasterLayer); } } }
/// <summary> /// 获取当前栅格图层中各种用地类型栅格的数量。区分栅格图层是否有属性表两种情况。 /// </summary> /// <param name="rasterLayer"></param> /// <returns></returns> public static int[] GetLandUseTypesCount(IRasterLayer rasterLayer) { int[] landusetypesCounts; IRasterBandCollection rbc = rasterLayer.Raster as IRasterBandCollection; IRasterBand rb = rbc.Item(0); ITable attributeTable = rb.AttributeTable; if (attributeTable != null) { int count = attributeTable.RowCount(null); int[] values = new int[count]; landusetypesCounts = new int[count]; ICursor cursor = attributeTable.Search(null, false); IRow row = cursor.NextRow(); int index = 0; //数据不一定有OBJECTID字段。同时列名称及顺序不一定符合条件Value,Count。 while (row != null) { values[index] = Convert.ToInt32(row.get_Value(GetColumnIndex(attributeTable, "VALUE"))); landusetypesCounts[index] = Convert.ToInt32(row.get_Value(GetColumnIndex(attributeTable, "COUNT"))); row = cursor.NextRow(); index++; } } else { rb.ComputeStatsAndHist(); IRasterHistogram rh = rb.Histogram; object rhCount = rh.Counts; double[] counts = (double[])rhCount; landusetypesCounts = new int[counts.Length]; IRasterUniqueValueRenderer rasterUniqueValueRenderer = (IRasterUniqueValueRenderer)rasterLayer.Renderer; IRasterRendererUniqueValues rasterRendererUniqueValues = (IRasterRendererUniqueValues)rasterUniqueValueRenderer; IUniqueValues uniqueValues = rasterRendererUniqueValues.UniqueValues; for (int j = 0; j < uniqueValues.Count; j++) { landusetypesCounts[j] = Convert.ToInt32(counts[j]); } } return(landusetypesCounts); }
public override void Execute(GrapeCity.Documents.Excel.Workbook workbook) { IWorksheet worksheet = workbook.Worksheets[0]; object[,] data = new object[, ] { { "Name", "City", "Birthday", "Eye color", "Weight", "Height" }, { "Richard", "New York", new DateTime(1968, 6, 8), "Blue", 80, 165 }, { "Nia", "New York", new DateTime(1972, 7, 3), "Brown", 72, 134 }, { "Jared", "New York", new DateTime(1964, 3, 2), "Hazel", 71, 180 }, { "Natalie", "Washington", new DateTime(1972, 8, 8), "Blue", 80, 163 }, { "Damon", "Washington", new DateTime(1986, 2, 2), "Hazel", 58, 176 }, { "Angela", "Washington", new DateTime(1993, 2, 15), "Brown", 71, 145 } }; worksheet.Range["A1:F7"].Value = data; //Unique rule. IUniqueValues condition = worksheet.Range["E2:E7"].FormatConditions.AddUniqueValues(); condition.DupeUnique = DupeUnique.Unique; condition.Font.Name = "Arial"; condition.Interior.Color = Color.Pink; }
/// <summary> /// 读取并填充土地利用类型信息。 /// </summary> private void FillLanduseInfo(string layerName) { dataGridViewLandUse.Rows.Clear(); //根据第一个图层的符号化设置读取土地利用类型信息。 //注意:因为一般来说符号化设置的设置和属性表一致,因为不需要考虑顺序不一致的问题。 IRasterLayer rasterLayer = ArcGISOperator.GetRasterLayerByName(layerName); IRasterUniqueValueRenderer rasterUniqueValueRenderer = (IRasterUniqueValueRenderer)rasterLayer.Renderer; IRasterRendererUniqueValues rasterRendererUniqueValues = (IRasterRendererUniqueValues)rasterUniqueValueRenderer; IUniqueValues uniqueValues = rasterRendererUniqueValues.UniqueValues; List <object> listUniqueValues = new List <object>(); for (int i = 0; i < uniqueValues.Count; i++) { listUniqueValues.Add(uniqueValues.get_UniqueValue(i)); } int classCount = rasterUniqueValueRenderer.get_ClassCount(0); for (int i = 0; i < classCount; i++) { dataGridViewLandUse.Rows.Add(); dataGridViewLandUse.Rows[i].Cells[0].Value = rasterUniqueValueRenderer.get_Label(0, i); } }
/// <summary> /// 当向导页的新一页出现前可以做的该页初始化工作。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void wizardLogistcCA_AfterSwitchPages(object sender, CristiPotlog.Controls.Wizard.AfterSwitchPagesEventArgs e) { //切换至逻辑回归计算页时,应首先读取地图图层信息,填写部分控件 if (e.OldIndex == 1 && e.NewIndex == 2) { //如果不是第一次打开窗体,则再次读取地图数据 application = ArcMap.Application; document = application.Document as IMxDocument; map = document.FocusMap; ArcGISOperator.FoucsMap = document.FocusMap; //如果图层选择框为空 if (comboBoxTrainingStartImage.Items.Count == 0) { for (int i = 0; i < map.LayerCount; i++) { string layerName = map.get_Layer(i).Name; comboBoxTrainingStartImage.Items.Add(layerName); comboBoxTrainingEndImage.Items.Add(layerName); comboBoxSimStartImage.Items.Add(layerName); comboBoxSimEndImage.Items.Add(layerName); } } } //切换到土地利用类型设置页时首先获取图层信息,然后填充土地利用类型 else if (e.OldIndex == 2 && e.NewIndex == 3) { listVariableLayersName.Clear(); trainingStartImageName = comboBoxTrainingStartImage.SelectedItem.ToString(); trainingEndImageName = comboBoxTrainingEndImage.SelectedItem.ToString(); for (int i = 0; i < dataGridViewVariableDatas.Rows.Count; i++) { listVariableLayersName.Add(dataGridViewVariableDatas.Rows[i].Cells[0].Value.ToString()); } //如果已经完成了回归,则不再进行 if (isRegressed) { return; } dataGridViewLandUse.Rows.Clear(); IRasterLayer rasterLayer = ArcGISOperator.GetRasterLayerByName(trainingStartImageName); IRasterUniqueValueRenderer rasterUniqueValueRenderer = (IRasterUniqueValueRenderer)rasterLayer.Renderer; IRasterRendererUniqueValues rasterRendererUniqueValues = (IRasterRendererUniqueValues)rasterUniqueValueRenderer; IUniqueValues uniqueValues = rasterRendererUniqueValues.UniqueValues; List <object> listUniqueValues = new List <object>(); for (int i = 0; i < uniqueValues.Count; i++) { listUniqueValues.Add(uniqueValues.get_UniqueValue(i)); } int classCount = rasterUniqueValueRenderer.get_ClassCount(0); for (int i = 0; i < classCount; i++) { dataGridViewLandUse.Rows.Add(); dataGridViewLandUse.Rows[i].Cells[0].Value = listUniqueValues[i]; dataGridViewLandUse.Rows[i].Cells[1].Value = rasterUniqueValueRenderer.get_Label(0, i); ISymbol symbol = rasterUniqueValueRenderer.get_Symbol(0, i); IFillSymbol fillSymbol = symbol as IFillSymbol; //ISimpleFillSymbol simpleFillSymbol = (ISimpleFillSymbol)symbol; //IColor esriColor = simpleFillSymbol.Color; IColor esriColor = fillSymbol.Color; IRgbColor rgbColor = new RgbColorClass(); rgbColor.CMYK = esriColor.CMYK; Color color = Color.FromArgb(rgbColor.Red, rgbColor.Green, rgbColor.Blue); dataGridViewLandUse.Rows[i].Cells[3].Value = GeneralOpertor.GetBitmap(15, 15, color); } ColumnLUCharacter.Items.Clear(); ColumnLUCharacter.Items.Add(resourceManager.GetString("String6")); ColumnLUCharacter.Items.Add(resourceManager.GetString("String7")); ColumnLUCharacter.Items.Add(resourceManager.GetString("String8")); } //切换到逻辑回归计算页面时进行计算 if (e.OldIndex == 3 && e.NewIndex == 4) { if (isRegressed) { return; } dataGridViewLogisticRegressionCoeff.Rows.Clear(); labelCalculte.Text = resourceManager.GetString("String9"); labelCalculte.Visible = true; progressBarCalculate.Visible = true; Application.DoEvents(); //1.读取起始图像、终止图像、各变量图像为数组 IRasterLayer rasterLayerStartImage = ArcGISOperator.GetRasterLayerByName(comboBoxTrainingStartImage.SelectedItem.ToString()); IRasterLayer rasterLayerEndImage = ArcGISOperator.GetRasterLayerByName(comboBoxTrainingEndImage.SelectedItem.ToString()); RasterSampling rasterSampling = new RasterSampling(); labelCalculte.Text = resourceManager.GetString("String12"); Application.DoEvents(); List <int> notNullRows; List <int> notNullColumns; trainingStartImage = ArcGISOperator.ReadRasterAndGetNotNullRowColumn(rasterLayerStartImage, out structRasterMetaData, out notNullRows, out notNullColumns); //获取最小空间范围 List <IRasterLayer> listVariablesLayers = new List <IRasterLayer>(); for (int i = 0; i < listVariableLayersName.Count; i++) { listVariablesLayers.Add(ArcGISOperator.GetRasterLayerByName(listVariableLayersName[i])); } ArcGISOperator.GetSmallestBound(rasterLayerStartImage, rasterLayerEndImage, listVariablesLayers, ref structRasterMetaData); labelCalculte.Text = resourceManager.GetString("String13"); Application.DoEvents(); trainingEndImage = ArcGISOperator.ReadRaster(rasterLayerEndImage, structRasterMetaData.NoDataValue); float[,] changeImage = GeneralOpertor.GetBinaryImageByTwoImages(trainingStartImage, trainingEndImage, structRasterMetaData.RowCount, structRasterMetaData.ColumnCount, structRasterMetaData.NoDataValue); //GeneralOpertor.WriteDataFloat(GetOutputFolder() + @"\lr_change.txt", changeImage, structRasterMetaData.RowCount, structRasterMetaData.ColumnCount); foreach (string layerName in listVariableLayersName) { labelCalculte.Text = resourceManager.GetString("String14") + layerName + "....."; Application.DoEvents(); varibaleImages.Add(ArcGISOperator.ReadRaster(ArcGISOperator.GetRasterLayerByName(layerName), structRasterMetaData.NoDataValue)); } //2.按设置的比例进行随机抽样,得到抽样结果,然后计算 //1.进行抽样 labelCalculte.Text = resourceManager.GetString("String11"); Application.DoEvents(); int samplingCellsCount = (int)(structRasterMetaData.RowCount * structRasterMetaData.ColumnCount * numericUpDownSamplingPrecent.Value / 100); float[,] datas = new float[samplingCellsCount, listVariableLayersName.Count + 1]; //抽取真值的比例默认设置为一半 datas = rasterSampling.SamplingData(varibaleImages, changeImage, (double)numericUpDownSamplingPrecent.Value, 50, structRasterMetaData, notNullRows, notNullColumns); //2.进行计算 labelCalculte.Text = resourceManager.GetString("String9"); Application.DoEvents(); //输出抽样结果数据 //GeneralOpertor.WriteDataFloat(GetOutputFolder() + @"\lrSamplingData.txt", datas, // rasterSampling.SamplingCellsCount, listVariableLayersName.Count + 1); //3.使用Accord.NET进行逻辑回归计算 labelCalculte.Text = resourceManager.GetString("String9"); Application.DoEvents(); LogisticRegression lr = new LogisticRegression(); double[] odds; double[] stde; double[] min; double[] max; lr.LogisticRegresssionAanlysis(datas, rasterSampling.SamplingCellsCount, listVariableLayersName.Count + 1, out coef, out odds, out stde, out min, out max); for (int i = 0; i < listVariableLayersName.Count + 1; i++) { dataGridViewLogisticRegressionCoeff.Rows.Add(); if (i == 0) { dataGridViewLogisticRegressionCoeff.Rows[i].Cells[0].Value = resourceManager.GetString("String15"); } else { dataGridViewLogisticRegressionCoeff.Rows[i].Cells[0].Value = listVariableLayersName[i - 1]; } dataGridViewLogisticRegressionCoeff.Rows[i].Cells[1].Value = coef[i]; dataGridViewLogisticRegressionCoeff.Rows[i].Cells[2].Value = odds[i]; dataGridViewLogisticRegressionCoeff.Rows[i].Cells[3].Value = stde[i]; dataGridViewLogisticRegressionCoeff.Rows[i].Cells[4].Value = max[i]; dataGridViewLogisticRegressionCoeff.Rows[i].Cells[5].Value = min[i]; } labelCalculte.Visible = false; progressBarCalculate.Visible = false; Application.DoEvents(); isRegressed = true; } //切换到模拟参数设置页时给定控件初始值,同时保存模拟数据信息 else if (e.OldIndex == 4 && e.NewIndex == 5) { if (comboBoxSimEndImage.SelectedIndex == -1) { buttonCalConvertCells.Enabled = false; } else { buttonCalConvertCells.Enabled = true; } } //切换到模拟过程参数设置页时给定控件初始值,同时保存模拟参数信息 else if (e.OldIndex == 5 && e.NewIndex == 6) { simulationStartImageName = comboBoxSimStartImage.SelectedItem.ToString(); if (comboBoxSimEndImage.SelectedIndex != -1) { simulationEndImageName = comboBoxSimEndImage.SelectedItem.ToString(); } convertCount = Convert.ToInt32(numericUpDownConvertCount.Value); simulationIterations = Convert.ToInt32(numericUpDownIterations.Value); delta = Convert.ToInt32(numericUpDownDelta.Value); numericUpDownRefresh.Value = 10; numericUpDownOutputImage.Value = 10; outputFolder = GetOutputFolder(); textBoxOutputFolder.Text = outputFolder; } //切换到完成页时填充摘要,同时保存模拟过程输出参数的信息 else if (e.OldIndex == 6 && e.NewIndex == 7) { refreshInterval = Convert.ToInt32(numericUpDownRefresh.Value); outputImageInterval = Convert.ToInt32(numericUpDownOutputImage.Value); if (radioButtonOutput.Checked) { isOutput = true; } else { isOutput = false; } outputFolder = textBoxOutputFolder.Text; textBoxSummay.Text = WriteSummay(); } }