← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12858: minor update related to 'Get org units inside radius' task.

 

------------------------------------------------------------
revno: 12858
committer: James Chang <jamesbchang@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2013-11-02 23:06:03 +0700
message:
  minor update related to 'Get org units inside radius' task.
added:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/GeoUtils.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/GeoUtilsTest.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java
  dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/MapUtils.java
  dhis-2/dhis-services/dhis-service-mapgeneration/src/test/java/org/hisp/dhis/mapgenerator/MapUtilsTest.java
  dhis-2/dhis-support/dhis-support-system/pom.xml
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.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/organisationunit/OrganisationUnitService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2013-11-01 08:11:31 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnitService.java	2013-11-02 16:06:03 +0000
@@ -382,17 +382,15 @@
     Collection<OrganisationUnit> getOrganisationUnitsBetweenByStatusLastUpdated( boolean status, Date lastUpdated, int first, int max );
 
     /**
-     * Retrieves the objects where its coordinate is within the 4 area points.
-     * 4 area points are
-     * Index 0: Maximum latitude (north edge of box shape)
-     * Index 1: Maxium longitude (east edge of box shape)
-     * Index 2: Minimum latitude (south edge of box shape)
-     * Index 3: Minumum longitude (west edge of box shape)
+     * Retrieves all the org units within the distance from center location.
      *
-     * @param box      the 4 area points.
+     * @param longitude        The longitude of the center location.
+     * @param latitude         The latitude of the center location.
+     * @param distance         The distance from center location.
+     * 
      * @return collection of objects.
      */
-    Collection<OrganisationUnit> getWithinCoordinateArea( double[] box );
+    Collection<OrganisationUnit> getWithinCoordinateArea( double longitude, double latitude, double distance );
     
     // -------------------------------------------------------------------------
     // OrganisationUnitHierarchy

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2013-11-01 08:11:31 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2013-11-02 16:06:03 +0000
@@ -30,6 +30,7 @@
 
 import static org.hisp.dhis.i18n.I18nUtils.i18n;
 
+import java.awt.geom.Point2D;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -50,6 +51,8 @@
 import org.hisp.dhis.system.util.ConversionUtils;
 import org.hisp.dhis.system.util.Filter;
 import org.hisp.dhis.system.util.FilterUtils;
+import org.hisp.dhis.system.util.GeoUtils;
+import org.hisp.dhis.system.util.ValidationUtils;
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.version.VersionService;
@@ -798,9 +801,34 @@
         return organisationUnitLevelStore.getMaxLevels();
     }
 
-    public Collection<OrganisationUnit> getWithinCoordinateArea( double[] box )
+    public Collection<OrganisationUnit> getWithinCoordinateArea( double longitude, double latitude, double distance )
     {
-        return organisationUnitStore.getWithinCoordinateArea( box );
+        Collection<OrganisationUnit> objects = organisationUnitStore.getWithinCoordinateArea( GeoUtils.getBoxShape( longitude, latitude, distance ) );
+
+        // Go through the list and remove the ones located farther than the distance.
+        if ( objects != null && objects.size() > 0 )
+        {
+            Iterator<OrganisationUnit> iter = objects.iterator();
+
+            Point2D centerPoint = new Point2D.Double( longitude, latitude );
+
+            while ( iter.hasNext() )
+            {
+                OrganisationUnit orgunit = iter.next();
+
+                double distancebetween = GeoUtils.getDistanceBetweenTwoPoints( centerPoint,
+                    ValidationUtils.getCoordinatePoint2D( orgunit.getCoordinates() ) );
+
+                if ( distancebetween > distance )
+                {
+                    // Remove the orgUnits that is outside of the distance range 
+                    // - due to the 'getWithinCoordinateArea' looking at square area instead of circle.
+                    iter.remove();
+                }
+            }
+        }
+
+        return objects;
     }
     
     // -------------------------------------------------------------------------

=== 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-11-01 08:11:31 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/MapUtils.java	2013-11-02 16:06:03 +0000
@@ -30,10 +30,8 @@
 
 import java.awt.Color;
 import java.awt.Graphics2D;
-import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.RenderingHints;
-import java.awt.geom.Point2D;
 import java.awt.image.BufferedImage;
 
 import org.geotools.feature.DefaultFeatureCollection;
