@Test @Category(UnitTest.class) public void testCropMultipleBounds() throws Exception { double epsilon = 0.005; Bounds topRowBounds = (ALL_ONES.getBounds(3, 0)).union(ALL_ONES.getBounds(3, 2)); Bounds bottomRowBounds = (ALL_ONES.getBounds(0, 0)).union(ALL_ONES.getBounds(0, 2)); Bounds subsetTopRowBounds = new Bounds(topRowBounds.w + epsilon, topRowBounds.s + epsilon, topRowBounds.e - epsilon, topRowBounds.n - epsilon); Bounds subsetBottomRowBounds = new Bounds(bottomRowBounds.w + epsilon, bottomRowBounds.s + epsilon, bottomRowBounds.e - epsilon, bottomRowBounds.n - epsilon); List<Bounds> userBounds = new ArrayList<>(); userBounds.add(subsetTopRowBounds); userBounds.add(subsetBottomRowBounds); MrsPyramidMetadata croppedMetadata = BoundsCropper.getCroppedMetadata(imageMetadata, userBounds, imageMetadata.getMaxZoomLevel()); Bounds imageBounds = imageMetadata.getBounds(); Bounds croppedBounds = croppedMetadata.getBounds(); Assert.assertTrue(croppedBounds.w == imageBounds.w); Assert.assertTrue(croppedBounds.w > subsetBottomRowBounds.w); Assert.assertTrue(croppedBounds.e == imageBounds.e); Assert.assertTrue(croppedBounds.e < subsetBottomRowBounds.e); Assert.assertTrue(croppedBounds.n == imageBounds.n); Assert.assertTrue(croppedBounds.n < subsetTopRowBounds.n); Assert.assertTrue(croppedBounds.s == imageBounds.s); Assert.assertTrue(croppedBounds.s > subsetBottomRowBounds.s); }
public static MrsPyramidMetadata getCroppedMetadata(MrsPyramidMetadata origMetadata, List<Bounds> userBounds, int zoomLevel) { double minX = Double.POSITIVE_INFINITY, minY = Double.POSITIVE_INFINITY; double maxX = Double.NEGATIVE_INFINITY, maxY = Double.NEGATIVE_INFINITY; for (Bounds bounds : userBounds) { if (bounds.w < minX) { minX = bounds.w; } if (bounds.s < minY) { minY = bounds.s; } if (bounds.e > maxX) { maxX = bounds.e; } if (bounds.n > maxY) { maxY = bounds.n; } } Bounds cropBounds = new Bounds(minX, minY, maxX, maxY); Bounds cropTileBounds = TMSUtils.tileBounds(cropBounds, zoomLevel, origMetadata.getTilesize()); Bounds imageBounds = origMetadata.getBounds(); if (!imageBounds.intersects(cropTileBounds)) { throw new IllegalArgumentException(String.format("Oops - cropped bounds and image bounds " + "do not overlap: cropped bounds=%s, image bounds=%s", cropTileBounds, imageBounds)); } Bounds newImageBounds = new Bounds(Math.max(cropTileBounds.w, imageBounds.w), Math.max(cropTileBounds.s, imageBounds.s), Math.min(cropTileBounds.e, imageBounds.e), Math.min(cropTileBounds.n, imageBounds.n)); TileBounds newTileBounds = TMSUtils.boundsToTile(newImageBounds, zoomLevel, origMetadata.getTilesize()); Pixel lowerPx = TMSUtils.latLonToPixels(newImageBounds.s, newImageBounds.w, zoomLevel, origMetadata.getTilesize()); Pixel upperPx = TMSUtils.latLonToPixels(newImageBounds.n, newImageBounds.e, zoomLevel, origMetadata.getTilesize()); LongRectangle pixelBounds = new LongRectangle(0, 0, upperPx.px - lowerPx.px, upperPx.py - lowerPx.py); MrsPyramidMetadata croppedMetadata = new MrsPyramidMetadata(); croppedMetadata.setPyramid(origMetadata.getPyramid()); croppedMetadata.setBounds(newImageBounds); croppedMetadata.setTilesize(origMetadata.getTilesize()); croppedMetadata.setTileType(origMetadata.getTileType()); croppedMetadata.setBands(1); croppedMetadata.setDefaultValues(origMetadata.getDefaultValues()); croppedMetadata.setMaxZoomLevel(zoomLevel); croppedMetadata.setName(zoomLevel, "" + zoomLevel); croppedMetadata.setTileBounds(zoomLevel, TileBounds.convertToLongRectangle(newTileBounds)); croppedMetadata.setPixelBounds(zoomLevel, pixelBounds); croppedMetadata.setProtectionLevel(origMetadata.getProtectionLevel()); return croppedMetadata; }
BoundsCropper { public static MrsPyramidMetadata getCroppedMetadata(MrsPyramidMetadata origMetadata, List<Bounds> userBounds, int zoomLevel) { double minX = Double.POSITIVE_INFINITY, minY = Double.POSITIVE_INFINITY; double maxX = Double.NEGATIVE_INFINITY, maxY = Double.NEGATIVE_INFINITY; for (Bounds bounds : userBounds) { if (bounds.w < minX) { minX = bounds.w; } if (bounds.s < minY) { minY = bounds.s; } if (bounds.e > maxX) { maxX = bounds.e; } if (bounds.n > maxY) { maxY = bounds.n; } } Bounds cropBounds = new Bounds(minX, minY, maxX, maxY); Bounds cropTileBounds = TMSUtils.tileBounds(cropBounds, zoomLevel, origMetadata.getTilesize()); Bounds imageBounds = origMetadata.getBounds(); if (!imageBounds.intersects(cropTileBounds)) { throw new IllegalArgumentException(String.format("Oops - cropped bounds and image bounds " + "do not overlap: cropped bounds=%s, image bounds=%s", cropTileBounds, imageBounds)); } Bounds newImageBounds = new Bounds(Math.max(cropTileBounds.w, imageBounds.w), Math.max(cropTileBounds.s, imageBounds.s), Math.min(cropTileBounds.e, imageBounds.e), Math.min(cropTileBounds.n, imageBounds.n)); TileBounds newTileBounds = TMSUtils.boundsToTile(newImageBounds, zoomLevel, origMetadata.getTilesize()); Pixel lowerPx = TMSUtils.latLonToPixels(newImageBounds.s, newImageBounds.w, zoomLevel, origMetadata.getTilesize()); Pixel upperPx = TMSUtils.latLonToPixels(newImageBounds.n, newImageBounds.e, zoomLevel, origMetadata.getTilesize()); LongRectangle pixelBounds = new LongRectangle(0, 0, upperPx.px - lowerPx.px, upperPx.py - lowerPx.py); MrsPyramidMetadata croppedMetadata = new MrsPyramidMetadata(); croppedMetadata.setPyramid(origMetadata.getPyramid()); croppedMetadata.setBounds(newImageBounds); croppedMetadata.setTilesize(origMetadata.getTilesize()); croppedMetadata.setTileType(origMetadata.getTileType()); croppedMetadata.setBands(1); croppedMetadata.setDefaultValues(origMetadata.getDefaultValues()); croppedMetadata.setMaxZoomLevel(zoomLevel); croppedMetadata.setName(zoomLevel, "" + zoomLevel); croppedMetadata.setTileBounds(zoomLevel, TileBounds.convertToLongRectangle(newTileBounds)); croppedMetadata.setPixelBounds(zoomLevel, pixelBounds); croppedMetadata.setProtectionLevel(origMetadata.getProtectionLevel()); return croppedMetadata; } }
BoundsCropper { public static MrsPyramidMetadata getCroppedMetadata(MrsPyramidMetadata origMetadata, List<Bounds> userBounds, int zoomLevel) { double minX = Double.POSITIVE_INFINITY, minY = Double.POSITIVE_INFINITY; double maxX = Double.NEGATIVE_INFINITY, maxY = Double.NEGATIVE_INFINITY; for (Bounds bounds : userBounds) { if (bounds.w < minX) { minX = bounds.w; } if (bounds.s < minY) { minY = bounds.s; } if (bounds.e > maxX) { maxX = bounds.e; } if (bounds.n > maxY) { maxY = bounds.n; } } Bounds cropBounds = new Bounds(minX, minY, maxX, maxY); Bounds cropTileBounds = TMSUtils.tileBounds(cropBounds, zoomLevel, origMetadata.getTilesize()); Bounds imageBounds = origMetadata.getBounds(); if (!imageBounds.intersects(cropTileBounds)) { throw new IllegalArgumentException(String.format("Oops - cropped bounds and image bounds " + "do not overlap: cropped bounds=%s, image bounds=%s", cropTileBounds, imageBounds)); } Bounds newImageBounds = new Bounds(Math.max(cropTileBounds.w, imageBounds.w), Math.max(cropTileBounds.s, imageBounds.s), Math.min(cropTileBounds.e, imageBounds.e), Math.min(cropTileBounds.n, imageBounds.n)); TileBounds newTileBounds = TMSUtils.boundsToTile(newImageBounds, zoomLevel, origMetadata.getTilesize()); Pixel lowerPx = TMSUtils.latLonToPixels(newImageBounds.s, newImageBounds.w, zoomLevel, origMetadata.getTilesize()); Pixel upperPx = TMSUtils.latLonToPixels(newImageBounds.n, newImageBounds.e, zoomLevel, origMetadata.getTilesize()); LongRectangle pixelBounds = new LongRectangle(0, 0, upperPx.px - lowerPx.px, upperPx.py - lowerPx.py); MrsPyramidMetadata croppedMetadata = new MrsPyramidMetadata(); croppedMetadata.setPyramid(origMetadata.getPyramid()); croppedMetadata.setBounds(newImageBounds); croppedMetadata.setTilesize(origMetadata.getTilesize()); croppedMetadata.setTileType(origMetadata.getTileType()); croppedMetadata.setBands(1); croppedMetadata.setDefaultValues(origMetadata.getDefaultValues()); croppedMetadata.setMaxZoomLevel(zoomLevel); croppedMetadata.setName(zoomLevel, "" + zoomLevel); croppedMetadata.setTileBounds(zoomLevel, TileBounds.convertToLongRectangle(newTileBounds)); croppedMetadata.setPixelBounds(zoomLevel, pixelBounds); croppedMetadata.setProtectionLevel(origMetadata.getProtectionLevel()); return croppedMetadata; }  }
BoundsCropper { public static MrsPyramidMetadata getCroppedMetadata(MrsPyramidMetadata origMetadata, List<Bounds> userBounds, int zoomLevel) { double minX = Double.POSITIVE_INFINITY, minY = Double.POSITIVE_INFINITY; double maxX = Double.NEGATIVE_INFINITY, maxY = Double.NEGATIVE_INFINITY; for (Bounds bounds : userBounds) { if (bounds.w < minX) { minX = bounds.w; } if (bounds.s < minY) { minY = bounds.s; } if (bounds.e > maxX) { maxX = bounds.e; } if (bounds.n > maxY) { maxY = bounds.n; } } Bounds cropBounds = new Bounds(minX, minY, maxX, maxY); Bounds cropTileBounds = TMSUtils.tileBounds(cropBounds, zoomLevel, origMetadata.getTilesize()); Bounds imageBounds = origMetadata.getBounds(); if (!imageBounds.intersects(cropTileBounds)) { throw new IllegalArgumentException(String.format("Oops - cropped bounds and image bounds " + "do not overlap: cropped bounds=%s, image bounds=%s", cropTileBounds, imageBounds)); } Bounds newImageBounds = new Bounds(Math.max(cropTileBounds.w, imageBounds.w), Math.max(cropTileBounds.s, imageBounds.s), Math.min(cropTileBounds.e, imageBounds.e), Math.min(cropTileBounds.n, imageBounds.n)); TileBounds newTileBounds = TMSUtils.boundsToTile(newImageBounds, zoomLevel, origMetadata.getTilesize()); Pixel lowerPx = TMSUtils.latLonToPixels(newImageBounds.s, newImageBounds.w, zoomLevel, origMetadata.getTilesize()); Pixel upperPx = TMSUtils.latLonToPixels(newImageBounds.n, newImageBounds.e, zoomLevel, origMetadata.getTilesize()); LongRectangle pixelBounds = new LongRectangle(0, 0, upperPx.px - lowerPx.px, upperPx.py - lowerPx.py); MrsPyramidMetadata croppedMetadata = new MrsPyramidMetadata(); croppedMetadata.setPyramid(origMetadata.getPyramid()); croppedMetadata.setBounds(newImageBounds); croppedMetadata.setTilesize(origMetadata.getTilesize()); croppedMetadata.setTileType(origMetadata.getTileType()); croppedMetadata.setBands(1); croppedMetadata.setDefaultValues(origMetadata.getDefaultValues()); croppedMetadata.setMaxZoomLevel(zoomLevel); croppedMetadata.setName(zoomLevel, "" + zoomLevel); croppedMetadata.setTileBounds(zoomLevel, TileBounds.convertToLongRectangle(newTileBounds)); croppedMetadata.setPixelBounds(zoomLevel, pixelBounds); croppedMetadata.setProtectionLevel(origMetadata.getProtectionLevel()); return croppedMetadata; }  static MrsPyramidMetadata getCroppedMetadata(MrsPyramidMetadata origMetadata,
    List<Bounds> userBounds, int zoomLevel); }
