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

http://alageospatialportal.googlecode.com/ · Java · 124 lines · 106 code · 8 blank · 10 comment · 23 complexity · d80b4af8696ce1e45550dfc2c3f59452 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.util.concurrent.ConcurrentHashMap;
  7. import java.util.concurrent.CountDownLatch;
  8. import java.util.concurrent.LinkedBlockingQueue;
  9. import org.ala.spatial.util.Grid;
  10. import org.ala.spatial.util.Layer;
  11. import org.ala.spatial.util.SimpleShapeFile;
  12. import org.ala.spatial.util.TabulationSettings;
  13. /**
  14. *
  15. * @author Adam
  16. */
  17. class LoadedPointsSamplingThread extends Thread {
  18. LinkedBlockingQueue<Integer> lbq;
  19. ConcurrentHashMap<String, String[]> output;
  20. CountDownLatch cdl;
  21. Layer[] layers;
  22. double[][] points;
  23. public LoadedPointsSamplingThread(LinkedBlockingQueue<Integer> lbq_, CountDownLatch cdl_, Layer[] layers_, double[][] points_, ConcurrentHashMap<String, String[]> output_) {
  24. lbq = lbq_;
  25. cdl = cdl_;
  26. layers = layers_;
  27. points = points_;
  28. output = output_;
  29. }
  30. public void run() {
  31. setPriority(MIN_PRIORITY);
  32. try {
  33. while (true) {
  34. Integer i = lbq.take();
  35. try {
  36. Layer l = layers[i];
  37. String[] sampling;
  38. if (l.type.equalsIgnoreCase("environmental")) {
  39. sampling = gridFileSampling(l);
  40. } else {
  41. sampling = shapeFileSampling(l);
  42. }
  43. output.put(l.name, sampling);
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. }
  47. cdl.countDown();
  48. }
  49. } catch (InterruptedException e) {
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. private String[] gridFileSampling(Layer l) {
  55. //use random access to grid file if small, so most cases.
  56. Grid grid = new Grid(TabulationSettings.environmental_data_path + l.name);
  57. float[] d = grid.getValues(points);
  58. String[] output = new String[d.length];
  59. for (int i = 0; i < d.length; i++) {
  60. if(Float.isNaN(d[i])) {
  61. output[i] = "";
  62. } else {
  63. output[i] = String.valueOf(d[i]);
  64. }
  65. }
  66. return output;
  67. }
  68. private String[] shapeFileSampling(Layer l) {
  69. SimpleShapeFile ssf = null;
  70. boolean indexed = false;
  71. try {
  72. ssf = new SimpleShapeFile(TabulationSettings.index_path
  73. + l.name);
  74. if(ssf != null) {
  75. indexed = true;
  76. }
  77. } catch (Exception e) {
  78. }
  79. if (ssf == null) {
  80. ssf = new SimpleShapeFile(TabulationSettings.environmental_data_path
  81. + l.name);
  82. }
  83. //dynamic thread count
  84. int thread_count = points.length / 20000;
  85. if (thread_count > TabulationSettings.analysis_threads) {
  86. thread_count = TabulationSettings.analysis_threads;
  87. } else if (thread_count == 0) {
  88. thread_count = 1;
  89. }
  90. int[] intersection = null;
  91. String[] nonIndexedLookup = null;
  92. if(indexed) {
  93. intersection = ssf.intersect(points, thread_count);
  94. } else {
  95. int column = ssf.getColumnIdx(l.fields[0].name);
  96. nonIndexedLookup = ssf.getColumnLookup(column);
  97. intersection = ssf.intersect(points, ssf.getColumnLookup(column), column);
  98. }
  99. String[] output = new String[intersection.length];
  100. for (int i = 0; i < intersection.length; i++) {
  101. if(intersection[i] >= 0) {
  102. if(indexed) {
  103. output[i] = ssf.getValueString(intersection[i]);
  104. } else {
  105. output[i] = nonIndexedLookup[intersection[i]];
  106. }
  107. if(output[i] != null) {
  108. output[i] = output[i].replace(",",".");
  109. }
  110. } else {
  111. output[i] = "";
  112. }
  113. }
  114. return output;
  115. }
  116. }