Search in sources :

Example 16 with InMemoryGridCoverageResource

use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource in project geotoolkit by Geomatys.

the class CoverageTileGeneratorTest method fullAreaGenerateTest.

/**
 * Test tile generation produces the expected results.
 */
@Test
public void fullAreaGenerateTest() throws DataStoreException, InterruptedException, IOException, TransformException {
    final BufferedImage image = new BufferedImage(180, 90, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g = image.createGraphics();
    g.setColor(Color.RED);
    g.fillRect(0, 0, 180, 90);
    g.dispose();
    final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
    final GeneralEnvelope dataEnvelope = new GeneralEnvelope(crs);
    dataEnvelope.setRange(0, 0, 180);
    dataEnvelope.setRange(1, 0, 90);
    final GridGeometry gridGeom = new GridGeometry(new GridExtent(180, 90), dataEnvelope, GridOrientation.HOMOTHETY);
    final GridCoverageBuilder gcb = new GridCoverageBuilder();
    gcb.setValues(image);
    gcb.setDomain(gridGeom);
    final GridCoverage coverage = gcb.build();
    final CoverageTileGenerator generator = new CoverageTileGenerator(new InMemoryGridCoverageResource(coverage));
    generator.setCoverageIsHomogeneous(false);
    final InMemoryTiledGridCoverageResource ipr = new InMemoryTiledGridCoverageResource(Names.createLocalName(null, null, "test"));
    final TileMatrixSet tileMatrixSet = (TileMatrixSet) ipr.createTileMatrixSet(TileMatrices.createWorldWGS84Template(4));
    generator.generate(tileMatrixSet, null, null, null);
    compare(generator, tileMatrixSet, null);
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) InMemoryTiledGridCoverageResource(org.geotoolkit.storage.memory.InMemoryTiledGridCoverageResource) TileMatrixSet(org.geotoolkit.storage.multires.TileMatrixSet) GridExtent(org.apache.sis.coverage.grid.GridExtent) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) Test(org.junit.Test)

Example 17 with InMemoryGridCoverageResource

use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource in project geotoolkit by Geomatys.

the class ReferenceToGridCoverageResourceConverter method convert.

/**
 * {@inheritDoc}
 *
 * @return GridCoverageResource.
 */
@Override
public GridCoverageResource convert(final Reference source, final Map<String, Object> params) throws UnconvertibleObjectException {
    final InputStream stream = getInputStreamFromReference(source);
    String encoding = null;
    if (params != null && params.get(ENCODING) != null) {
        encoding = (String) params.get(ENCODING);
    }
    ImageInputStream imageStream = null;
    try {
        // decode form base64 stream
        if (encoding != null && encoding.equals(WPSEncoding.BASE64.getValue())) {
            final String encodedImage = IOUtilities.toString(stream);
            final byte[] byteData = Base64.getDecoder().decode(encodedImage.trim());
            if (byteData != null && byteData.length > 0) {
                try (InputStream is = new ByteArrayInputStream(byteData)) {
                    imageStream = ImageIO.createImageInputStream(is);
                }
            }
        } else {
            imageStream = ImageIO.createImageInputStream(stream);
        }
        if (imageStream != null) {
            final ImageReader reader;
            if (source.getMimeType() != null) {
                reader = XImageIO.getReaderByMIMEType(source.getMimeType(), imageStream, null, null);
            } else {
                reader = XImageIO.getReader(imageStream, null, Boolean.FALSE);
            }
            ImageCoverageReader imgReader = CoverageIO.createSimpleReader(reader);
            GridCoverage cov2d = imgReader.read(new GridCoverageReadParam());
            return new InMemoryGridCoverageResource(cov2d);
        } else {
            throw new UnconvertibleObjectException("Error during image stream acquisition.");
        }
    } catch (MalformedURLException ex) {
        throw new UnconvertibleObjectException("ReferenceType grid coverage invalid input : Malformed url", ex);
    } catch (DataStoreException ex) {
        throw new UnconvertibleObjectException("ReferenceType grid coverage invalid input : Can't read coverage", ex);
    } catch (IOException ex) {
        throw new UnconvertibleObjectException("ReferenceType grid coverage invalid input : IO", ex);
    } finally {
        if (imageStream != null) {
            try {
                imageStream.close();
            } catch (IOException ex) {
                LOGGER.log(Level.WARNING, "Error during release the image stream.", ex);
            }
        }
    }
}
Also used : ImageCoverageReader(org.geotoolkit.coverage.io.ImageCoverageReader) MalformedURLException(java.net.MalformedURLException) DataStoreException(org.apache.sis.storage.DataStoreException) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) ImageInputStream(javax.imageio.stream.ImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) ByteArrayInputStream(java.io.ByteArrayInputStream) GridCoverageReadParam(org.geotoolkit.coverage.io.GridCoverageReadParam) ImageReader(javax.imageio.ImageReader)

