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