Search in sources :

Example 51 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.

the class AreaReportPDF method speciesPage.

private void speciesPage(boolean isSpecies, FileWriter fw, String areaName, String title, String notes, int tableNumber, int count, int countKosher, int figureNumber, String imageUrl, String csv) throws Exception {
    if (count < 0)
        count = 0;
    String imageUrlActual = imageUrl;
    if (imageUrlActual != null) {
        imageUrlActual = filePath + "/" + imageUrlActual;
    }
    fw.write("<table id='species'>");
    fw.write("<tr>");
    fw.write("<td id='title'><h1>");
    fw.write(title);
    fw.write("</h1></td>");
    fw.write("</tr><tr>");
    fw.write("<td>");
    fw.write("<br />Number of " + title.toLowerCase() + ": <b>" + count + "</b>");
    fw.write("</td>");
    fw.write("</tr><tr>");
    fw.write("<td><br />");
    fw.write(notes);
    fw.write("</td>");
    fw.write("</tr><tr>");
    if (countKosher >= 0) {
        fw.write("<td>");
        fw.write("<br />Number of " + title.toLowerCase() + " (spatially valid only): <b>" + countKosher + "</b>");
        fw.write("</td>");
        fw.write("</tr><tr>");
    }
    if ((count > 0 || countKosher > 0) && imageUrlActual != null) {
        fw.write("<td>");
        fw.write("<br /><img src='" + imageUrlActual + "' />");
        fw.write("</td>");
        fw.write("</tr><tr>");
        fw.write("<td id='figure'>");
        fw.write("<b>Figure " + figureNumber + ":</b> Map of " + title + " in " + areaName);
        fw.write("</td>");
        fw.write("</tr><tr>");
    }
    // tabulation table
    if ((count > 0 || countKosher > 0) && csv != null) {
        CSVReader r = new CSVReader(new StringReader(csv));
        fw.write("<td id='tableNumber'><br /><b>Table " + tableNumber + ":</b> " + title);
        if (speciesLinks.get(title.replace("lifeform - ", "")) != null) {
            fw.write("<a href='" + speciesLinks.get(title.replace("lifeform - ", "")) + "'>(Link to full list)</a>");
        }
        fw.write("</td></tr><tr><td>");
        fw.write("<table id='table'>");
        // reorder select columns
        int[] columnOrder;
        if (isSpecies) {
            columnOrder = new int[] { 8, 1, 10, 11 };
            fw.write("<tr><td>Family</td><td id='scientificName' >Scientific Name</td><td>Common Name</td><td>No. Occurrences</td>");
            for (int i = 0; i < CommonData.getSpeciesListAdditionalColumnsHeader().size(); i++) {
                columnOrder = Arrays.copyOf(columnOrder, columnOrder.length + 1);
                columnOrder[columnOrder.length - 1] = 11 + 1 + i;
                fw.write("<td>" + CommonData.getSpeciesListAdditionalColumnsHeader().get(i) + "</td>");
            }
            fw.write("</tr>");
        } else if ("JournalMap Articles".equals(title)) {
            // authors (last_name, first_name), publish_year, title, publication.name, doi, JournalMap URL
            columnOrder = new int[] { 0, 1, 2, 3, 4, 5 };
            fw.write("<tr><td>Author/s</td><td>Year</td><td>Title</td><td>Publication</td><td>DOI</td><td>URL</td></tr>");
        } else {
            columnOrder = new int[] { 4, 1, 3, 7, 8, 11, 12 };
            fw.write("<tr><td>Family</td><td id='scientificName' >Scientific Name</td><td>Common Name</td><td>Min Depth</td><td>Max Depth</td><td>Area Name</td><td>Area sq km</td></tr>");
        }
        // use fixed header
        r.readNext();
        String[] line;
        int row = 0;
        while ((line = r.readNext()) != null) {
            fw.write("<tr>");
            for (int i = 0; i < columnOrder.length && columnOrder[i] < line.length; i++) {
                fw.write("<td><div>" + line[columnOrder[i]] + "</div></td>");
            }
            fw.write("</tr>");
            row++;
        }
        fw.write("</table>");
        fw.write("<td>");
    }
    fw.write("</tr>");
    fw.write("</table>");
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader)