@@ -42,7 +40,6 @@
 import org.geotools.map.FeatureLayer;
 import org.geotools.map.Layer;
 import org.geotools.map.MapContent;
-import org.geotools.referencing.GeodeticCalculator;
 import org.geotools.renderer.GTRenderer;
 import org.geotools.renderer.lite.StreamingRenderer;
 import org.geotools.styling.Style;
@@ -292,63 +289,5 @@
 
         return image;
     }
-    
-    /**
-     * Returns boundaries of a box shape which centre is the point defined by the 
-     * given longitude and latitude. The distance between the center point and the
-     * edges of the box is defined in meters by the given distance. Based on standard
-     * EPSG:4326 long/lat projection. The result is an array of length 4 where
-     * the values at each index are:
-     * 
-     * <ul>
-     * <li>Index 0: Maximum latitude (north edge of box shape).</li>
-     * <li>Index 1: Maxium longitude (east edge of box shape).</li>
-     * <li>Index 2: Minimum latitude (south edge of box shape).</li>
-     * <li>Index 3: Minumum longitude (west edge of box shape).</li>
-     * </ul>
-     * 
-     * @param longitude the longitude.
-     * @param latitude the latitude.
-     * @param distance the distance in meters to each box edge.
-     * @return an array of length 4.
-     */
-    public static double[] getBoxShape( double longitude, double latitude, double distance )
-    {
-        double[] box = new double[4];
-        
-        GeodeticCalculator calc = new GeodeticCalculator();
-        calc.setStartingGeographicPoint( longitude, latitude );
-        
-        calc.setDirection( 0, distance );
-        Point2D north = calc.getDestinationGeographicPoint();
-        
-        calc.setDirection( 90, distance );
-        Point2D east = calc.getDestinationGeographicPoint();
-        
-        calc.setDirection( 180, distance );
-        Point2D south = calc.getDestinationGeographicPoint();
-        
-        calc.setDirection( -90, distance );
-        Point2D west = calc.getDestinationGeographicPoint();
-        
-        box[0] = north.getY();
-        box[1] = east.getX();
-        box[2] = south.getY();
-        box[3] = west.getX();
-        
-        return box;
-    }
-    
-    /**
-     * Creates the distance between two points.
-     */
-    public static double getDistanceBetweenTwoPoints( Point2D from, Point2D to)
-    {                        
-        GeodeticCalculator calc = new GeodeticCalculator();
-        calc.setStartingGeographicPoint( from );
-        calc.setDestinationGeographicPoint( to);
-        
-        return calc.getOrthodromicDistance();
-    }
-    
+  
 }

=== modified file 'dhis-2/dhis-services/dhis-service-mapgeneration/src/test/java/org/hisp/dhis/mapgenerator/MapUtilsTest.java'
--- dhis-2/dhis-services/dhis-service-mapgeneration/src/test/java/org/hisp/dhis/mapgenerator/MapUtilsTest.java	2013-10-28 13:17:58 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/test/java/org/hisp/dhis/mapgenerator/MapUtilsTest.java	2013-11-02 16:06:03 +0000
@@ -28,7 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.mapgeneration.MapUtils.getBoxShape;
 import static org.hisp.dhis.mapgeneration.MapUtils.getWidthHeight;
 import static org.junit.Assert.assertEquals;
 
@@ -38,9 +37,7 @@
  * Lars Helge Overland
  */
 public class MapUtilsTest
