Search in sources :

Example 11 with SpatialMetadata

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

the class DimapExtension method fillSpatialMetaData.

@Override
public SpatialMetadata fillSpatialMetaData(TiffImageReader reader, SpatialMetadata metadata) throws IOException {
    final Path metadataFile = searchMetadataFile(reader.getInput());
    if (metadataFile == null)
        throw new IOException("Dimap metadata file not found.");
    final Document doc;
    try {
        doc = DomUtilities.read(metadataFile);
    } catch (ParserConfigurationException | SAXException ex) {
        throw new IOException(ex);
    }
    final Element dimapNode = doc.getDocumentElement();
    // add new format to SpatialMetadata
    final SpatialMetadata dimapMetadata = new SpatialMetadata(DimapMetadataFormat.INSTANCE, reader, metadata);
    dimapMetadata.mergeTree(DimapMetadataFormat.NATIVE_FORMAT, new LazyLoadElement(metadataFile));
    boolean geotkFormat = false;
    final String[] formatNames = dimapMetadata.getMetadataFormatNames();
    for (int i = 0; i < formatNames.length; i++) {
        String formatName = formatNames[i];
        if (formatName.equals(SpatialMetadataFormat.GEOTK_FORMAT_NAME)) {
            geotkFormat = true;
        }
    }
    // ensure GEOTK format before override parts of metadata
    if (!geotkFormat) {
        return dimapMetadata;
    }
    try {
        // temporal
        final Date prodDate = DimapAccessor.getImagingDate(dimapNode);
        if (prodDate != null) {
            GeoTiffExtension.setOrCreateSliceDimension(dimapMetadata, CommonCRS.Temporal.JAVA.crs(), prodDate.getTime());
        }
    } catch (FactoryException e) {
        throw new IOException(e.getMessage(), e);
    }
    // add SampleDimensions definition to SpatialMetadata
    addSampleDimensions(dimapMetadata, dimapNode);
    return dimapMetadata;
}
Also used : Path(java.nio.file.Path) SpatialMetadata(org.geotoolkit.image.io.metadata.SpatialMetadata) FactoryException(org.opengis.util.FactoryException) LazyLoadElement(org.geotoolkit.util.dom.LazyLoadElement) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) Date(java.util.Date) SAXException(org.xml.sax.SAXException) LazyLoadElement(org.geotoolkit.util.dom.LazyLoadElement) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 12 with SpatialMetadata

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

the class DimapExtension method writeDimapMetadata.