Example 18 with InMemoryGridCoverageResource

use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource in project geotoolkit by Geomatys.

the class IsolineTest method test3.

@Test
public void test3() throws Exception {
    final GeneralEnvelope env = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
    env.setRange(0, 0, 3);
    env.setRange(1, 0, 3);
    final GridCoverageBuilder gcb = new GridCoverageBuilder();
    gcb.setDomain(new GridGeometry(new GridExtent(3, 4), PixelInCell.CELL_CORNER, new AffineTransform2D(1, 0, 0, -0.75, 0, 3.0), env.getCoordinateReferenceSystem()));
    gcb.setValues(BufferedImages.toDataBuffer1D(new float[][] { { 10, 10, 20 }, { 10, 10, 20 }, { 10, 15, 10 }, { 10, 15, 10 } }), null);
    gcb.setRanges(new SampleDimension.Builder().setName(0).build());
    final GridCoverage coverage = gcb.build();
    final GridCoverageResource ref = new InMemoryGridCoverageResource(coverage);
    final ProcessDescriptor desc = ProcessFinder.getProcessDescriptor(GeotkProcessingRegistry.NAME, IsolineDescriptor.NAME);
    final ParameterValueGroup procparams = desc.getInputDescriptor().createValue();
    procparams.parameter("inCoverageRef").setValue(ref);
    procparams.parameter("inIntervals").setValue(new double[] { 15 });
    final org.geotoolkit.process.Process process = desc.createProcess(procparams);
    final ParameterValueGroup result = process.call();
    FeatureSet col = (FeatureSet) result.parameter("outFeatureCollection").getValue();
    assertEquals(1, FeatureStoreUtilities.getCount(col, true).longValue());
    final Feature feature = col.features(false).iterator().next();
    final LineString geom = (LineString) feature.getPropertyValue(AttributeConvention.GEOMETRY);
    assertIsolineEquals("LINESTRING (2 2.625, 2 1.875, 2.5 1.5)", geom);
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridExtent(org.apache.sis.coverage.grid.GridExtent) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) SampleDimension(org.apache.sis.coverage.SampleDimension) Feature(org.opengis.feature.Feature) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) LineString(org.locationtech.jts.geom.LineString) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) ProcessDescriptor(org.geotoolkit.process.ProcessDescriptor) FeatureSet(org.apache.sis.storage.FeatureSet) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) Test(org.junit.Test)

Example 19 with InMemoryGridCoverageResource

use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource in project geotoolkit by Geomatys.

the class IsolineTest method test4.

/**
 * A single threshold is detected near a no-data border. The algorithm should consider that not enough data is
 * available to draw a segment.
 */
