/webportal/src/main/java/org/ala/spatial/sampling/Sampling.java

http://alageospatialportal.googlecode.com/ · Java · 296 lines · 188 code · 22 blank · 86 comment · 49 complexity · b694e22b1a86e57837b579edd1d5b4d8 MD5 · raw file

  1. package org.ala.spatial.sampling;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.util.ArrayList;
  5. import java.util.Properties;
  6. import java.util.concurrent.CountDownLatch;
  7. import java.util.concurrent.LinkedBlockingQueue;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import org.ala.spatial.util.CommonData;
  11. /**
  12. * builder for sampling index.
  13. *
  14. * requires OccurrencesIndex to be up to date.
  15. *
  16. * operates on GridFiles
  17. * operates on Shape Files
  18. *
  19. * @author adam
  20. *
  21. */
  22. public class Sampling {
  23. static void sample(double[][] points, String facetName, String[] output) {
  24. String shapeFieldName = CommonData.getFacetShapeNameField(facetName);
  25. String layerName = CommonData.getFacetLayerName(facetName);
  26. long start = System.currentTimeMillis();
  27. System.out.println("start sampling " + points.length + " points in " + facetName + ":" + layerName);
  28. if (shapeFieldName != null) {
  29. intersectShape(CommonData.settings.get("sampling_files_path") + "shape/" + layerName, shapeFieldName, points, output);
  30. } else if (facetName.startsWith("cl")) {
  31. //shape_diva?
  32. intersectShapeGrid(CommonData.settings.get("sampling_files_path") + "shape_diva/" + layerName, points, output);
  33. } else if (facetName.startsWith("el")) {
  34. intersectGrid(CommonData.settings.get("sampling_files_path") + "diva/" + layerName, points, output);
  35. } else {
  36. //look for an analysis layer
  37. // File maxent = new File(CommonData.settings.get("analysis_output_dir") + "maxent/" + facetName + "/" + facetName + ".grd");
  38. // File aloc = new File(CommonData.settings.get("analysis_output_dir") + "aloc/" + facetName + "/" + "aloc" + ".grd");
  39. // String[] split = facetName.split("_");
  40. // File sxsSRichness = null;
  41. // File sxsODensity = null;
  42. // File gdmTransLayer = null;
  43. // File envelope = null;
  44. // if(split != null && split.length > 1) {
  45. // if(split[1].equals("srichness")) {
  46. // sxsSRichness = new File(CommonData.settings.get("analysis_output_dir") + "sitesbyspecies/" + split[0] + "/species_richness.grd");
  47. // } else if(split[1].equals("odensity")) {
  48. // sxsODensity = new File(CommonData.settings.get("analysis_output_dir") + "sitesbyspecies/" + split[0] + "/occurrence_density.grd");
  49. // } else {
  50. // int pos = facetName.indexOf("_");
  51. // String[] gdmparts = new String [] {facetName.substring(0,pos), facetName.substring(pos+1) };
  52. // gdmTransLayer = new File(CommonData.settings.get("analysis_output_dir") + "gdm/" + split[0] + "/" + gdmparts[1] + "Tran.grd");
  53. // }
  54. // envelope = new File(CommonData.settings.get("analysis_output_dir") + "envelope/" + split[0] + "/envelope.grd");
  55. // }
  56. //
  57. // System.out.println("Analysis layer: " + facetName);
  58. // System.out.println(maxent.getPath() + ", " + maxent.exists());
  59. // System.out.println(aloc.getPath() + ", " + aloc.exists());
  60. // if (sxsSRichness != null) {
  61. // System.out.println(sxsSRichness.getPath() + ", " + sxsSRichness.exists());
  62. // }
  63. // if (sxsODensity != null) {
  64. // System.out.println(sxsODensity.getPath() + ", " + sxsODensity.exists());
  65. // }
  66. // if (gdmTransLayer != null) {
  67. // System.out.println(gdmTransLayer.getPath() + ", " + gdmTransLayer.exists());
  68. // }
  69. // if (envelope != null) {
  70. // System.out.println(envelope.getPath() + ", " + envelope.exists());
  71. // }
  72. //
  73. // File file = null;
  74. // if (maxent.exists()) {
  75. // file = maxent;
  76. // } else if (aloc.exists()) {
  77. // file = aloc;
  78. // } else if (sxsSRichness != null && sxsSRichness.exists()) {
  79. // file = sxsSRichness;
  80. // } else if (sxsODensity != null && sxsODensity.exists()) {
  81. // file = sxsODensity;
  82. // } else if(gdmTransLayer != null && gdmTransLayer.exists()) {
  83. // file = gdmTransLayer;
  84. // } else if (envelope != null && envelope.exists()) {
  85. // file = envelope;
  86. // }
  87. String [] info = getAnalysisLayerInfo(facetName);
  88. if (info != null) {
  89. System.out.println("found file for sampling: " + info[1]);
  90. intersectGrid(info[1], points, output);
  91. }
  92. }
  93. System.out.println("finished sampling " + points.length + " points in " + facetName + ":" + layerName + " in " + (System.currentTimeMillis() - start) + "ms");
  94. }
  95. static void intersectGrid(String filename, double[][] points, String[] output) {
  96. try {
  97. Grid grid = new Grid(filename);
  98. float[] values = null;
  99. // if(points.length > 10000) {
  100. //load whole grid
  101. values = grid.getValues3(points, 40960);
  102. // } else {
  103. // values = grid.getValues(points);
  104. // }
  105. if (values != null) {
  106. for (int i = 0; i < output.length; i++) {
  107. if (Float.isNaN(values[i])) {
  108. output[i] = "n/a";
  109. } else {
  110. output[i] = String.valueOf(values[i]);
  111. }
  112. }
  113. }
  114. } catch (Exception e) {
  115. System.out.println("Error with grid: " + filename);
  116. e.printStackTrace();
  117. }
  118. }
  119. static void intersectShapeGrid(String filename, double[][] points, String[] output) {
  120. try {
  121. System.out.println("intersectShapeGrid: " + filename);
  122. Grid grid = new Grid(filename);
  123. Properties p = new Properties();
  124. p.load(new FileReader(filename + ".txt"));
  125. float[] values = null;
  126. values = grid.getValues3(points, 40960);
  127. if (values != null) {
  128. for (int i = 0; i < output.length; i++) {
  129. System.out.print(", " + values[i]);
  130. if (Float.isNaN(values[i])) {
  131. output[i] = "n/a";
  132. } else {
  133. String v = p.getProperty(String.valueOf((int)values[i]));
  134. System.out.print("=" + v);
  135. output[i] = v;
  136. }
  137. }
  138. }
  139. } catch (Exception e) {
  140. System.out.println("Error with shape grid: " + filename);
  141. e.printStackTrace();
  142. }
  143. }
  144. static void intersectShape(String filename, String fieldName, double[][] points, String[] output) {
  145. try {
  146. SimpleShapeFile ssf = CommonData.ssfCache.get(filename.replace(CommonData.settings.get("sampling_files_path") + "shape/", ""));
  147. if (ssf == null) {
  148. ssf = new SimpleShapeFile(filename, fieldName);
  149. }
  150. String[] catagories;
  151. int column_idx = ssf.getColumnIdx(fieldName);
  152. catagories = ssf.getColumnLookup(column_idx);
  153. int[] values = ssf.intersect(points, catagories, column_idx, Integer.parseInt(CommonData.settings.get("sampling_thread_count")));
  154. if (values != null) {
  155. for (int i = 0; i < output.length; i++) {
  156. if (values[i] >= 0) {
  157. output[i] = catagories[values[i]];
  158. } else {
  159. output[i] = "n/a";
  160. }
  161. }
  162. }
  163. } catch (Exception e) {
  164. System.out.println("Error with shapefile: " + filename);
  165. e.printStackTrace();
  166. }
  167. }
  168. public static ArrayList<String[]> sampling(ArrayList<String> facetIds, double[][] points) {
  169. long start = System.currentTimeMillis();
  170. int threadCount = Integer.parseInt(CommonData.settings.get("sampling_thread_count"));
  171. SamplingThread[] threads = new SamplingThread[threadCount];
  172. LinkedBlockingQueue<Integer> lbq = new LinkedBlockingQueue();
  173. CountDownLatch cdl = new CountDownLatch(facetIds.size());
  174. ArrayList<String[]> output = new ArrayList<String[]>();
  175. for (int i = 0; i < facetIds.size(); i++) {
  176. output.add(new String[points.length]);
  177. lbq.add(i);
  178. }
  179. for (int i = 0; i < threadCount; i++) {
  180. threads[i] = new SamplingThread(lbq, cdl, facetIds, points, output);
  181. threads[i].start();
  182. }
  183. try {
  184. cdl.await();
  185. } catch (InterruptedException ex) {
  186. Logger.getLogger(Sampling.class.getName()).log(Level.SEVERE, null, ex);
  187. } finally {
  188. for (int i = 0; i < threadCount; i++) {
  189. try {
  190. threads[i].interrupt();
  191. } catch (Exception e) {
  192. e.printStackTrace();
  193. }
  194. }
  195. }
  196. System.out.println("End sampling, threads=" + threadCount
  197. + " layers=" + facetIds.size()
  198. + " in " + (System.currentTimeMillis() - start) + "ms");
  199. return output;
  200. }
  201. /**
  202. * get info on an analysis layer
  203. * @param id layer id as String
  204. * @return String [] with [0] = analysis id, [1] = path to grid file, [2] = analysis type
  205. */
  206. static public String[] getAnalysisLayerInfo(String id) {
  207. String analysisOutputPath = CommonData.settings.get("analysis_output_dir");
  208. String gid, filename, name;
  209. gid = filename = name = null;
  210. if (id.startsWith("species_")) {
  211. //maxent layer
  212. gid = id.substring("species_".length());
  213. filename = analysisOutputPath + File.separator + "maxent" + File.separator + gid + File.separator + gid;
  214. name = "Prediction";
  215. } else if (id.startsWith("aloc_")) {
  216. //aloc layer
  217. gid = id.substring("aloc_".length());
  218. filename = analysisOutputPath + File.separator + "aloc" + File.separator + gid + File.separator + "aloc";
  219. name = "Classification";
  220. } else if (id.startsWith("odensity_")) {
  221. //occurrence density layer
  222. gid = id.substring("odensity_".length());
  223. filename = analysisOutputPath + File.separator + "sitesbyspecies" + File.separator + gid + File.separator + "occurrence_density";
  224. name = "Occurrence Density";
  225. } else if (id.startsWith("srichness_")) {
  226. //species richness layer
  227. gid = id.substring("srichness_".length());
  228. filename = analysisOutputPath + File.separator + "sitesbyspecies" + File.separator + gid + File.separator + "species_richness";
  229. name = "Species Richness";
  230. } else if (id.endsWith("_odensity")) {
  231. //occurrence density layer
  232. gid = id.substring(0, id.length() - "odensity_".length());
  233. filename = analysisOutputPath + File.separator + "sitesbyspecies" + File.separator + gid + File.separator + "occurrence_density";
  234. name = "Occurrence Density";
  235. } else if (id.endsWith("_srichness")) {
  236. //species richness layer
  237. gid = id.substring(0, id.length() - "srichness_".length());
  238. filename = analysisOutputPath + File.separator + "sitesbyspecies" + File.separator + gid + File.separator + "species_richness";
  239. name = "Species Richness";
  240. } else if (id.startsWith("envelope_")) {
  241. //envelope layer
  242. gid = id.substring("envelope_".length());
  243. filename = analysisOutputPath + File.separator + "envelope" + File.separator + gid + File.separator + "envelope";
  244. name = "Environmental Envelope";
  245. } else if (id.startsWith("gdm_")) {
  246. //gdm layer
  247. int pos1 = id.indexOf("_");
  248. int pos2 = id.lastIndexOf("_");
  249. String[] gdmparts = new String [] {id.substring(0,pos1), id.substring(pos1+1, pos2), id.substring(pos2+1) };
  250. gid = gdmparts[2];
  251. filename = analysisOutputPath + File.separator + "gdm" + File.separator + gid + File.separator + gdmparts[1];
  252. //Layer tmpLayer = layerDao.getLayerByName(gdmparts[1].replaceAll("Tran", ""));
  253. //name = "Transformed " + tmpLayer.getDisplayname();
  254. name = "Transformed " + CommonData.getFacetLayerDisplayName(gdmparts[1].replaceAll("Tran", ""));
  255. } else if (id.contains("_")) {
  256. //2nd form of gdm layer name, why?
  257. int pos = id.indexOf("_");
  258. String[] gdmparts = new String [] {id.substring(0,pos), id.substring(pos+1) };
  259. gid = gdmparts[0];
  260. filename = analysisOutputPath + File.separator + "gdm" + File.separator + gid + File.separator + gdmparts[1] + "Tran";
  261. System.out.println("id: " + id);
  262. System.out.println("parts: " + gdmparts[0] + ", " + gdmparts[1]);
  263. System.out.println("filename: " + filename);
  264. //Layer tmpLayer = layerDao.getLayerByName(gdmparts[1].replaceAll("Tran", ""));
  265. //name = "Transformed " + tmpLayer.getDisplayname();
  266. name = "Transformed " + CommonData.getFacetLayerDisplayName(gdmparts[1]);
  267. }
  268. if (gid != null) {
  269. return new String[] {gid, filename, name};
  270. } else {
  271. return null;
  272. }
  273. }
  274. }