public static bool UniqueValueRenderer(IColorRamp colorRamp, IRasterLayer rasterLayer, string renderfiled = "Value") { try { IRasterUniqueValueRenderer uniqueValueRenderer = new RasterUniqueValueRendererClass(); IRasterRenderer pRasterRenderer = uniqueValueRenderer as IRasterRenderer; pRasterRenderer.Raster = rasterLayer.Raster; pRasterRenderer.Update(); IUniqueValues uniqueValues = new UniqueValuesClass(); IRasterCalcUniqueValues calcUniqueValues = new RasterCalcUniqueValuesClass(); calcUniqueValues.AddFromRaster(rasterLayer.Raster, 0, uniqueValues);//iBand=0 IRasterRendererUniqueValues renderUniqueValues = uniqueValueRenderer as IRasterRendererUniqueValues; renderUniqueValues.UniqueValues = uniqueValues; uniqueValueRenderer.Field = renderfiled; colorRamp.Size = uniqueValues.Count; uniqueValueRenderer.HeadingCount = 1; uniqueValueRenderer.set_Heading(0, "All Data Value"); uniqueValueRenderer.set_ClassCount(0, uniqueValues.Count); bool pOk; colorRamp.CreateRamp(out pOk); IRasterRendererColorRamp pRasterRendererColorRamp = uniqueValueRenderer as IRasterRendererColorRamp; pRasterRendererColorRamp.ColorRamp = colorRamp; for (int i = 0; i < uniqueValues.Count; i++) { uniqueValueRenderer.AddValue(0, i, uniqueValues.get_UniqueValue(i)); uniqueValueRenderer.set_Label(0, i, uniqueValues.get_UniqueValue(i).ToString()); IFillSymbol fs = new SimpleFillSymbol(); //fs.Color = colorRamp.get_Color(i); IColor color = new RgbColorClass(); if ((Convert.ToByte(uniqueValues.get_UniqueValue(i)) == 255) || (Convert.ToByte(uniqueValues.get_UniqueValue(i)) == 0)) { color.NullColor = true; color.Transparency = 0; fs.Color = color; } else { IRgbColor rgbColor = color as IRgbColor; rgbColor.Red = 0; rgbColor.Green = 255; rgbColor.Blue = 0; fs.Color = rgbColor as IColor; } uniqueValueRenderer.set_Symbol(0, i, fs as ISymbol); } pRasterRenderer.Update(); rasterLayer.Renderer = pRasterRenderer; } catch (Exception ex) { return(false); } return(true); }
public void drawLine() { IUniqueValues uniqueValues = new UniqueValuesClass(); IRasterCalcStatsHistogram calcstatsHistogram = new RasterCalcStatsHistogramClass(); IStatsHistogram statsHistogram = new StatsHistogramClass(); calcstatsHistogram.ComputeFromRaster(rasterLayer.Raster, 0, statsHistogram); IRasterCalcUniqueValues calcUniqueValues = new RasterCalcUniqueValuesClass(); try { calcUniqueValues.AddFromRaster(rasterLayer.Raster, 0, uniqueValues); double[] ArrVal = new double[uniqueValues.Count]; System.Int32[] ArrFreq = new System.Int32[uniqueValues.Count]; for (var i = 0; i < uniqueValues.Count; i++) { ArrVal[i] = Convert.ToDouble(uniqueValues.get_UniqueValue(i)); ArrFreq[i] = uniqueValues.get_UniqueCount(i); } valueChooseInChart1.drawLine(ArrVal, ArrFreq, Convert.ToInt32(spinEdit1.Text)); } catch { valueChooseInChart1.drawline(statsHistogram.Min, statsHistogram.Max, Convert.ToInt16(spinEdit1.Text)); } }
public static void uniqueRender(IRasterLayer rasterLayer) { if (rasterLayer == null) { return; } IRaster raster = rasterLayer.Raster; IRasterUniqueValueRenderer render = new RasterUniqueValueRendererClass(); IRasterRenderer rasterRender = render as IRasterRenderer; rasterRender.Raster = raster; rasterRender.Update(); IUniqueValues uniqueValues = new UniqueValuesClass(); IRasterCalcUniqueValues calcUniqueValues = new RasterCalcUniqueValuesClass(); calcUniqueValues.AddFromRaster(raster, 0, uniqueValues); IRasterRendererUniqueValues renderUniqueValues = render as IRasterRendererUniqueValues; renderUniqueValues.UniqueValues = uniqueValues; render.Field = "Value"; render.HeadingCount = 1; render.set_Heading(0, "淹没水深"); render.set_ClassCount(0, uniqueValues.Count); IRandomColorRamp ramp = new RandomColorRampClass(); ramp.Size = uniqueValues.Count; bool b = true; ramp.CreateRamp(out b); for (int i = 0; i < uniqueValues.Count; i++) { render.AddValue(0, i, uniqueValues.get_UniqueValue(i)); render.set_Label(0, i, uniqueValues.get_UniqueValue(i).ToString()); ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass(); fillSymbol.Color = ramp.get_Color(i); render.set_Symbol(0, i, fillSymbol as ISymbol); } rasterRender.Update(); rasterLayer.Renderer = rasterRender; }
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> /// 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); } } }
public double[] getOraDataStringArray() { List <double> arr = new List <double>(); arr.Clear(); if (this.RasterLayerClass != null && this.RasterLayerClass.AttributeTable != null) { ITable tb = this.RasterLayerClass.AttributeTable as ITable; if (tb != null) { object[] objArr = this.Unique(tb, "Value", ""); foreach (object obj in objArr) { arr.Add(double.Parse(obj.ToString())); } arr.Sort(); } } else if (this.RasterLayerClass.AttributeTable == null) { //渲染方式获取Value值的方法 //this.RasterLayerClass.Renderer = new RasterValueClass(); for (int ib = 0; ib < this.RasterLayerClass.BandCount; ib++) { IUniqueValues uv = new UniqueValuesClass(); IRasterCalcUniqueValues rcuv = new RasterCalcUniqueValuesClass(); rcuv.AddFromRaster(this.RasterLayerClass.Raster, ib, uv); for (int i = 0; i < uv.Count; i++) { arr.Add(double.Parse(uv.get_UniqueValue(i).ToString())); } } arr.Sort(); } return(arr.ToArray()); }
/// <summary> /// 设置图层渲染器。 /// </summary> /// <param name="rasterLayer">需要渲染唯一值的栅格图层。</param> /// <param name="renderfiled">渲染的字段(可选,默认为Value)。</param> /// <returns></returns> public IRasterRenderer UniqueValueRender(IRasterLayer rasterLayer, string renderfiled = "Value") { try { //这是从头用到尾的对象 IRasterUniqueValueRenderer uniqueValueRenderer = new RasterUniqueValueRendererClass { Field = renderfiled }; IRasterRenderer pRasterRenderer = uniqueValueRenderer as IRasterRenderer; //计算栅格唯一值 IRasterCalcUniqueValues calcUniqueValues = new RasterCalcUniqueValuesClass(); IUniqueValues uniqueValues = new UniqueValuesClass(); calcUniqueValues.AddFromRaster(rasterLayer.Raster, 0, uniqueValues); //设置唯一值 IRasterRendererUniqueValues renderUniqueValues = uniqueValueRenderer as IRasterRendererUniqueValues; renderUniqueValues.UniqueValues = uniqueValues; //创建色带 IRgbColor pFromColor = FromIC; IRgbColor pToColor = ToIC; IAlgorithmicColorRamp colorRamp = new AlgorithmicColorRampClass { FromColor = pFromColor, ToColor = pToColor, Size = uniqueValues.Count }; bool pOk; colorRamp.CreateRamp(out pOk); //设置标题 uniqueValueRenderer.HeadingCount = 1; uniqueValueRenderer.set_Heading(0, "All Data Value"); uniqueValueRenderer.set_ClassCount(0, uniqueValues.Count); //设置色带 IRasterRendererColorRamp pRasterRendererColorRamp = uniqueValueRenderer as IRasterRendererColorRamp; pRasterRendererColorRamp.ColorRamp = colorRamp; //需要对算出来的唯一值升序重排 double[] tmp = new double[uniqueValues.Count]; for (int i = 0; i < uniqueValues.Count; i++) { tmp[i] = Convert.ToDouble(uniqueValues.get_UniqueValue(i)); } System.Array.Sort(tmp); //对每一个唯一值设置颜色 for (int i = 0; i < uniqueValues.Count; i++) { //添加唯一值并设置标签 uniqueValueRenderer.AddValue(0, i, tmp[i]); uniqueValueRenderer.set_Label(0, i, tmp[i].ToString()); //透明色及参数 IRgbColor zerocolor = new RgbColorClass() { Transparency = 0, NullColor = true, }; IFillSymbol fs = new SimpleFillSymbol(); //唯一值填充符号 if (tmp[i] == 0) //将值为0的栅格颜色设为透明 { fs = new SimpleFillSymbol { Color = zerocolor }; } else //值不为零则设置为色带对应索引颜色 { fs.Color = colorRamp.get_Color(i); } uniqueValueRenderer.set_Symbol(0, i, fs as ISymbol); //对唯一值设置色带对应颜色 } return(pRasterRenderer); } catch (Exception ex) { MessageBox.Show(ex.ToString()); return(null); } }