org.geotools.coverage.grid.GridEnvelope2D Java Examples

The following examples show how to use org.geotools.coverage.grid.GridEnvelope2D. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Get the array of region parameters covered by the {@link GridCoverage2D coverage}. 
 * 
 * @param gridCoverage the coverage.
 * @return the array of region parameters as [n, s, w, e, xres, yres, cols, rows]
 */
public static double[] getRegionArrayFromGridCoverage( GridCoverage2D gridCoverage ) {
    Envelope envelope = gridCoverage.getEnvelope();
    DirectPosition lowerCorner = envelope.getLowerCorner();
    double[] westSouth = lowerCorner.getCoordinate();
    DirectPosition upperCorner = envelope.getUpperCorner();
    double[] eastNorth = upperCorner.getCoordinate();

    GridGeometry2D gridGeometry = gridCoverage.getGridGeometry();
    GridEnvelope2D gridRange = gridGeometry.getGridRange2D();
    int height = gridRange.height;
    int width = gridRange.width;

    AffineTransform gridToCRS = (AffineTransform) gridGeometry.getGridToCRS();
    double xRes = XAffineTransform.getScaleX0(gridToCRS);
    double yRes = XAffineTransform.getScaleY0(gridToCRS);

    double[] params = new double[]{eastNorth[1], westSouth[1], westSouth[0], eastNorth[0], xRes, yRes, width, height};

    return params;
}
 
Example #2
Source File: RasterReader.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates the rgb image symbol.
 *
 * @param sym the sym
 * @param cov the cov
 * @param raster the raster
 */
private void createRGBImageSymbol(
        RasterSymbolizer sym, GridCoverage2D cov, WritableRaster raster) {
    double dest;
    List<Double> valueList = new ArrayList<>();

    GridEnvelope2D gridRange2D = cov.getGridGeometry().getGridRange2D();
    for (int x = 0; x < gridRange2D.getWidth(); x++) {
        for (int y = 0; y < gridRange2D.getHeight(); y++) {
            try {
                dest = raster.getSampleDouble(x, y, 0);

                if (!valueList.contains(dest)) {
                    valueList.add(dest);
                }
            } catch (Exception e) {
                ConsoleManager.getInstance().exception(this, e);
            }
        }
    }

    ColorMapImpl colourMap = new ColorMapImpl();

    // Sort the unique sample values in ascending order
    Collections.sort(valueList);

    // Create colour amp entries in the colour map for all the sample values
    for (Double value : valueList) {
        ColorMapEntry entry = new ColorMapEntryImpl();
        Literal colourExpression =
                ff.literal(ColourUtils.fromColour(ColourUtils.createRandomColour()));
        entry.setColor(colourExpression);
        entry.setQuantity(ff.literal(value.doubleValue()));

        colourMap.addColorMapEntry(entry);
    }

    colourMap.setType(ColorMap.TYPE_VALUES);
    sym.setColorMap(colourMap);
}
 
Example #3
Source File: Raster.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public GridGeometry2D getGridGeometry() {
    if (gridGeometry == null) {
        Envelope envelope = new Envelope2D(crs, west, south, east - west, north - south);
        GridEnvelope2D gridRange = new GridEnvelope2D(0, 0, cols, rows);
        gridGeometry = new GridGeometry2D(gridRange, envelope);
    }
    return gridGeometry;
}
 
