/alaspatial/src/main/java/org/ala/spatial/analysis/service/LoadedPointsService.java

http://alageospatialportal.googlecode.com/ · Java · 186 lines · 152 code · 17 blank · 17 comment · 32 complexity · 5fd5fdaee8ab1b88d3af9bebdce9e0b1 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.analysis.service;
  6. import java.io.BufferedInputStream;
  7. import java.io.BufferedOutputStream;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileOutputStream;
  11. import java.io.ObjectInputStream;
  12. import java.io.ObjectOutputStream;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15. import java.util.Map.Entry;
  16. import org.ala.spatial.analysis.index.BoundingBoxes;
  17. import org.ala.spatial.analysis.index.OccurrenceRecordNumbers;
  18. import org.ala.spatial.util.Layer;
  19. import org.ala.spatial.util.SimpleRegion;
  20. import org.ala.spatial.util.TabulationSettings;
  21. /**
  22. * Allow alaspatial to operate on loaded points (lat long)
  23. *
  24. * Used in
  25. * - Sampling
  26. * - Maxent
  27. * - Clustering
  28. *
  29. * @author Adam
  30. */
  31. public class LoadedPointsService {
  32. /**
  33. * store for ID and {last access time (Long) , points (double[][2]) }
  34. */
  35. static HashMap<String, Object[]> clusters = new HashMap<String, Object[]>();
  36. static public void addCluster(String id, LoadedPoints points) {
  37. Object[] o = clusters.get(id);
  38. if (o == null) {
  39. freeCluster();
  40. o = new Object[2];
  41. }
  42. o[0] = new Long(System.currentTimeMillis());
  43. o[1] = points;
  44. store(id, o);
  45. clusters.put(id, o);
  46. }
  47. private static void freeCluster() {
  48. if (clusters.size() > TabulationSettings.cluster_lookup_size) { //TODO: unique setting
  49. Long time_min = new Long(0);
  50. Long time_max = new Long(0);
  51. for (Entry<String, Object[]> e : clusters.entrySet()) {
  52. if (time_min == 0 || (Long) e.getValue()[0] < time_min) {
  53. time_min = (Long) e.getValue()[0];
  54. }
  55. if (time_max == 0 || (Long) e.getValue()[0] < time_max) {
  56. time_max = (Long) e.getValue()[0];
  57. }
  58. }
  59. Long time_mid = (time_max - time_min) / 2 + time_min;
  60. for (Entry<String, Object[]> e : clusters.entrySet()) {
  61. if ((Long) e.getValue()[0] < time_mid) {
  62. clusters.remove(e.getKey());
  63. }
  64. }
  65. }
  66. }
  67. static void store(String key, Object[] o) {
  68. try {
  69. File file = new File(TabulationSettings.file_store
  70. + "points" + key + ".dat");
  71. FileOutputStream fos = new FileOutputStream(file);
  72. BufferedOutputStream bos = new BufferedOutputStream(fos);
  73. ObjectOutputStream oos = new ObjectOutputStream(bos);
  74. LoadedPoints lp = (LoadedPoints) (o[1]);
  75. oos.writeObject(lp);
  76. oos.close();
  77. } catch (Exception e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. static Object[] retrieve(String key) {
  82. try {
  83. File file = new File(TabulationSettings.file_store
  84. + "points" + key + ".dat");
  85. if (file.exists()) {
  86. FileInputStream fis = new FileInputStream(file);
  87. BufferedInputStream bis = new BufferedInputStream(fis);
  88. ObjectInputStream ois = new ObjectInputStream(bis);
  89. LoadedPoints lp = (LoadedPoints) ois.readObject();
  90. ois.close();
  91. addCluster(key, lp);
  92. return clusters.get(key);
  93. }
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }
  97. return null;
  98. }
  99. static LoadedPoints getLoadedPoints(String id) {
  100. Object[] o = clusters.get(id);
  101. if (o == null) {
  102. o = retrieve(id);
  103. }
  104. if (o != null) {
  105. return (LoadedPoints) o[1];
  106. }
  107. return null;
  108. }
  109. static public double[][] getPoints(String id, SimpleRegion region, ArrayList<OccurrenceRecordNumbers> records) {
  110. LoadedPoints lp = getLoadedPoints(id);
  111. if (lp != null) {
  112. return lp.getPoints(region, records);
  113. }
  114. return null;
  115. }
  116. static public double[] getPointsFlat(String id, SimpleRegion region, ArrayList<OccurrenceRecordNumbers> records) {
  117. LoadedPoints lp = getLoadedPoints(id);
  118. if (lp != null) {
  119. return lp.getPointsFlat(region, records);
  120. }
  121. return null;
  122. }
  123. static public String getSampling(String id, Layer[] layers, SimpleRegion region, ArrayList<OccurrenceRecordNumbers> records, int max_rows) {
  124. LoadedPoints lp = getLoadedPoints(id);
  125. if (lp != null) {
  126. return lp.getSampling(layers, region, records, max_rows);
  127. }
  128. return null;
  129. }
  130. public static String[] getIds(String id) {
  131. LoadedPoints lp = getLoadedPoints(id);
  132. if (lp != null) {
  133. return lp.getIds();
  134. }
  135. return null;
  136. }
  137. public static double [] getBoundingBox(String id) {
  138. LoadedPoints lp = getLoadedPoints(id);
  139. if (lp != null) {
  140. double[][] points = getPoints(id, null, null);
  141. double minx = points[0][0];
  142. double miny = points[0][1];
  143. double maxx = points[0][0];
  144. double maxy = points[0][1];
  145. for (int i = 0; i < points.length; i++) {
  146. if (minx > points[i][0]) {
  147. minx = points[i][0];
  148. }
  149. if (maxx < points[i][0]) {
  150. maxx = points[i][0];
  151. }
  152. if (miny > points[i][1]) {
  153. miny = points[i][1];
  154. }
  155. if (maxy < points[i][1]) {
  156. maxy = points[i][1];
  157. }
  158. }
  159. double [] bb = new double[4];
  160. bb[0] = minx;
  161. bb[1] = miny;
  162. bb[2] = maxx;
  163. bb[3] = maxy;
  164. BoundingBoxes.putLSIDBoundingBox(id, bb);
  165. return bb;
  166. }
  167. return null;
  168. }
  169. }