PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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