← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11604: Map geneation, made it possible to specify height of map

 

------------------------------------------------------------
revno: 11604
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-08-07 16:07:18 +0200
message:
  Map geneation, made it possible to specify height of map
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapgeneration/MapGenerationService.java
  dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsMapGenerationService.java
  dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/LegendSet.java
  dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/MapUtils.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapController.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapgeneration/MapGenerationService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapgeneration/MapGenerationService.java	2013-07-24 12:38:12 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapgeneration/MapGenerationService.java	2013-08-07 14:07:18 +0000
@@ -68,8 +68,9 @@
      * Generate an image that represents this map.
      * 
      * @param map the map that will be rendered,
-     * @param width the width of the map image.
+     * @param width the maximum width of the map image.
+     * @param height the maxium height of the map image.
      * @return the rendered map image or null if there is no data for the map view.
      */
-    BufferedImage generateMapImage( Map map, int width );
+    BufferedImage generateMapImage( Map map, Integer width, Integer height );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsMapGenerationService.java'
--- dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsMapGenerationService.java	2013-07-24 12:38:12 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsMapGenerationService.java	2013-08-07 14:07:18 +0000
@@ -94,19 +94,23 @@
 
     public BufferedImage generateMapImage( Map map )
     {
-        return generateMapImage( map, 512 );
+        return generateMapImage( map, 512, null );
     }
     
