/alaspatial/src/main/java/org/ala/spatial/util/DataDumper.java

http://alageospatialportal.googlecode.com/ · Java · 202 lines · 135 code · 34 blank · 33 comment · 27 complexity · 9dde7304929dbba0f481efe9604620ec MD5 · raw file

  1. package org.ala.spatial.util;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.PrintWriter;
  6. import java.util.ArrayList;
  7. import java.util.Map;
  8. import org.ala.spatial.analysis.index.OccurrencesCollection;
  9. import org.ala.spatial.analysis.index.OccurrencesFilter;
  10. import org.ala.spatial.analysis.index.OccurrencesSpeciesList;
  11. import org.ala.spatial.analysis.service.ShapeIntersectionService;
  12. /**
  13. * Backend utility to generate data dumps for special request
  14. * to bypass any limits
  15. *
  16. * -a specieslist generates a species list
  17. *
  18. * -shp shapefile get the shape from the shapefile
  19. * -wkt wkt use the wkt as the shape
  20. *
  21. * -o outputfile output file, defaults to screen
  22. *
  23. * @author ajay
  24. */
  25. public class DataDumper {
  26. private static String[] cmdargs;
  27. public static String generateSpeciesList(String[] args) {
  28. cmdargs = args;
  29. return generateSpeciesList();
  30. }
  31. /**
  32. * From org.ala.spatial.analysis.service.FilteringService.getSpeciesList
  33. *
  34. * @return
  35. */
  36. public static String generateSpeciesList() {
  37. String area = getArgsValue("-wkt");
  38. if (!area.equals("")) {
  39. area = getShapefileArea();
  40. if (area.equals("")) {
  41. System.out.println("Please provide either a valid Shapefile or WKT");
  42. return usage();
  43. }
  44. }
  45. generateSpeciesList(area, getArgsValue("-o"));
  46. return "";
  47. }
  48. public static String generateSpeciesList(File shapefile, String outfile) {
  49. Map shape = ShapefileUtils.loadShapefile(shapefile);
  50. if (shape.containsKey("wkt")) {
  51. System.out.println("got wkt: " + (String)shape.get("wkt"));
  52. return generateSpeciesList((String) shape.get("wkt"), outfile);
  53. } else {
  54. return "";
  55. }
  56. }
  57. public static String generateSpeciesList(String area, String outfile) {
  58. System.out.println("parsing area...");
  59. SimpleRegion region = SimpleShapeFile.parseWKT(area);
  60. System.out.println("applying filter...");
  61. OccurrencesFilter occurrencesFilter = new OccurrencesFilter(region, 9000000);
  62. String[] output = null;
  63. System.out.print("getting species list...");
  64. ArrayList<OccurrencesSpeciesList> osl = OccurrencesCollection.getSpeciesList(occurrencesFilter);
  65. System.out.println(" done. ");
  66. if (osl != null && osl.size() > 0) {
  67. System.out.println("got " + osl.size() + " records");
  68. StringBuffer sb = new StringBuffer();
  69. for (String s : osl.get(0).getSpeciesList()) {
  70. sb.append(s).append("|");
  71. }
  72. //any species distributions?
  73. if (region != null) {
  74. int[] r = ShapeIntersectionService.getIntersections(region);
  75. if (r != null) {
  76. String[] lsids = ShapeIntersectionService.convertToLsids(r);
  77. String str = sb.toString();
  78. for (int i = 0; i < lsids.length; i++) {
  79. if (!str.contains(lsids[i])) {
  80. //append the missing entry
  81. System.out.println("getting species list from distribution...");
  82. sb.append(OccurrencesSpeciesList.getSpeciesListEntryFromADistribution(lsids[i])).append("|");
  83. }
  84. }
  85. }
  86. }
  87. System.out.println("done.");
  88. System.out.println("processing species list");
  89. output = sb.toString().split("\\|");
  90. java.util.Arrays.sort(output);
  91. }
  92. System.out.println("prettifying...");
  93. StringBuffer sb = new StringBuffer();
  94. sb.append("Family Name,Scientific Name,Common name/s,Taxon rank,Scientific Name LSID,Number of Occurrences\r\n");
  95. for (String s : output) {
  96. sb.append("\"");
  97. sb.append(s.replaceAll("\\*", "\",\""));
  98. sb.append("\"");
  99. sb.append("\r\n");
  100. }
  101. // write to file if available
  102. if (outfile != null && !outfile.equals("")) {
  103. System.out.println("writing to " + outfile + " ...");
  104. try {
  105. PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outfile)));
  106. out.write(sb.toString());
  107. out.close();
  108. } catch (Exception e) {
  109. System.out.println("Unable to write to file");
  110. e.printStackTrace(System.out);
  111. }
  112. }
  113. System.out.println("completed");
  114. return "";
  115. }
  116. private static String getShapefileArea() {
  117. String shp = getArgsValue("-shp");
  118. if (!shp.equals("")) {
  119. Map shape = ShapefileUtils.loadShapefile(new File(shp));
  120. if (shape.containsKey("wkt")) {
  121. return (String) shape.get("wkt");
  122. }
  123. }
  124. return "";
  125. }
  126. private static String getArgsValue(String key) {
  127. for (int i = 0; i < cmdargs.length; i++) {
  128. if (cmdargs[i].trim().toLowerCase().equals(key)) {
  129. if ((i + 1) < cmdargs.length - 1) {
  130. return cmdargs[i + 1].trim();
  131. } else {
  132. return usage();
  133. }
  134. }
  135. }
  136. return usage();
  137. }
  138. private static String usage() {
  139. System.out.println("Usage: ");
  140. System.out.println("\tDataDumper -a specieslist -shp /data/shapefiles/region.shp");
  141. System.out.println("\tDataDumper -a specieslist -wkt \"POLYGON((128.153 -29.349,128.153 -28.172,130.971 -28.172,130.971 -29.349,128.153 -29.349))\"");
  142. return "";
  143. }
  144. public static void main(String[] args) {
  145. if (args.length < 2) {
  146. usage();
  147. return;
  148. }
  149. cmdargs = args;
  150. if (getArgsValue("-a").toLowerCase().equals("specieslist")) {
  151. generateSpeciesList();
  152. } else {
  153. usage();
  154. }
  155. // for (int a=0; a<args.length; a++) {
  156. // String v = args[a];
  157. // if (v.trim().equals("-a")) {
  158. // if ((a+1) < args.length-1) {
  159. // if ("specieslist".equals(args[a+1].trim().toLowerCase())) {
  160. // generateSpeciesList();
  161. // }
  162. // } else {
  163. // usage();
  164. // }
  165. // }
  166. // }
  167. }
  168. }