use of org.geotoolkit.image.io.metadata.SpatialMetadata in project geotoolkit by Geomatys.
the class ImageReaderAdapter method createMetadata.
/**
* Creates a new stream or image metadata. This method is invoked by the public
* {@link #getStreamMetadata()} and {@link #getImageMetadata(int)} methods. The
* default implementation delegates to the corresponding method of the
* {@linkplain #main} reader. Then there is a choice:
* <p>
* <ul>
* <li>If the metadata is null or already an instance of {@link SpatialMetadata},
* returns it unchanged.</li>
* <li>Otherwise wraps the result in a new {@link SpatialMetadata}, which will
* delegate the request for any metadata format other than
* {@value org.geotoolkit.image.io.metadata.SpatialMetadataFormat#GEOTK_FORMAT_NAME}
* to the wrapped format.</li>
* </ul>
*/
@Override
protected SpatialMetadata createMetadata(final int imageIndex) throws IOException {
if (imageIndex >= 0) {
final IIOMetadata metadata = main.getImageMetadata(imageIndex);
if (metadata != null) {
if (metadata instanceof SpatialMetadata) {
final SpatialMetadata sm = (SpatialMetadata) metadata;
sm.setReadOnly(false);
return sm;
}
return new SpatialMetadata(false, this, metadata);
}
} else {
final IIOMetadata metadata = main.getStreamMetadata();
if (metadata != null) {
if (metadata instanceof SpatialMetadata) {
final SpatialMetadata sm = (SpatialMetadata) metadata;
sm.setReadOnly(false);
return sm;
}
return new SpatialMetadata(true, this, metadata);
}
}
return null;
}
use of org.geotoolkit.image.io.metadata.SpatialMetadata in project geotoolkit by Geomatys.
the class SpatialImageReader method getSpatialMetadata.
/**
* Returns spatial metadata associated with the given image.
* This method performs the following choice:
* <p>
* <ul>
* <li>If the metadata from a previous call were cached for the given index, return those metadata.</li>
* <li>Otherwise if {@link #isIgnoringMetadata()} is {@code true}, return {@code null}.</li>
* <li>Otherwise invoke <code>{@linkplain #createMetadata(int) createMetadata}(imageIndex)</code>
* and cache the result.</li>
* </ul>
*
* @param imageIndex The image index, or -1 for stream metadata.
* @return The spatial metadata, or {@code null} if none.
* @throws IOException if an error occurs during reading.
*/
private SpatialMetadata getSpatialMetadata(final int imageIndex) throws IOException {
/*
* Checks if a cached instance is available.
*/
final int cacheIndex = imageIndex + 1;
if (metadata != null && cacheIndex >= 0 && cacheIndex < metadata.length) {
final SpatialMetadata candidate = metadata[cacheIndex];
if (candidate != null) {
return (candidate != SpatialMetadata.EMPTY) ? candidate : null;
}
}
if (isIgnoringMetadata()) {
return null;
}
/*
* Creates a new instance and cache it.
*/
SpatialMetadata candidate = createMetadata(imageIndex);
if (candidate != null) {
candidate.setReadOnly(true);
}
if (metadata == null) {
metadata = new SpatialMetadata[Math.max(cacheIndex + 1, 4)];
}
if (cacheIndex >= metadata.length) {
metadata = Arrays.copyOf(metadata, Math.max(cacheIndex + 1, metadata.length * 2));
}
metadata[cacheIndex] = (candidate != null) ? candidate : SpatialMetadata.EMPTY;
return candidate;
}
use of org.geotoolkit.image.io.metadata.SpatialMetadata in project geotoolkit by Geomatys.
the class AsciiGridReader method createMetadata.
/**
* Returns metadata associated with the given 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;
}
ensureHeaderRead();
final PixelOrientation po;
if (xCenter) {
po = yCenter ? PixelOrientation.CENTER : PixelOrientation.valueOf("UPPER");
} else {
po = yCenter ? PixelOrientation.valueOf("LEFT") : PixelOrientation.UPPER_LEFT;
// We really want UPPER_LEFT, not LOWER_LEFT in the above condition, because
// we are reverting the direction of the y axis in the computation of origin
// and offset vectors.
}
final double[] origin = new double[] { xll, yll + scaleX * (height - (yCenter ? 1 : 0)) };
final double[] bounds = new double[] { xll + scaleY * (width - (xCenter ? 1 : 0)), yll };
final SpatialMetadata metadata = new SpatialMetadata(false, this, null);
final GridDomainAccessor domain = new GridDomainAccessor(metadata);
domain.setOrigin(origin);
domain.addOffsetVector(scaleX, 0);
domain.addOffsetVector(0, -scaleY);
domain.setLimits(new int[2], new int[] { width - 1, height - 1 });
domain.setSpatialRepresentation(origin, bounds, null, po);
final boolean hasRange = !Double.isInfinite(minValue) && !Double.isInfinite(maxValue);
final boolean hasFill = !Double.isNaN(fillValue);
if (hasRange || hasFill) {
final DimensionAccessor dimensions = new DimensionAccessor(metadata);
dimensions.selectChild(dimensions.appendChild());
if (hasRange)
dimensions.setValueRange(minValue, maxValue);
if (hasFill)
dimensions.setFillSampleValues(fillValue);
}
return metadata;
}
use of org.geotoolkit.image.io.metadata.SpatialMetadata in project geotoolkit by Geomatys.
the class ImageCoverageWriterInspector method assertRectifiedGridEquals.
/**
* Tests that the rectified grid is equals to the given affine transform coefficients.
*
* @since 3.17
*/
public void assertRectifiedGridEquals(final double scaleX, final double scaleY, final double translateX, final double translateY) throws ImageMetadataException {
assertInstanceOf("Expected a spatial metadata", SpatialMetadata.class, metadata);
final MetadataHelper helper = new MetadataHelper(null);
final RectifiedGrid rg = ((SpatialMetadata) metadata).getInstanceForType(RectifiedGrid.class);
final AffineTransform tr = helper.getAffineTransform(rg, null);
assertEquals("shearX", 0, tr.getShearX(), EPS);
assertEquals("shearY", 0, tr.getShearY(), EPS);
assertEquals("scaleX", scaleX, tr.getScaleX(), EPS);
assertEquals("scaleY", scaleY, tr.getScaleY(), EPS);
assertEquals("translateX", translateX, tr.getTranslateX(), EPS);
assertEquals("translateY", translateY, tr.getTranslateY(), EPS);
}
use of org.geotoolkit.image.io.metadata.SpatialMetadata in project geotoolkit by Geomatys.
the class AsciiGridWriter method write.
/**
* Appends a complete image stream containing a single image.
*
* @param streamMetadata The stream metadata (ignored in default implementation).
* @param image The image or raster to be written.
* @param parameters The write parameters, or null if the whole image will be written.
* @throws IOException If an error occurred while writing to the stream.
*/
@Override
public void write(final IIOMetadata streamMetadata, final IIOImage image, final ImageWriteParam parameters) throws IOException {
processImageStarted();
final BufferedWriter out = getWriter(parameters);
final ImageDimension size = computeSize(image, parameters);
/*
* Write the header.
*/
final Map<String, String> header = new LinkedHashMap<>(8);
header.put("NCOLS", String.valueOf(size.width));
header.put("NROWS", String.valueOf(size.height));
final SpatialMetadata metadata = convertImageMetadata(image.getMetadata(), null, parameters);
String fillValue = DEFAULT_FILL;
if (metadata != null) {
fillValue = prepareHeader(metadata, header, parameters);
}
writeHeader(header, out);
/*
* Write the pixel values.
*/
final RectIter iterator = createRectIter(image, parameters);
final int dataType = getSampleModel(image, parameters).getDataType();
final float progressScale = 100f / size.getNumSampleValues();
int numSampleValues = 0, nextProgress = 0;
boolean moreBands = false;
if (!iterator.finishedBands())
do {
if (moreBands) {
// Separate bands by a blank line.
out.write('\n');
}
if (!iterator.finishedLines())
do {
if (numSampleValues >= nextProgress) {
// Informs about progress only every 2000 numbers.
processImageProgress(progressScale * numSampleValues);
nextProgress = numSampleValues + 2000;
}
if (abortRequested()) {
processWriteAborted();
return;
}
char separator = '\n';
if (!iterator.finishedPixels())
do {
final String value;
switch(dataType) {
case DataBuffer.TYPE_DOUBLE:
{
final double v = iterator.getSampleDouble();
value = Double.isNaN(v) ? fillValue : Double.toString(v);
break;
}
case DataBuffer.TYPE_FLOAT:
{
final float v = iterator.getSampleFloat();
value = Float.isNaN(v) ? fillValue : Float.toString(v);
break;
}
default:
{
value = Integer.toString(iterator.getSample());
break;
}
case DataBuffer.TYPE_USHORT:
case DataBuffer.TYPE_BYTE:
{
value = Integer.toString(iterator.getSample() & 0x7FFFFFFF);
break;
}
}
out.write(separator);
out.write(value);
separator = ' ';
} while (!iterator.nextPixelDone());
numSampleValues += size.width;
iterator.startPixels();
} while (!iterator.nextLineDone());
iterator.startLines();
moreBands = true;
} while (!iterator.nextBandDone());
out.write('\n');
out.flush();
processImageComplete();
}
Aggregations