PageRenderTime 33ms CodeModel.GetById 13ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

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