private void writeDimapMetadata(SpatialMetadata metadata) {
    if (metadata instanceof SpatialMetadata) {
        final SpatialMetadata md = (SpatialMetadata) metadata;
        final int index = Arrays.binarySearch(md.getMetadataFormatNames(), DimapMetadataFormat.NATIVE_FORMAT);
        if (index >= 0) {
            // found some dimap metadatas, write them
            final Node node = md.getAsTree(DimapMetadataFormat.NATIVE_FORMAT);
            // TODO
            final Object output = null;
            try {
                DomUtilities.write((Document) node, output);
            } catch (TransformerException | IOException ex) {
                LOGGER.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
    }
}
Also used : SpatialMetadata(org.geotoolkit.image.io.metadata.SpatialMetadata) Node(org.w3c.dom.Node) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException)

Example 13 with SpatialMetadata

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

the class GeoTiffMetaDataReader method readSpatialMetaData.

/**
 * Read the Spatial Metadatas.
 *
 * @return SpatialMetadata
 * @throws NoSuchAuthorityCodeException
 * @throws FactoryException
 * @throws java.io.IOException
 */
public SpatialMetadata readSpatialMetaData() throws NoSuchAuthorityCodeException, FactoryException, IOException {
    final SpatialMetadata spatialMetadata = new SpatialMetadata(SpatialMetadataFormat.getImageInstance(SpatialMetadataFormat.GEOTK_FORMAT_NAME));
    boolean mustMockCrs = true, mustMockGrid = true;
    if (geoKeyDir != null) {
        try {
            final int[] structure = readTiffShorts(getNodeByLocalName(geoKeyDir, TAG_GEOTIFF_SHORTS));
            // first line (4 int) contain the version and number of keys
            // Header={KeyDirectoryVersion, KeyRevision, MinorRevision, NumberOfKeys}
            final int directoryVersion = structure[0];
            final int keyVersion = structure[1];
            final int minorVersion = structure[2];
            final int nbKeys = structure[3];
            // read all entries
            final ValueMap entries = new ValueMap();
            for (int i = 0, l = 4; i < nbKeys; i++, l += 4) {
                final Object value;
                final int valueKey = structure[l + 0];
                final int valuelocation = structure[l + 1];
                final int valueNb = structure[l + 2];
                final int valueOffset = structure[l + 3];
                if (valuelocation == 0) {
                    // value is located in the offset field
                    value = valueOffset;
                } else {
                    // value is in another tag
                    value = readValue(valuelocation, valueOffset, valueNb);
                }
                entries.put(valueKey, value);
            }
            // create the spatial metadatas.
            try {
                fillGridMetaDatas(spatialMetadata, entries);
                mustMockGrid = false;
            } catch (Exception e) {
                LOGGER.log(Level.WARNING, "Cannot properly fill grid related metadata", e);
            }
            try {
                final GeoTiffCRSReader crsReader = new GeoTiffCRSReader();
                crsReader.fillCRSMetaDatas(spatialMetadata, entries);
                mustMockCrs = false;
            } catch (Exception e) {
                LOGGER.log(Level.WARNING, "Cannot properly convert referencing information into a CRS", e);
            }
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, "Cannot properly read referencing information from headers", e);
        }
    }
    if (mustMockCrs) {
        new ReferencingBuilder(spatialMetadata).setCoordinateReferenceSystem(PredefinedCRS.GRID_2D);
    }
    if (mustMockGrid) {
        GridDomainAccessor gridDomainAccessor = new GridDomainAccessor(spatialMetadata);
        gridDomainAccessor.setAll(AffineTransforms2D.castOrCopy(new Matrix3()), readBounds(), CellGeometry.AREA, PixelOrientation.UPPER_LEFT);
    }
    if (mustMockCrs || mustMockGrid)
        spatialMetadata.clearInstancesCache();
    // -- looks for additional informations
    final ThirdPartyMetaDataReader thirdReader = new ThirdPartyMetaDataReader(imageMetadata);
    thirdReader.fillSpatialMetaData(spatialMetadata);
    return spatialMetadata;
}
Also used : SpatialMetadata(org.geotoolkit.image.io.metadata.SpatialMetadata) ReferencingBuilder(org.geotoolkit.image.io.metadata.ReferencingBuilder) GridDomainAccessor(org.geotoolkit.internal.image.io.GridDomainAccessor) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) IOException(java.io.IOException) NoninvertibleMatrixException(org.apache.sis.referencing.operation.matrix.NoninvertibleMatrixException) FactoryException(org.opengis.util.FactoryException) Matrix3(org.apache.sis.referencing.operation.matrix.Matrix3)

Example 14 with SpatialMetadata

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

the class GeotiffTest method temporalTest.

/**
 * Improve Reader / Writer to write and read date in tiff format.
 *
 * @throws IOException if problem during
 */
@Test
public void temporalTest() throws IOException, FactoryException {
    final RenderedImage testedImg = ImageUtils.createGreyScaledInterleavedImage(2, 2, SampleType.BYTE, 3);
    // -- temporal CRS
    final NamedIdentifier name = new NamedIdentifier(null, "TemporalReferenceSystem");
    final CoordinateReferenceSystem crsSource = new GeodeticObjectBuilder().addName((GenericName) name).createCompoundCRS(CommonCRS.WGS84.geographic(), CommonCRS.Temporal.JAVA.crs());
    final SpatialMetadata sm = new SpatialMetadata(SpatialMetadataFormat.getImageInstance(SpatialMetadataFormat.GEOTK_FORMAT_NAME));
    final ReferencingBuilder builder = new ReferencingBuilder(sm);
    builder.setCoordinateReferenceSystem(crsSource);
    final long time = System.currentTimeMillis();
    final double[] origin = new double[] { -45, -45, time };
    final double[] envMax = new double[] { 45, 45, System.currentTimeMillis() };
    final int[] high = new int[] { 1, 1, 1 };
    final GridDomainAccessor accessor = new GridDomainAccessor(sm);
    accessor.setRectifiedGridDomain(origin, envMax, null, high, null, false);
    accessor.setSpatialRepresentation(origin, envMax, null, PixelOrientation.UPPER_LEFT);
    final File filTest = File.createTempFile("dateTest", "tiff", directory);
    filTest.mkdirs();
    writer.setOutput(filTest);
    final IIOImage img = new IIOImage(testedImg, null, sm);
    writer.write(img);
    writer.dispose();
    reader.setInput(filTest);
    SpatialMetadata readerMeth = reader.getImageMetadata(0);
    reader.dispose();
    final GridDomainAccessor gda = new GridDomainAccessor(readerMeth);
    final double[] outDate = gda.getAttributeAsDoubles("origin", false);
    // -- date truncated at second and time exprimate in milli second
    // -- tolerance at 1000 to avoid milli second from truncature.
    assertEquals(time, outDate[2], 1E3);
}
Also used : SpatialMetadata(org.geotoolkit.image.io.metadata.SpatialMetadata) GridDomainAccessor(org.geotoolkit.internal.image.io.GridDomainAccessor) IIOImage(javax.imageio.IIOImage) GenericName(org.opengis.util.GenericName) ReferencingBuilder(org.geotoolkit.image.io.metadata.ReferencingBuilder) NamedIdentifier(org.apache.sis.referencing.NamedIdentifier) GeodeticObjectBuilder(org.apache.sis.internal.referencing.GeodeticObjectBuilder) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) RenderedImage(java.awt.image.RenderedImage) File(java.io.File) Test(org.junit.Test)

