Search in sources :

Example 41 with MapLayers

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);
}
Also used : Path(java.nio.file.Path) ImageCoverageReader(org.geotoolkit.coverage.io.ImageCoverageReader) GeneralDirectPosition(org.apache.sis.geometry.GeneralDirectPosition) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) PixelIterator(org.apache.sis.image.PixelIterator) InputStream(java.io.InputStream) MapLayer(org.apache.sis.portrayal.MapLayer) GridCoverageReadParam(org.geotoolkit.coverage.io.GridCoverageReadParam) RenderedImage(java.awt.image.RenderedImage) MapLayers(org.apache.sis.portrayal.MapLayers)

Example 42 with MapLayers

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);
}
Also used : Path(java.nio.file.Path) Parameters(org.apache.sis.parameter.Parameters) DataStore(org.apache.sis.storage.DataStore) MapLayer(org.apache.sis.portrayal.MapLayer) Resource(org.apache.sis.storage.Resource) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) MapLayers(org.apache.sis.portrayal.MapLayers)

Example 43 with MapLayers

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);
}
Also used : FeatureType(org.opengis.feature.FeatureType) ArrayList(java.util.ArrayList) JasperReport(net.sf.jasperreports.engine.JasperReport) Dimension(java.awt.Dimension) Feature(org.opengis.feature.Feature) URL(java.net.URL) MapDef(org.geotoolkit.report.graphic.map.MapDef) SceneDef(org.geotoolkit.display2d.service.SceneDef) OutputDef(org.geotoolkit.display2d.service.OutputDef) CanvasDef(org.geotoolkit.display2d.service.CanvasDef) File(java.io.File) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 44 with MapLayers

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;
}
Also used : J2DCanvasBuffered(org.geotoolkit.display2d.canvas.J2DCanvasBuffered) MapLayer(org.apache.sis.portrayal.MapLayer) Resource(org.apache.sis.storage.Resource) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) BandedCoverageResource(org.geotoolkit.storage.coverage.BandedCoverageResource) RenderingContext2D(org.geotoolkit.display2d.canvas.RenderingContext2D) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Dimension(java.awt.Dimension) Presentation(org.apache.sis.internal.map.Presentation) ExceptionPresentation(org.apache.sis.internal.map.ExceptionPresentation) Envelope(org.opengis.geometry.Envelope) BufferedImage(java.awt.image.BufferedImage) MapLayers(org.apache.sis.portrayal.MapLayers)

Example 45 with MapLayers

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);
        }
    }
}
Also used : SolidColorPainter(org.geotoolkit.display2d.canvas.painter.SolidColorPainter) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) Hints(org.geotoolkit.factory.Hints) GO2Hints(org.geotoolkit.display2d.GO2Hints) FactoryException(org.opengis.util.FactoryException) ContextContainer2D(org.geotoolkit.display2d.container.ContextContainer2D) Color(java.awt.Color) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) Envelope(org.opengis.geometry.Envelope) PortrayalException(org.geotoolkit.display.PortrayalException) FactoryException(org.opengis.util.FactoryException) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) DisjointExtentException(org.apache.sis.coverage.grid.DisjointExtentException) NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) ProcessException(org.geotoolkit.process.ProcessException) DataStoreException(org.apache.sis.storage.DataStoreException) CanvasMonitor(org.geotoolkit.display.canvas.control.CanvasMonitor) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) MapLayers(org.apache.sis.portrayal.MapLayers) PortrayalException(org.geotoolkit.display.PortrayalException)

Aggregations

MapLayers (org.apache.sis.portrayal.MapLayers)104 Dimension (java.awt.Dimension)61 BufferedImage (java.awt.image.BufferedImage)57 Test (org.junit.Test)57 MapLayer (org.apache.sis.portrayal.MapLayer)51 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)47 Rectangle (java.awt.Rectangle)26 CanvasDef (org.geotoolkit.display2d.service.CanvasDef)26 SceneDef (org.geotoolkit.display2d.service.SceneDef)26 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)22 Coordinate (org.locationtech.jts.geom.Coordinate)17 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)16 MutableStyle (org.geotoolkit.style.MutableStyle)16 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)16 Graphics2D (java.awt.Graphics2D)15 SampleDimension (org.apache.sis.coverage.SampleDimension)15 Feature (org.opengis.feature.Feature)14 FeatureType (org.opengis.feature.FeatureType)14 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)13 FeatureSet (org.apache.sis.storage.FeatureSet)13