Search in sources :

Example 6 with ImageCoverageReader

use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.

the class WPSConvertersUtils method convertToWMSComplex.

public static ComplexData convertToWMSComplex(String wpsVersion, Object object, String mimeType, String encoding, String schema, Map<String, Object> params) throws UnconvertibleObjectException {
    ArgumentChecks.ensureNonNull("Object", object);
    WPSIO.checkSupportedFormat(object.getClass(), WPSIO.IOType.INPUT, mimeType, encoding, schema);
    final Map<String, Object> parameters = new HashMap<>();
    parameters.put(WPSObjectConverter.TMP_DIR_PATH, params.get(OUT_STORAGE_DIR));
    parameters.put(WPSObjectConverter.TMP_DIR_URL, params.get(OUT_STORAGE_URL));
    parameters.put(WPSObjectConverter.ENCODING, encoding);
    parameters.put(WPSObjectConverter.MIME, mimeType);
    parameters.put(WPSObjectConverter.SCHEMA, schema);
    parameters.put(WPSObjectConverter.WPSVERSION, wpsVersion);
    parameters.put(WPSObjectConverter.GMLVERSION, GML_VERSION.get(wpsVersion));
    parameters.put(WPSObjectConverter.JOB_ID, params.get(CURRENT_JOB_ID));
    final ComplexData complex = new ComplexData(Collections.singletonList(new Format(encoding, mimeType, schema, null)));
    final Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put("url", (String) params.get(WMS_INSTANCE_URL));
    jsonMap.put("type", "WMS");
    jsonMap.put("version", "1.3.0");
    final String layerName = params.get(WMS_LAYER_NAME) + "_" + System.currentTimeMillis();
    jsonMap.put("title", layerName);
    jsonMap.put("layers", layerName);
    final Path coverageFile = Paths.get((String) params.get(WMS_STORAGE_DIR), layerName + ".tiff");
    try {
        // Set the envelope crs to 4326 because of client request :
        final CoordinateReferenceSystem outCRS = CommonCRS.WGS84.geographic();
        Envelope env = null;
        Integer crsCode = null;
        if (object instanceof GridCoverage) {
            final GridCoverage coverage = (GridCoverage) object;
            CoverageIO.write(coverage, "GEOTIFF", coverageFile);
            env = Envelopes.transform(coverage.getGridGeometry().getEnvelope(), outCRS);
            crsCode = IdentifiedObjects.lookupEPSG(coverage.getCoordinateReferenceSystem());
        } else if (object instanceof File || object instanceof Path) {
            final Path objPath = (object instanceof File) ? ((File) object).toPath() : (Path) object;
            final ImageCoverageReader reader = CoverageIO.createSimpleReader(objPath);
            env = Envelopes.transform(reader.getGridGeometry().getEnvelope(), outCRS);
            crsCode = IdentifiedObjects.lookupEPSG(reader.getGridGeometry().getCoordinateReferenceSystem());
            IOUtilities.copy(objPath, coverageFile, StandardCopyOption.REPLACE_EXISTING);
        } else if (object instanceof ImageCoverageReader) {
            final ImageCoverageReader reader = (ImageCoverageReader) object;
            env = Envelopes.transform(reader.getGridGeometry().getEnvelope(), outCRS);
            crsCode = IdentifiedObjects.lookupEPSG(reader.getGridGeometry().getCoordinateReferenceSystem());
            Object in = reader.getInput();
            if (in == null) {
                throw new IOException("Input coverage is invalid.");
            } else if (in instanceof File || in instanceof Path) {
                final Path inPath = (in instanceof File) ? ((File) in).toPath() : (Path) in;
                IOUtilities.copy(inPath, coverageFile, StandardCopyOption.REPLACE_EXISTING);
            } else if (in instanceof InputStream) {
                IOUtilities.writeStream((InputStream) in, coverageFile);
            } else {
                throw new IOException("Input coverage is invalid.");
            }
        }
        if (crsCode == null) {
            crsCode = 3857;
        }
        final double xMin = env.getLowerCorner().getOrdinate(0);
        final double xMax = env.getUpperCorner().getOrdinate(0);
        final double yMin = env.getLowerCorner().getOrdinate(1);
        final double yMax = env.getUpperCorner().getOrdinate(1);
        final Map<String, String> bboxMap = new HashMap<>();
        bboxMap.put("bounds", xMin + "," + yMin + "," + xMax + "," + yMax);
        bboxMap.put("crs", "EPSG:4326");
        jsonMap.put("bbox", bboxMap);
        jsonMap.put("srs", "EPSG:" + crsCode);
        final String json = new ObjectMapper().writeValueAsString(jsonMap);
        complex.getContent().add(json);
        return complex;
    } catch (TransformException e) {
        throw new UnconvertibleObjectException("The geographic envelope of the layer can't be retrieved", e);
    } catch (Exception e) {
        throw new UnconvertibleObjectException(e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) ImageCoverageReader(org.geotoolkit.coverage.io.ImageCoverageReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TransformException(org.opengis.referencing.operation.TransformException) IOException(java.io.IOException) Envelope(org.opengis.geometry.Envelope) URISyntaxException(java.net.URISyntaxException) TransformException(org.opengis.referencing.operation.TransformException) JAXBException(javax.xml.bind.JAXBException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) FactoryException(org.opengis.util.FactoryException) DataStoreException(org.apache.sis.storage.DataStoreException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Format(org.geotoolkit.wps.xml.v200.Format) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) ComplexData(org.geotoolkit.wps.xml.v200.ComplexData) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 7 with ImageCoverageReader

use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.

the class GeoTiffRWTest method testStreamWriting.

// TODO : Activate if the writer manage ImageOutputStream.
@Test
@Ignore
public void testStreamWriting() throws DataStoreException, IOException {
    File file = TestData.file(GeoTiffRWTest.class, "002025_0100_010722_l7_01_utm2.tiff");
    ImageCoverageReader reader = new ImageCoverageReader();
    reader.setInput(file);
    try {
        // first test
        GridCoverage coverage = reader.read(null);
        final File tempFile = File.createTempFile("coverage", ".tiff", tempDir);
        tempFile.deleteOnExit();
        final FileOutputStream stream = new FileOutputStream(tempFile);
        final IIOImage iioimage = new IIOImage(coverage.render(null), null, reader.getCoverageMetadata());
        final ImageWriter writer = ImageIO.getImageWritersByFormatName("geotiff").next();
        writer.setOutput(ImageIO.createImageOutputStream(stream));
        writer.write(null, iioimage, null);
        writer.dispose();
    } finally {
        reader.dispose();
    }
}
Also used : ImageCoverageReader(org.geotoolkit.coverage.io.ImageCoverageReader) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) FileOutputStream(java.io.FileOutputStream) ImageWriter(javax.imageio.ImageWriter) File(java.io.File) IIOImage(javax.imageio.IIOImage) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with ImageCoverageReader

use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.

the class FileCoverageResource method acquireReader.

private ImageCoverageReader acquireReader() throws DataStoreException {
    final ImageCoverageReader reader = new ImageCoverageReader();
    try {
        final ImageReader ioreader = ((FileCoverageStore) store).createReader(file, spi);
        if (spi == null) {
            // format was on AUTO. keep the spi for futur reuse.
            spi = ioreader.getOriginatingProvider();
        }
        reader.setInput(ioreader);
    } catch (IOException ex) {
        throw new DataStoreException(ex.getMessage(), ex);
    }
    return reader;
}
Also used : ImageCoverageReader(org.geotoolkit.coverage.io.ImageCoverageReader) DataStoreException(org.apache.sis.storage.DataStoreException) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader)

Example 9 with ImageCoverageReader

use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.

the class FileCoverageResource method read.

@Override
public GridCoverage read(GridGeometry domain, int... range) throws DataStoreException {
    final ImageCoverageReader reader = acquireReader();
    try {
        final GridCoverageReadParam param = new GridCoverageReadParam();
        if (range != null && range.length > 0) {
            param.setSourceBands(range);
            param.setDestinationBands(IntStream.range(0, range.length).toArray());
        }
        if (domain != null && domain.isDefined(org.apache.sis.coverage.grid.GridGeometry.ENVELOPE)) {
            /*
                 * Modify envelope: when we encounter a slice, use the median value instead of the slice width
                 * to avoid multiple coverage occurence of coverages at envelope border intersections.
                 */
            Envelope envelope = domain.getEnvelope();
            int startDim = 0;
            if (!domain.isDefined(org.apache.sis.coverage.grid.GridGeometry.EXTENT)) {
                domain = new GridGeometry(PixelInCell.CELL_CENTER, getGridGeometry().getGridToCRS(PixelInCell.CELL_CENTER), envelope, GridRoundingMode.ENCLOSING);
            }
            GeneralEnvelope modified = null;
            final GridExtent extent = domain.getExtent();
            for (final SingleCRS part : CRS.getSingleComponents(envelope.getCoordinateReferenceSystem())) {
                final int crsDim = part.getCoordinateSystem().getDimension();
                if (crsDim == 1 && extent.getSize(startDim) == 1) {
                    if (modified == null) {
                        envelope = modified = new GeneralEnvelope(envelope);
                    }
                    double m = modified.getMedian(startDim);
                    modified.setRange(startDim, m, m);
                } else if (crsDim == 3) {
                    // might be 3d geographic/projected crs, see if we can split a vertical axis
                    VerticalCRS vcrs = CRS.getVerticalComponent(part, true);
                    if (vcrs != null) {
                        int idx = AxisDirections.indexOfColinear(part.getCoordinateSystem(), vcrs.getCoordinateSystem());
                        if (idx >= 0) {
                            if (modified == null) {
                                envelope = modified = new GeneralEnvelope(envelope);
                            }
                            try {
                                final int vidx = startDim + idx;
                                final MathTransform gridToCRS = domain.getGridToCRS(PixelInCell.CELL_CENTER);
                                final TransformSeparator ts = new TransformSeparator(gridToCRS);
                                ts.addTargetDimensions(vidx);
                                final MathTransform vtrs = ts.separate();
                                final double[] vcoord = new double[] { extent.getLow(vidx) };
                                vtrs.transform(vcoord, 0, vcoord, 0, 1);
                                final double m = vcoord[0];
                                modified.setRange(startDim + idx, m, m);
                            } catch (TransformException | FactoryException ex) {
                            // we have try, no luck
                            }
                        }
                    }
                }
                startDim += crsDim;
            }
            param.setEnvelope(envelope);
            final double[] resolution = domain.getResolution(true);
            param.setResolution(resolution);
        }
        return reader.read(param);
    } finally {
        reader.dispose();
    }
}
Also used : SingleCRS(org.opengis.referencing.crs.SingleCRS) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) ImageCoverageReader(org.geotoolkit.coverage.io.ImageCoverageReader) GridExtent(org.apache.sis.coverage.grid.GridExtent) MathTransform(org.opengis.referencing.operation.MathTransform) Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) VerticalCRS(org.opengis.referencing.crs.VerticalCRS) GridCoverageReadParam(org.geotoolkit.coverage.io.GridCoverageReadParam) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) TransformSeparator(org.apache.sis.referencing.operation.transform.TransformSeparator)

