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