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