@Test
public void test4() throws Exception {
    final GeneralEnvelope env = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
    env.setRange(0, 0, 7);
    env.setRange(1, 0, 7);
    final GridCoverageBuilder gcb = new GridCoverageBuilder();
    gcb.setDomain(new GridGeometry(new GridExtent(7, 6), PixelInCell.CELL_CORNER, new AffineTransform2D(1, 0, 0, -1.1666666666666667, 0, 7.0), env.getCoordinateReferenceSystem()));
    gcb.setValues(BufferedImages.toDataBuffer1D(new float[][] { { Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN }, { Float.NaN, 3.164f, 2.91f, 2.78f, 1.03f, -2.086f, Float.NaN }, { Float.NaN, 3.41f, 5.41f, 4.66f, 4.28f, 0.163f, Float.NaN }, { Float.NaN, 3.78f, 0.41f, -0.83f, -0.83f, 0.663f, Float.NaN }, { Float.NaN, -0.58f, -2.83f, -1.21f, -0.83f, 0.038f, Float.NaN }, { Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN } }), null);
    gcb.setRanges(new SampleDimension.Builder().setName(0).build());
    final GridCoverage coverage = gcb.build();
    final GridCoverageResource ref = new InMemoryGridCoverageResource(coverage);
    double[] intervales = { 3.163 };
    final ProcessDescriptor desc = ProcessFinder.getProcessDescriptor(GeotkProcessingRegistry.NAME, IsolineDescriptor.NAME);
    final ParameterValueGroup procparams = desc.getInputDescriptor().createValue();
    procparams.parameter("inCoverageRef").setValue(ref);
    procparams.parameter("inIntervals").setValue(intervales);
    final org.geotoolkit.process.Process process = desc.createProcess(procparams);
    final ParameterValueGroup result = process.call();
    FeatureSet col = (FeatureSet) result.parameter("outFeatureCollection").getValue();
    assertEquals(0, FeatureStoreUtilities.getCount(col, true).longValue());
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridExtent(org.apache.sis.coverage.grid.GridExtent) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) SampleDimension(org.apache.sis.coverage.SampleDimension) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) ProcessDescriptor(org.geotoolkit.process.ProcessDescriptor) FeatureSet(org.apache.sis.storage.FeatureSet) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) Test(org.junit.Test)

Example 20 with InMemoryGridCoverageResource

use of org.geotoolkit.storage.memory.InMemoryGridCoverageResource in project geotoolkit by Geomatys.

the class IsolineTest method testTiledImage.

/**
 * Test isoline creation on a tiled image.
 */
