Index: /other-projects/maori-lang-detection/src/org/greenstone/atea/CountryCodeCountsMapData.java
===================================================================
--- /other-projects/maori-lang-detection/src/org/greenstone/atea/CountryCodeCountsMapData.java	(revision 33852)
+++ /other-projects/maori-lang-detection/src/org/greenstone/atea/CountryCodeCountsMapData.java	(revision 33853)
@@ -12,4 +12,6 @@
 import java.util.List;
 import java.util.Map;
+
+//import java.lang.Math; //automatically imported apparently
 
 import org.apache.commons.csv.*;
@@ -328,10 +330,10 @@
 
     // create rectangular "histogram" for each country code
-    private Geometry toPolygon(JsonObject json, int count, int HISTOGRAM_WIDTH) {
+    private Geometry toPolygon(JsonObject json, final int count, final int HISTOGRAM_WIDTH) {
 	int half_width = HISTOGRAM_WIDTH/2;
 	double vertical_factor = 1.0;
 	
-	Double lng = json.get("lng").getAsDouble();
-	Double lat = json.get("lat").getAsDouble();
+	final Double lng = json.get("lng").getAsDouble();
+	final Double lat = json.get("lat").getAsDouble();
 
 	String countryCode = json.get("countrycode").getAsString();
@@ -345,4 +347,5 @@
 	// to grow northwards.
 	Double north = lat + (vertical_factor * count);
+	
 	while (north > 90) {
 	    // recalculate north after decreasing histogram's vertical growth
@@ -355,4 +358,62 @@
 	Double west = lng - half_width;
 	Double south = lat;
+	    
+	// Check if we're dealing with very large numbers, in which case, we can have follow off the longitude edges
+	// Max longitude values are -180 to 180. So a max of 360 units between them.
+	if((east + Math.abs(west)) > 360 || east > 180 || west < -180) {
+	    half_width = HISTOGRAM_WIDTH/2; // reset half_width
+	    
+	    double v_tmp_count = Math.sqrt(count);
+	    double h_tmp_count = Math.floor(v_tmp_count);
+	    v_tmp_count = Math.ceil(v_tmp_count);
+
+	    /*
+	    System.err.println("HERE, count = " + count);
+	    System.err.println("HERE, v = " + v_tmp_count);
+	    System.err.println("HERE, h = " + h_tmp_count);
+	    System.err.println("HERE, lat = " + lat);
+	    System.err.println("HERE, lng = " + lng + "\n");
+	    */
+
+	    north = lat + v_tmp_count;
+	    south = lat;
+	    east = lng + (h_tmp_count/2 * half_width); // a certain width, half_width, represents one unit in the x axis
+	    west = lng - (h_tmp_count/2 * half_width);
+
+	    if(north > 90) {
+		// centre vertically on lat
+		north = lat + (v_tmp_count/2);
+		south = lat - (v_tmp_count/2);
+	    }
+
+	    if(west < -180.0) {
+		double h_diff = lng - 180.0; // the country's longitude (lng) is h_diff from the western edge
+		west = 180.0; // maximise western edge
+		east = h_tmp_count - h_diff/half_width;// then grow the remainder of h_tmp_count in the opposite (eastern) direction
+	    }	    
+	    else if(east > 180.0) {
+		double h_diff = 180.0 - lng; // the country's longitude (lng) is h_diff from the eastern edge
+		east = 180.0; // maximise eastern edge
+		west = -1 * (h_tmp_count - (h_diff/half_width)); // then grow the remainder of h_tmp_count in the opposite (western) direction
+	    }
+	    
+	    // CAN'T centre on country, (lat,lng), as we don't know whether either of lat or lng has gone past the edge
+	    //north = lat + (v_tmp_count/2);
+	    //south = lat - (v_tmp_count/2);
+	    //east = lng + (h_tmp_count/2);
+	    //west = lng - (h_tmp_count/2);
+	    
+	    // hopefully we don't exceed +90/-90 lat and +/-180 longitude
+	    // "Longitude is in the range -180 and +180 specifying coordinates west and east of the Prime Meridian, respectively.
+	    // For reference, the Equator has a latitude of 0Â°, the North pole has a latitude of 90Â° north (written 90Â° N or +90Â°),
+	    // and the South pole has a latitude of -90Â°."
+
+	    /*
+	    System.err.println("north = " + north);
+	    System.err.println("south = " + south);
+	    System.err.println("east = " + east);
+	    System.err.println("west = " + west);
+	    */
+	}
 	
 	List<List<Position>> outerList = new LinkedList<List<Position>>();