BoundsCropper { public static MrsPyramidMetadata getCroppedMetadata(MrsPyramidMetadata origMetadata, List<Bounds> userBounds, int zoomLevel) { double minX = Double.POSITIVE_INFINITY, minY = Double.POSITIVE_INFINITY; double maxX = Double.NEGATIVE_INFINITY, maxY = Double.NEGATIVE_INFINITY; for (Bounds bounds : userBounds) { if (bounds.w < minX) { minX = bounds.w; } if (bounds.s < minY) { minY = bounds.s; } if (bounds.e > maxX) { maxX = bounds.e; } if (bounds.n > maxY) { maxY = bounds.n; } } Bounds cropBounds = new Bounds(minX, minY, maxX, maxY); Bounds cropTileBounds = TMSUtils.tileBounds(cropBounds, zoomLevel, origMetadata.getTilesize()); Bounds imageBounds = origMetadata.getBounds(); if (!imageBounds.intersects(cropTileBounds)) { throw new IllegalArgumentException(String.format("Oops - cropped bounds and image bounds " + "do not overlap: cropped bounds=%s, image bounds=%s", cropTileBounds, imageBounds)); } Bounds newImageBounds = new Bounds(Math.max(cropTileBounds.w, imageBounds.w), Math.max(cropTileBounds.s, imageBounds.s), Math.min(cropTileBounds.e, imageBounds.e), Math.min(cropTileBounds.n, imageBounds.n)); TileBounds newTileBounds = TMSUtils.boundsToTile(newImageBounds, zoomLevel, origMetadata.getTilesize()); Pixel lowerPx = TMSUtils.latLonToPixels(newImageBounds.s, newImageBounds.w, zoomLevel, origMetadata.getTilesize()); Pixel upperPx = TMSUtils.latLonToPixels(newImageBounds.n, newImageBounds.e, zoomLevel, origMetadata.getTilesize()); LongRectangle pixelBounds = new LongRectangle(0, 0, upperPx.px - lowerPx.px, upperPx.py - lowerPx.py); MrsPyramidMetadata croppedMetadata = new MrsPyramidMetadata(); croppedMetadata.setPyramid(origMetadata.getPyramid()); croppedMetadata.setBounds(newImageBounds); croppedMetadata.setTilesize(origMetadata.getTilesize()); croppedMetadata.setTileType(origMetadata.getTileType()); croppedMetadata.setBands(1); croppedMetadata.setDefaultValues(origMetadata.getDefaultValues()); croppedMetadata.setMaxZoomLevel(zoomLevel); croppedMetadata.setName(zoomLevel, "" + zoomLevel); croppedMetadata.setTileBounds(zoomLevel, TileBounds.convertToLongRectangle(newTileBounds)); croppedMetadata.setPixelBounds(zoomLevel, pixelBounds); croppedMetadata.setProtectionLevel(origMetadata.getProtectionLevel()); return croppedMetadata; }  static MrsPyramidMetadata getCroppedMetadata(MrsPyramidMetadata origMetadata,
    List<Bounds> userBounds, int zoomLevel);  }