Example 52 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.

the class PointComparison method onClick$btnCompare.

public void onClick$btnCompare(Event event) {
    try {
        // sampling
        String url = CommonData.getSettings().getProperty("layers_batch_intersect_url") + "/intersect/batch";
        NameValuePair[] params = new NameValuePair[2];
        params[0] = new NameValuePair("fids", layerList());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < points.size(); i++) {
            try {
                // validate points as doubles
                String part = "" + Double.parseDouble(points.get(i)[1]) + "," + Double.parseDouble(points.get(i)[0]);
                if (sb.length() > 0) {
                    sb.append(",");
                }
                sb.append(part);
            } catch (Exception e) {
            }
        }
        params[1] = new NameValuePair("points", sb.toString());
        String batchId = Util.readUrlPost(url, params);
        JSONParser jp = new JSONParser();
        batchId = ((JSONObject) jp.parse(batchId)).get("batchId").toString();
        LOGGER.debug("batch intersect id: " + batchId);
        Thread.sleep(2000);
        // 30s
        long maxtime = 30000;
        long starttime = System.currentTimeMillis();
        String csv = "";
        while (starttime + maxtime > System.currentTimeMillis()) {
            Thread.sleep(2000);
            String response = Util.readUrl(url + "/" + batchId);
            try {
                String status = ((JSONObject) jp.parse(response)).get("status").toString();
                if ("finished".equals(status)) {
                    URL get = new URL(((JSONObject) jp.parse(response)).get("downloadUrl").toString());
                    InputStream is = get.openStream();
                    ZipInputStream zip = new ZipInputStream(is);
                    ZipEntry ze = zip.getNextEntry();
                    if (ze != null) {
                        csv = new String(StreamUtils.getBytes(zip), "UTF-8");
                    }
                    zip.close();
                    is.close();
                    break;
                }
            } catch (Exception e) {
            }
        }
        // transpose csv and merge longitude & latitude columns
        List<String[]> data = new CSVReader(new StringReader(csv)).readAll();
        List<Integer> rowNumbers = new ArrayList<Integer>();
        for (int j = 2; j < data.get(0).length; j++) {
            for (int i = 1; i < data.size(); i++) {
                if (data.get(i)[j] != null && !data.get(i)[j].isEmpty() && !"n/a".equals(data.get(i)[j])) {
                    rowNumbers.add(j);
                    break;
                }
            }
        }
        String[][] tdata = new String[rowNumbers.size() + 1][data.size()];
        StringBuilder tcsv = new StringBuilder();
        int row = 0;
        for (int i = 0; i < data.size(); i++) {
            if (i > 0) {
                tcsv.append(",");
            }
            tdata[row][i] = data.get(i)[0] + " " + data.get(i)[1];
            tcsv.append(tdata[0][i]);
        }
        row++;
        for (int j = 2; j < data.get(0).length; j++) {
            if (rowNumbers.size() > 0 && rowNumbers.get(0) == j) {
                rowNumbers.remove(0);
                tcsv.append("\n");
                for (int i = 0; i < data.size(); i++) {
                    if (i > 0) {
                        tcsv.append(",");
                    }
                    if (i == 0) {
                        tcsv.append("\"").append(layerDisplayNames.get(data.get(i)[j])).append("\"");
                        tdata[row][i] = layerDisplayNames.get(data.get(i)[j]);
                    } else {
                        tcsv.append(data.get(i)[j]);
                        tdata[row][i] = data.get(i)[j];
                    }
                }
                row++;
            }
        }
        comparisonCsv = tcsv.toString();
        btnDownload.setDisabled(false);
        lbResults.setModel(new SimpleListModel<String[]>(tdata));
    } catch (Exception e) {
        LOGGER.error("error comparing points", e);
    }
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) CSVReader(au.com.bytecode.opencsv.CSVReader) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) URL(java.net.URL) ZipInputStream(java.util.zip.ZipInputStream) JSONObject(org.json.simple.JSONObject) StringReader(java.io.StringReader) JSONParser(org.json.simple.parser.JSONParser)

