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