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