Search in sources :

Example 26 with SpatialMetadata

use of org.geotoolkit.image.io.metadata.SpatialMetadata in project geotoolkit by Geomatys.

the class TextMatrixImageReader method createMetadata.

/**
 * Returns metadata associated with the given image.
 * Calling this method may force loading of full image.
 *
 * @param  imageIndex The image index.
 * @return The metadata, or {@code null} if none.
 * @throws IOException If an error occurs reading the data information from the input source.
 */
@Override
protected SpatialMetadata createMetadata(final int imageIndex) throws IOException {
    if (imageIndex >= 0) {
        if (data == null || !completed) {
            if (load(imageIndex, true)) {
                return null;
            }
        }
        final float padValue = (float) getPadValue(imageIndex);
        float minimum = Float.POSITIVE_INFINITY;
        float maximum = Float.NEGATIVE_INFINITY;
        for (int i = 0; i < data.length; i++) {
            final float value = data[i];
            if (value != padValue) {
                if (value < minimum)
                    minimum = value;
                if (value > maximum)
                    maximum = value;
            }
        }
        final SpatialMetadata metadata = new SpatialMetadata(false, this, null);
        final DimensionAccessor accessor = new DimensionAccessor(metadata);
        accessor.selectChild(accessor.appendChild());
        if (minimum < maximum) {
            accessor.setValueRange(minimum, maximum);
        }
        if (!Float.isNaN(padValue)) {
            accessor.setFillSampleValues(padValue);
        }
        return metadata;
    }
    return super.createMetadata(imageIndex);
}
Also used : SpatialMetadata(org.geotoolkit.image.io.metadata.SpatialMetadata) DimensionAccessor(org.geotoolkit.internal.image.io.DimensionAccessor) Point(java.awt.Point)

Example 27 with SpatialMetadata

use of org.geotoolkit.image.io.metadata.SpatialMetadata in project geotoolkit by Geomatys.

the class TextRecordImageReader method createMetadata.

/**
 * Returns metadata associated with the given image.
 * Calling this method may force loading of full image.
 *
 * @param  imageIndex The image index.
 * @return The metadata, or {@code null} if none.
 * @throws IOException If an error occurs reading the data information from the input source.
 */
@Override
protected SpatialMetadata createMetadata(final int imageIndex) throws IOException {
    if (imageIndex < 0) {
        // Stream metadata.
        return null;
    }
    final SpatialMetadata metadata = new SpatialMetadata(false, this, null);
    /*
         * Computes the smallest bounding box containing the full image in user coordinates.
         * This implementation searches for minimum and maximum values in x and y columns as
         * returned by getColumnX() and getColumnY(). Reminder: xmax and ymax are INCLUSIVE
         * in the code below, as well as (width-1) and (height-1).
         */
    final TextRecordList records = getRecords(imageIndex, false);
    final int xColumn = records.xColumn;
    final int yColumn = records.yColumn;
    final int width = records.getPointCount(xColumn);
    final int height = records.getPointCount(yColumn);
    final double xmin = records.getMinimum(xColumn);
    final double ymin = records.getMinimum(yColumn);
    final double xmax = records.getMaximum(xColumn);
    final double ymax = records.getMaximum(yColumn);
    final double padValue = getPadValue(imageIndex);
    final GridDomainAccessor domain = new GridDomainAccessor(metadata);
    // Note: the swapping of ymax and ymin below is intentional,
    // since values on the y axis are increasing downward.
    domain.setAll(xmin, ymax, xmax, ymin, width, height, true, null);
    /*
         * Now adds the valid range of sample values for each band.
         */
    final DimensionAccessor dimensions = new DimensionAccessor(metadata);
    final int numBands = records.getNumBands();
    for (int band = 0; band < numBands; band++) {
        final int column = records.getColumnForBand(band);
        dimensions.selectChild(dimensions.appendChild());
        dimensions.setValueRange(records.getMinimum(column), records.getMaximum(column));
        dimensions.setFillSampleValues(padValue);
    }
    return metadata;
}
Also used : SpatialMetadata(org.geotoolkit.image.io.metadata.SpatialMetadata) GridDomainAccessor(org.geotoolkit.internal.image.io.GridDomainAccessor) DimensionAccessor(org.geotoolkit.internal.image.io.DimensionAccessor)

Example 28 with SpatialMetadata

use of org.geotoolkit.image.io.metadata.SpatialMetadata in project geotoolkit by Geomatys.

