use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.
the class CoverageImageTest method coverageReaderTest.
/**
* Test between output image from renderer and source image within {@link GridCoverageReader}.
*
* @throws PortrayalException
* @throws DataStoreException
* @throws IOException
*/
@Test
public void coverageReaderTest() throws PortrayalException, DataStoreException, IOException {
ImageIO.scanForPlugins();
Setup.initialize(null);
final File input = new File("src/test/resources/org/geotoolkit/display2d/clouds.jpg");
final ImageCoverageReader reader = CoverageIO.createSimpleReader(input);
final BufferedImage img = ImageIO.read(input);
final GridCoverage gridcov = reader.read(null);
proportionalityCoefficient = 2;
final MapLayer cl = MapBuilder.createCoverageLayer(input);
// Envelope result
resEnv = gridcov.getGridGeometry().getEnvelope();
srcWidth = img.getWidth();
srcHeight = img.getHeight();
testImageLayer(img, cl);
}
use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.
the class FileCoverageResource method getSampleDimensions.
@Override
public synchronized List<SampleDimension> getSampleDimensions() throws DataStoreException {
if (cachedSampleDimensions == null) {
final ImageCoverageReader reader = acquireReader();
try {
// Reader are allowed to return null when dimensions are unknown
List<SampleDimension> sd = reader.getSampleDimensions();
if (sd == null) {
final GridCoverageReadParam param = new GridCoverageReadParam();
GeneralEnvelope envelope = new GeneralEnvelope(reader.getGridGeometry().getEnvelope());
for (int i = 0, n = envelope.getDimension(); i < n; i++) {
double min = envelope.getMinimum(i);
double span = envelope.getSpan(i);
envelope.setRange(i, min, min + span / 100.0);
}
param.setEnvelope(reader.getGridGeometry().getEnvelope());
param.setDeferred(true);
final GridCoverage coverage = reader.read(param);
final RenderedImage img = coverage.render(null);
final SampleModel sampleModel = img.getSampleModel();
final int numBands = sampleModel.getNumBands();
final NumberRange<?> range = TypeMap.getRange(TypeMap.getSampleDimensionType(sampleModel, 0));
sd = new ArrayList<>();
for (int i = 0; i < numBands; i++) {
sd.add(new SampleDimension.Builder().setName(i).addQuantitative("data", range, (MathTransform1D) MathTransforms.linear(1.0, 0.0), Units.UNITY).build());
}
}
cachedSampleDimensions = Collections.unmodifiableList(sd);
} finally {
try {
reader.dispose();
} catch (DataStoreException ex) {
LOGGER.log(Level.WARNING, ex.getMessage(), ex);
}
}
}
return cachedSampleDimensions;
}
use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.
the class CoverageReaderDemo method main.
public static void main(String[] args) throws Exception {
Demos.init();
// Create a temp file to extract data from jar file.
final Path tempData = Files.createTempFile("tempCvg", ".grb");
try (InputStream stream = CoverageReaderDemo.class.getResourceAsStream("/data/grib/Atlantic.wave.grb")) {
IOUtilities.writeStream(stream, tempData);
}
/*
* DEFERRED READING
*/
final GridCoverageReadParam readParam = new GridCoverageReadParam();
// Here is the parameter which tells the reader to perform lazy loading.
readParam.setDeferred(true);
final ImageCoverageReader reader = CoverageIO.createSimpleReader(tempData);
final GridCoverage coverage = reader.read(readParam);
// Ok, so how to use it now ?
// You can get pixel values directly.
coverage.evaluator().apply(new GeneralDirectPosition(-100, 10, 0));
// But in most cases ...
// ... You will acquire iterator for fast and safe browsing.
final RenderedImage cvgData = coverage.render(null);
final PixelIterator pxIterator = PixelIterator.create(cvgData);
// What should you avoid to do with deferred reading ?
// Don't asked for the entire Raster of the image, it load all image data in memory.
cvgData.getData();
// Do not close your coverage reader before you've ended using your coverage, it would close connexion to the source,
// and tile loading will return you an error.
reader.dispose();
try {
cvgData.getTile(cvgData.getMinTileX(), cvgData.getMinTileY());
} catch (Exception e) {
e.printStackTrace();
}
// final File input = new File(CoverageReaderDemo.class.getResource("/data/coverage/clouds.jpg").toURI());
// final GridCoverageReader reader = CoverageIO.createSimpleReader(input);
// //print the iso 19115 metadata
// final Metadata metadata = reader.getMetadata();
// System.out.println(metadata);
//
// //read a piece of coverage
// final GridCoverageReadParam param = new GridCoverageReadParam();
// param.setResolution(1,1);
// param.setEnvelope(new Rectangle2D.Double(0, 0, 100, 100), CommonCRS.WGS84.normalizedGeographic());
//
// final GridCoverage2D coverage = (GridCoverage2D) reader.read(0, param);
// coverage.show();
/*
* USING THE RENDERER.
*/
// create a mapcontext
final MapLayers context = MapBuilder.createContext();
final MapLayer cl = MapBuilder.createCoverageLayer(tempData);
context.getComponents().add(cl);
// display it
// FXMapFrame.show(context);
}
use of org.geotoolkit.coverage.io.ImageCoverageReader 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);
}
}
}
}
use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.
the class GeoTiffRWTest method test.
private void test(final String fileName, final CoordinateReferenceSystem crs, final AffineTransform gridToCRS) throws IOException, DataStoreException {
File file = TestData.file(GeoTiffRWTest.class, fileName);
ImageCoverageReader reader = new ImageCoverageReader();
reader.setInput(file);
try {
// first test
GridCoverage coverage = reader.read(null);
compare(coverage, crs, gridToCRS);
// write it and test again
file = write(coverage, reader.getCoverageMetadata());
reader.dispose();
final ImageReader imgReader = new TiffImageReader(new TiffImageReader.Spi());
imgReader.setInput(file);
reader = (ImageCoverageReader) CoverageIO.createSimpleReader(imgReader);
// second test
coverage = reader.read(null);
compare(coverage, crs, gridToCRS);
} finally {
reader.dispose();
}
}
Aggregations