← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12450: Map PNG, moved code from static support methods onto objects

 

Merge authors:
  Lars Helge Øverland (larshelge)
------------------------------------------------------------
revno: 12450 [merge]
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2013-10-06 22:40:11 +0200
message:
  Map PNG, moved code from static support methods onto objects
modified:
  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/InternalMapLayer.java
  dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/IntervalSet.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-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-10-06 17:25:44 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsMapGenerationService.java	2013-10-06 20:38:51 +0000
@@ -246,7 +246,7 @@
             
             if ( orgUnit != null && orgUnit.hasCoordinates() && orgUnit.hasFeatureType() )
             {
-                buildSingleGeoToolsMapObjectForMapLayer( mapLayer, mapValue.getValue(), orgUnit );
+                mapLayer.addSingleGeoToolsMapObject( mapValue.getValue(), orgUnit );
             }
         }
 
@@ -255,11 +255,13 @@
         
         if ( hasLegendSet )
         {
-            IntervalSet.getIntervalSet( mapLayer, mapView.getLegendSet() );
+            mapLayer.setIntervalSetFromLegendSet( mapView.getLegendSet() );
+            mapLayer.distributeAndUpdateMapObjectsInIntervalSet();
         }
         else
         {
-            IntervalSet.applyIntervalSetToMapLayer( DistributionStrategy.STRATEGY_EQUAL_RANGE, mapLayer, mapLayer.getClasses() );
+            mapLayer.applyIntervalSetToMapLayer( DistributionStrategy.STRATEGY_EQUAL_RANGE, mapLayer.getClasses() );
+            mapLayer.distributeAndUpdateMapObjectsInIntervalSet();
         }
         
         // Update the radius of each map object in this map layer according to
@@ -294,30 +296,6 @@
         return mapValues;
     }
     
