← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5383: Map generation: made it more robust against empty polygons

 

------------------------------------------------------------
revno: 5383
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2011-12-13 12:36:45 +0100
message:
  Map generation: made it more robust against empty polygons
modified:
  dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsPrimitiveFromJsonFactory.java
  dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/MapUtils.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/GeoToolsPrimitiveFromJsonFactory.java'
--- dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsPrimitiveFromJsonFactory.java	2011-12-13 09:43:27 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/GeoToolsPrimitiveFromJsonFactory.java	2011-12-13 11:36:45 +0000
@@ -84,12 +84,17 @@
     public static MultiPolygon createMultiPolygonFromJson( JsonNode json )
     {
         // Native array of polygons to pass to GeoFactory
-        Polygon[] polygons = new Polygon[json.size()];
+        Polygon[] polygons = new Polygon[MapUtils.getNonEmptyNodes( json )];
 
         // Read all the polygons from the json array
         for ( int i = 0; i < json.size(); i++ )
         {
-            polygons[i] = createPolygonFromJson( json.get( i ) );
+            JsonNode node = json.get( i );
+            
+            if ( MapUtils.nodeIsNonEmpty( node ) )
+            {
+                polygons[i] = createPolygonFromJson( node );
+            }
         }
 
         // Create the multi-polygon from factory
@@ -122,7 +127,11 @@
             for ( int i = 1; i < shell.size(); i++ )
             {
                 JsonNode hole = json.get( i );
-                holes[i] = createLinearRingFromJson( hole );
+                
+                if ( hole != null && hole.size() > 0 )
+                {
+                    holes[i] = createLinearRingFromJson( hole );
+                }
             }
         }
 
@@ -139,12 +148,17 @@
     public static LinearRing createLinearRingFromJson( JsonNode json )
     {
         // Native array of coordinates to pass to GeoFactory
-        Coordinate[] coords = new Coordinate[json.size()];
+        Coordinate[] coords = new Coordinate[MapUtils.getNonEmptyNodes( json )];
 
         // Read the json array of coordinates
         for ( int i = 0; i < json.size(); i++ )
         {
-            coords[i] = createCoordinateFromJson( json.get( i ) );
+            JsonNode node = json.get( i );
+            
+            if ( MapUtils.nodeIsNonEmpty( node ) )
+            {
+                coords[i] = createCoordinateFromJson( node );
+            }
         }
 
         // Create the linear-ring from factory

=== 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	2011-12-13 09:43:27 +0000
+++ dhis-2/dhis-services/dhis-service-mapgeneration/src/main/java/org/hisp/dhis/mapgeneration/MapUtils.java	2011-12-13 11:36:45 +0000
@@ -29,6 +29,8 @@
 
 import java.awt.Color;
 
+import org.codehaus.jackson.JsonNode;
+
 /**
  * Utility class.
  * 
@@ -97,4 +99,36 @@
         
         return new Color( Integer.parseInt( string, COLOR_RADIX ) );
     }
+    
+    /**
+     * Returns the number of non empty sub JsonNodes in the given JsonNode.
+     * 
+     * @param json the JsonNode.
+     * @return the number of non empty sub JsonNodes.
+     */
+    public static int getNonEmptyNodes( JsonNode json )
+    {
+        int count = 0;
+        
+        for ( int i = 0; i < json.size(); i++ )
+        {
+            JsonNode node = json.get( i );
+            
+            count = nodeIsNonEmpty( node ) ? ++count : count;
+        }
+        
+        return count;
+    }
+    
+    /**
+     * Indicates whether the given JsonNode is empty, which implies that the
+     * node is not null and has a size greater than 0.
+     * 
+     * @param json the JsonNode.
+     * @return true if the given JsonNode is non empty, false otherwise.
+     */
+    public static boolean nodeIsNonEmpty( JsonNode json )
+    {
+        return json != null && json.size() > 0;
+    }
 }