-{
-    private static final double DELTA = 0.01;
-    
+{    
     @Test
     public void testGetWidthHeight()
     {
@@ -68,25 +65,4 @@
         getWidthHeight( null, null, 0, 0, 0.5 );
     }
     
-    @Test
-    public void testGetBoxShape()
-    {
-        // Equator
-        
-        double[] box = getBoxShape( 0, 0, 110574.27 );
-        
-        assertEquals( 1d, box[0], DELTA );
-        assertEquals( 1d, box[1], DELTA );
-        assertEquals( -1d, box[2], DELTA );
-        assertEquals( -1d, box[3], DELTA );
-        
-        // Punta Arenas
-
-        box = getBoxShape( -71, -53, 67137.20 );
-
-        assertEquals( -52.4, box[0], DELTA );
-        assertEquals( -70d, box[1], DELTA );
-        assertEquals( -53.6, box[2], DELTA );
-        assertEquals( -72d, box[3], DELTA );        
-    }
 }

=== modified file 'dhis-2/dhis-support/dhis-support-system/pom.xml'
--- dhis-2/dhis-support/dhis-support-system/pom.xml	2013-10-17 06:57:37 +0000
+++ dhis-2/dhis-support/dhis-support-system/pom.xml	2013-11-02 16:06:03 +0000
@@ -53,6 +53,13 @@
       <artifactId>poi</artifactId>
     </dependency>
     
+    <!-- GeoTools -->
+    
+    <dependency>
+      <groupId>org.geotools</groupId>
+      <artifactId>gt-referencing</artifactId>
+    </dependency>
+    
     <!-- Other -->
     
     <dependency>

=== added file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/GeoUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/GeoUtils.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/GeoUtils.java	2013-11-02 16:06:03 +0000
@@ -0,0 +1,68 @@
+package org.hisp.dhis.system.util;
+
+import java.awt.geom.Point2D;
+
+import org.geotools.referencing.GeodeticCalculator;
+
+public class GeoUtils
+{
+    
+    /**
+     * Returns boundaries of a box shape which centre is the point defined by the 
+     * given longitude and latitude. The distance between the center point and the
+     * edges of the box is defined in meters by the given distance. Based on standard
+     * EPSG:4326 long/lat projection. The result is an array of length 4 where
+     * the values at each index are:
+     * 
+     * <ul>
+     * <li>Index 0: Maximum latitude (north edge of box shape).</li>
+     * <li>Index 1: Maxium longitude (east edge of box shape).</li>
+     * <li>Index 2: Minimum latitude (south edge of box shape).</li>
+     * <li>Index 3: Minumum longitude (west edge of box shape).</li>
+     * </ul>
+     * 
+     * @param longitude the longitude.
+     * @param latitude the latitude.
+     * @param distance the distance in meters to each box edge.
+     * @return an array of length 4.
+     */
+    public static double[] getBoxShape( double longitude, double latitude, double distance )
+    {
+        double[] box = new double[4];
+        
+        GeodeticCalculator calc = new GeodeticCalculator();
+        calc.setStartingGeographicPoint( longitude, latitude );
+        
+        calc.setDirection( 0, distance );
+        Point2D north = calc.getDestinationGeographicPoint();
+        
+        calc.setDirection( 90, distance );
+        Point2D east = calc.getDestinationGeographicPoint();
+        
+        calc.setDirection( 180, distance );
+        Point2D south = calc.getDestinationGeographicPoint();
+        
+        calc.setDirection( -90, distance );
+        Point2D west = calc.getDestinationGeographicPoint();
+        
+        box[0] = north.getY();
+        box[1] = east.getX();
+        box[2] = south.getY();
+        box[3] = west.getX();
+        
+        return box;
+    }
+    
+    /**
+     * Creates the distance between two points.
+     */
+    public static double getDistanceBetweenTwoPoints( Point2D from, Point2D to)
+    {                        
+        GeodeticCalculator calc = new GeodeticCalculator();
+        calc.setStartingGeographicPoint( from );
+        calc.setDestinationGeographicPoint( to);
+        
+        return calc.getOrthodromicDistance();
+    }
+    
+}

=== added file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/GeoUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/GeoUtilsTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/GeoUtilsTest.java	2013-11-02 16:06:03 +0000
@@ -0,0 +1,64 @@
+package org.hisp.dhis.system.util;
+
+/*
+ * Copyright (c) 2004-2013, 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 static org.hisp.dhis.system.util.GeoUtils.getBoxShape;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+/**
+ * Lars Helge Overland
+ */
+public class GeoUtilsTest
+{
+    private static final double DELTA = 0.01;
+    
+    @Test
+    public void testGetBoxShape()
+    {
+        // Equator
+        
+        double[] box = getBoxShape( 0, 0, 110574.27 );
+        
+        assertEquals( 1d, box[0], DELTA );
+        assertEquals( 1d, box[1], DELTA );
+        assertEquals( -1d, box[2], DELTA );
+        assertEquals( -1d, box[3], DELTA );
+        
+        // Punta Arenas
+
+        box = getBoxShape( -71, -53, 67137.20 );
+
+        assertEquals( -52.4, box[0], DELTA );
+        assertEquals( -70d, box[1], DELTA );
+        assertEquals( -53.6, box[2], DELTA );
+        assertEquals( -72d, box[3], DELTA );        
+    }
+}

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.java	2013-11-01 08:11:31 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.java	2013-11-02 16:06:03 +0000
@@ -28,6 +28,16 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
 import org.hisp.dhis.api.controller.AbstractCrudController;
 import org.hisp.dhis.api.controller.WebMetaData;
 import org.hisp.dhis.api.controller.WebOptions;
@@ -38,12 +48,10 @@
 import org.hisp.dhis.common.Pager;
 import org.hisp.dhis.dxf2.metadata.MetaData;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
-import org.hisp.dhis.mapgeneration.MapUtils;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.organisationunit.comparator.OrganisationUnitByLevelComparator;
-import org.hisp.dhis.system.util.ValidationUtils;
 import org.hisp.dhis.user.CurrentUserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
@@ -54,18 +62,6 @@
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import java.awt.geom.Point2D;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -270,50 +266,39 @@
         Double longitude = Double.parseDouble( options.getOptions().get( "longitude" ) );
         Double latitude = Double.parseDouble( options.getOptions().get( "latitude" ) );
         Double distance = Double.parseDouble( options.getOptions().get( "distance" ) );
-        
-        entityList = new ArrayList<OrganisationUnit>( organisationUnitService.getWithinCoordinateArea( MapUtils.getBoxShape( longitude, latitude, distance ) ) );
-        
-        Point2D centerPoint = new Point2D.Double( longitude, latitude );
-        
+        String orgUnitGroupSetId = options.getOptions().get( "orgUnitGroupSetId" );
+        
+        entityList = new ArrayList<OrganisationUnit>( organisationUnitService.getWithinCoordinateArea( longitude, latitude, distance ) );
+                
         if ( entityList != null )
-        {
-            Iterator<OrganisationUnit> iter = entityList.iterator();
-
-            while ( iter.hasNext() )
+        {            
+            for( OrganisationUnit orgunit : entityList )
             {
-                OrganisationUnit orgunit = iter.next();
-
-                double distancebetween = MapUtils.getDistanceBetweenTwoPoints( centerPoint,
-                    ValidationUtils.getCoordinatePoint2D( orgunit.getCoordinates() ) );
-
-                if ( distancebetween > distance )
-                {
-                    // Remove the orgUnits that is outside of the distance range 
-                    // - due to the 'getWithinCoordinateArea' looking at square area instead of circle.
-                    iter.remove();
-                }
-                else
-                {
-                    // Clear out all data not needed for this task
-                    orgunit.removeAllDataSets();
-                    orgunit.removeAllUsers();
-                    orgunit.removeAllOrganisationUnitGroups();
-
-                    Set<AttributeValue> attributeValues = orgunit.getAttributeValues();
-                    attributeValues.clear();
-
-                    // Add OrgUnit Group Symbol into attributes
+                // Clear out all data not needed for this task
+                orgunit.removeAllDataSets();
+                orgunit.removeAllUsers();
+                orgunit.removeAllOrganisationUnitGroups();
+
+                Set<AttributeValue> attributeValues = orgunit.getAttributeValues();
+                attributeValues.clear();
+
+                // Add OrgUnit Group Symbol into attributes
+                if( orgUnitGroupSetId != null )
+                {
                     for ( OrganisationUnitGroup orgunitGroup : orgunit.getGroups() )
                     {
-                        AttributeValue attributeValue = new AttributeValue();
-                        attributeValue.setAttribute( new Attribute( "OrgUnitGroupSymbol", "OrgUnitGroupSymbol" ) );
-                        attributeValue.setValue( orgunitGroup.getSymbol() );
-
-                        attributeValues.add( attributeValue );
+                        if( orgunitGroup.getGroupSet().getUid() == orgUnitGroupSetId )
+                        {                        
+                            AttributeValue attributeValue = new AttributeValue();
+                            attributeValue.setAttribute( new Attribute( "OrgUnitGroupSymbol", "OrgUnitGroupSymbol" ) );
+                            attributeValue.setValue( orgunitGroup.getSymbol() );
+        
+                            attributeValues.add( attributeValue );
+                        }
                     }
-
-                    orgunit.setAttributeValues( attributeValues );
                 }
+                
+                orgunit.setAttributeValues( attributeValues );
             }
         }