Class ImageGenerator


  • public class ImageGenerator
    extends Object
    An utility class for simple image map generation.

    A sample usage could be the overlay of vector layers.

     ImageGenerator imgGen = new ImageGenerator();
     imgGen.addFeaturePath(reticolo, null);
     imgGen.setLayers();
     imgGen.dumpPngImage(imagePath, bounds, 300, 300, 100);
     
    Since:
    0.7.3
    Author:
    Andrea Antonello (www.hydrologis.com)
    • Constructor Summary

      Constructors 
      Constructor Description
      ImageGenerator​(IHMProgressMonitor monitor, org.opengis.referencing.crs.CoordinateReferenceSystem forceCrs)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addCoveragePath​(String coveragePath)
      Add a new coverage file path.
      void addCoverageRegion​(org.geotools.coverage.grid.GridGeometry2D coverageRegion)
      Add a coverage read region (this has to have same index as addCoveragePath.
      void addFeaturePath​(String featurePath, String filter)
      Add a new feature file path.
      void addShapesPath​(String shapesPath)
      Method to add a file that contains a list of shapes/texts to add.
      void dispose()  
      void drawImage​(Graphics2D g2d, org.geotools.geometry.jts.ReferencedEnvelope ref, int imageWidth, int imageHeight, double buffer)  
      BufferedImage drawImage​(org.geotools.geometry.jts.ReferencedEnvelope ref, int imageWidth, int imageHeight, double buffer)
      Draw the map on an image.
      BufferedImage drawImageWithNewMapContent​(org.geotools.geometry.jts.ReferencedEnvelope ref, int imageWidth, int imageHeight, double buffer)
      Draw the map on an image creating a new MapContent.
      void dump2Graphics2D​(Graphics2D graphics2d, org.geotools.geometry.jts.ReferencedEnvelope bounds, double scale, EPaperFormat paperFormat, Double dpi, BufferedImage legend, int legendX, int legendY, String scalePrefix, float scaleSize, int scaleX, int scaleY)
      Create an image for a given paper size and scale.
      void dumpJpgImage​(String imagePath, org.geotools.geometry.jts.ReferencedEnvelope bounds, int imageWidth, int imageHeight, double buffer, int[] rgbCheck)
      Writes an image of maps drawn to a jpg file.
      void dumpPngImage​(String imagePath, org.geotools.geometry.jts.ReferencedEnvelope bounds, int imageWidth, int imageHeight, double buffer, int[] rgbCheck)
      Writes an image of maps drawn to a png file.
      void dumpPngImageForScaleAndPaper​(String imagePath, org.geotools.geometry.jts.ReferencedEnvelope bounds, double scale, EPaperFormat paperFormat, Double dpi, BufferedImage legend, int legendX, int legendY, String scalePrefix, float scaleSize, int scaleX, int scaleY)
      Create an image for a given paper size and scale.
      BufferedImage getImageWithCheck​(org.geotools.geometry.jts.ReferencedEnvelope bounds, int imageWidth, int imageHeight, double buffer, int[] rgbCheck)
      Draw the map on an image.
      org.geotools.geometry.jts.ReferencedEnvelope setLayers()
      Set the layers that have to be drawn.
      void setWMS​(String wmsURL)
      Set a WMS service to handle as raster layer.
    • Constructor Detail

      • ImageGenerator

        public ImageGenerator​(IHMProgressMonitor monitor,
                              org.opengis.referencing.crs.CoordinateReferenceSystem forceCrs)
    • Method Detail

      • addCoveragePath

        public void addCoveragePath​(String coveragePath)
        Add a new coverage file path.

        The order will be considered. First paths are drawn first.

        Parameters:
        coveragePath - the path to add.
      • addCoverageRegion

        public void addCoverageRegion​(org.geotools.coverage.grid.GridGeometry2D coverageRegion)
        Add a coverage read region (this has to have same index as addCoveragePath.
        Parameters:
        coverageRegion - the region to read.
      • setWMS

        public void setWMS​(String wmsURL)
        Set a WMS service to handle as raster layer.
        Parameters:
        wmsURL - the WMS url and layer name in the format: http://wmsurl#layername
      • addFeaturePath

        public void addFeaturePath​(String featurePath,
                                   String filter)
        Add a new feature file path.

        The order will be considered. First paths are drawn first.

        Parameters:
        featurePath - the path to add.
      • addShapesPath

        public void addShapesPath​(String shapesPath)
        Method to add a file that contains a list of shapes/texts to add.

        This is applied on top of the final image. Positions are in pixel.

        Supported are:

        • text;x;y;mytext;colorrgba;size
        • box;x;y;w;h;strokewidth;fillrgba;strokergba
        • roundedbox;x;y;w;h;round;strokewidth;fillrgba;strokergba
        • ...

      • setLayers

        public org.geotools.geometry.jts.ReferencedEnvelope setLayers()
                                                               throws Exception
        Set the layers that have to be drawn.

        This has to be called before the drawing process.

        Returns:
        the max envelope of the data.
        Throws:
        Exception
      • drawImage

        public BufferedImage drawImage​(org.geotools.geometry.jts.ReferencedEnvelope ref,
                                       int imageWidth,
                                       int imageHeight,
                                       double buffer)
        Draw the map on an image.
        Parameters:
        bounds - the area of interest.
        imageWidth - the width of the image to produce.
        imageHeight - the height of the image to produce.
        buffer - the buffer to add around the map bounds in map units.
        Returns:
        the image.
      • drawImage

        public void drawImage​(Graphics2D g2d,
                              org.geotools.geometry.jts.ReferencedEnvelope ref,
                              int imageWidth,
                              int imageHeight,
                              double buffer)
      • drawImageWithNewMapContent

        public BufferedImage drawImageWithNewMapContent​(org.geotools.geometry.jts.ReferencedEnvelope ref,
                                                        int imageWidth,
                                                        int imageHeight,
                                                        double buffer)
        Draw the map on an image creating a new MapContent.
        Parameters:
        bounds - the area of interest.
        imageWidth - the width of the image to produce.
        imageHeight - the height of the image to produce.
        buffer - the buffer to add around the map bounds in map units.
        Returns:
        the image.
      • dispose

        public void dispose()
      • dumpPngImage

        public void dumpPngImage​(String imagePath,
                                 org.geotools.geometry.jts.ReferencedEnvelope bounds,
                                 int imageWidth,
                                 int imageHeight,
                                 double buffer,
                                 int[] rgbCheck)
                          throws IOException
        Writes an image of maps drawn to a png file.
        Parameters:
        imagePath - the path to which to write the image.
        bounds - the area of interest.
        imageWidth - the width of the image to produce.
        imageHeight - the height of the image to produce.
        buffer - the buffer to add around the map bounds in map units.
        rgbCheck - an rgb tripled. If not null and the image generated is composed only of that color, then the tile is not generated. This can be useful to avoid generation of empty tiles.
        Throws:
        IOException
      • dumpJpgImage

        public void dumpJpgImage​(String imagePath,
                                 org.geotools.geometry.jts.ReferencedEnvelope bounds,
                                 int imageWidth,
                                 int imageHeight,
                                 double buffer,
                                 int[] rgbCheck)
                          throws IOException
        Writes an image of maps drawn to a jpg file.
        Parameters:
        imagePath - the path to which to write the image.
        bounds - the area of interest.
        imageWidth - the width of the image to produce.
        imageHeight - the height of the image to produce.
        buffer - the buffer to add around the map bounds in map units.
        rgbCheck - an rgb tripled. If not null and the image generated is composed only of that color, then the tile is not generated. This can be useful to avoid generation of empty tiles.
        Throws:
        IOException
      • getImageWithCheck

        public BufferedImage getImageWithCheck​(org.geotools.geometry.jts.ReferencedEnvelope bounds,
                                               int imageWidth,
                                               int imageHeight,
                                               double buffer,
                                               int[] rgbCheck)
                                        throws IOException
        Draw the map on an image.
        Parameters:
        bounds - the area of interest.
        imageWidth - the width of the image to produce.
        imageHeight - the height of the image to produce.
        buffer - the buffer to add around the map bounds in map units.
        rgbCheck - an rgb tripled. If not null and the image generated is composed only of that color, then the tile is not generated. This can be useful to avoid generation of empty tiles.
        Throws:
        IOException
      • dumpPngImageForScaleAndPaper

        public void dumpPngImageForScaleAndPaper​(String imagePath,
                                                 org.geotools.geometry.jts.ReferencedEnvelope bounds,
                                                 double scale,
                                                 EPaperFormat paperFormat,
                                                 Double dpi,
                                                 BufferedImage legend,
                                                 int legendX,
                                                 int legendY,
                                                 String scalePrefix,
                                                 float scaleSize,
                                                 int scaleX,
                                                 int scaleY)
                                          throws Exception
        Create an image for a given paper size and scale.
        Parameters:
        imagePath - the path to which to write the image.
        bounds - the area of interest. In this case only the center is considered. The bounds are recalculated based in paper size and scale.
        scale - the scale wanted for the map.
        paperFormat - the paper format to use.
        dpi - the wanted dpi. If null, 72dpi is used as default.
        legend - an optional legend image.
        legendX - the X position of the legend in the final image.
        legendY - the Y position of the legend in the final image.
        scalePrefix - if not null, this string will be added before the scale definition. If null, no scale definition will be added.
        scaleSize - a size for the scale.
        scaleX - the X position of the scale in the final image.
        scaleY - the X position of the scale in the final image.
        Throws:
        Exception
        Since:
        0.7.6
      • dump2Graphics2D

        public void dump2Graphics2D​(Graphics2D graphics2d,
                                    org.geotools.geometry.jts.ReferencedEnvelope bounds,
                                    double scale,
                                    EPaperFormat paperFormat,
                                    Double dpi,
                                    BufferedImage legend,
                                    int legendX,
                                    int legendY,
                                    String scalePrefix,
                                    float scaleSize,
                                    int scaleX,
                                    int scaleY)
                             throws Exception
        Create an image for a given paper size and scale.
        Parameters:
        imagePath - the path to which to write the image.
        bounds - the area of interest. In this case only the center is considered. The bounds are recalculated based in paper size and scale.
        scale - the scale wanted for the map.
        paperFormat - the paper format to use.
        dpi - the wanted dpi. If null, 72dpi is used as default.
        legend - an optional legend image.
        legendX - the X position of the legend in the final image.
        legendY - the Y position of the legend in the final image.
        scalePrefix - if not null, this string will be added before the scale definition. If null, no scale definition will be added.
        scaleSize - a size for the scale.
        scaleX - the X position of the scale in the final image.
        scaleY - the X position of the scale in the final image.
        Throws:
        Exception
        Since:
        0.7.6