← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11445: PNG map support for maps with multiple layers

 

------------------------------------------------------------
revno: 11445
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-07-18 19:16:22 +0200
message:
  PNG map support for maps with multiple layers
added:
  dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/InternalMap.java
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/MapUtils.java
  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/MapViewController.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	2012-02-07 16:03:05 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/mapgeneration/MapGenerationService.java	2013-07-18 17:16:22 +0000
@@ -29,6 +29,7 @@
 
 import java.awt.image.BufferedImage;
 
+import org.hisp.dhis.mapping.Map;
 import org.hisp.dhis.mapping.MapView;
 
 /**
@@ -46,7 +47,7 @@
 public interface MapGenerationService
 {
     public final String ID = MapGenerationService.class.getName();
-
+    
     /**
      * Generate an image that represents this map.
      * 
@@ -54,4 +55,12 @@
      * @return the rendered map image or null if there is no data for the map view.
      */
     public BufferedImage generateMapImage( MapView mapView );
+    
+    /**
+     * Generate an image that represents this map.
+     * 
+     * @param mapView the map view that will be rendered
+     * @return the rendered map image or null if there is no data for the map view.
+     */
+    public BufferedImage generateMapImage( Map map );
 }

=== 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-18 16:02:00 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsMapGenerationService.java	2013-07-18 17:16:22 +0000
@@ -36,13 +36,13 @@
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import org.apache.commons.lang.StringUtils;
 import org.hisp.dhis.analytics.AnalyticsService;
 import org.hisp.dhis.analytics.DataQueryParams;
 import org.hisp.dhis.common.Grid;
 import org.hisp.dhis.mapgeneration.IntervalSet.DistributionStrategy;
+import org.hisp.dhis.mapping.Map;
 import org.hisp.dhis.mapping.MapView;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
