PageRenderTime 42ms CodeModel.GetById 19ms app.highlight 20ms RepoModel.GetById 0ms app.codeStats 0ms

/alaspatial/src/main/java/org/ala/spatial/analysis/index/OccurrencesCollection.java

http://alageospatialportal.googlecode.com/
Java | 493 lines | 366 code | 67 blank | 60 comment | 131 complexity | 1960eec12473984e4018bf7f0cf3c7bb 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.index;
  6
  7import java.util.ArrayList;
  8import java.util.TreeSet;
  9import java.util.Vector;
 10import org.ala.spatial.analysis.cluster.Record;
 11import org.ala.spatial.util.SimpleRegion;
 12import org.ala.spatial.util.TabulationSettings;
 13
 14/**
 15 *
 16 * @author Adam
 17 */
 18public class OccurrencesCollection {
 19
 20    static ArrayList<Dataset> datasets = null;
 21
 22    static public void init() {
 23        datasets = new ArrayList<Dataset>();
 24    }
 25
 26    public static ArrayList<OccurrenceRecordNumbers> getRecordNumbers(OccurrencesFilter filter) {
 27        ArrayList<OccurrenceRecordNumbers> orn = new ArrayList<OccurrenceRecordNumbers>();
 28
 29        //get OccurrenceSpecies from all enabled and ready datasets
 30        for (Dataset d : datasets) {
 31            if (d.isEnabled() && d.isReady()) {
 32                int[] r = d.getOccurrencesIndex().getRecordNumbers(filter);
 33                if (r != null) {
 34                    orn.add(new OccurrenceRecordNumbers(d.getUniqueName(), r));
 35                }
 36            }
 37        }
 38        return orn;
 39    }
 40
 41    public static double[] getPoints(OccurrencesFilter filter) {
 42        ArrayList<double[]> ap = new ArrayList<double[]>();
 43
 44        //get OccurrenceSpecies from all enabled and ready datasets
 45        int count = 0;
 46        for (Dataset d : datasets) {
 47            if (d.isEnabled() && d.isReady()) {
 48                double[] p = d.getOccurrencesIndex().getPoints(filter);
 49                if (p != null) {
 50                    count += p.length;
 51                    ap.add(p);
 52                }
 53            }
 54        }
 55
 56        double[] ps;
 57        if(ap.size() == 1) {
 58            ps = ap.get(0);
 59        } else {
 60            ps = new double[count];
 61            int p = 0;
 62            for (int i = 0; i < ap.size(); i++) {
 63                System.arraycopy(ap.get(i),0,ps,p,ap.get(i).length);
 64                p += ap.get(i).length;
 65            }
 66        }
 67
 68        return ps;
 69    }
 70
 71    public static double[] getPoints(/*SpeciesColourOption sco*/ String lookupName, String key) {
 72        ArrayList<double[]> ap = new ArrayList<double[]>();
 73
 74        //get OccurrenceSpecies from all enabled and ready datasets
 75        int count = 0;
 76        for (Dataset d : datasets) {
 77            if (d.isEnabled() && d.isReady()) {
 78                double[] p = d.getOccurrencesIndex().getPoints(/*sco*/ lookupName, key);
 79                if (p != null) {
 80                    count += p.length;
 81                    ap.add(p);
 82                }
 83            }
 84        }
 85
 86        double[] ps;
 87        if(ap.size() == 1) {
 88            ps = ap.get(0);
 89        } else {
 90            ps = new double[count];
 91            int p = 0;
 92            for (int i = 0; i < ap.size(); i++) {
 93                System.arraycopy(ap.get(i),0,ps,p,ap.get(i).length);
 94                p += ap.get(i).length;
 95            }
 96        }
 97
 98        return ps;
 99    }
100
101    public static double[] getPoints(OccurrencesFilter filter, ArrayList<SpeciesColourOption> extra) {
102        ArrayList<double[]> ap = new ArrayList<double[]>();
103
104        //get OccurrenceSpecies from all enabled and ready datasets
105        int count = 0;
106        for (Dataset d : datasets) {
107            if (d.isEnabled() && d.isReady()) {
108                ArrayList<SpeciesColourOption> extraPart = null;
109                if (extra != null && extra.size() > 0) {
110                    extraPart = new ArrayList<SpeciesColourOption>(extra.size());
111                    for (int i = 0; i < extra.size(); i++) {
112                        extraPart.add(SpeciesColourOption.fromSpeciesColourOption(extra.get(i)));
113                    }
114                }
115                double[] p = d.getOccurrencesIndex().getPoints(filter, extraPart);
116                if (p != null) {
117                    count += p.length;
118                    ap.add(p);
119
120                    if (extra != null && extra.size() > 0) {
121                        for (int i = 0; i < extra.size(); i++) {
122                            extra.get(i).append(extraPart.get(i));
123                        }
124                    }
125                }
126            }
127        }
128
129        double[] ps;
130        if(ap.size() == 1) {
131            ps = ap.get(0);
132        } else {
133            ps = new double[count];
134            int p = 0;
135            for (int i = 0; i < ap.size(); i++) {
136                System.arraycopy(ap.get(i),0,ps,p,ap.get(i).length);
137                p += ap.get(i).length;
138            }
139        }
140
141        return ps;
142    }
143
144    public static ArrayList<String> getFullRecords(OccurrencesFilter filter) {
145        ArrayList<String> as = new ArrayList<String>();
146
147        for (Dataset d : datasets) {
148            if (d.isEnabled() && d.isReady()) {
149                ArrayList<String> s = d.getOccurrencesIndex().getFullRecords(filter);
150                if (s != null) {
151                    as.addAll(s);
152                }
153            }
154        }
155
156        return as;
157    }
158
159    /*static String [][] getPartialRecords(<OccurrencesFilter filter) {
160    ArrayList<String[][]> as = new ArrayList<String[][]>();
161
162    for(Dataset d : datasets) {
163    if(d.isEnabled()) {
164    ArrayList<String[][]> s = d.getOccurrencesIndex().getPartialRecords(filter);
165    if(s != null) {
166    as.addAll(s);
167    }
168    }
169    }
170
171    return as;
172    }*/
173    /**
174     * get species 
175     * @param filters
176     * @return
177     */
178    public static ArrayList<OccurrencesSpeciesList> getSpeciesList(OccurrencesFilter filter) {
179        ArrayList<OccurrencesSpeciesList> al = new ArrayList<OccurrencesSpeciesList>();
180
181        for (Dataset d : datasets) {
182            if (d.isEnabled() && d.isReady()) {
183                OccurrencesSpeciesList osl = new OccurrencesSpeciesList(d.getUniqueName(), filter);
184                if (osl.getSpeciesCount() > 0) {
185                    al.add(osl);
186                }
187            }
188        }
189
190        //TODO merge arrays
191
192        return al;
193    }
194
195    public static Dataset getDataset(String id) {
196        for (Dataset d : datasets) {
197            if (d.getUniqueName().equals(id)) {
198                return d;
199            }
200        }
201        return null;
202    }
203
204    public static String[] getFirstName(String species) {
205        return SpeciesIndex.getFirstName(species);
206    }
207
208    public static String getCommonNames(String name, String[] aslist, int maxRecords) {
209        return SpeciesIndex.getCommonNames(name, aslist, maxRecords);
210    }
211
212    public static ArrayList<OccurrenceRecordNumbers> lookup(String key, String value) {
213        ArrayList<OccurrenceRecordNumbers> records = new ArrayList<OccurrenceRecordNumbers>();
214        for (Dataset d : datasets) {
215            if (d.isEnabled() && d.isReady()) {
216                int[] s = d.getOccurrencesIndex().lookup(key, value);
217                if (s != null) {
218                    records.add(new OccurrenceRecordNumbers(d.getUniqueName(), s));
219                }
220            }
221        }
222
223        if (records.size() == 0) {
224            records = null;
225        }
226
227        return records;
228    }
229
230    /**
231     *
232     * @param id dataset uniqueId as String
233     * @return OccurrencesIndex for this dataset, if it is ready.  Otherwise null.
234     */
235    static OccurrencesIndex getOccurrencesIndex(String id) {
236        Dataset d = getDataset(id);
237        if (d != null) {
238            return d.getOccurrencesIndex();
239        }
240        return null;
241    }
242
243    public static Vector getRecords(OccurrencesFilter occurrencesFilter) {
244        Vector<Record> ap = new Vector<Record>();
245
246        //get OccurrenceSpecies from all enabled and ready datasets
247        for (Dataset d : datasets) {
248            if (d.isEnabled() && d.isReady()) {
249                Vector<Record> p = d.getOccurrencesIndex().getRecords(occurrencesFilter);
250                if (p != null) {
251                    ap.addAll(p);
252                }
253            }
254        }
255
256        return ap;
257    }
258
259    /**
260     * returns a list of (species names / type / count) for valid
261     * .beginsWith matches
262     *
263     * @param filter begins with text to search for (NOT LSID, e.g. species name)
264     * @param limit limit on output
265     * @return formatted species matches as String[]
266     */
267    public static String[] findSpecies(String filter, int limit) {
268        return SpeciesIndex.filterBySpeciesName(filter, limit);
269    }
270
271    public static double[] getPointsMinusSensitiveSpecies(OccurrencesFilter occurrencesFilter, StringBuffer removedSpecies) {
272        ArrayList<double[]> ap = new ArrayList<double[]>();
273
274        //get OccurrenceSpecies from all enabled and ready datasets
275        int count = 0;
276        for (Dataset d : datasets) {
277            if (d.isEnabled() && d.isReady()) {
278                double[] p = d.getOccurrencesIndex().getPointsMinusSensitiveSpecies(occurrencesFilter, removedSpecies);
279                if (p != null) {
280                    count += p.length;
281                    ap.add(p);
282                }
283            }
284        }
285
286        if (count == 0) {
287            return null;
288        }
289
290        double[] ps = new double[count];
291        int p = 0;
292        for (int i = 0; i < ap.size(); i++) {
293            for (double d : ap.get(i)) {
294                ps[p] = d;
295                p++;
296            }
297        }
298
299        return ps;
300    }
301
302    public static ArrayList<OccurrenceRecordNumbers> getGridSampleSet(LayerFilter new_filter) {
303        ArrayList<OccurrenceRecordNumbers> output = new ArrayList<OccurrenceRecordNumbers>();
304        for (Dataset d : datasets) {
305            if (d.isEnabled() && d.isReady()) {
306                FilteringIndex fi = d.getFilteringIndex();
307                if (fi != null) {
308                    int[] records = fi.getGridSampleSet(new_filter);
309                    if (records != null) {
310                        OccurrenceRecordNumbers orn = new OccurrenceRecordNumbers(d.getUniqueName(), records);
311                        output.add(orn);
312                    }
313                }
314            }
315        }
316        return output;
317    }
318
319    public static ArrayList<OccurrenceRecordNumbers> getCatagorySampleSet(LayerFilter new_filter) {
320        throw new UnsupportedOperationException("Not yet implemented");
321    }
322
323    public static OccurrencesSpeciesList getSpeciesList(ArrayList<OccurrenceRecordNumbers> rk) {
324        ArrayList<OccurrencesSpeciesList> al = new ArrayList<OccurrencesSpeciesList>();
325
326        for (Dataset d : datasets) {
327            if (d.isEnabled() && d.isReady()) {
328                for (int i = 0; i < rk.size(); i++) {
329                    if (!rk.get(i).getName().equals(d.getUniqueName())) {
330                        continue;
331                    }
332                    OccurrencesSpeciesList osl = new OccurrencesSpeciesList(d.getUniqueName(),
333                            new OccurrencesFilter(rk, 100000000));
334                    if (osl != null && osl.getSpeciesCount() > 0) {
335                        al.add(osl);
336                    }
337                }
338            }
339        }
340
341        //TODO: merge results
342        if (al.size() > 0) {
343            return al.get(0);
344        }
345
346        return null;
347    }
348
349    /**
350     *
351     * @param keyEnd
352     * @param lsid
353     * @param layer1
354     * @param x1
355     * @param x2
356     * @param layer2
357     * @param y1
358     * @param y2
359     * @return number of records highlighted as int
360     */
361    public static int highlightLsid(String keyEnd, String lsid, Object [] filters) {
362        int count = 0;
363
364        for (Dataset d : datasets) {
365            if (d.isEnabled() && d.isReady()) {
366                count += d.getOccurrencesIndex().highlightLsid(keyEnd, lsid, filters);
367            }
368        }
369        return count;
370    }
371
372    public static int registerLSIDArea(String id, String lsid, SimpleRegion region) {
373        int count = 0;
374
375        for (Dataset d : datasets) {
376            if (d.isEnabled() && d.isReady()) {
377                count += d.getOccurrencesIndex().registerLSIDArea(id, lsid, region);
378            }
379        }
380        return count;
381    }
382
383    public static int registerLSIDRecords(String id, String lsid, ArrayList<OccurrenceRecordNumbers> records) {
384        int count = 0;
385
386        for (Dataset d : datasets) {
387            if (d.isEnabled() && d.isReady()) {
388                count += d.getOccurrencesIndex().registerLSIDRecords(id, lsid, records);
389            }
390        }
391        return count;
392    }
393
394    public static int registerLSID(String id, String[] lsids) {
395        int count = 0;
396
397        for (Dataset d : datasets) {
398            if (d.isEnabled() && d.isReady()) {
399                count += d.getOccurrencesIndex().registerLSID(id, lsids);
400            }
401        }
402        return count;
403    }
404
405    public static int registerArea(String id, SimpleRegion region) {
406        int count = 0;
407
408        for (Dataset d : datasets) {
409            if (d.isEnabled() && d.isReady()) {
410                count += d.getOccurrencesIndex().registerArea(id, region);
411            }
412        }
413        return count;
414    }
415
416    public static int registerRecords(String id, ArrayList<OccurrenceRecordNumbers> records) {
417        int count = 0;
418
419        for (Dataset d : datasets) {
420            if (d.isEnabled() && d.isReady()) {
421                count += d.getOccurrencesIndex().registerRecords(id, records);
422            }
423        }
424        return count;
425    }
426
427    public static int registerHighlight(String lsid, String id, String pid, boolean include) {
428        int count = 0;
429
430        for (Dataset d : datasets) {
431            if (d.isEnabled() && d.isReady()) {
432                count += d.getOccurrencesIndex().registerHighlight(new OccurrencesFilter(lsid, 10000000), id, pid, include);
433            }
434        }
435        return count;
436    }
437
438    public static String getColourLegend(String lsid, String colourMode) {
439        throw new UnsupportedOperationException("Not yet implemented");
440    }
441
442    static String[] listLookups() {
443        TreeSet<String> ts = new TreeSet<String>();
444        for (Dataset d : datasets) {
445            if (d.isEnabled() && d.isReady()) {
446                String [] as = d.getOccurrencesIndex().listLookups();
447                if(as != null) {
448                    for(String s : as) {
449                        ts.add(s);
450                    }
451                }
452            }
453        }
454
455        if(ts.size() == 0) {
456            return null;
457        } else {
458            String [] as = new String[ts.size()];
459            int p = 0;
460            for(String s : ts) {
461                as[p] = s;
462                p++;
463            }
464            return as;
465        }
466    }
467
468    static String[] listLookupKeys(String lookupName) {
469        TreeSet<String> ts = new TreeSet<String>();
470        for (Dataset d : datasets) {
471            if (d.isEnabled() && d.isReady()) {
472                String [] as = d.getOccurrencesIndex().listLookupKeys(lookupName);
473                if(as != null) {
474                    for(String s : as) {
475                        ts.add(s);
476                    }
477                }
478            }
479        }
480
481        if(ts.size() == 0) {
482            return null;
483        } else {
484            String [] as = new String[ts.size()];
485            int p = 0;
486            for(String s : ts) {
487                as[p] = s;
488                p++;
489            }
490            return as;
491        }
492    }
493}