-    private InternalMapObject buildSingleGeoToolsMapObjectForMapLayer( InternalMapLayer mapLayer,
-        double mapValue, OrganisationUnit orgUnit )
-    {
-        // Create and setup an internal map object
-        InternalMapObject mapObject = new InternalMapObject();
-        mapObject.setName( orgUnit.getName() );
-        mapObject.setValue( mapValue );
-        mapObject.setFillOpacity( mapLayer.getOpacity() );
-        mapObject.setStrokeColor( mapLayer.getStrokeColor() );
-        mapObject.setStrokeWidth( mapLayer.getStrokeWidth() );
-
-        // Build and set the GeoTools-specific geometric primitive that outlines
-        // the org unit on the map
-        mapObject.setGeometry( InternalMapObject.buildAndApplyGeometryForOrganisationUnit( orgUnit ) );
-
-        // Add the map object to the map layer
-        mapLayer.addMapObject( mapObject );
-
-        // Set the map layer for the map object
-        mapObject.setMapLayer( mapLayer );
-
-        return mapObject;
-    }
-
     private BufferedImage combineLegendAndMapImages( BufferedImage legendImage, BufferedImage mapImage )
     {
         Assert.isTrue( legendImage != null );

=== modified file 'dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/InternalMapLayer.java'
--- dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/InternalMapLayer.java	2013-10-06 17:01:11 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/InternalMapLayer.java	2013-10-06 20:38:51 +0000
@@ -32,6 +32,10 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.hisp.dhis.mapgeneration.IntervalSet.DistributionStrategy;
+import org.hisp.dhis.mapping.MapLegend;
+import org.hisp.dhis.mapping.MapLegendSet;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.Period;
 import org.springframework.util.Assert;
 
@@ -77,14 +81,19 @@
 
     protected List<InternalMapObject> mapObjects;
 
-    /**
-     * Constructs a map layer with no initial map objects.
-     */
+    // -------------------------------------------------------------------------
+    // Constructors
+    // -------------------------------------------------------------------------
+
     public InternalMapLayer()
     {
         this.mapObjects = new ArrayList<InternalMapObject>();
     }
 
+    // -------------------------------------------------------------------------
+    // Logic
+    // -------------------------------------------------------------------------
+
     /**
      * Interpolates the radii of this map layer's set of map objects according
      * the highest and lowest values among them.
@@ -131,19 +140,198 @@
     }
 
     /**
-     * Gets the collection of all the map objects associated with this map
-     * layer.
-     * 
-     * @return the list of map objects
-     */
+     * Creates a map object and adds it to this map layer. Sets this map layer
+     * on the map object.
+     * 
+     * @param mapValue the map values to set on the map object.
+     * @param orgUnit the organisation unit which name to set on the map object.
+     */
+    public void addSingleGeoToolsMapObject( double mapValue, OrganisationUnit orgUnit )
+    {
+        // Create and setup an internal map object
+        InternalMapObject mapObject = new InternalMapObject();
+        mapObject.setName( orgUnit.getName() );
+        mapObject.setValue( mapValue );
+        mapObject.setFillOpacity( opacity );
+        mapObject.setStrokeColor( strokeColor );
+        mapObject.setStrokeWidth( strokeWidth );
+
+        // Build and set the GeoTools-specific geometric primitive that outlines
+        // the org unit on the map
+        mapObject.setGeometry( InternalMapObject.buildAndApplyGeometryForOrganisationUnit( orgUnit ) );
+
+        // Add the map object to the map layer
+        this.addMapObject( mapObject );
+
+        // Set the map layer for the map object
+        mapObject.setMapLayer( this );
+    }
+
+    /**
+     * Sets an interval set on this map layer based on the given legend set.
+     * 
+     * @param legendSet the legend set.
+     */
+    public void setIntervalSetFromLegendSet( MapLegendSet legendSet )
+    {
+        IntervalSet intervalSet = new IntervalSet();
+        
+        for ( MapLegend legend : legendSet.getMapLegends() )
+        {
+            Color color = MapUtils.createColorFromString( legend.getColor() );
+            
+            Interval interval = new Interval( color, legend.getStartValue(), legend.getEndValue() );
+            
+            intervalSet.getIntervals().add( interval );
+        }
+        
+        this.intervalSet = intervalSet;
+    }
+
+    /**
+     * Distribute this map layer's map objects into the given interval set and
+     * update each map object with its interval.
+     */
+    public void distributeAndUpdateMapObjectsInIntervalSet()
+    {
+        for ( InternalMapObject mapObject : mapObjects )
+        {
+            for ( Interval interval : intervalSet.getIntervals() )
+            {
+                // If the map object's value is within this interval's
+                // boundaries, add it to this interval
+                if ( mapObject.getValue() >= interval.getValueLow() && mapObject.getValue() <= interval.getValueHigh() )
+                {
+                    // Add map object to interval and set interval for map object
+                    interval.addMember( mapObject );
+                    mapObject.setInterval( interval );
+
+                    // Do not add to more than one interval
+                    break;
+                }
+            }
+        }
+    }
+
+    /**
+     * Creates and applies a fixed length interval set to the given map layer.
+     * 
+     * How map objects are distributed among intervals depends on the
+     * distribution strategy that is used, which may be either 'equal range' or
+     * 'equal size'.
+     * 
+     * The 'equal range' strategy is defined by passing
+     * DistributionStrategy.STRATEGY_EQUAL_RANGE to this method. It creates and
+     * applies to the given map layer a fixed length interval set distributing
+     * map objects into intervals that has the same range.
+     * 
+     * The 'equal size' strategy is defined by passing
+     * DistributionStrategy.STRATEGY_EQUAL_SIZE to this method. It creates and
+     * applies to the given map layer a fixed length interval set distributing
+     * map objects into intervals that has (optimally) the same amount of map
+     * objects.
+     * 
+     * For example, given the map object collection of a map layer
+     * [a:3,b:2,c:5,d:18,e:0,f:50,g:22], where the objects with the lowest and
+     * highest values are e:0 and f:50, this collection of map objects will
+     * distribute differently into intervals depending on the distribution
+     * strategy chosen.
+     * 
+     * Strategy 'equal range' with length 5: interval [e:0,b:2,a:3,c:5] range
+     * 0-10 size 4 interval [d:18] range 11-20 size 1 interval [g:22] range
+     * 21-30 size 1 interval [] range 31-40 size 0 interval [f:50] range 41-50
+     * size 1
+     * 
+     * Strategy 'equal size' with length 5: interval [e:0,b:2] range 0-2 size 2
+     * interval [a:3,c:5] range 3-5 size 2 interval [d:18] range 5-18 size 1
+     * interval [g:22] range 18-22 size 1 interval [f:50] range 22-50 size 1
+     * 
+     * @param strategy the desired distribution strategy
+     * @param mapLayer the map layer whose map objects to distribute
+     * @param length the number of intervals in the set
+     * @return the created interval set that was applied to this map layer
+     */
+    public void applyIntervalSetToMapLayer( DistributionStrategy strategy, int length )
+    {
+        if ( DistributionStrategy.STRATEGY_EQUAL_RANGE == strategy )
+        {
+            applyEqualRangeIntervalSetToMapLayer( length );
+        }
+        else if ( DistributionStrategy.STRATEGY_EQUAL_SIZE == strategy )
+        {
+            throw new RuntimeException( "This distribution strategy is not implemented yet!" );
+        }
+        else
+        {
+            throw new RuntimeException( "The interval distribution strategy " + strategy + " is not implemented (yet)!" );
+        }
+    }
+
+    /**
+     * Creates and applies to the given map layer a fixed length interval set
+     * distributing map objects into intervals that has the same range.
+     * 
+     * @param mapLayer the map layer whose map objects to distribute
+     * @param length the number of equal sized intervals
+     * @return the created interval set that was applied to this map layer
+     */
+    public void applyEqualRangeIntervalSetToMapLayer( int length )
+    {
+        Assert.isTrue( length > 0 );
+        Assert.isTrue( mapObjects != null );
+        Assert.isTrue( mapObjects.size() > 0 );
+
+        IntervalSet intervalSet = new IntervalSet();
+
+        // Determine the objects with the min and max values
+        for ( InternalMapObject mapObject : mapObjects )
+        {
+            if ( intervalSet.getObjectLow() == null || mapObject.getValue() < intervalSet.getObjectLow().getValue() )
+            {
+                intervalSet.setObjectLow( mapObject );
+            }
+            
+            if ( intervalSet.getObjectHigh() == null || mapObject.getValue() > intervalSet.getObjectHigh().getValue() )
+            {
+                intervalSet.setObjectHigh( mapObject );
+            }
+        }
+
+        // Determine and set the color for each of the intervals according to
+        // the highest and lowest values
+        for ( int i = 0; i < length; i++ )
+        {
+            // Determine the boundaries the interval covers
+            double low = MapUtils.lerp( intervalSet.getObjectLow().getValue(), intervalSet.getObjectHigh().getValue(), (i + 0.0) / length );
+            double high = MapUtils.lerp( intervalSet.getObjectLow().getValue(), intervalSet.getObjectHigh().getValue(), (i + 1.0) / length );
+
+            // Determine the color of the interval
+            Color color = MapUtils.lerp( colorLow, colorHigh, (i + 0.5) / length );
+
+            // Create and setup a new interval
+            Interval interval = new Interval( low, high );
+            interval.setColor( color );
+
+            // Add it to the set
+            intervalSet.getIntervals().add( interval );
+        }
+
+        // Set this interval set for the map layer
+        this.intervalSet = intervalSet;
+    }
+
+    // -------------------------------------------------------------------------
+    // Getters and setters
+    // -------------------------------------------------------------------------
+
     public List<InternalMapObject> getMapObjects()
     {
-        return this.mapObjects;
+        return mapObjects;
     }
 
     public String getName()
     {
-        return this.name;
+        return name;
     }
 
     public void setName( String name )
@@ -153,7 +341,7 @@
 
     public Period getPeriod()
     {
-        return this.period;
+        return period;
     }
 
     public void setPeriod( Period period )
@@ -163,7 +351,7 @@
 
     public int getRadiusHigh()
     {
-        return this.radiusHigh;
+        return radiusHigh;
     }
 
     public void setRadiusHigh( int radiusHigh )
@@ -173,7 +361,7 @@
 
     public int getRadiusLow()
     {
-        return this.radiusLow;
+        return radiusLow;
     }
 
     public void setRadiusLow( int radiusLow )
@@ -183,7 +371,7 @@
 
     public Color getColorHigh()
     {
-        return this.colorHigh;
+        return colorHigh;
     }
 
     public void setColorHigh( Color colorHigh )
@@ -193,7 +381,7 @@
 
     public Color getColorLow()
     {
-        return this.colorLow;
+        return colorLow;
     }
 
     public void setColorLow( Color colorLow )
@@ -203,7 +391,7 @@
 
     public float getOpacity()
     {
-        return this.opacity;
+        return opacity;
     }
 
     public void setOpacity( float opacity )
@@ -223,7 +411,7 @@
 
     public Color getStrokeColor()
     {
-        return this.strokeColor;
+        return strokeColor;
     }
 
     public void setStrokeColor( Color strokeColor )
@@ -233,7 +421,7 @@
 
     public int getStrokeWidth()
     {
-        return this.strokeWidth;
+        return strokeWidth;
     }
 
     public void setStrokeWidth( int strokeWidth )
@@ -243,7 +431,7 @@
 
     public IntervalSet getIntervalSet()
     {
-        return this.intervalSet;
+        return intervalSet;
     }
 
     public void setIntervalSet( IntervalSet intervalSet )

=== modified file 'dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/IntervalSet.java'
--- dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/IntervalSet.java	2013-10-06 17:25:44 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/IntervalSet.java	2013-10-06 20:40:11 +0000
@@ -28,14 +28,9 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.awt.Color;
 import java.util.ArrayList;
 import java.util.List;
 
-import org.hisp.dhis.mapping.MapLegend;
-import org.hisp.dhis.mapping.MapLegendSet;
-import org.springframework.util.Assert;
-
 /**
  * An interval set is a collection of map objects that are distributed into
  * intervals according to their value.
@@ -55,6 +50,10 @@
     
     private InternalMapObject objectHigh;
 
+    // -------------------------------------------------------------------------
+    // Constructors
+    // -------------------------------------------------------------------------
+
     public IntervalSet()
     {
     }
@@ -64,209 +63,37 @@
         STRATEGY_EQUAL_RANGE, STRATEGY_EQUAL_SIZE
     }
 
-    /**
-     * Creates and applies a fixed length interval set to the given map layer.
-     * 
-     * How map objects are distributed among intervals depends on the
-     * distribution strategy that is used, which may be either 'equal range' or
-     * 'equal size'.
-     * 
-     * The 'equal range' strategy is defined by passing
-     * DistributionStrategy.STRATEGY_EQUAL_RANGE to this method. It creates and
-     * applies to the given map layer a fixed length interval set distributing
-     * map objects into intervals that has the same range.
-     * 
-     * The 'equal size' strategy is defined by passing
-     * DistributionStrategy.STRATEGY_EQUAL_SIZE to this method. It creates and
-     * applies to the given map layer a fixed length interval set distributing
-     * map objects into intervals that has (optimally) the same amount of map
-     * objects.
-     * 
-     * For example, given the map object collection of a map layer
-     * [a:3,b:2,c:5,d:18,e:0,f:50,g:22], where the objects with the lowest and
-     * highest values are e:0 and f:50, this collection of map objects will
-     * distribute differently into intervals depending on the distribution
-     * strategy chosen.
-     * 
-     * Strategy 'equal range' with length 5: interval [e:0,b:2,a:3,c:5] range
-     * 0-10 size 4 interval [d:18] range 11-20 size 1 interval [g:22] range
-     * 21-30 size 1 interval [] range 31-40 size 0 interval [f:50] range 41-50
-     * size 1
-     * 
-     * Strategy 'equal size' with length 5: interval [e:0,b:2] range 0-2 size 2
-     * interval [a:3,c:5] range 3-5 size 2 interval [d:18] range 5-18 size 1
-     * interval [g:22] range 18-22 size 1 interval [f:50] range 22-50 size 1
-     * 
-     * @param strategy the desired distribution strategy
-     * @param mapLayer the map layer whose map objects to distribute
-     * @param length the number of intervals in the set
-     * @return the created interval set that was applied to this map layer
-     */
-    public static IntervalSet applyIntervalSetToMapLayer( DistributionStrategy strategy, InternalMapLayer mapLayer,
-        int length )
-    {
-        if ( DistributionStrategy.STRATEGY_EQUAL_RANGE == strategy )
-        {
-            return applyEqualRangeIntervalSetToMapLayer( mapLayer, length );
-        }
-        else if ( DistributionStrategy.STRATEGY_EQUAL_SIZE == strategy )
-        {
-            return applyEqualSizeIntervalSetToMapLayer( mapLayer, length );
-        }
-        else
-        {
-            throw new RuntimeException( "The interval distribution strategy " + strategy + " is not implemented (yet)!" );
-        }
-    }
-
-    /**
-     * Creates and applies to the given map layer a fixed length interval set
-     * distributing map objects into intervals that has the same range.
-     * 
-     * @param mapLayer the map layer whose map objects to distribute
-     * @param length the number of equal sized intervals
-     * @return the created interval set that was applied to this map layer
-     */
-    private static IntervalSet applyEqualRangeIntervalSetToMapLayer( InternalMapLayer mapLayer, int length )
-    {
-        Assert.isTrue( mapLayer != null );
-        Assert.isTrue( length > 0 );
-        Assert.isTrue( mapLayer.getMapObjects() != null );
-        Assert.isTrue( mapLayer.getMapObjects().size() > 0 );
-
-        IntervalSet intervalSet = new IntervalSet();
-
-        // Determine the objects with the min and max values
-        for ( InternalMapObject mapObject : mapLayer.getMapObjects() )
-        {
-            if ( intervalSet.getObjectLow() == null || mapObject.getValue() < intervalSet.getObjectLow().getValue() )
-            {
-                intervalSet.objectLow = mapObject;
-            }
-            
-            if ( intervalSet.getObjectHigh() == null || mapObject.getValue() > intervalSet.getObjectHigh().getValue() )
-            {
-                intervalSet.objectHigh = mapObject;
-            }
-        }
-
-        // Determine and set the color for each of the intervals according to
-        // the highest and lowest values
-        for ( int i = 0; i < length; i++ )
-        {
-            // Determine the boundaries the interval covers
-            double low = MapUtils.lerp( intervalSet.getObjectLow().getValue(), intervalSet.getObjectHigh().getValue(), (i + 0.0) / length );
-            double high = MapUtils.lerp( intervalSet.getObjectLow().getValue(), intervalSet.getObjectHigh().getValue(), (i + 1.0) / length );
-
-            // Determine the color of the interval
-            Color color = MapUtils.lerp( mapLayer.getColorLow(), mapLayer.getColorHigh(), (i + 0.5) / length );
-
-            // Create and setup a new interval
-            Interval interval = new Interval( low, high );
-            interval.setColor( color );
-
-            // Add it to the set
-            intervalSet.getIntervals().add( interval );
-        }
-
-        // Distribute this map layer's objects among the intervals in the set
-        distributeAndUpdateMapObjectsForMapLayer( mapLayer, intervalSet );
-
-        // Set this interval set for the map layer
-        mapLayer.setIntervalSet( intervalSet );
-
-        return intervalSet;
-    }
-
-    public static IntervalSet getIntervalSet( InternalMapLayer mapLayer, MapLegendSet legendSet )
-    {
-        IntervalSet intervalSet = new IntervalSet();
-        
-        for ( MapLegend legend : legendSet.getMapLegends() )
-        {
-            Color color = MapUtils.createColorFromString( legend.getColor() );
-            
-            Interval interval = new Interval( color, legend.getStartValue(), legend.getEndValue() );
-            
-            intervalSet.getIntervals().add( interval );
-        }
-        
-        distributeAndUpdateMapObjectsForMapLayer( mapLayer, intervalSet );
-        
-        mapLayer.setIntervalSet( intervalSet );
-        
-        return intervalSet;
-    }
-    
-    /**
-     * Creates and applies to the given map layer a fixed length interval set
-     * distributing map objects into intervals that has (optimally) the same
-     * amount of map objects. TODO Implement this method
-     * 
-     * @param mapLayer the map layer whose map objects to distribute
-     * @param length the number of equal sized intervals
-     * @return the created interval set that was applied to this map layer
-     */
-    private static IntervalSet applyEqualSizeIntervalSetToMapLayer( InternalMapLayer mapLayer, int length )
-    {
-        throw new RuntimeException( "This distribution strategy is not implemented yet!" );
-    }
-
-    /**
-     * Distribute a map layer's map objects into the given interval set and
-     * update each map object with its interval.
-     * 
-     * @param mapLayer the map layer whose objects to distribute
-     * @param intervalSet the interval set
-     */
-    private static void distributeAndUpdateMapObjectsForMapLayer( InternalMapLayer mapLayer, IntervalSet intervalSet )
-    {
-        for ( InternalMapObject mapObject : mapLayer.getMapObjects() )
-        {
-            for ( Interval interval : intervalSet.getIntervals() )
-            {
-                // If the map object's value is within this interval's
-                // boundaries, add it to this interval
-                if ( mapObject.getValue() >= interval.getValueLow() && mapObject.getValue() <= interval.getValueHigh() )
-                {
-                    // Add map object to interval and set interval for map object
-                    interval.addMember( mapObject );
-                    mapObject.setInterval( interval );
-
-                    // Do not add to more than one interval
-                    break;
-                }
-            }
-        }
-    }
-
-    /**
-     * Returns all the intervals in this interval set.
-     * 
-     * @return the list of intervals
-     */
+    // -------------------------------------------------------------------------
+    // Getters and setters
+    // -------------------------------------------------------------------------
+
     public List<Interval> getIntervals()
     {
         return intervals;
     }
 
-    /**
-     * Gets the map object with the lowest value in this interval set.
-     * 
-     * @return the object with the lowest value
-     */
+    public void setIntervals( List<Interval> intervals )
+    {
+        this.intervals = intervals;
+    }
+
     public InternalMapObject getObjectLow()
     {
         return objectLow;
     }
 
-    /**
-     * Gets the map object with the highest value in this interval set
-     * 
-     * @return the object with the highest value
-     */
+    public void setObjectLow( InternalMapObject objectLow )
+    {
+        this.objectLow = objectLow;
+    }
+
     public InternalMapObject getObjectHigh()
     {
         return objectHigh;
     }
+
+    public void setObjectHigh( InternalMapObject objectHigh )
+    {
+        this.objectHigh = objectHigh;
+    }
 }