Example 10 with ImageCoverageReader

use of org.geotoolkit.coverage.io.ImageCoverageReader in project geotoolkit by Geomatys.

the class StringToCoverageReaderConverter method apply.

@Override
public ImageCoverageReader apply(final String s) throws UnconvertibleObjectException {
    if (s == null) {
        throw new UnconvertibleObjectException("Empty Coverage File");
    }
    try {
        String url = new String();
        if (s.startsWith("file:")) {
            url = s;
        } else {
            url = "file:" + s;
        }
        ImageCoverageReader reader = CoverageIO.createSimpleReader(new URL(url));
        if (reader == null) {
            throw new UnconvertibleObjectException("Invalid Coverage File");
        }
        return reader;
    } catch (MalformedURLException ex) {
        throw new UnconvertibleObjectException(ex);
    } catch (DataStoreException ex) {
        throw new UnconvertibleObjectException(ex);
    }
}
Also used : UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) ImageCoverageReader(org.geotoolkit.coverage.io.ImageCoverageReader) MalformedURLException(java.net.MalformedURLException) DataStoreException(org.apache.sis.storage.DataStoreException) URL(java.net.URL)

Aggregations

ImageCoverageReader (org.geotoolkit.coverage.io.ImageCoverageReader)11 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)7 DataStoreException (org.apache.sis.storage.DataStoreException)6 File (java.io.File)4 IOException (java.io.IOException)4 ImageReader (javax.imageio.ImageReader)4 GridCoverageReadParam (org.geotoolkit.coverage.io.GridCoverageReadParam)4 InputStream (java.io.InputStream)3 MalformedURLException (java.net.MalformedURLException)3 Path (java.nio.file.Path)3 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)3 RenderedImage (java.awt.image.RenderedImage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 SampleDimension (org.apache.sis.coverage.SampleDimension)2 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)2 MapLayer (org.apache.sis.portrayal.MapLayer)2 TiffImageReader (org.geotoolkit.image.io.plugin.TiffImageReader)2 Test (org.junit.Test)2 Envelope (org.opengis.geometry.Envelope)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1