PageRenderTime 69ms CodeModel.GetById 21ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 0ms

/webportal/src/main/java/org/ala/spatial/data/LegendObject.java

http://alageospatialportal.googlecode.com/
Java | 196 lines | 152 code | 22 blank | 22 comment | 42 complexity | 593ea00fdaf9f1b1f1e5adea9a0d9b55 MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5package org.ala.spatial.data;
  6
  7import java.io.Serializable;
  8import java.util.Comparator;
  9import java.util.HashMap;
 10import org.ala.spatial.data.QueryField.FieldType;
 11
 12/**
 13 *
 14 * @author Adam
 15 */
 16public class LegendObject implements Serializable {
 17
 18    Legend numericLegend;
 19    QueryField.FieldType fieldType;
 20    //categorical legend
 21    final public static int[] colours = {0x003366CC, 0x00DC3912, 0x00FF9900, 0x00109618, 0x00990099, 0x000099C6, 0x00DD4477, 0x0066AA00, 0x00B82E2E, 0x00316395, 0x00994499, 0x0022AA99, 0x00AAAA11, 0x006633CC, 0x00E67300, 0x008B0707, 0x00651067, 0x00329262, 0x005574A6, 0x003B3EAC, 0x00B77322, 0x0016D620, 0x00B91383, 0x00F4359E, 0x009C5935, 0x00A9C413, 0x002A778D, 0x00668D1C, 0x00BEA413, 0x000C5922, 0x00743411};
 22    final public static int DEFAULT_COLOUR = 0x00FFFFFF;
 23    final public static boolean LIMIT_LEGEND = true;
 24    //[0] is colour, [1] is count
 25    HashMap<String, int[]> categories;
 26    String[] categoryNameOrder;
 27    String colourMode;
 28
 29    public LegendObject(Legend numericLegend, QueryField.FieldType fieldType) {
 30        this.numericLegend = numericLegend;
 31        this.fieldType = fieldType;
 32    }
 33
 34    public LegendObject(String[] categories, int[] counts) {
 35        Object[] objects = new Object[categories.length];
 36        for (int i = 0; i < objects.length; i++) {
 37            Object[] o = new Object[2];
 38            o[0] = categories[i];
 39            o[1] = new Integer(counts[i]);
 40            objects[i] = o;
 41        }
 42        //sort by count in descending order
 43        java.util.Arrays.sort(objects, new Comparator<Object>() {
 44
 45            @Override
 46            public int compare(Object o1, Object o2) {
 47                return ((Integer) ((Object[]) o2)[1])
 48                        - ((Integer) ((Object[]) o1)[1]);
 49            }
 50        });
 51        //assign colours and put in hashmap
 52        this.categories = new HashMap<String, int[]>();
 53        categoryNameOrder = new String[objects.length];
 54        for (int i = 0; i < objects.length; i++) {
 55            String category = (String) ((Object[]) objects[i])[0];
 56            int[] data = new int[2];
 57            data[0] = colours[(i < colours.length) ? i : colours.length - 1];
 58            data[1] = (Integer) ((Object[]) objects[i])[1];
 59            this.categories.put(category, data);
 60            categoryNameOrder[i] = category;
 61        }
 62    }
 63
 64    /**
 65     * Get legend as a table.
 66     *
 67     * CSV
 68     * (header) name, red, green, blue, count CR
 69     * (records) string, 0-255, 0-255, 0-255, integer CR
 70     * 
 71     * @return
 72     */
 73    public String getTable() {
 74        StringBuilder sb = new StringBuilder();
 75        sb.append("name,red,green,blue,count");
 76        if (numericLegend != null) {
 77            if (numericLegend.getMinMax() != null) { //catch manually set numeric fields without numeric data
 78                double[] minmax = numericLegend.getMinMax();
 79                double[] cutoffs = numericLegend.getCutoffdoubles();
 80                double[] cutoffMins = numericLegend.getCutoffMindoubles();
 81
 82                String format = "%.4f";
 83                if (fieldType == QueryField.FieldType.INT
 84                        || fieldType == QueryField.FieldType.LONG) {
 85                    format = "%.0f";
 86                }
 87
 88                //unknown
 89                if (numericLegend.countOfNaN > 0) {
 90                    sb.append("\nn/a,0,0,0,").append(numericLegend.countOfNaN);
 91                }
 92
 93                if (minmax[0] == minmax[1]) {
 94                    String rgb = getRGB(numericLegend.getColour(minmax[0]));
 95                    sb.append("\n").append(String.format(format, minmax[0])).append(",").append(rgb).append(",");
 96                } else {
 97                    for (int i = 0; i < cutoffs.length; i++) {
 98                        if (i == 0 || cutoffs[i-1] < cutoffs[i]) {
 99                            String rgb = getRGB(numericLegend.getColour(cutoffs[i]));
100                            sb.append("\n>= ").append(String.format(format, cutoffMins[i])).append(" and ");
101                            sb.append("<= ").append(String.format(format, cutoffs[i])).append(",").append(rgb).append(",").append(numericLegend.groupSizesArea[i]);
102                        }
103                    }
104                }
105            }
106        } else {
107            for (int i = 0; i < categoryNameOrder.length && i < colours.length; i++) {
108                if (!LIMIT_LEGEND || i < colours.length - 1 || categoryNameOrder.length == colours.length) {
109                    int[] data = categories.get(categoryNameOrder[i]);
110                    String rgb = getRGB(data[0]);
111                    sb.append("\n\"").append(categoryNameOrder[i].replace("\"", "\"\"")).append("\",").append(rgb).append(",").append(data[1]);
112                } else {
113                    int count = 0;
114                    for (int j = i; j < categoryNameOrder.length; j++) {
115                        int[] data = categories.get(categoryNameOrder[i]);
116                        count += data[1];
117                    }
118                    String rgb = getRGB(colours[colours.length - 1]);
119                    sb.append("\n").append(categoryNameOrder.length - i).append(" more,").append(rgb).append(",").append(count);
120                }
121            }
122        }
123
124        return sb.toString();
125    }
126
127    public int getColour(String value) {
128        if (numericLegend != null) {
129            try {
130                return getColour(Double.parseDouble(value));
131            } catch (Exception e) {
132                return DEFAULT_COLOUR;
133            }
134        } else {
135            int[] data = categories.get(value);
136
137            if (data != null) {
138                return data[0];
139            } else {
140                return DEFAULT_COLOUR;
141            }
142        }
143    }
144
145    public int getColour(double value) {
146        if (numericLegend != null) {
147            return numericLegend.getColour(value);
148        } else {
149            int i = (int) value;
150            if (i >= 0 && i < categoryNameOrder.length) {
151                return getColour(categoryNameOrder[i]);
152            } else {
153                return DEFAULT_COLOUR;
154            }
155        }
156    }
157
158    public static String getRGB(int colour) {
159        return ((colour >> 16) & 0x000000ff) + ","
160                + ((colour >> 8) & 0x000000ff) + ","
161                + (colour & 0x000000ff);
162    }
163
164    public double[] getMinMax() {
165        if (numericLegend != null) {
166            return numericLegend.getMinMax();
167        } else {
168            double[] d = {0, categoryNameOrder.length};
169            return d;
170        }
171    }
172
173    public String[] getCategories() {
174        return categoryNameOrder;
175    }
176
177    public String getColourMode() {
178        return colourMode;
179    }
180
181    public void setColourMode(String colourMode) {
182        this.colourMode = colourMode;
183    }
184
185    public Legend getNumericLegend() {
186        return numericLegend;
187    }
188
189    void setNumericLegend(Legend numericLegend) {
190        this.numericLegend = numericLegend;
191    }
192
193    public FieldType getFieldType() {
194        return fieldType;
195    }
196}