/sigmah/src/test/java/org/sigmah/server/report/generator/map/CoincidentPointsClusterTest.java
Java | 128 lines | 72 code | 40 blank | 16 comment | 4 complexity | 613572d53626ec6919100f3a25b1d02d MD5 | raw file
1/* 2 * All Sigmah code is released under the GNU General Public License v3 3 * See COPYRIGHT.txt and LICENSE.txt. 4 */ 5 6package org.sigmah.server.report.generator.map; 7 8import org.junit.Assert; 9import org.junit.Test; 10import org.sigmah.server.report.generator.MapSymbol; 11import org.sigmah.shared.report.content.LatLng; 12import org.sigmah.shared.report.content.Point; 13 14import java.io.BufferedReader; 15import java.io.InputStreamReader; 16import java.util.ArrayList; 17import java.util.List; 18 19/* 20* Real example that didn't work. 21* 22* @author Alex Bertram 23*/ 24public class CoincidentPointsClusterTest extends GraphTest { 25 26 27 @Test 28 public void testSimpleData() throws Exception { 29 List<PointValue> points = new ArrayList<PointValue>(); 30 points.add(new PointValue(null, new MapSymbol(), 7.0, new Point(0, 0))); 31 points.add(new PointValue(null, new MapSymbol(), 2.0, new Point(0, 0))); 32 points.add(new PointValue(null, new MapSymbol(), 41.0, new Point(100, 100))); 33 points.add(new PointValue(null, new MapSymbol(), 9.0, new Point(0, 0))); 34 points.add(new PointValue(null, new MapSymbol(), 39.0, new Point(100, 100))); 35 36 double originalSum = 7 + 2 + 9 + 41 + 39; 37 38 // Now build the graph 39 40 MarkerGraph graph = new MarkerGraph(points, 41 new BubbleLayerGenerator.IntersectionCalculator(15)); 42 43 GeneticSolver solver = new GeneticSolver(); 44 45 List<Cluster> clusters = solver.solve(graph, new GsLogCalculator(5, 15), 46 new CircleFitnessFunctor(), UpperBoundsCalculator.calculate(graph, new FixedRadiiCalculator(5))); 47 48 // check to make sure all values were included 49 double sumAfterClustering = 0; 50 for (Cluster cluster : clusters) { 51 sumAfterClustering += cluster.sumValues(); 52 } 53 54 Assert.assertEquals(originalSum, sumAfterClustering, 0.0001); 55 56 Assert.assertEquals(2, clusters.size()); 57 58 saveClusters(graph, "clusterTest-solution", clusters); 59 60 61 } 62 63 64 @Test 65 public void testRealData() throws Exception { 66 67 // Define projection for the test case 68 TiledMap map = new TiledMap(492, 690, new LatLng(2.293492496, 30.538372993), 9); 69 70 // Read data 71 BufferedReader in = new BufferedReader(new InputStreamReader( 72 GraphTest.class.getResourceAsStream("/distribscolaire-points.csv"))); 73 74 double originalSum = 0; 75 76 List<PointValue> points = new ArrayList<PointValue>(); 77 while (in.ready()) { 78 79 String line = in.readLine(); 80 String[] columns = line.split(","); 81 82 double lat = Double.parseDouble(columns[0]); 83 double lng = Double.parseDouble(columns[1]); 84 85 PointValue pv = new PointValue(); 86 pv.px = map.fromLatLngToPixel(new LatLng(lat, lng)); 87 pv.value = Double.parseDouble(columns[2]); 88 pv.symbol = new MapSymbol(); 89 90 originalSum += pv.value; 91 92 points.add(pv); 93 } 94 95 // Now build the graph 96 97 MarkerGraph graph = new MarkerGraph(points, 98 new BubbleLayerGenerator.IntersectionCalculator(15)); 99 100 // make sure nothing was lost in the merging of coincident points 101 double nodeSum = 0; 102 for (MarkerGraph.Node node : graph.getNodes()) { 103 nodeSum += node.getPointValue().value; 104 } 105 Assert.assertEquals("values after construction of graph", originalSum, nodeSum, 0.001); 106 107 108 saveGraphImage("clusterTest2", graph, 15); 109 110 GeneticSolver solver = new GeneticSolver(); 111 112 List<Cluster> clusters = solver.solve(graph, new GsLogCalculator(5, 15), 113 new CircleFitnessFunctor(), UpperBoundsCalculator.calculate(graph, new FixedRadiiCalculator(5))); 114 115 // check to make sure all values were included 116 double sumAfterClustering = 0; 117 for (Cluster cluster : clusters) { 118 sumAfterClustering += cluster.sumValues(); 119 } 120 121 Assert.assertEquals(originalSum, sumAfterClustering, 0.001); 122 123 Assert.assertEquals(16, clusters.size()); 124 125 saveClusters(graph, "clusterTest-solution", clusters); 126 } 127 128}