@Test
public void testTiledImage() throws NoSuchIdentifierException, ProcessException, DataStoreException, ParseException {
    /* create a tiled image with 4 tiles
              0 1   2 3
            +-----+-----+
          0 | 0 0 | 0 0 |
          1 | 0 1 | 1 0 |
            +-----+-----+
          2 | 0 1 | 1 0 |
          3 | 0 0 | 0 0 |
            +-----+-----+
            Result geometry is an octogone, a square with cutted edges
        */
    final BufferedImage tile00 = BufferedImages.createImage(2, 2, 1, DataBuffer.TYPE_DOUBLE);
    final BufferedImage tile10 = BufferedImages.createImage(2, 2, 1, DataBuffer.TYPE_DOUBLE);
    final BufferedImage tile01 = BufferedImages.createImage(2, 2, 1, DataBuffer.TYPE_DOUBLE);
    final BufferedImage tile11 = BufferedImages.createImage(2, 2, 1, DataBuffer.TYPE_DOUBLE);
    final WritableRaster raster00 = tile00.getRaster();
    final WritableRaster raster10 = tile10.getRaster().createWritableTranslatedChild(2, 0);
    final WritableRaster raster01 = tile01.getRaster().createWritableTranslatedChild(0, 2);
    final WritableRaster raster11 = tile11.getRaster().createWritableTranslatedChild(2, 2);
    raster00.setSample(1 + 0, 1 + 0, 0, 1);
    raster10.setSample(0 + 2, 1 + 0, 0, 1);
    raster01.setSample(1 + 0, 0 + 2, 0, 1);
    raster11.setSample(0 + 2, 0 + 2, 0, 1);
    final WritableTiledImage img = new WritableTiledImage(null, tile00.getColorModel(), 4, 4, 0, 0, new WritableRaster[] { raster00, raster10, raster01, raster11 });
    final MathTransform gridtoCrs = new AffineTransform2D(1, 0, 0, 1, 0, 0);
    final GridCoverageBuilder gcb = new GridCoverageBuilder();
    gcb.setDomain(new GridGeometry(new GridExtent(4, 4), PixelInCell.CELL_CENTER, gridtoCrs, CommonCRS.WGS84.normalizedGeographic()));
    gcb.setValues(img);
    gcb.setRanges(new SampleDimension.Builder().setName(0).build());
    final GridCoverage coverage = gcb.build();
    final GridCoverageResource ref = new InMemoryGridCoverageResource(coverage);
    final ProcessDescriptor desc = ProcessFinder.getProcessDescriptor(GeotkProcessingRegistry.NAME, IsolineDescriptor.NAME);
    final ParameterValueGroup procparams = desc.getInputDescriptor().createValue();
    procparams.parameter("inCoverageRef").setValue(ref);
    procparams.parameter("inIntervals").setValue(new double[] { 0.5 });
    final org.geotoolkit.process.Process process = desc.createProcess(procparams);
    final ParameterValueGroup result = process.call();
    final FeatureSet col = (FeatureSet) result.parameter("outFeatureCollection").getValue();
    final List<Feature> features = col.features(false).collect(Collectors.toList());
    assertEquals(1L, features.size());
    final LineString geom = (LineString) features.get(0).getPropertyValue(AttributeConvention.GEOMETRY);
    assertIsolineEquals("LINESTRING (1 0.5, 2 0.5, 2.5 1, 2.5 2, 2 2.5, 1 2.5, 0.5 2, 0.5 1, 1 0.5)", geom);
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) GridExtent(org.apache.sis.coverage.grid.GridExtent) MathTransform(org.opengis.referencing.operation.MathTransform) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) SampleDimension(org.apache.sis.coverage.SampleDimension) Feature(org.opengis.feature.Feature) BufferedImage(java.awt.image.BufferedImage) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) LineString(org.locationtech.jts.geom.LineString) WritableRaster(java.awt.image.WritableRaster) WritableTiledImage(org.apache.sis.internal.coverage.j2d.WritableTiledImage) InMemoryGridCoverageResource(org.geotoolkit.storage.memory.InMemoryGridCoverageResource) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) ProcessDescriptor(org.geotoolkit.process.ProcessDescriptor) FeatureSet(org.apache.sis.storage.FeatureSet) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) Test(org.junit.Test)

Aggregations

InMemoryGridCoverageResource (org.geotoolkit.storage.memory.InMemoryGridCoverageResource)38 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)34 Test (org.junit.Test)33 GridExtent (org.apache.sis.coverage.grid.GridExtent)32 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)32 SampleDimension (org.apache.sis.coverage.SampleDimension)28 AffineTransform2D (org.apache.sis.internal.referencing.j2d.AffineTransform2D)27 GridCoverageBuilder (org.apache.sis.coverage.grid.GridCoverageBuilder)26 GridCoverageResource (org.apache.sis.storage.GridCoverageResource)25 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)22 RenderedImage (java.awt.image.RenderedImage)18 BufferedGridCoverage (org.apache.sis.coverage.grid.BufferedGridCoverage)17 WritableRenderedImage (java.awt.image.WritableRenderedImage)16 PixelIterator (org.apache.sis.image.PixelIterator)15 WritablePixelIterator (org.apache.sis.image.WritablePixelIterator)15 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)14 BufferedImage (java.awt.image.BufferedImage)13 Graphics2D (java.awt.Graphics2D)6 GridCoverage2D (org.apache.sis.coverage.grid.GridCoverage2D)6 FeatureSet (org.apache.sis.storage.FeatureSet)6