[geotk] Rendering a map image in the background

Westgård Trond trondwe at IMR.no
Thu Aug 26 09:47:15 EDT 2010

I want the user to be able to make reports from a postgis database into a pdf document. The document will contain text, tables and maps.
To do this I want to use a JasperReport template and fill this, but first I have to make a map "invisible" to the user.

In the code below I am able to get data from a shapefile into a MapLayer, but when I ask the map to be painted it stops with a NullPointerException.

Is there anyone that may give me some hints on what I am doing wrong?



package geotoolkittest;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import org.geotoolkit.data.FeatureCollection;
import org.geotoolkit.data.FileDataStoreFactory;
import org.geotoolkit.data.query.QueryBuilder;
import org.geotoolkit.data.shapefile.ShapefileDataStore;
import org.geotoolkit.data.shapefile.ShapefileDataStoreFactory;
import org.geotoolkit.display2d.canvas.DefaultRenderingContext2D;
import org.geotoolkit.display2d.canvas.RenderingContext2D;
import org.geotoolkit.gui.swing.go2.JMap2D;
import org.geotoolkit.map.FeatureMapLayer;
import org.geotoolkit.map.MapBuilder;
import org.geotoolkit.map.MapContext;
import org.geotoolkit.map.MapLayer;
import org.geotoolkit.referencing.CRS;
import org.geotoolkit.referencing.crs.DefaultGeographicCRS;
import org.geotoolkit.style.DefaultMutableStyle;
import org.geotoolkit.style.MutableStyle;
import org.geotoolkit.util.RandomStyleFactory;
import org.opengis.feature.Feature;
import org.opengis.feature.type.Name;
import org.opengis.geometry.Envelope;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.TransformException;
import org.opengis.util.FactoryException;

public class Map {

     * A test to make "a map in the background" not visible to the user
     * When the image of the map is rendered it is the plan to use JaperReport to add it to a
     * document that will be given to the user as a pdf doucument.
     * At the moment I am happy if I succeed to produce the "outpicture.png" file
    public static void main(String[] args) {

        try {
            URL shapefileURL = new URL("file:///C:/GIS/Data/Europe_one_new.shp");
            ShapefileDataStore shapefileDataStore;
            ShapefileDataStoreFactory shapefileDataStoreFactory = new ShapefileDataStoreFactory();
            FileDataStoreFactory fdsf = (FileDataStoreFactory) shapefileDataStoreFactory;
            shapefileDataStore = (ShapefileDataStore) fdsf.createDataStore(shapefileURL);

            JMap2D myMap = new JMap2D();

            CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
            MapContext context = MapBuilder.createContext(crs);


            Name name = shapefileDataStore.getFeatureType().getName();
            FeatureCollection<Feature> featureSource = shapefileDataStore.createSession(true).getFeatureCollection(QueryBuilder.all(name));
            MutableStyle style = RandomStyleFactory.createRandomVectorStyle(featureSource);
            FeatureMapLayer layer = MapBuilder.createFeatureLayer(featureSource, style);
            Envelope envelope = layer.getBounds();

            boolean add = myMap.getContainer().getContext().layers().add(layer);

            RenderingContext2D rcd = new DefaultRenderingContext2D(myMap.getCanvas());

            myMap.getHandler().getCanvas().getBackgroundPainter().paint(rcd); // FAILS WITH A NULLPOINTER EXCEPTION
            Image myimg = myMap.getCanvas().getSnapShot();
            final BufferedImage mybuffimg = new BufferedImage(myimg.getWidth(null), myimg.getHeight(null), BufferedImage.TYPE_INT_ARGB);
            final Graphics g = mybuffimg.getGraphics();
            g.drawImage(myimg, 0, 0, null);
            File outpict = new File("c:\\temp\\ outpicture.png");
            ImageIO.write(mybuffimg, "png", outpict);

        } catch (Exception ex) {
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/geotoolkit/attachments/20100826/b47db8db/attachment.html

More information about the Geotoolkit mailing list