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);
}
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;
}
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);
}
}
}
}
}
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());
}
Aggregations