@@ -85,23 +85,38 @@
 
     public BufferedImage generateMapImage( MapView mapView )
     {
-        Assert.isTrue( mapView != null );
+        Map map = new Map();
+        
+        map.getMapViews().add( mapView );
+        
+        return generateMapImage( map );
+    }
+    
+    public BufferedImage generateMapImage( Map map )
+    {
+        Assert.isTrue( map != null );
 
         int height = 512;
 
-        // Build internal map layer representation
-        InternalMapLayer mapLayer = buildSingleInternalMapLayer( mapView );
-
-        if ( mapLayer == null )
+        InternalMap internalMap = new InternalMap();
+        
+        for ( MapView mapView : map.getMapViews() )
+        {        
+            InternalMapLayer mapLayer = buildSingleInternalMapLayer( mapView );
+            
+            internalMap.getLayers().add( mapLayer );
+        }
+        
+        if ( internalMap.getLayers().isEmpty() )
         {
             return null;
         }
         
         // Build representation of a map using GeoTools, then render as image
-        BufferedImage mapImage = MapUtils.render( mapLayer, height );
+        BufferedImage mapImage = MapUtils.render( internalMap, height );
 
         // Build the legend set, then render it to an image
-        LegendSet legendSet = new LegendSet( mapLayer );
+        LegendSet legendSet = new LegendSet( internalMap.getLayers().get( 0 ) ); //TODO
         BufferedImage legendImage = legendSet.render( height );
 
         // Combine the legend image and the map image into one image
@@ -137,7 +152,7 @@
         List<OrganisationUnit> organisationUnits = new ArrayList<OrganisationUnit>( organisationUnitService.
             getOrganisationUnitsAtLevel( mapView.getOrganisationUnitLevel().getLevel(), mapView.getParentOrganisationUnit() ) );
 
-        Map<String, OrganisationUnit> uidOuMap = new HashMap<String, OrganisationUnit>();
+        java.util.Map<String, OrganisationUnit> uidOuMap = new HashMap<String, OrganisationUnit>();
         
         for ( OrganisationUnit ou : organisationUnits )
         {

=== added file 'dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/InternalMap.java'
--- dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/InternalMap.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/InternalMap.java	2013-07-18 17:16:22 +0000
@@ -0,0 +1,50 @@
+package org.hisp.dhis.mapgeneration;
+
+/*
+ * Copyright (c) 2011, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class InternalMap
+{
+    private List<InternalMapLayer> layers = new ArrayList<InternalMapLayer>();
+    
+    public InternalMap()
+    {
+    }
+
+    public List<InternalMapLayer> getLayers()
+    {
+        return layers;
+    }
+
+    public void setLayers( List<InternalMapLayer> layers )
+    {
+        this.layers = layers;
+    }
+}

=== 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-07-18 16:02:00 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/MapUtils.java	2013-07-18 17:16:22 +0000
@@ -154,19 +154,23 @@
     // Map
     // -------------------------------------------------------------------------
 
-    public static BufferedImage render( InternalMapLayer map )
+    public static BufferedImage render( InternalMap map )
     {
         return render( map, DEFAULT_MAP_WIDTH );
     }
 
-    public static BufferedImage render( InternalMapLayer map, int imageWidth )
+    public static BufferedImage render( InternalMap map, int imageWidth )
     {
         MapContent mapContent = new MapContent();
 
         // Convert map objects to features, and add them to the map
-        for ( InternalMapObject mapObject : map.getMapObjects() )
+        
+        for ( InternalMapLayer mapLayer : map.getLayers() )
         {
-            mapContent.addLayer( createFeatureLayerFromMapObject( mapObject ) );
+            for ( InternalMapObject mapObject : mapLayer.getMapObjects() )
+            {
+                mapContent.addLayer( createFeatureLayerFromMapObject( mapObject ) );
+            }
         }
 
         // Create a renderer for this map

=== 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-05-27 16:40:21 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapController.java	2013-07-18 17:16:22 +0000
@@ -27,14 +27,26 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import static org.hisp.dhis.period.PeriodType.getPeriodFromIsoString;
+
+import java.awt.image.BufferedImage;
+import java.io.InputStream;
+import java.util.Iterator;
+
+import javax.imageio.ImageIO;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
 import org.hisp.dhis.api.controller.AbstractCrudController;
 import org.hisp.dhis.api.utils.ContextUtils;
+import org.hisp.dhis.api.utils.ContextUtils.CacheStrategy;
 import org.hisp.dhis.dataelement.DataElementOperandService;
 import org.hisp.dhis.dataelement.DataElementService;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.i18n.I18nFormat;
 import org.hisp.dhis.i18n.I18nManager;
 import org.hisp.dhis.indicator.IndicatorService;
+import org.hisp.dhis.mapgeneration.MapGenerationService;
 import org.hisp.dhis.mapping.Map;
 import org.hisp.dhis.mapping.MapView;
 import org.hisp.dhis.mapping.MappingService;
@@ -50,13 +62,6 @@
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ResponseStatus;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.InputStream;
-import java.util.Iterator;
-
-import static org.hisp.dhis.period.PeriodType.getPeriodFromIsoString;
-
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  * @author Lars Helge Overland
@@ -95,6 +100,12 @@
     @Autowired
     private I18nManager i18nManager;
 
+    @Autowired
+    private MapGenerationService mapGenerationService;
+
+    @Autowired
+    private ContextUtils contextUtils;
+
     //--------------------------------------------------------------------------
     // CRUD
     //--------------------------------------------------------------------------
@@ -211,6 +222,14 @@
         }
     }
 
+    @RequestMapping(value = { "/{uid}/data", "/{uid}/data.png" }, method = RequestMethod.GET)
+    public void getMapData( @PathVariable String uid, HttpServletResponse response ) throws Exception
+    {
+        Map map = mappingService.getMap( uid );
+
+        renderMapViewPng( map, response );
+    }
+
     //--------------------------------------------------------------------------
     // Supportive methods
     //--------------------------------------------------------------------------
@@ -275,4 +294,21 @@
             view.setOrganisationUnitGroupSet( organisationUnitGroupService.getOrganisationUnitGroupSet( view.getOrganisationUnitGroupSet().getUid() ) );
         }
     }
+
+    private void renderMapViewPng( Map map, HttpServletResponse response )
+        throws Exception
+    {
+        BufferedImage image = mapGenerationService.generateMapImage( map );
+
+        if ( image != null )
+        {
+            contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PNG, CacheStrategy.RESPECT_SYSTEM_SETTING, "map.png", false );
+
+            ImageIO.write( image, "PNG", response.getOutputStream() );
+        }
+        else
+        {
+            response.setStatus( HttpServletResponse.SC_NO_CONTENT );
+        }
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapViewController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapViewController.java	2012-12-13 17:52:21 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/mapping/MapViewController.java	2013-07-18 17:16:22 +0000
@@ -69,11 +69,11 @@
     private OrganisationUnitService organisationUnitService;
 
     @Autowired
+    private MapGenerationService mapGenerationService;
+
+    @Autowired
     private ContextUtils contextUtils;
 
-    @Autowired
-    private MapGenerationService mapGenerationService;
-
     @RequestMapping(value = { "/{uid}/data", "/{uid}/data.png" }, method = RequestMethod.GET)
     public void getMap( @PathVariable String uid, HttpServletResponse response ) throws Exception
     {