/**
   * Return the row and column of the active region matrix for a give coordinate *
   *
   * <p>NOTE: basically the inverse of {@link
   * JGrassUtilities#rowColToCenterCoordinates(JGrassRegion, int, int)}
   *
   * @param active the active region
   * @param coord
   * @return and int array containing row and col
   */
  public static int[] coordinateToNearestRowCol(JGrassRegion active, Coordinate coord) {

    double easting = coord.x;
    double northing = coord.y;
    int[] rowcol = new int[2];
    if (easting > active.getEast()
        || easting < active.getWest()
        || northing > active.getNorth()
        || northing < active.getSouth()) {
      return null;
    }

    double minx = active.getWest();
    double ewres = active.getWEResolution();
    for (int i = 0; i < active.getCols(); i++) {
      minx = minx + ewres;
      if (easting < minx) {
        rowcol[1] = i;
        break;
      }
    }

    double maxy = active.getNorth();
    double nsres = active.getNSResolution();
    for (int i = 0; i < active.getRows(); i++) {
      maxy = maxy - nsres;
      if (northing > maxy) {
        rowcol[0] = i;
        break;
      }
    }

    return rowcol;
  }
  public void write(IIOMetadata streamMetadata, IIOImage image, ImageWriteParam param)
      throws IOException {

    hasListeners =
        (this.progressListeners != null && (!(this.progressListeners.isEmpty()))) ? true : false;

    if (hasListeners) {
      clearAbortRequest();
      // Broadcast the start of the image write operation
      processImageStarted(0);
    }

    RenderedImage renderedImage = image.getRenderedImage();
    // PlanarImage inputRenderedImage = PlanarImage.wrapRenderedImage(renderedImage);

    // Raster data = renderedImage.getData();
    // final RectIter iterator = RectIterFactory.create(data, null);

    // writing
    noDataValue = rasterWriter.getNoData();
    writeRegion.setCols(renderedImage.getWidth());
    writeRegion.setRows(renderedImage.getHeight());
    int nColumns = writeRegion.getCols();
    int nRows = writeRegion.getRows();
    double west = writeRegion.getWest();
    double south = writeRegion.getSouth();
    double cellsizeX = writeRegion.getWEResolution();
    double cellsizeY = writeRegion.getNSResolution();
    rasterWriter.writeRaster(
        renderedImage, nColumns, nRows, west, south, cellsizeX, cellsizeY, noDataValue);

    if (hasListeners) {
      // Checking the status of the write operation (aborted/completed)
      if (rasterWriter.isAborting()) processWriteAborted();
      else processImageComplete();
    }
    // rasterWriter.close();
  }