Example 53 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.

the class BiocacheQuery method speciesList.

/**
 * Get species list for this query.
 *
 * @return species list as String containing CSV.
 */
@Override
public String speciesList() {
    if (speciesList != null) {
        return speciesList;
    }
    HttpClient client = new HttpClient();
    String url = biocacheServer + SPECIES_LIST_SERVICE_CSV + "&q=" + getQ() + getQc();
    LOGGER.debug(url);
    GetMethod get = new GetMethod(url);
    try {
        client.executeMethod(get);
        speciesList = get.getResponseBodyAsString();
        // add 'Other' correction and add additional columns
        List<String> header = CommonData.getSpeciesListAdditionalColumnsHeader();
        StringBuilder newlist = new StringBuilder();
        int total = getOccurrenceCount();
        CSVReader csv = new CSVReader(new StringReader(speciesList));
        String[] line;
        int count = 0;
        int lastpos = 0;
        while ((line = csv.readNext()) != null) {
            int nextpos = speciesList.indexOf('\n', lastpos + 1);
            if (nextpos < 0)
                nextpos = speciesList.length();
            newlist.append(speciesList.substring(lastpos, nextpos));
            List<String> list = header;
            if (lastpos != 0) {
                list = CommonData.getSpeciesListAdditionalColumns(header, line[0]);
            }
            for (int i = 0; i < list.size(); i++) {
                newlist.append(",\"").append(list.get(i).replace("\"", "\"\"").replace("\\", "\\\\")).append("\"");
            }
            lastpos = nextpos;
            try {
                count += Integer.parseInt(line[line.length - 1]);
            } catch (Exception e) {
            }
        }
        if (total - count > 0) {
            String correction = "\n,,,,,,,,,,Other (not species rank)," + (total - count);
            newlist.append(correction);
        }
        speciesList = newlist.toString();
    } catch (Exception e) {
        LOGGER.error("error getting species list from: " + url);
    }
    return speciesList;
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 54 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.

the class MapComposer method openChecklistSpecies.

void openChecklistSpecies(String lsids, String wkt, boolean mapIfOnlyOne) {
    try {
        // species checklists
        String[] finallist = Util.getDistributionsOrChecklists(StringConstants.CHECKLISTS, wkt, lsids, null);
        // open for optional mapping of areas
        if (finallist.length > 1) {
            if (mapIfOnlyOne && finallist.length == 2) {
                try {
                    String[] row;
                    CSVReader csv = new CSVReader(new StringReader(finallist[1]));
                    row = csv.readNext();
                    csv.close();
                    if (getMapLayerWMS(CommonData.getSpeciesChecklistWMSFromSpcode(row[0])[1]) == null) {
                        // map it
                        String[] mapping = CommonData.getSpeciesChecklistWMSFromSpcode(row[0]);
                        String displayName = mapping[0] + " area";
                        if (row[11] != null && row[11].length() > 0) {
                            displayName = row[11];
                        }
                        String layerName = getNextAreaLayerName(row[0] + " area");
                        String html = Util.getMetadataHtmlForDistributionOrChecklist(row[0], row, layerName);
                        MapLayer ml = getMapComposer().addWMSLayer(layerName, displayName, mapping[1], 0.6f, html, null, LayerUtilitiesImpl.WKT, null, null);
                        ml.setSPCode(row[0]);
                        setupMapLayerAsDistributionArea(ml);
                    }
                } catch (Exception e) {
                    LOGGER.error("error opening checklist species", e);
                }
            } else {
                if (hasFellow(StringConstants.DISTRIBUTION_RESULTS)) {
                    getFellowIfAny(StringConstants.DISTRIBUTION_RESULTS).detach();
                }
                Map params = new HashMap();
                params.put(StringConstants.TITLE, "Checklist species");
                params.put(StringConstants.SIZE, String.valueOf(finallist.length - 1));
                params.put(StringConstants.TABLE, finallist);
                Window window = (Window) Executions.createComponents("WEB-INF/zul/results/AnalysisDistributionResults.zul", this, params);
                try {
                    window.setParent(this);
                    window.doModal();
                } catch (Exception e) {
                    LOGGER.error("error opening checklist species dialog", e);
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("error opening distribution area dialog", e);
    }
}
Also used : Window(org.zkoss.zul.Window) CSVReader(au.com.bytecode.opencsv.CSVReader) HasMapLayer(au.org.emii.portal.menu.HasMapLayer) MapLayer(au.org.emii.portal.menu.MapLayer) RemoteMap(au.org.emii.portal.util.RemoteMap) ParseException(org.json.simple.parser.ParseException)

Example 55 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.

the class SpeciesListResults method populateList.

public void populateList() {
    if (selectedArea == null) {
        selectedArea = new SelectedArea(null, getMapComposer().getViewArea());
    }
    try {
        Query sq = QueryUtil.queryFromSelectedArea(null, selectedArea, extraParams, false, geospatialKosher);
        sq = sq.newFacet(new Facet("occurrence_status_s", "absent", false), false);
        if (sq.getSpeciesCount() <= 0) {
            getMapComposer().showMessage("No species records in the active area.");
            results = null;
            popupListboxResults.setVisible(false);
            resultsLabel.setVisible(false);
            this.detach();
            return;
        }
        // remove header
        String speciesList = chooseEndemic ? sq.endemicSpeciesList() : sq.speciesList();
        results = speciesList.substring(speciesList.indexOf('\n') + 1).split("\n");
        java.util.Arrays.sort(results);
        // results should already be sorted
        String[] tmp = results;
        if (results.length > 200) {
            tmp = java.util.Arrays.copyOf(results, 200);
            resultsLabel.setValue("preview of first 200 of " + results.length + " species found");
        } else {
            resultsLabel.setValue("preview of all " + results.length + " species found");
        }
        popupListboxResults.setModel(new ListModelArray(tmp, false));
        popupListboxResults.setItemRenderer(new ListitemRenderer() {

            public void render(Listitem li, Object data, int itemIdx) {
                String s = (String) data;
                CSVReader reader = new CSVReader(new StringReader(s));
                String[] ss;
                try {
                    ss = reader.readNext();
                } catch (Exception e) {
                    ss = new String[0];
                }
                if (ss == null || ss.length == 0) {
                    return;
                }
                Listcell lc = new Listcell(ss[0]);
                lc.setParent(li);
                int col = 1;
                while (col < ss.length) {
                    lc = new Listcell(ss[col]);
                    lc.setParent(li);
                    col++;
                }
                try {
                    reader.close();
                } catch (IOException e) {
                    LOGGER.error("error closing after reading species list", e);
                }
            }
        });
    } catch (Exception e) {
        LOGGER.error("error reading species list data", e);
    }
}
Also used : Query(au.org.ala.spatial.util.Query) CSVReader(au.com.bytecode.opencsv.CSVReader) SelectedArea(au.org.emii.portal.menu.SelectedArea) IOException(java.io.IOException) IOException(java.io.IOException) StringReader(java.io.StringReader) Facet(au.org.ala.legend.Facet)

Aggregations

CSVReader (au.com.bytecode.opencsv.CSVReader)82 IOException (java.io.IOException)29 InputStreamReader (java.io.InputStreamReader)27 ArrayList (java.util.ArrayList)16 FileReader (java.io.FileReader)11 StringReader (java.io.StringReader)11 HashMap (java.util.HashMap)9 BufferedReader (java.io.BufferedReader)8 InputStream (java.io.InputStream)6 File (java.io.File)5 Reader (java.io.Reader)5 HttpClient (org.apache.commons.httpclient.HttpClient)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 Test (org.junit.Test)5 DBException (org.jkiss.dbeaver.DBException)4 Query (au.org.ala.spatial.util.Query)3 TransformationExample (eu.esdihumboldt.cst.test.TransformationExample)3 Date (java.util.Date)3 LinkedHashMap (java.util.LinkedHashMap)3 JSONArray (org.json.simple.JSONArray)3