Example #4
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the parameters of the region covered by the {@link GridCoverage2D coverage}. 
 * 
 * @param gridCoverage the coverage.
 * @return the {@link HashMap map} of parameters. ( {@link #NORTH} and the 
 *          other static vars can be used to retrieve them.
 */
public static RegionMap getRegionParamsFromGridCoverage( GridCoverage2D gridCoverage ) {
    RegionMap envelopeParams = new RegionMap();

    Envelope envelope = gridCoverage.getEnvelope();

    DirectPosition lowerCorner = envelope.getLowerCorner();
    double[] westSouth = lowerCorner.getCoordinate();
    DirectPosition upperCorner = envelope.getUpperCorner();
    double[] eastNorth = upperCorner.getCoordinate();

    GridGeometry2D gridGeometry = gridCoverage.getGridGeometry();
    GridEnvelope2D gridRange = gridGeometry.getGridRange2D();
    int height = gridRange.height;
    int width = gridRange.width;

    AffineTransform gridToCRS = (AffineTransform) gridGeometry.getGridToCRS();
    double xRes = XAffineTransform.getScaleX0(gridToCRS);
    double yRes = XAffineTransform.getScaleY0(gridToCRS);

    envelopeParams.put(NORTH, eastNorth[1]);
    envelopeParams.put(SOUTH, westSouth[1]);
    envelopeParams.put(WEST, westSouth[0]);
    envelopeParams.put(EAST, eastNorth[0]);
    envelopeParams.put(XRES, xRes);
    envelopeParams.put(YRES, yRes);
    envelopeParams.put(ROWS, (double) height);
    envelopeParams.put(COLS, (double) width);

    return envelopeParams;
}
 
Example #5
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the parameters of the region covered by the {@link ImageMosaicReader}. 
 * 
 * @param reader the ImageMosaicReader.
 * @return the {@link HashMap map} of parameters. ( {@link #NORTH} and the 
 *          other static vars can be used to retrieve them.
 */
public static RegionMap getRegionParamsFromImageMosaicReader( ImageMosaicReader reader ) throws IOException {
    RegionMap envelopeParams = new RegionMap();

    Envelope envelope = reader.getOriginalEnvelope();

    DirectPosition lowerCorner = envelope.getLowerCorner();
    double[] westSouth = lowerCorner.getCoordinate();
    DirectPosition upperCorner = envelope.getUpperCorner();
    double[] eastNorth = upperCorner.getCoordinate();

    GridEnvelope2D gridRange = (GridEnvelope2D) reader.getOriginalGridRange();
    int height = gridRange.height;
    int width = gridRange.width;
    double[][] resolutionLevels = reader.getResolutionLevels();
    double xRes = resolutionLevels[0][0];
    double yRes = resolutionLevels[0][1];

    envelopeParams.put(NORTH, eastNorth[1]);
    envelopeParams.put(SOUTH, westSouth[1]);
    envelopeParams.put(WEST, westSouth[0]);
    envelopeParams.put(EAST, eastNorth[0]);
    envelopeParams.put(XRES, xRes);
    envelopeParams.put(YRES, yRes);
    envelopeParams.put(ROWS, (double) height);
    envelopeParams.put(COLS, (double) width);

    return envelopeParams;
}
 
Example #6
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the array of rows and cols. 
 * 
 * @param gridCoverage the coverage.
 * @return the array as [cols, rows]
 */
public static int[] getRegionColsRows( GridCoverage2D gridCoverage ) {
    GridGeometry2D gridGeometry = gridCoverage.getGridGeometry();
    GridEnvelope2D gridRange = gridGeometry.getGridRange2D();
    int height = gridRange.height;
    int width = gridRange.width;
    int[] params = new int[]{width, height};
    return params;
}
 
Example #7
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the cols and rows ranges to use to loop the original gridcoverage.
 * 
 * @param gridCoverage the coverage.
 * @param subregion the sub region of the coverage to get the cols and rows to loop on.
 * @return the array of looping values in the form [minCol, maxCol, minRow, maxRow].
 * @throws Exception
 */
public static int[] getLoopColsRowsForSubregion( GridCoverage2D gridCoverage, Envelope2D subregion ) throws Exception {
    GridGeometry2D gridGeometry = gridCoverage.getGridGeometry();
    GridEnvelope2D subRegionGrid = gridGeometry.worldToGrid(subregion);
    int minCol = subRegionGrid.x;
    int maxCol = subRegionGrid.x + subRegionGrid.width;
    int minRow = subRegionGrid.y;
    int maxRow = subRegionGrid.y + subRegionGrid.height;
    return new int[]{minCol, maxCol, minRow, maxRow};
}
 
Example #8
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public static RegionMap gridGeometry2RegionParamsMap( GridGeometry2D gridGeometry ) {
    RegionMap envelopeParams = new RegionMap();

    Envelope envelope = gridGeometry.getEnvelope2D();
    DirectPosition lowerCorner = envelope.getLowerCorner();
    double[] westSouth = lowerCorner.getCoordinate();
    DirectPosition upperCorner = envelope.getUpperCorner();
    double[] eastNorth = upperCorner.getCoordinate();

    GridEnvelope2D gridRange = gridGeometry.getGridRange2D();
    int height = gridRange.height;
    int width = gridRange.width;

    AffineTransform gridToCRS = (AffineTransform) gridGeometry.getGridToCRS();
    double xRes = XAffineTransform.getScaleX0(gridToCRS);
    double yRes = XAffineTransform.getScaleY0(gridToCRS);

    envelopeParams.put(NORTH, eastNorth[1]);
    envelopeParams.put(SOUTH, westSouth[1]);
    envelopeParams.put(WEST, westSouth[0]);
    envelopeParams.put(EAST, eastNorth[0]);
    envelopeParams.put(XRES, xRes);
    envelopeParams.put(YRES, yRes);
    envelopeParams.put(ROWS, (double) height);
    envelopeParams.put(COLS, (double) width);

    return envelopeParams;
}
 
Example #9
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public static GridGeometry2D gridGeometryFromRegionValues( double north, double south, double east, double west, int cols,
        int rows, CoordinateReferenceSystem crs ) {
    Envelope envelope = new Envelope2D(crs, west, south, east - west, north - south);
    GridEnvelope2D gridRange = new GridEnvelope2D(0, 0, cols, rows);
    GridGeometry2D gridGeometry2D = new GridGeometry2D(gridRange, envelope);
    return gridGeometry2D;
}
 
Example #10
Source File: CoverageUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Calculates the profile of a raster map between given {@link Coordinate coordinates}.
 * 
 * <p>Note that novalues and points outside of the given raster region are 
 * added to the list with a {@link HMConstants#doubleNovalue novalue} elevation.
 * 
 * @param mapIter the {@link RandomIter map iterator}.
 * @param gridGeometry the gridgeometry of the map.
 * @param coordinates the {@link Coordinate}s to create the profile on.
 * @return the list of {@link ProfilePoint}s.
 * @throws TransformException
 */
public static List<ProfilePoint> doProfile( RandomIter mapIter, GridGeometry2D gridGeometry, Coordinate... coordinates )
        throws TransformException {
    List<ProfilePoint> profilePointsList = new ArrayList<ProfilePoint>();

    GridEnvelope2D gridRange = gridGeometry.getGridRange2D();
    int rows = gridRange.height;
    int cols = gridRange.width;
    AffineTransform gridToCRS = (AffineTransform) gridGeometry.getGridToCRS();
    double xres = XAffineTransform.getScaleX0(gridToCRS);
    double yres = XAffineTransform.getScaleY0(gridToCRS);

    double step = Math.min(xres, yres);

    LineString line = GeometryUtilities.gf().createLineString(coordinates);
    double lineLength = line.getLength();
    LengthIndexedLine indexedLine = new LengthIndexedLine(line);

    double progressive = 0.0;
    GridCoordinates2D gridCoords;
    while( progressive < lineLength + step ) { // run over by a step to make sure we get the
                                               // last coord back from the extractor
        Coordinate c = indexedLine.extractPoint(progressive);
        gridCoords = gridGeometry.worldToGrid(new DirectPosition2D(c.x, c.y));
        double value = HMConstants.doubleNovalue;
        if (// envelope2d.contains(c.x, c.y) &&
        isInside(cols - 1, rows - 1, gridCoords)) {
            value = mapIter.getSampleDouble(gridCoords.x, gridCoords.y, 0);
        }
        ProfilePoint profilePoint = new ProfilePoint(progressive, value, c.x, c.y);
        profilePointsList.add(profilePoint);
        progressive = progressive + step;
    }
    return profilePointsList;
}
 
Example #11
Source File: PrintUtilities.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public static String getRegionPrint( GridCoverage2D coverage ) {
    org.opengis.geometry.Envelope envelope = coverage.getEnvelope();

    DirectPosition lowerCorner = envelope.getLowerCorner();
    double[] westSouth = lowerCorner.getCoordinate();
    DirectPosition upperCorner = envelope.getUpperCorner();
    double[] eastNorth = upperCorner.getCoordinate();

    GridGeometry2D gridGeometry = coverage.getGridGeometry();
    GridEnvelope2D gridRange = gridGeometry.getGridRange2D();

    String numberSpace = "                ";
    String numberRest = "                  ";

    StringBuilder sb = new StringBuilder();
    sb.append("        +----------------------   ").append(String.format("%17.8f", eastNorth[1]))
            .append("   ----------------------+").append("\n");
    sb.append("        +                         ").append(String.format("%17.8f", gridRange.getMinY()))
            .append("                         +").append("\n");
    sb.append("        +                         ").append(numberSpace).append("                          +").append("\n");
    sb.append("        +                         ").append(numberSpace).append("                          +").append("\n");
    sb.append(String.format("%17.8f", westSouth[0])).append(numberRest).append(numberSpace).append(numberRest)
            .append(String.format("%17.8f", eastNorth[0])).append("\n");
    sb.append(String.format("%17.8f", gridRange.getMinX())).append(numberRest).append(numberSpace).append(numberRest)
            .append(String.format("%17.8f", gridRange.getMaxX())).append("\n");
    sb.append("        +                         ").append(numberSpace).append("                          +").append("\n");
    sb.append("        +                         ").append(numberSpace).append("                          +").append("\n");
    sb.append("        +                         ").append(String.format("%17.8f", gridRange.getMaxY()))
            .append("                         +").append("\n");
    sb.append("        +----------------------   ").append(String.format("%17.8f", westSouth[1]))
            .append("   ----------------------+").append("\n");

    return sb.toString();
}
 
Example #12
Source File: GeoWaveRasterReader.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public GridEnvelope getOriginalGridRange(final String coverageName) {
  try (CloseableIterator<InternalDataStatistics<?, ?, ?>> statisticsIt =
      geowaveStatisticsStore.getDataStatistics(
          getAdapterId(coverageName),
          RasterBoundingBoxStatistics.STATS_TYPE,
          authorizationSPI.getAuthorizations())) {

    int width = 0;
    int height = 0;
    // try to use both the bounding box and the overview statistics to
    // determine the width and height at the highest resolution
    InternalDataStatistics<?, ?, ?> statistics = null;
    if (statisticsIt.hasNext()) {
      statistics = statisticsIt.next();
    }
    if ((statistics != null) && (statistics instanceof BoundingBoxDataStatistics)) {
      final BoundingBoxDataStatistics<?, ?> bboxStats =
          (BoundingBoxDataStatistics<?, ?>) statistics;
      try (CloseableIterator<InternalDataStatistics<?, ?, ?>> overviewStatisticsIt =
          geowaveStatisticsStore.getDataStatistics(
              getAdapterId(coverageName),
              OverviewStatistics.STATS_TYPE,
              authorizationSPI.getAuthorizations())) {
        statistics = null;
        if (overviewStatisticsIt.hasNext()) {
          statistics = overviewStatisticsIt.next();
        }
        if ((statistics != null) && (statistics instanceof OverviewStatistics)) {
          final OverviewStatistics overviewStats = (OverviewStatistics) statistics;
          width =
              (int) Math.ceil(
                  ((bboxStats.getMaxX() - bboxStats.getMinX())
                      / overviewStats.getResolutions()[0].getResolution(0)));
          height =
              (int) Math.ceil(
                  ((bboxStats.getMaxY() - bboxStats.getMinY())
                      / overviewStats.getResolutions()[0].getResolution(1)));
        }
      }
    }

    return new GridEnvelope2D(0, 0, width, height);
  }
}