use of org.apache.sis.portrayal.MapLayers 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.apache.sis.portrayal.MapLayers in project geotoolkit by Geomatys.
the class CoverageStoreDemo method main.
public static void main(String[] args) throws Exception {
Demos.init();
WorldFileImageReader.Spi.registerDefaults(null);
WorldFileImageWriter.Spi.registerDefaults(null);
Path dataResources = IOUtilities.getResourceAsPath("data");
final Parameters params = Parameters.castOrWrap(FileCoverageProvider.PARAMETERS_DESCRIPTOR.createValue());
params.getOrCreate(FileCoverageProvider.PATH).setValue(dataResources.toUri());
params.getOrCreate(FileCoverageProvider.TYPE).setValue("jpg-wf");
final DataStore store = DataStores.open(params);
// create a mapcontext
final MapLayers context = MapBuilder.createContext();
for (Resource ref : DataStores.flatten(store, true, GridCoverageResource.class)) {
final MapLayer cl = MapBuilder.createCoverageLayer(ref);
context.getComponents().add(cl);
}
// display it
// FXMapFrame.show(context);
}
use of org.apache.sis.portrayal.MapLayers in project geotoolkit by Geomatys.
the class JasperReportServiceTest method testPDF.
@Test
public void testPDF() throws JRException, IOException, DataStoreException {
final MapLayers context = createContext();
final URL template = JasperReportServiceTest.class.getResource("/report/MapReport.jrxml");
final Entry<JasperReport, FeatureType> entry = JasperReportService.prepareTemplate(template);
final JasperReport report = entry.getKey();
final FeatureType type = entry.getValue();
List<Feature> collection = new ArrayList<>();
Feature feature = type.newInstance();
CanvasDef cdef = new CanvasDef(new Dimension(1, 1), context.getEnvelope().get());
cdef.setBackground(Color.RED);
feature.setPropertyValue("map", new MapDef(cdef, new SceneDef(context), null));
collection.add(feature);
final File tempfile = File.createTempFile("report", ".pdf");
tempfile.deleteOnExit();
OutputDef output = new OutputDef(JasperReportService.MIME_PDF, tempfile);
JasperReportService.generateReport(report, collection, null, output);
// just test it's not empty
assertTrue(tempfile.length() > 1000);
}
use of org.apache.sis.portrayal.MapLayers in project geotoolkit by Geomatys.
the class DefaultPortrayalService method present.
// //////////////////////////////////////////////////////////////////////////
// PRESENTING A CONTEXT ////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////
/**
* Generate presentation objects for a scene.
* @param canvasDef
* @param sceneDef
* @return stream of Presentation instance.
*/
public static Stream<Presentation> present(final CanvasDef canvasDef, final SceneDef sceneDef) throws PortrayalException {
Stream<Presentation> stream = Stream.empty();
final Envelope contextEnv = canvasDef.getEnvelope();
final CoordinateReferenceSystem crs = contextEnv.getCoordinateReferenceSystem();
final Dimension dim = canvasDef.getDimension();
final BufferedImage img = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);
final J2DCanvasBuffered canvas = new J2DCanvasBuffered(crs, canvasDef.getDimension(), sceneDef.getHints());
prepareCanvas(canvas, canvasDef, sceneDef);
final RenderingContext2D renderContext = canvas.prepareContext(img.createGraphics());
final MapLayers context = sceneDef.getContext();
final List<MapLayer> layers = MapBuilder.getLayers(context);
for (MapLayer layer : layers) {
if (!layer.isVisible())
continue;
final Resource resource = layer.getData();
stream = Stream.concat(stream, present(layer, resource, renderContext));
}
return stream;
}
use of org.apache.sis.portrayal.MapLayers in project geotoolkit by Geomatys.
the class DefaultPortrayalService method prepareCanvas.
public static void prepareCanvas(final J2DCanvas canvas, final CanvasDef canvasDef, final SceneDef sceneDef) throws PortrayalException {
final ContextContainer2D renderer = new ContextContainer2D(canvas);
canvas.setContainer(renderer);
final Color bgColor = canvasDef.getBackground();
if (bgColor != null) {
canvas.setBackgroundPainter(new SolidColorPainter(bgColor));
}
final CanvasMonitor monitor = canvasDef.getMonitor();
if (monitor != null) {
canvas.setMonitor(monitor);
}
final Hints hints = sceneDef.getHints();
if (hints != null) {
for (Entry<?, ?> entry : hints.entrySet()) {
canvas.setRenderingHint((Key) entry.getKey(), entry.getValue());
}
}
final MapLayers context = sceneDef.getContext();
renderer.setContext(context);
GridGeometry gridGeometry = canvasDef.getGridGeometry();
if (gridGeometry != null) {
try {
canvas.setGridGeometry(gridGeometry);
} catch (FactoryException ex) {
throw new PortrayalException("Could not set objective crs", ex);
}
} else {
final Envelope contextEnv = canvasDef.getEnvelope();
final CoordinateReferenceSystem crs = contextEnv.getCoordinateReferenceSystem();
try {
canvas.setObjectiveCRS(crs);
} catch (TransformException | FactoryException ex) {
throw new PortrayalException("Could not set objective crs", ex);
}
// we specifically say to not repect X/Y proportions
canvas.setAxisProportions(!canvasDef.isStretchImage());
// setVisibleArea -> setAxisRange -> setRange.
if (contextEnv != null) {
try {
canvas.setGridGeometry(canvasDef.getOrCreateGridGeometry());
} catch (Exception e) {
// Rollback to previous behavior
try {
canvas.setVisibleArea(contextEnv);
if (canvasDef.getAzimuth() != 0) {
canvas.rotate(-Math.toRadians(canvasDef.getAzimuth()));
}
} catch (NoninvertibleTransformException | TransformException ex) {
ex.addSuppressed(e);
throw new PortrayalException(ex);
}
}
}
}
// paints all extensions
final List<PortrayalExtension> extensions = sceneDef.extensions();
if (extensions != null) {
for (final PortrayalExtension extension : extensions) {
if (extension != null)
extension.completeCanvas(canvas);
}
}
}
Aggregations