public static IRasterDataset createFileRasterDataset(IRasterWorkspace2 irasterWorkspace2_0, string string_0, int int_0, rstPixelType rstPixelType_0, ISpatialReference ispatialReference_0) { try { IRasterDataset dataset = null; IPoint origin = new Point(); origin.PutCoords(0.0, 0.0); if (ispatialReference_0 == null) { ispatialReference_0 = new UnknownCoordinateSystem() as ISpatialReference; } dataset = irasterWorkspace2_0.CreateRasterDataset(string_0, "IMAGINE Image", origin, 200, 100, 1.0, 1.0, int_0, rstPixelType_0, ispatialReference_0, true); IRawPixels pixels = null; IPixelBlock3 block = null; IPnt tlc = null; IPnt size = null; IRasterBandCollection bands = (IRasterBandCollection)dataset; pixels = (IRawPixels)bands.Item(0); IRasterProps props = (IRasterProps)pixels; tlc = new DblPnt(); tlc.SetCoords(0.0, 0.0); size = new DblPnt(); size.SetCoords((double)props.Width, (double)props.Height); block = (IPixelBlock3)pixels.CreatePixelBlock(size); pixels.Read(tlc, (IPixelBlock)block); object[,] objArray = (object[, ])block.get_PixelDataByRef(0); for (int i = 0; i < props.Width; i++) { for (int j = 0; j < props.Height; j++) { objArray[i, j] = (i * j) % 255; } } object cache = pixels.AcquireCache(); pixels.Write(tlc, (IPixelBlock)block); pixels.ReturnCache(cache); return(dataset); } catch (Exception exception) { Debug.WriteLine(exception.Message); return(null); } }
public static double GetPixelVal(IRaster pRaster, IPoint pPoint) { IPixelBlock pPixelBlock; IPnt pPnt; IPnt pCurrentPnt; ISpatialReference pSR; pSR = pPoint.SpatialReference; pPnt = new DblPnt(); pPnt.SetCoords(1.0, 1.0); pPixelBlock = pRaster.CreatePixelBlock(pPnt); pCurrentPnt = GetCurrPnt(pRaster, pPoint.X, pPoint.Y, pSR); pRaster.Read(pCurrentPnt, pPixelBlock); return(Convert.ToDouble(string.Format("{0,10:###0.0000}", pPixelBlock.GetVal(0, 0, 0)))); }
public void Reclassify() { InitColorRamp(); IRasterWorkspace pRWS; IWorkspaceFactory pWorkspaceFactory; pWorkspaceFactory = new RasterWorkspaceFactory(); String wsp; int r; int l; r = ComboBoxInLayer.Text.LastIndexOf("\\"); l = ComboBoxInLayer.Text.LastIndexOf("."); wsp = ComboBoxInLayer.Text.Substring(0, r); pRWS = (IRasterWorkspace)pWorkspaceFactory.OpenFromFile(wsp, 0); try { string ws = System.IO.Path.GetDirectoryName(ComboBoxInLayer.Text); string fbs = System.IO.Path.GetFileName(ComboBoxInLayer.Text); IWorkspaceFactory pWork = new RasterWorkspaceFactory(); IRasterWorkspace pRasterWs = pWork.OpenFromFile(ws, 0) as IRasterWorkspace; pRster = pRasterWs.OpenRasterDataset(fbs).CreateDefaultRaster(); IRasterBandCollection pRasterBandCol; pRasterBandCol = (IRasterBandCollection)pRster; IRawPixels pRawpixel; pRawpixel = (IRawPixels)pRasterBandCol.Item(0); IRasterProps pRasterProps; pRasterProps = (IRasterProps)pRawpixel; IPnt pSize; pSize = new DblPnt(); pSize.SetCoords(pRasterProps.Width, pRasterProps.Height); IPixelBlock pPixelBlock; pPixelBlock = pRawpixel.CreatePixelBlock(pSize); IPnt pPnt; pPnt = new DblPnt(); pPnt.X = 0; //the origin (top left corner) of the PixelBlock pPnt.Y = 0; pRawpixel.Read(pPnt, pPixelBlock); float PixelValue; minValue = 10000000; maxValue = 0; List <double> allValue = new List <double>(); for (int i = 0; i < pPixelBlock.Width; i++) { for (int j = 0; j < pPixelBlock.Height; j++) { PixelValue = (float)pPixelBlock.GetVal(0, i, j); allValue.Add(PixelValue); if (PixelValue != null) { if (PixelValue > maxValue) { maxValue = PixelValue; } if (PixelValue < minValue) { minValue = PixelValue; } } } } IRasterBand pRasterBand = new RasterBand(); pRasterBand = pRasterBandCol.Item(0); pRasterBand.ComputeStatsAndHist(); IRasterStatistics pRasterStatistic; //double[] amount = pRasterBand.Histogram.Counts; pRasterStatistic = pRasterBand.Statistics; maxValue = (float)pRasterStatistic.Maximum; minValue = (float)pRasterStatistic.Minimum; //if (comboBox1.Text.Trim() == "等间隔") //{//先求每个等分间距 float Tmpincrement; DataRow FilterRow; Tmpincrement = (maxValue - minValue) / Convert.ToInt16(CmbLineCount.Text); FilterDataTable.Rows.Clear(); for (int i = 1; i <= Convert.ToInt16(CmbLineCount.Text); i++) { FilterRow = FilterDataTable.NewRow(); FilterRow[0] = Convert.ToString(minValue + Tmpincrement * (i - 1)) + "~" + Convert.ToString(minValue + Tmpincrement * i); FilterRow[1] = i; FilterRow[2] = i; FilterDataTable.Rows.Add(FilterRow); } DataGridFilterData.DataSource = FilterDataTable; DataGridFilterData.Refresh(); // } //else // { ////allValue.Sort(); ////List<double> valuesort = new List<double>(); ////valuesort = BublleSort(allValue); //allValue.Sort(); //int amount = pPixelBlock.Width * pPixelBlock.Height; //int Tmpincrement; //DataRow FilterRow; //Tmpincrement = (amount) / Convert.ToInt16(CmbLineCount.Text); //FilterDataTable.Rows.Clear(); //for (int i = 1; i < Convert.ToInt16(CmbLineCount.Text); i++) //{ // FilterRow = FilterDataTable.NewRow(); // if (allValue[Tmpincrement * (i - 1)] == allValue[Tmpincrement * (i)]) // break; // FilterRow[0] = Convert.ToString(allValue[Tmpincrement * (i - 1)]) + "~" + Convert.ToString(allValue[Tmpincrement * (i)]); // FilterRow[1] = i; // FilterDataTable.Rows.Add(FilterRow); //} //if (allValue[Tmpincrement * Convert.ToInt16(CmbLineCount.Text)] != maxValue) //{ // FilterRow = FilterDataTable.NewRow(); // FilterRow[0] = Convert.ToString(allValue[Tmpincrement * Convert.ToInt16(CmbLineCount.Text)]) + "~" + Convert.ToString(maxValue); // FilterRow[1] = Convert.ToInt16(CmbLineCount.Text); // FilterDataTable.Rows.Add(FilterRow); //} //DataGridFilterData.DataSource = FilterDataTable; //DataGridFilterData.Refresh(); //} } catch (Exception ex) { MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } }
public string GetCellValueOrig_MayNotBeRight(string layerName, IPoint pLoc, IMap pMap) { try { IPnt pBlockSize = new DblPnt(); pBlockSize.SetCoords(1.0, 1.0); IRasterLayer pLayer = null; IPixelBlock pPixelBlock; object vValue; // string sPixelVals; // sPixelVals = "No Raster"; IRasterProps pRasterProps; double dXSize; double dYSize; IPnt pPixel; pPixel = new DblPnt(); pLayer = GetRasterLayer(pMap, layerName); if (pLayer == null) return ""; pPixelBlock = pLayer.Raster.CreatePixelBlock(pBlockSize); pRasterProps = pLayer.Raster as IRasterProps; dXSize = pRasterProps.Extent.XMax - pRasterProps.Extent.XMin; dYSize = pRasterProps.Extent.YMax - pRasterProps.Extent.YMin; dXSize = dXSize / pRasterProps.Width; dYSize = dYSize / pRasterProps.Height; pPixel.X = (pLoc.X - pRasterProps.Extent.XMin) / dXSize; pPixel.Y = (pRasterProps.Extent.YMax - pLoc.Y) / dYSize; pLayer.Raster.Read(pPixel, pPixelBlock); for (int j = 0; j < pPixelBlock.Planes; j++) { vValue = pPixelBlock.GetVal(j, 0, 0); if (vValue.ToString() != "No Raster") return vValue.ToString(); //= sPixelVals + ", "; } //if (sPixelVals != "No Raster") // return ""; //else return "No Raster"; } catch (Exception ex) { MessageBox.Show("GetView: " + ex.Message); return "No Raster"; } }
public string GetCellValue(string layerName, IPoint pLoc, IMap pMap) { try { IRasterLayer pLayer = null; double dblX = pLoc.X; double dblY = pLoc.Y; pLayer = GetRasterLayer(pMap, layerName); if (pLayer == null) return ""; TransformDataXYToPixelXY(pLayer.Raster, ref dblX, ref dblY); IPnt pBlockSize = new DblPnt(); pBlockSize.SetCoords(1.0, 1.0); IPixelBlock pPixelBlock; pPixelBlock = pLayer.Raster.CreatePixelBlock(pBlockSize); IPnt pPixel; pPixel = new DblPnt(); pPixel.X = dblX; pPixel.Y = dblY; pLayer.Raster.Read(pPixel, pPixelBlock); object vValue; for (int j = 0; j < pPixelBlock.Planes; j++) { vValue = pPixelBlock.GetVal(j, 0, 0); if (vValue.ToString() != "No Raster") return vValue.ToString(); //= sPixelVals + ", "; } //if (sPixelVals != "No Raster") // return ""; //else return "No Raster"; } catch (Exception ex) { MessageBox.Show("GetView: " + ex.Message); return "No Raster"; } }
private void ShowRasterInfo(IRasterLayer rLayer) { rtxbRasterInfo.Text = "Boand Number is " + rLayer.BandCount.ToString();// "\r\n" +"Resolution is " + rLayer.DisplayResolutionFactor.ToString() + "\r\n" + "Row number is " + rLayer.RowCount.ToString(); IRaster raster = rLayer.Raster; //Raster Data IRasterBandCollection rBandCon = raster as IRasterBandCollection; int numBands = rBandCon.Count; for (int i = 0; i < numBands; i++) { // IRasterBand rBand = rBandCon.Item(i); IRasterProps rasterData = rBand as IRasterProps; //Raster Height int height = rasterData.Height; //Raster Width int width = rasterData.Width; //Raster Cell Size IPnt cell = rasterData.MeanCellSize(); rstPixelType rType = rasterData.PixelType; MessageBox.Show(rType.ToString()); if (rType == rstPixelType.PT_LONG) { //Error code //Read Raster //Wrong QI //IRaster pRaster = rBand as IRaster; ITable rasterTable = rBand.AttributeTable; IQueryFilter qFilter = new QueryFilter(); for (int j = 0; j < rasterTable.Fields.FieldCount; j++) { IField aField = rasterTable.Fields.get_Field(j); qFilter.AddField(aField.ToString()); } ICursor cursor = rasterTable.Search(qFilter, true); IRow aRow = cursor.NextRow(); if (aRow != null) { //aRow.get_Value(0); MessageBox.Show(cell.X.ToString() + "x" + cell.Y + ":" + aRow.get_Value(0).ToString()); //Read Raster // rBand. } } // Create pixelblock IRawPixels rawPixels = rBand as IRawPixels; //cannot be used as follows //IRawPixels rawPixels = raster as IRawPixels; IPnt pixelBlockOrigin = new DblPnt(); pixelBlockOrigin.SetCoords(0, 0); IPnt pixelBlockSize = new DblPnt(); pixelBlockSize.SetCoords(rasterData.Width, rasterData.Height); IPixelBlock3 pixelBlock3 = (IPixelBlock3)rawPixels.CreatePixelBlock(pixelBlockSize); //Read rawPixels.Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3); //GetVal( int brand, int X, int Y); brand=0 int x = 3600; int y = 800; object cellD = pixelBlock3.GetVal(0, x, y); //MessageBox.Show(cellD.ToString()); rtxbRasterInfo.Text = rtxbRasterInfo.Text + "\r\n" + rBand.Bandname.ToString() + ":" + width.ToString() + "x" + height.ToString() + ":(" + x.ToString() + "," + y.ToString() + ")=" + cellD.ToString(); } }
private void BtnInput_Click(object sender, EventArgs e) { OpenFileDialog OpenFileDlg = new OpenFileDialog(); String strPath; // '文件名 OpenFileDlg.FileName = ""; OpenFileDlg.Filter = "tif files (*.tif)|*.tif|All files (*.*)|*.*"; //过滤文件类型 OpenFileDlg.FilterIndex = 2; OpenFileDlg.ShowDialog(); strPath = OpenFileDlg.FileName.Trim(); layerpath.Text = strPath; //获取图层的栅格信息 IRasterWorkspace pRWS; IWorkspaceFactory pWorkspaceFactory; pWorkspaceFactory = new RasterWorkspaceFactory(); String wsp; int r; int l; r = layerpath.Text.LastIndexOf("\\"); l = layerpath.Text.LastIndexOf("."); wsp = layerpath.Text.Substring(0, r); pRWS = (IRasterWorkspace)pWorkspaceFactory.OpenFromFile(wsp, 0); try { string ws = Path.GetDirectoryName(layerpath.Text); string fbs = Path.GetFileName(layerpath.Text); IWorkspaceFactory pWork = new RasterWorkspaceFactory(); IRasterWorkspace pRasterWs = pWork.OpenFromFile(ws, 0) as IRasterWorkspace; pRster = pRasterWs.OpenRasterDataset(fbs).CreateDefaultRaster(); //pRster = pRWS.OpenRasterDataset(sp) IRasterBandCollection pRasterBandCol; pRasterBandCol = (IRasterBandCollection)pRster; IRawPixels pRawpixel; pRawpixel = (IRawPixels)pRasterBandCol.Item(0); IRasterProps pRasterProps; pRasterProps = (IRasterProps)pRawpixel; IPnt pSize; pSize = new DblPnt(); pSize.SetCoords(pRasterProps.Width, pRasterProps.Height); IPixelBlock pPixelBlock; pPixelBlock = pRawpixel.CreatePixelBlock(pSize); IPnt pPnt; pPnt = new DblPnt(); pPnt.X = 0; //the origin (top left corner) of the PixelBlock pPnt.Y = 0; pRawpixel.Read(pPnt, pPixelBlock); float PixelValue; minValue = 10000000; maxValue = 0; for (int i = 0; i < pPixelBlock.Width; i++) { for (int j = 0; j < pPixelBlock.Height; j++) { PixelValue = (float)pPixelBlock.GetVal(0, i, j); if (PixelValue != null) { if (PixelValue > maxValue) { maxValue = PixelValue; } if (PixelValue < minValue) { minValue = PixelValue; } } } } IRasterBand pRasterBand; pRasterBand = pRasterBandCol.Item(0); pRasterBand.ComputeStatsAndHist(); IRasterStatistics pRasterStatistic; pRasterStatistic = pRasterBand.Statistics; maxValue = (float)pRasterStatistic.Maximum; minValue = (float)pRasterStatistic.Minimum; //先求每个等分间距 float Tmpincrement; DataRow FilterRow; Tmpincrement = (maxValue - minValue) / Convert.ToInt16(CmbLineCount.Text); FilterDataTable.Rows.Clear(); for (int i = 1; i < Convert.ToInt16(CmbLineCount.Text); i++) { FilterRow = FilterDataTable.NewRow(); FilterRow[0] = Convert.ToString(minValue + Tmpincrement * (i - 1)) + "~" + Convert.ToString(minValue + Tmpincrement * i); FilterRow[1] = i; FilterDataTable.Rows.Add(FilterRow); } DataGridFilterData.DataSource = FilterDataTable; DataGridFilterData.Refresh(); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } }