示例#1
0
  public void testCropRectangle() throws Exception {
    final Envelope rasterEnvelope = geoRasterSrc.getMetadata().getEnvelope();
    geoRasterSrc.save(tmpData + "1.tif");
    final int buffer = (int) (rasterEnvelope.getWidth() / 2.3);
    final Rectangle2D cropRectangle =
        new Rectangle2D.Double(
            rasterEnvelope.getMinX() + buffer,
            rasterEnvelope.getMinY() + buffer,
            rasterEnvelope.getWidth() - 2 * buffer,
            rasterEnvelope.getHeight() - 2 * buffer);
    geoRasterDst = geoRasterSrc.doOperation(new Crop(cropRectangle));
    geoRasterDst.save(tmpData + "2.tif");

    assertTrue(geoRasterDst.getWidth() > 0);
    assertTrue(geoRasterDst.getHeight() > 0);

    final ImagePlus srcImagePlus = geoRasterSrc.getImagePlus();
    final ImagePlus dstImagePlus = geoRasterDst.getImagePlus();
    RasterMetadata dstMetadata = geoRasterDst.getMetadata();
    RasterMetadata srcMetadata = geoRasterSrc.getMetadata();
    assertTrue(dstMetadata.getEnvelope().getMinX() < cropRectangle.getMinX());
    assertTrue(dstMetadata.getEnvelope().getMinY() < cropRectangle.getMinY());
    assertTrue(dstMetadata.getEnvelope().getMaxX() > cropRectangle.getMaxX());
    assertTrue(dstMetadata.getEnvelope().getMaxY() > cropRectangle.getMaxY());
    assertTrue(dstMetadata.getEnvelope().getWidth() < srcMetadata.getEnvelope().getWidth());
    checkCrop(geoRasterDst.getMetadata().getEnvelope(), srcImagePlus, dstImagePlus);
  }
示例#2
0
  @Test
  public void testGrid2Grid() throws Exception {
    GeoRaster gr = GeoRasterFactory.createGeoRaster(otherData + "sample.asc");
    gr.open();
    final RasterMetadata originalMetadata = gr.getMetadata();
    final float[] pixels = gr.getFloatPixels();

    final File file2 = new File(tmpData + "1.asc");
    gr.save(file2.getAbsolutePath());
    gr = GeoRasterFactory.createGeoRaster(file2.getAbsolutePath());
    gr.open();

    final float[] gridPixels = gr.getFloatPixels();
    assertTrue(gridPixels.length == pixels.length);
    equals(pixels, gridPixels);
    final RasterMetadata newM = gr.getMetadata();

    assertTrue(newM.getEnvelope().equals(originalMetadata.getEnvelope()));
    assertTrue(newM.getNCols() == originalMetadata.getNCols());
    assertTrue(newM.getNRows() == originalMetadata.getNRows());
    assertTrue(newM.getPixelSize_X() == originalMetadata.getPixelSize_X());
    assertTrue(newM.getPixelSize_Y() == originalMetadata.getPixelSize_Y());
    assertTrue(newM.getRotation_X() == originalMetadata.getRotation_X());
    assertTrue(newM.getRotation_Y() == originalMetadata.getRotation_Y());
    assertTrue(newM.getXulcorner() == originalMetadata.getXulcorner());
    assertTrue(newM.getYulcorner() == originalMetadata.getYulcorner());
  }
示例#3
0
  private GeoRaster checkRasterMetadataIO(GeoRaster grSource) {
    Value v = ValueFactory.createValue(grSource);
    Value v2 = ValueFactory.createValue(v.getType(), v.getBytes());
    GeoRaster gr = v2.getAsRaster();
    assertEquals(gr.getMetadata(), grSource.getMetadata());

    return gr;
  }
示例#4
0
  private void checkCrop(
      final Envelope envelope, final ImagePlus srcPixelProvider, final ImagePlus dstPixelProvider)
      throws IOException {
    // check metadata
    final RasterMetadata dstMetadata = geoRasterDst.getMetadata();
    final float pixelSize_X = dstMetadata.getPixelSize_X();
    final float pixelSize_Y = dstMetadata.getPixelSize_Y();
    final float halfPixelSize_X = dstMetadata.getPixelSize_X() / 2;
    final float halfPixelSize_Y = Math.abs(dstMetadata.getPixelSize_Y()) / 2;
    final int ncols = dstMetadata.getNCols();
    final int nrows = dstMetadata.getNRows();
    final double xulcorner = dstMetadata.getXulcorner();
    final double yulcorner = dstMetadata.getYulcorner();

    assertTrue(pixelSize_X == geoRasterDst.getMetadata().getPixelSize_X());
    assertTrue(ncols * pixelSize_X == dstMetadata.getEnvelope().getWidth());
    assertTrue(
        xulcorner - (pixelSize_X / 2) + ncols * pixelSize_X == dstMetadata.getEnvelope().getMaxX());

    assertTrue(pixelSize_Y == geoRasterDst.getMetadata().getPixelSize_Y());
    assertTrue(nrows * Math.abs(pixelSize_Y) == dstMetadata.getEnvelope().getHeight());
    assertTrue(
        yulcorner - (pixelSize_Y / 2) - nrows * Math.abs(pixelSize_Y)
            == dstMetadata.getEnvelope().getMinY());

    // check raster values
    for (double y = envelope.getMinY() + halfPixelSize_Y; y < envelope.getMaxY(); y = y + 1) {
      for (double x = envelope.getMinX() + halfPixelSize_X; x < envelope.getMaxX(); x = x + 1) {
        final Point2D srcPixel = geoRasterSrc.fromRealWorldToPixel(x, y);
        final Point2D dstPixel = geoRasterDst.fromRealWorldToPixel(x, y);
        final float srcPixelValue =
            srcPixelProvider
                .getProcessor()
                .getPixelValue((int) srcPixel.getX(), (int) srcPixel.getY());
        final float dstPixelValue =
            dstPixelProvider
                .getProcessor()
                .getPixelValue((int) dstPixel.getX(), (int) dstPixel.getY());
        if (Float.isNaN(srcPixelValue)) {
          assertTrue(Float.isNaN(dstPixelValue));
        } else {
          assertTrue("pixel[" + x + ", " + y + "]", srcPixelValue == dstPixelValue);
        }
      }
    }
  }
示例#5
0
  public void testCropAll() throws Exception {
    final Envelope rasterEnvelope = geoRasterSrc.getMetadata().getEnvelope();
    final Rectangle2D cropRectangle =
        new Rectangle2D.Double(
            rasterEnvelope.getMinX(),
            rasterEnvelope.getMinY(),
            rasterEnvelope.getWidth(),
            rasterEnvelope.getHeight());
    geoRasterDst = geoRasterSrc.doOperation(new Crop(cropRectangle));

    assertTrue(geoRasterDst.getWidth() > 0);
    assertTrue(geoRasterDst.getHeight() > 0);

    RasterMetadata dstMetadata = geoRasterDst.getMetadata();
    RasterMetadata srcMetadata = geoRasterSrc.getMetadata();
    assertTrue(dstMetadata.equals(srcMetadata));
  }
示例#6
0
  public void testCropPolygon() throws Exception {
    Envelope rasterEnvelope = geoRasterSrc.getMetadata().getEnvelope();
    final int bufferSize = (int) (rasterEnvelope.getWidth() / 2.3);
    rasterEnvelope =
        new Envelope(
            new Coordinate(
                rasterEnvelope.getMinX() + bufferSize, rasterEnvelope.getMinY() + bufferSize),
            new Coordinate(
                rasterEnvelope.getMaxX() - bufferSize, rasterEnvelope.getMaxY() - bufferSize));
    final LinearRing polygon = (LinearRing) EnvelopeUtil.toGeometry(rasterEnvelope);
    geoRasterSrc.save(tmpData + "1.tif");
    geoRasterDst = geoRasterSrc.doOperation(new Crop(polygon));
    geoRasterDst.save(tmpData + "2.png");

    assertTrue(geoRasterDst.getWidth() > 0);
    assertTrue(geoRasterDst.getHeight() > 0);

    final ImagePlus srcImagePlus = geoRasterSrc.getImagePlus();
    final ImagePlus dstImagePlus = geoRasterDst.getImagePlus();
    checkCrop(geoRasterDst.getMetadata().getEnvelope(), srcImagePlus, dstImagePlus);
  }
示例#7
0
  public void testCropLeHavre() throws Exception {
    String src = externalData + "geotif/littlelehavre.tif";
    GeoRaster geoRaster = GeoRasterFactory.createGeoRaster(src);
    geoRaster.open();
    RasterMetadata metadata = geoRaster.getMetadata();
    Crop crop =
        new Crop(
            new Rectangle2D.Double(metadata.getXulcorner(), metadata.getYulcorner(), 10, -110));
    // Just test it doesn't throw any exception
    geoRaster.doOperation(crop);

    assertTrue(true);
  }