PageRenderTime 64ms CodeModel.GetById 15ms app.highlight 7ms RepoModel.GetById 39ms app.codeStats 0ms

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