/sigmah/src/test/java/org/sigmah/server/report/generator/map/ComplexPoints.java

http://sigma-h.googlecode.com/ · Java · 103 lines · 67 code · 24 blank · 12 comment · 5 complexity · a41b70751c46c892551c184bd39878ef 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. package org.sigmah.server.report.generator.map;
  6. import org.sigmah.shared.report.content.Extents;
  7. import org.sigmah.shared.report.content.LatLng;
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.util.ArrayList;
  12. import java.util.Collections;
  13. import java.util.Comparator;
  14. import java.util.List;
  15. /**
  16. *
  17. * Reasonably complex, real-world point input data for testing
  18. *
  19. * @author Alex Bertram
  20. */
  21. public class ComplexPoints {
  22. public double originalSum;
  23. public Extents extents;
  24. public List<PointValue> points;
  25. public List<LatLng> latlngs;
  26. public MarkerGraph graph;
  27. ComplexPoints() throws IOException {
  28. BufferedReader in = new BufferedReader(new InputStreamReader(
  29. GraphTest.class.getResourceAsStream("/msa-points.csv")));
  30. extents = Extents.emptyExtents();
  31. originalSum = 0;
  32. points = new ArrayList<PointValue>();
  33. latlngs = new ArrayList<LatLng>();
  34. while(in.ready()) {
  35. String line = in.readLine();
  36. String[] columns = line.split(",");
  37. int partnerId = Integer.parseInt(columns[1]);
  38. double lng = Double.parseDouble(columns[2]);
  39. double lat = Double.parseDouble(columns[3]);
  40. double value = Double.parseDouble(columns[4]);
  41. PointValue pv = new PointValue();
  42. pv.value = value;
  43. points.add(pv);
  44. originalSum += value;
  45. latlngs.add(new LatLng(lat, lng));
  46. extents.grow(lat, lng);
  47. }
  48. // project
  49. int zoom = TileMath.zoomLevelForExtents(extents, 640, 480);
  50. TiledMap map = new TiledMap(640, 480, extents.center(), zoom);
  51. for(int i=0;i!= points.size(); ++i) {
  52. points.get(i).px = map.fromLatLngToPixel(latlngs.get(i));
  53. }
  54. // build graph
  55. graph = new MarkerGraph(points, new MarkerGraph.IntersectionCalculator() {
  56. public boolean intersects(MarkerGraph.Node a, MarkerGraph.Node b) {
  57. return a.getPoint().distance(b.getPoint()) < 30;
  58. }
  59. });
  60. }
  61. public List<List<MarkerGraph.Node>> getLargestN(int count) {
  62. List<List<MarkerGraph.Node>> subgraphs = graph.getSubgraphs();
  63. Collections.sort(subgraphs, new Comparator<List<MarkerGraph.Node>>() {
  64. public int compare(List<MarkerGraph.Node> o1, List<MarkerGraph.Node> o2) {
  65. if(o1.size() > o2.size()) {
  66. return -1;
  67. } else if(o1.size() < o2.size()) {
  68. return +1;
  69. } else {
  70. return 0;
  71. }
  72. }
  73. });
  74. while(subgraphs.size() > count) {
  75. subgraphs.remove(count);
  76. }
  77. return subgraphs;
  78. }
  79. }