-    public BufferedImage generateMapImage( Map map, int width )
+    public BufferedImage generateMapImage( Map map, Integer width, Integer height )
     {
         Assert.isTrue( map != null );
-        Assert.isTrue( width > LegendSet.LEGEND_TOTAL_WIDTH );
+        
+        if ( width == null && height == null )
+        {
+            width = MapUtils.DEFAULT_MAP_WIDTH;
+        }
 
         InternalMap internalMap = new InternalMap();
         
         for ( MapView mapView : map.getMapViews() )
         {        
-            InternalMapLayer mapLayer = buildSingleInternalMapLayer( mapView );
+            InternalMapLayer mapLayer = getSingleInternalMapLayer( mapView );
             
             if ( mapLayer != null )
             {
@@ -120,11 +124,11 @@
         }
         
         // Build representation of a map using GeoTools, then render as image
-        BufferedImage mapImage = MapUtils.render( internalMap, ( width - LegendSet.LEGEND_TOTAL_WIDTH ) );
+        BufferedImage mapImage = MapUtils.render( internalMap, width, height );
 
         // Build the legend set, then render it to an image
         LegendSet legendSet = new LegendSet( internalMap.getLayers().get( 0 ) ); //TODO
-        BufferedImage legendImage = legendSet.render( width );
+        BufferedImage legendImage = legendSet.render();
 
         // Combine the legend image and the map image into one image
         BufferedImage finalImage = combineLegendAndMapImages( legendImage, mapImage );
@@ -150,7 +154,7 @@
 
     private static final int DEFAULT_RADIUS_LOW = 15;
 
-    private InternalMapLayer buildSingleInternalMapLayer( MapView mapView )
+    private InternalMapLayer getSingleInternalMapLayer( MapView mapView )
     {
         if ( mapView == null || mapView.getPeriod() == null || mapView.getParentOrganisationUnit() == null )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/LegendSet.java'
--- dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/LegendSet.java	2013-07-24 15:55:13 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/LegendSet.java	2013-08-07 14:07:18 +0000
@@ -28,10 +28,8 @@
  */
 
 import java.awt.Color;
-import java.awt.Dimension;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
-import java.awt.geom.AffineTransform;
 import java.awt.image.BufferedImage;
 import java.util.LinkedList;
 import java.util.List;
@@ -81,53 +79,23 @@
      * @param imageMaxHeight
      * @return
      */
-    public BufferedImage render( int imageMaxHeight )
+    public BufferedImage render()
     {
-        Dimension imageDimensions = calculateImageWidthAndHeight( imageMaxHeight );
-        int imageWidth = (int) imageDimensions.getWidth();
-        int imageHeight = (int) imageDimensions.getHeight();
+        int imageWidth = LEGEND_TOTAL_WIDTH;
+        int imageHeight = calculateImageHeight();
         BufferedImage image = new BufferedImage( imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB );
         Graphics2D g = (Graphics2D) image.getGraphics();
 
-        // Overwrite if one of the legends is bigger than imageMaxHeight
-        if ( imageHeight > imageMaxHeight )
-        {
-            imageMaxHeight = imageHeight;
-        }
-
-        // Draw a background if the background color is specified
-        // NOTE It will be transparent otherwise, which is desired
-        /*
-        if ( backgroundColor != null )
-        {
-            g.setColor( backgroundColor );
-            g.fill( new Rectangle( 0, 0, imageWidth, imageHeight ) );
-        }*/
-
         // Turn anti-aliasing on
         g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
 
-        int y = 0;
-        int col = 0;
-        AffineTransform orginalTransform = g.getTransform();
-
         g.translate( LEGEND_MARGIN_LEFT, 0 );
 
         // Draw legends
         for ( Legend legend : legends )
         {
-            if ( y + legend.getHeight() >= imageMaxHeight )
-            {
-                col++;
-                y = 0;
-                g.setTransform( orginalTransform );
-                g.translate( col * LEGEND_WIDTH, 0 );
-            }
-
             legend.draw( g );
             g.translate( 0, LEGEND_MARGIN_BOTTOM );
-
-            y += legend.getHeight() + LEGEND_MARGIN_BOTTOM;
         }
 
         return image;
@@ -174,34 +142,15 @@
         backgroundColor = c;
     }
 
-    private Dimension calculateImageWidthAndHeight( int maxImageHeight )
+    private int calculateImageHeight()
     {
-        int imageWidth = LEGEND_WIDTH;
-        int imageHeight = maxImageHeight;
-
-        // Ensure that every legend fits the maxImageHeight
-        for ( Legend legend : legends )
-        {
-            if ( legend.getHeight() + LEGEND_MARGIN_BOTTOM > imageHeight )
-            {
-                imageHeight = legend.getHeight() + LEGEND_MARGIN_BOTTOM;
-            }
-        }
-
-        int y = 0;
-
-        // Calculate image width
-        for ( Legend legend : legends )
-        {
-            if ( legend.getHeight() + LEGEND_MARGIN_BOTTOM + y >= imageHeight )
-            {
-                imageWidth += LEGEND_WIDTH;
-                y = 0;
-            }
-
-            y += legend.getHeight() + LEGEND_MARGIN_BOTTOM;
-        }
-
-        return new Dimension( imageWidth, imageHeight );
+        int imageHeight = 0;
+
+        for ( Legend legend : legends )
+        {
+            imageHeight += legend.getHeight() + LEGEND_MARGIN_BOTTOM;
+        }
+        
+        return imageHeight;
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/MapUtils.java'
--- dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/MapUtils.java	2013-08-07 13:03:17 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/MapUtils.java	2013-08-07 14:07:18 +0000
@@ -57,7 +57,7 @@
     private static final String COLOR_PREFIX = "#";
     private static final int COLOR_RADIX = 16;
 
-    private static final int DEFAULT_MAP_WIDTH = 500;
+    public static final int DEFAULT_MAP_WIDTH = 500;
 
     /**
      * Linear interpolation of int.
@@ -154,12 +154,7 @@
     // Map
     // -------------------------------------------------------------------------
 
-    public static BufferedImage render( InternalMap map )
-    {
-        return render( map, DEFAULT_MAP_WIDTH );
-    }
-
-    public static BufferedImage render( InternalMap map, int imageWidth )
+    public static BufferedImage render( InternalMap map, Integer maxWidth, Integer maxHeight )
     {
         MapContent mapContent = new MapContent();
 
@@ -174,22 +169,27 @@
         }
 
         // Create a renderer for this map
+        
         GTRenderer renderer = new StreamingRenderer();
         renderer.setMapContent( mapContent );
 
         // Calculate image height
-        // TODO Might want to add a margin of say 25 pixels surrounding the map
+        
         ReferencedEnvelope mapBounds = mapContent.getMaxBounds();
-        double imageHeightFactor = mapBounds.getSpan( 1 ) / mapBounds.getSpan( 0 );
-        Rectangle imageBounds = new Rectangle( 0, 0, imageWidth, (int) Math.ceil( imageWidth * imageHeightFactor ) );
+        double widthToHeightFactor = mapBounds.getSpan( 0 ) / mapBounds.getSpan( 1 );
+        int[] widthHeight = getWidthHeight( maxWidth, maxHeight, widthToHeightFactor );
+        
+        //LegendSet.LEGEND_TOTAL_WIDTH;
+        
+        Rectangle imageBounds = new Rectangle( 0, 0, widthHeight[0], widthHeight[1] );
 
         // Create an image and get the graphics context from it
+        
         BufferedImage image = new BufferedImage( imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_ARGB );
         Graphics2D g = (Graphics2D) image.getGraphics();
 
         g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
         
-        // Render the map
         renderer.paint( g, imageBounds, mapBounds );
 
         mapContent.dispose();
@@ -230,7 +230,7 @@
         {
             maxHeight = (int) Math.ceil( maxWidth / widthToHeightFactor );
         }
-                
+                        
         int[] result = { maxWidth, maxHeight };
         
         return result;

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapController.java	2013-07-24 12:38:12 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapController.java	2013-08-07 14:07:18 +0000
@@ -226,12 +226,13 @@
     @RequestMapping(value = { "/{uid}/data", "/{uid}/data.png" }, method = RequestMethod.GET)
     public void getMapData( 
         @PathVariable String uid, 
-        @RequestParam( required = false, defaultValue = "512" ) Integer width, 
+        @RequestParam( required = false ) Integer width, 
+        @RequestParam( required = false ) Integer height, 
         HttpServletResponse response ) throws Exception
     {
         Map map = mappingService.getMap( uid );
 
-        renderMapViewPng( map, width, response );
+        renderMapViewPng( map, width, height, response );
     }
 
     //--------------------------------------------------------------------------
@@ -299,10 +300,10 @@
         }
     }
 
-    private void renderMapViewPng( Map map, int width, HttpServletResponse response )
+    private void renderMapViewPng( Map map, Integer width, Integer height, HttpServletResponse response )
         throws Exception
     {
-        BufferedImage image = mapGenerationService.generateMapImage( map, width );
+        BufferedImage image = mapGenerationService.generateMapImage( map, width, height );
 
         if ( image != null )
         {