private void ReadHeader() { try { _dataset = Gdal.Open(Filename, Access.GA_Update); } catch { try { _dataset = Gdal.Open(Filename, Access.GA_ReadOnly); } catch (Exception ex) { throw new GdalException(ex.ToString()); } } Init(_dataset.RasterXSize, _dataset.RasterYSize); NumBands = _dataset.RasterCount; WorldFile = new WorldFile { Affine = new double[6] }; double[] test = new double[6]; _dataset.GetGeoTransform(test); Bounds = new RasterBounds(Height, Width, test); WorldFile.Affine = test; DoClose(); }
private void ReadHeader() { _dataset = Helpers.Open(Filename); Init(_dataset.RasterXSize, _dataset.RasterYSize); NumBands = _dataset.RasterCount; WorldFile = new WorldFile { Affine = new double[6] }; double[] test = new double[6]; _dataset.GetGeoTransform(test); Bounds = new RasterBounds(Height, Width, test); WorldFile.Affine = test; Close(); }
/// <summary> /// Gets the size of the whole image, but doesn't keep the image open unless it was already open. /// </summary> private void ReadHeader() { EnsureDatasetOpen(); double[] test = new double[6]; _dataset.GetGeoTransform(test); Bounds = new RasterBounds(_dataset.RasterYSize, _dataset.RasterXSize, test); _red = _dataset.GetRasterBand(1); _numOverviews = _red.GetOverviewCount(); Close(); }
/// <summary> /// Executes the Erase Opaeration tool programmatically /// Ping deleted static for external testing 01/2010. /// </summary> /// <param name="input1">The first input raster.</param> /// <param name="input2">The second input raster.</param> /// <param name="output">The output raster.</param> /// <param name="cancelProgressHandler">The progress handler.</param> /// <returns>Boolean, true if the merge is successful.</returns> public bool Execute(IRaster input1, IRaster input2, IRaster output, ICancelProgressHandler cancelProgressHandler) { // Validates the input and output data if (input1 == null || input2 == null || output == null) { return(false); } Extent envelope = UnionEnvelope(input1, input2); // Figures out which raster has smaller cells IRaster smallestCellRaster = input1.CellWidth < input2.CellWidth ? input1 : input2; // Given the envelope of the two rasters we calculate the number of columns / rows int noOfCol = Convert.ToInt32(Math.Abs(envelope.Width / smallestCellRaster.CellWidth)); int noOfRow = Convert.ToInt32(Math.Abs(envelope.Height / smallestCellRaster.CellHeight)); // create output raster output = Raster.CreateRaster(output.Filename, string.Empty, noOfCol, noOfRow, 1, typeof(int), new[] { string.Empty }); RasterBounds bound = new RasterBounds(noOfRow, noOfCol, envelope); output.Bounds = bound; output.NoDataValue = input1.NoDataValue; int previous = 0; int max = output.Bounds.NumRows + 1; for (int i = 0; i < output.Bounds.NumRows; i++) { for (int j = 0; j < output.Bounds.NumColumns; j++) { Coordinate cellCenter = output.CellToProj(i, j); var v1 = input1.ProjToCell(cellCenter); double val1; if (v1.Row <= input1.EndRow && v1.Column <= input1.EndColumn && v1.Row > -1 && v1.Column > -1) { val1 = input1.Value[v1.Row, v1.Column]; } else { val1 = input1.NoDataValue; } var v2 = input2.ProjToCell(cellCenter); double val2; if (v2.Row <= input2.EndRow && v2.Column <= input2.EndColumn && v2.Row > -1 && v2.Column > -1) { val2 = input2.Value[v2.Row, v2.Column]; } else { val2 = input2.NoDataValue; } if (val1 == input1.NoDataValue && val2 == input2.NoDataValue) { output.Value[i, j] = output.NoDataValue; } else if (val1 != input1.NoDataValue && val2 == input2.NoDataValue) { output.Value[i, j] = val1; } else if (val1 == input1.NoDataValue && val2 != input2.NoDataValue) { output.Value[i, j] = val2; } else { output.Value[i, j] = val1; } if (cancelProgressHandler.Cancel) { return(false); } } int current = Convert.ToInt32(Math.Round(i * 100D / max)); // only update when increment in persentage if (current > previous) { cancelProgressHandler.Progress(string.Empty, current, current + TextStrings.progresscompleted); } previous = current; } // output = Temp; output.Save(); return(true); }
/// <summary> /// Executes the Erase Opaeration tool programaticaly /// Ping Yang deleted static for external testing 01/2010 /// </summary> /// <param name="input">The input raster</param> /// <param name="oldValue">The original double value representing no-data</param> /// <param name="newValue">The new double value representing no-data</param> /// <param name="output">The output raster</param> /// <param name="cancelProgressHandler">The progress handler</param> /// <returns></returns> public bool Execute( IRaster input, double oldValue, double newValue, IRaster output, ICancelProgressHandler cancelProgressHandler) { // Validates the input and output data if (input == null || newValue == 0 || output == null) { return(false); } Extent envelope = input.Bounds.Extent; int noOfCol = input.NumColumns; int noOfRow = input.NumRows; int previous = 0; Type dataType = input.DataType; // create output raster output = Raster.CreateRaster( output.Filename, string.Empty, noOfCol, noOfRow, 1, dataType, new[] { string.Empty }); RasterBounds bound = new RasterBounds(noOfRow, noOfCol, envelope); output.Bounds = bound; output.NoDataValue = newValue; // Loop throug every cell int max = output.Bounds.NumRows + 1; for (int i = 0; i < output.Bounds.NumRows; i++) { for (int j = 0; j < output.Bounds.NumColumns; j++) { if (input.Value[i, j] == oldValue) { output.Value[i, j] = newValue; } else { output.Value[i, j] = input.Value[i, j]; } if (cancelProgressHandler.Cancel) { return(false); } } int current = Convert.ToInt32(Math.Round(i * 100D / max)); // only update when increment in persentage if (current > previous) { cancelProgressHandler.Progress(string.Empty, current, current + TextStrings.progresscompleted); } previous = current; } // output = Temp; output.Save(); return(true); }
/// <summary> /// Executes the Raster distance calculation /// Ping Yang deleted static for external testing 10/2010 /// </summary> /// <param name="input">The input raster</param> /// <param name="output">The output raster</param> /// <param name="maxDistance">The maximum distance value. Cells with a larger distance to nearest /// target cell than maxDistance will be assigned 'no data' value.</param> /// <param name="cancelProgressHandler">The progress handler</param> /// <returns>true if execution successful, false otherwise</returns> public bool Execute(IRaster input, IRaster output, double maxDistance, ICancelProgressHandler cancelProgressHandler) { // Validates the input and output data if (input == null || output == null) { return(false); } // Creates the output raster with the same bounds as the input RasterBounds bounds = (RasterBounds)input.Bounds.Copy(); output = Raster.CreateRaster( output.Filename, string.Empty, bounds.NumColumns, bounds.NumRows, 1, typeof(int), new[] { string.Empty }); // output.CreateNew(output.Filename, "", bounds.NumColumns, bounds.NumRows, 1, typeof(int), new string[] { "" }); // output = Raster.CreateNewRaster(output.Filename, bounds.NumRows, bounds.NumColumns, RasterDataTypes.INTEGER); output.Bounds = bounds; // internally we reference output as an integer type raster. Raster <int> outRaster = output as Raster <int>; if (outRaster != null) { int numColumns = outRaster.NumColumns; int numRows = outRaster.NumRows; int lastUpdate = 0; // declare two jagged arrays for storing the (Rx, Ry) vectors. // rX is distance from current cell to nearest target cell measured in the x-direction // rY is distance from current cell to nearest target cell measured in the y-direction // the actual distance can be calculated as sqrt(rX^2 + rY^2). // in the resulting distance raster we store the squared distance as well as the rX, rY relative coordinates // to improve computation speed int[][] aRx = new int[numRows][]; int[][] aRy = new int[numRows][]; int[][] aSqDist = new int[numRows][]; const int infD = int.MaxValue; const int targetVal = 0; // initialize the arrays for (int i = 0; i < numRows; i++) { aRx[i] = new int[numColumns]; aRy[i] = new int[numColumns]; aSqDist[i] = new int[numColumns]; ReadInputRow(input, i, aSqDist[i], targetVal, infD); } // ******************************************************************* // raster distance calculation pass one - top to bottom, left to right // ******************************************************************* // int[] row1 = new int[numColumns]; // int[] row2 = new int[numColumns]; int[] aNcels = new int[4]; // the values of four neighbouring cells (W, NW, N, NE) int[] aDiff = new int[4]; // the | y coordinate distances to nearest target cell for (int row = 1; row < numRows; row++) { ////read the row from input raster // ReadInputRow(input, rowIndex, row2, targetVal, infD); for (int col = 1; col < numColumns - 1; col++) { int val = aSqDist[row][col]; // Continue processing only if the current cell is not a target if (val == targetVal) { continue; } // read the values of the cell's neighbours aNcels[0] = aSqDist[row][col - 1]; // W aNcels[1] = aSqDist[row - 1][col - 1]; // NW aNcels[2] = aSqDist[row - 1][col]; // N aNcels[3] = aSqDist[row - 1][col + 1]; // NE // calculate the squared euclidean distances to each neighbouring cell and to the nearest target cell aDiff[0] = (aNcels[0] < infD) ? aNcels[0] + 2 * aRx[row][col - 1] + 1 : infD; aDiff[1] = (aNcels[1] < infD) ? aNcels[1] + 2 * (aRx[row - 1][col - 1] + aRy[row - 1][col - 1] + 1) : infD; aDiff[2] = (aNcels[2] < infD) ? aNcels[2] + 2 * aRy[row - 1][col] + 1 : infD; aDiff[3] = (aNcels[3] < infD) ? aNcels[3] + 2 * (aRx[row - 1][col + 1] + aRy[row - 1][col + 1] + 1) : infD; // find neighbouring cell with minimum distance difference int minDiff = aDiff[0]; int minDiffCell = 0; for (int i = 1; i < 4; i++) { if (aDiff[i] < minDiff) { minDiff = aDiff[i]; minDiffCell = i; } } // if a neighbouring cell with known distance was found: if (minDiff < infD) { // assign the minimum euclidean distance aSqDist[row][col] = minDiff; // update the (rX, rY) cell-to-nearest-target vector switch (minDiffCell) { case 0: // W aRx[row][col] = aRx[row][col - 1] + 1; aRy[row][col] = aRy[row][col - 1]; break; case 1: // NW aRx[row][col] = aRx[row - 1][col - 1] + 1; aRy[row][col] = aRy[row - 1][col - 1] + 1; break; case 2: // N aRx[row][col] = aRx[row - 1][col]; aRy[row][col] = aRy[row - 1][col] + 1; break; case 3: // NE aRx[row][col] = aRx[row - 1][col + 1] + 1; aRy[row][col] = aRy[row - 1][col + 1] + 1; break; } } // end of update (rX, rY) cell-to-nearest-target vector } // end or current row processing - report progress int percent = (int)(row / (double)numRows * 100f); if (percent > lastUpdate) { lastUpdate += 1; cancelProgressHandler.Progress( string.Empty, lastUpdate, TextStrings.Pass1 + lastUpdate + TextStrings.progresscompleted); if (cancelProgressHandler.Cancel) { return(false); } } } // ******************************************************************* // end of first pass for loop // ******************************************************************* // ******************************************************************* // raster distance calculation PASS TWO - bottom to top, right to left // ******************************************************************* lastUpdate = 0; for (int row = numRows - 2; row > 0; row--) { for (int col = numColumns - 2; col > 0; col--) { int val = aSqDist[row][col]; // Continue processing only if the current cell is not a target if (val == targetVal) { continue; } // read the values of the cell's neighbours aNcels[0] = aSqDist[row][col + 1]; // E aNcels[1] = aSqDist[row + 1][col + 1]; // SE aNcels[2] = aSqDist[row + 1][col]; // S aNcels[3] = aSqDist[row + 1][col - 1]; // SW // calculate the squared euclidean distances to each neighbouring cell and to the nearest target cell aDiff[0] = (aNcels[0] < infD) ? aNcels[0] + 2 * aRx[row][col + 1] + 1 : infD; aDiff[1] = (aNcels[1] < infD) ? aNcels[1] + 2 * (aRx[row + 1][col + 1] + aRy[row + 1][col + 1] + 1) : infD; aDiff[2] = (aNcels[2] < infD) ? aNcels[2] + 2 * aRy[row + 1][col] + 1 : infD; aDiff[3] = (aNcels[3] < infD) ? aNcels[3] + 2 * (aRx[row + 1][col - 1] + aRy[row + 1][col - 1] + 1) : infD; // find neighbouring cell with minimum distance difference int minDiff = aDiff[0]; int minDiffCell = 0; for (int i = 1; i < 4; i++) { if (aDiff[i] < minDiff) { minDiff = aDiff[i]; minDiffCell = i; } } // if a neighbouring cell with known distance smaller than current known distance was found: if (minDiff < val) { // assign the minimum euclidean distance aSqDist[row][col] = minDiff; // update the (rX, rY) cell-to-nearest-target vector switch (minDiffCell) { case 0: // E aRx[row][col] = aRx[row][col + 1] + 1; aRy[row][col] = aRy[row][col + 1]; break; case 1: // SE aRx[row][col] = aRx[row + 1][col + 1] + 1; aRy[row][col] = aRy[row + 1][col + 1] + 1; break; case 2: // S aRx[row][col] = aRx[row + 1][col]; aRy[row][col] = aRy[row + 1][col] + 1; break; case 3: // SW aRx[row][col] = aRx[row + 1][col - 1] + 1; aRy[row][col] = aRy[row + 1][col - 1] + 1; break; } } // end of update (rX, rY) cell-to-nearest-target vector } // Write row to output raster WriteOutputRow(outRaster, row, aSqDist[row]); // Report progress int percent = (int)(row / (double)numRows * 100f); if (percent > lastUpdate) { lastUpdate += 1; cancelProgressHandler.Progress( string.Empty, lastUpdate, TextStrings.Pass2 + lastUpdate + TextStrings.progresscompleted); if (cancelProgressHandler.Cancel) { return(false); } } } } // ******************************************************************* // end of second pass proximity calculation loop // ******************************************************************* // save output raster output.Save(); return(true); }
/// <summary> /// Executes the ReSample Opaeration tool programaticaly /// Ping deleted the static property for external testing. /// </summary> /// <param name="input1">The input raster.</param> /// <param name="newCellHeight">The size of the cell's hight.</param> /// <param name="newCellWidth">The size of the cell's width.</param> /// <param name="output">The output raster.</param> /// <param name="cancelProgressHandler">The progress handler.</param> /// <returns>Boolean, true if the method was successful.</returns> public bool Execute( IRaster input1, double newCellHeight, double newCellWidth, IRaster output, ICancelProgressHandler cancelProgressHandler) { // Validates the input and output data if (input1 == null || newCellWidth == 0 || output == null) { return(false); } Extent envelope = input1.Bounds.Extent; // Calculate new number of columns and rows int noOfCol = Convert.ToInt32(Math.Abs(envelope.Width / newCellWidth)); int noOfRow = Convert.ToInt32(Math.Abs(envelope.Height / newCellHeight)); int previous = 0; // ************OLD Method ////Create the new raster with the appropriate dimensions // Raster Temp = new Raster(); ////Temp.CreateNew(output.Filename, noOfRow, noOfCol, input1.DataType); // Temp.CreateNew(output.Filename, "", noOfCol, noOfRow, 1, input1.DataType, new string[] { "" }); // Temp.CellWidth = newCellSize; // Temp.CellHeight = oldCellSize; // Temp.Xllcenter = input1.Bounds.Envelope.Minimum.X + (Temp.CellWidth / 2); // Temp.Yllcenter = input1.Bounds.Envelope.Minimum.Y + (Temp.CellHeight / 2); // *************** // create output raster output = Raster.CreateRaster( output.Filename, string.Empty, noOfCol, noOfRow, 1, input1.DataType, new[] { string.Empty }); RasterBounds bound = new RasterBounds(noOfRow, noOfCol, envelope); output.Bounds = bound; output.NoDataValue = input1.NoDataValue; RcIndex index1; // Loop throug every cell for new value int max = output.Bounds.NumRows + 1; for (int i = 0; i < output.Bounds.NumRows; i++) { for (int j = 0; j < output.Bounds.NumColumns; j++) { // Projet the cell position to Map Coordinate cellCenter = output.CellToProj(i, j); index1 = input1.ProjToCell(cellCenter); double val; if (index1.Row <= input1.EndRow && index1.Column <= input1.EndColumn && index1.Row > -1 && index1.Column > -1) { val = input1.Value[index1.Row, index1.Column] == input1.NoDataValue ? output.NoDataValue : input1.Value[index1.Row, index1.Column]; } else { val = output.NoDataValue; } output.Value[i, j] = val; if (cancelProgressHandler.Cancel) { return(false); } } int current = Convert.ToInt32(Math.Round(i * 100D / max)); // only update when increment in persentage if (current > previous) { cancelProgressHandler.Progress(string.Empty, current, current + TextStrings.progresscompleted); } previous = current; } // output = Temp; output.Save(); return(true); }
/// <summary> /// This will resample the cells. /// If the cell size is zero, this will default to the shorter of the width or height /// divided by 256. /// </summary> /// <param name="input1">the input raster.</param> /// <param name="cellHeight">The new cell height or null.</param> /// <param name="cellWidth">The new cell width or null.</param> /// <param name="outputFileName">The string name of the output raster.</param> /// <param name="progressHandler">An interface for handling the progress messages.</param> /// <returns>The resampled raster.</returns> public static IRaster Resample(IRaster input1, double cellHeight, double cellWidth, string outputFileName, IProgressHandler progressHandler) { if (input1 == null) { return(null); } Extent envelope = input1.Bounds.Extent; if (cellHeight == 0) { cellHeight = envelope.Height / 256; } if (cellWidth == 0) { cellWidth = envelope.Width / 256; } // Calculate new number of columns and rows int noOfCol = Convert.ToInt32(Math.Abs(envelope.Width / cellWidth)); int noOfRow = Convert.ToInt32(Math.Abs(envelope.Height / cellHeight)); IRaster output = Raster.CreateRaster(outputFileName, string.Empty, noOfCol, noOfRow, 1, input1.DataType, new[] { string.Empty }); RasterBounds bound = new RasterBounds(noOfRow, noOfCol, envelope); output.Bounds = bound; output.NoDataValue = input1.NoDataValue; int max = output.Bounds.NumRows; ProgressMeter pm = new ProgressMeter(progressHandler, "ReSize Cells", max); // Loop through every cell for new value for (int i = 0; i < max; i++) { for (int j = 0; j < output.Bounds.NumColumns; j++) { // Project the cell position to Map Coordinate cellCenter = output.CellToProj(i, j); var index1 = input1.ProjToCell(cellCenter); double val; if (index1.Row <= input1.EndRow && index1.Column <= input1.EndColumn && index1.Row > -1 && index1.Column > -1) { val = input1.Value[index1.Row, index1.Column] == input1.NoDataValue ? output.NoDataValue : input1.Value[index1.Row, index1.Column]; } else { val = output.NoDataValue; } output.Value[i, j] = val; } pm.CurrentValue = i; } output.Save(); pm.Reset(); return(output); }