Example 15 with SpatialMetadata

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

the class GeotiffTest method noDataTest.

/**
 * Improve Reader / Writer to write and read multiple noData values.
 * Test noData with 2 values out of sample interval boundary,
 * 2 values on sample boundary interval border, and a value which intersect interval.
 * We expect 5 saved nodata categories and to sample boundaries categories.
 * In our case : nodata : {0, 1, 128, 254, 255}, sample interval : [0 ; 255] (Byte space)
 * Expected 7 categories : [0 ; 0], [1 ; 1], ]1 ; 128[, [128 ; 128], ]128 ; 254[, [254 ; 254], [255 ; 255] in ascending order.
 * @throws java.io.IOException if problem during reading writing action.
 */
@Test
public void noDataTest() throws IOException {
    final RenderedImage testedImg = ImageUtils.createGreyScaledInterleavedImage(2, 2, SampleType.BYTE, 3);
    final IIOMetadataNode root = new IIOMetadataNode(TAG_GEOTIFF_IFD);
    final GeoTiffMetaDataStack stack = new GeoTiffMetaDataStack(root);
    stack.setMinSampleValue(1, 1, 1);
    stack.setMaxSampleValue(254, 254, 254);
    stack.setNoData("0.0");
    stack.setNoData("1.0");
    stack.setNoData("128.0");
    stack.setNoData("254.0");
    stack.setNoData("255.0");
    stack.flush();
    final File filTest = File.createTempFile("multipleNodata", "tiff", directory);
    filTest.mkdirs();
    writer.setOutput(filTest);
    final IIOImage img = new IIOImage(testedImg, null, new IIOTiffMetadata(root));
    writer.write(img);
    writer.dispose();
    reader.setInput(filTest);
    SpatialMetadata sm = reader.getImageMetadata(0);
    reader.dispose();
    final DimensionAccessor dimAccess = new DimensionAccessor(sm);
    final List<SampleDimension> sDim = dimAccess.getSampleDimensions();
    assertEquals("gridSampleDimension number", 3, sDim.size());
    final SampleDimension gsd = sDim.get(0);
    final List<Category> categories = gsd.getCategories();
    assertEquals("categories number", 7, categories.size());
    // -- expected number range array
    // -- [0 ; 0], [1 ; 1], ]1 ; 128[, [128 ; 128], ]128 ; 254[, [254 ; 254], [255 ; 255]
    // -- in category specification all getted value are inclusive
    // -- expected array become :
    final double[] expectedArray = new double[] { // -- 7 categories
    0, // -- 7 categories
    0, 1, 1, 2, 127, 128, 128, 129, 253, 254, 254, 255, 255 };
    final double[] foundArray = new double[14];
    int fAId = 0;
    for (final Category cat : categories) {
        final NumberRange r = cat.getSampleRange();
        foundArray[fAId++] = r.getMinDouble(true);
        foundArray[fAId++] = r.getMaxDouble(true);
    }
    assertArrayEquals(expectedArray, foundArray, 1E-12);
}
Also used : Category(org.apache.sis.coverage.Category) SpatialMetadata(org.geotoolkit.image.io.metadata.SpatialMetadata) SampleDimension(org.apache.sis.coverage.SampleDimension) IIOImage(javax.imageio.IIOImage) NumberRange(org.apache.sis.measure.NumberRange) IIOTiffMetadata(org.geotoolkit.image.io.plugin.IIOTiffMetadata) RenderedImage(java.awt.image.RenderedImage) DimensionAccessor(org.geotoolkit.internal.image.io.DimensionAccessor) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) File(java.io.File) Test(org.junit.Test)

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