the class WorldFileImageWriter method writeImageMetadata.

/**
 * Invoked by the {@code write} methods when image metadata needs to be written.
 * The default implementation writes the <cite>World File</cite> if an affine
 * transform can be build from the {@linkplain RectifiedGrid rectified grid domain}.
 */
@Override
protected void writeImageMetadata(final IIOMetadata metadata, final int imageIndex, final ImageWriteParam param) throws IOException {
    if (imageIndex != 0) {
        throw new IIOException(Errors.getResources(locale).getString(Errors.Keys.IndexOutOfBounds_1, imageIndex));
    }
    if (metadata instanceof SpatialMetadata) {
        final SpatialMetadata md = (SpatialMetadata) metadata;
        final RectifiedGrid rf = md.getInstanceForType(RectifiedGrid.class);
        if (rf != null) {
            final MetadataHelper mh = new MetadataHelper(md);
            final AffineTransform tr = mh.getAffineTransform(rf, param);
            final Object path = createOutput("tfw");
            if (path != null) {
                try (OutputStream out = IOUtilities.openWrite(path)) {
                    SupportFiles.writeTFW(out, tr);
                }
            }
        }
        /*
             * Write the CRS if non-null and not an instance of ImageCRS. The ImageCRS case is
             * excluded because it is the default CRS assigned by WorldFileImageReader when no
             * ".prj" file were found.
             */
        final CoordinateReferenceSystem crs = md.getInstanceForType(CoordinateReferenceSystem.class);
        if (crs != null && !(crs instanceof ImageCRS)) {
            final Object path = createOutput("prj");
            if (path != null) {
                try (OutputStream out = IOUtilities.openWrite(path)) {
                    PrjFiles.write(crs, out);
                }
            }
        }
    }
}
Also used : MetadataHelper(org.geotoolkit.image.io.metadata.MetadataHelper) SpatialMetadata(org.geotoolkit.image.io.metadata.SpatialMetadata) ImageCRS(org.opengis.referencing.crs.ImageCRS) RectifiedGrid(org.opengis.coverage.grid.RectifiedGrid) OutputStream(java.io.OutputStream) AffineTransform(java.awt.geom.AffineTransform) IIOException(javax.imageio.IIOException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 29 with SpatialMetadata

use of org.geotoolkit.image.io.metadata.SpatialMetadata in project geotoolkit by Geomatys.

the class TextMatrixImageReaderTest method testMetadata.

/**
 * Tests the metadata of the {@link "matrix.txt"} file.
 *
 * @throws IOException if an error occurred while reading the file.
 */
@Test
public void testMetadata() throws IOException {
    prepareImageReader(true);
    assertEquals(20, reader.getWidth(0));
    assertEquals(42, reader.getHeight(0));
    assertNull(reader.getStreamMetadata());
    final SpatialMetadata metadata = (SpatialMetadata) reader.getImageMetadata(0);
    assertNotNull(metadata);
    assertMultilinesEquals(decodeQuotes(GEOTK_FORMAT_NAME + '\n' + "└───ImageDescription\n" + "    └───Dimensions\n" + "        └───Dimension\n" + "            ├───minValue=“-1.893”\n" + "            ├───maxValue=“31.14”\n" + "            └───fillSampleValues=“-9999.0”\n"), metadata.toString());
}
Also used : SpatialMetadata(org.geotoolkit.image.io.metadata.SpatialMetadata)

Aggregations

SpatialMetadata (org.geotoolkit.image.io.metadata.SpatialMetadata)29 DimensionAccessor (org.geotoolkit.internal.image.io.DimensionAccessor)9 IOException (java.io.IOException)6 GridDomainAccessor (org.geotoolkit.internal.image.io.GridDomainAccessor)6 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)5 IIOMetadata (javax.imageio.metadata.IIOMetadata)4 SampleDimension (org.apache.sis.coverage.SampleDimension)4 SpatialImageReader (org.geotoolkit.image.io.SpatialImageReader)4 ReferencingBuilder (org.geotoolkit.image.io.metadata.ReferencingBuilder)4 RectifiedGrid (org.opengis.coverage.grid.RectifiedGrid)4 FactoryException (org.opengis.util.FactoryException)4 AffineTransform (java.awt.geom.AffineTransform)3 ImageReader (javax.imageio.ImageReader)3 MetadataHelper (org.geotoolkit.image.io.metadata.MetadataHelper)3 RenderedImage (java.awt.image.RenderedImage)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2