/**
  * Reads a {@link GridCoverage2D} from the grass raster.
  *
  * @param jGrassMapEnvironment the {@link JGrassMapEnvironment}.
  * @param readRegion the region to read, or <code>null</code>.
  * @return the read coverage.
  * @throws Exception
  */
 public static GridCoverage2D getGridcoverageFromGrassraster(
     JGrassMapEnvironment jGrassMapEnvironment, JGrassRegion readRegion) throws Exception {
   CoordinateReferenceSystem crs = jGrassMapEnvironment.getCoordinateReferenceSystem();
   JGrassRegion jGrassRegion = readRegion;
   if (jGrassRegion == null) jGrassRegion = jGrassMapEnvironment.getActiveRegion();
   GeneralParameterValue[] readParams =
       JGrassCatalogUtilities.createGridGeometryGeneralParameter(
           jGrassRegion.getCols(),
           jGrassRegion.getRows(),
           jGrassRegion.getNorth(),
           jGrassRegion.getSouth(),
           jGrassRegion.getWest(),
           jGrassRegion.getEast(),
           crs);
   AbstractGridFormat format =
       (AbstractGridFormat) new GrassCoverageFormatFactory().createFormat();
   GridCoverageReader reader = format.getReader(jGrassMapEnvironment.getCELL());
   GridCoverage2D mapCoverage = ((GridCoverage2D) reader.read(readParams));
   return mapCoverage;
 }
  public static void writeGridCoverageFromGrassraster(
      File mapFile, JGrassRegion writeRegion, GridCoverage2D grassCoverage) throws Exception {
    JGrassMapEnvironment mapEnvironment = new JGrassMapEnvironment(mapFile);
    GrassCoverageFormat format = new GrassCoverageFormatFactory().createFormat();
    GridCoverageWriter writer = format.getWriter(mapEnvironment.getCELL(), null);

    GeneralParameterValue[] readParams = null;
    if (writeRegion == null) {
      writeRegion = mapEnvironment.getActiveRegion();
    }
    readParams =
        JGrassCatalogUtilities.createGridGeometryGeneralParameter(
            writeRegion.getCols(),
            writeRegion.getRows(),
            writeRegion.getNorth(),
            writeRegion.getSouth(),
            writeRegion.getEast(),
            writeRegion.getWest(),
            mapEnvironment.getCoordinateReferenceSystem());

    writer.write(grassCoverage, readParams);
  }