Search in sources :

Example 16 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project hive by apache.

the class OpenCSVSerde method deserialize.

@Override
public Object deserialize(final Writable blob) throws SerDeException {
    Text rowText = (Text) blob;
    CSVReader csv = null;
    try {
        csv = newReader(new CharArrayReader(rowText.toString().toCharArray()), separatorChar, quoteChar, escapeChar);
        final String[] read = csv.readNext();
        for (int i = 0; i < numCols; i++) {
            if (read != null && i < read.length) {
                row.set(i, read[i]);
            } else {
                row.set(i, null);
            }
        }
        return row;
    } catch (final Exception e) {
        throw new SerDeException(e);
    } finally {
        if (csv != null) {
            try {
                csv.close();
            } catch (final Exception e) {
                LOG.error("fail to close csv writer ", e);
            }
        }
    }
}
Also used : CharArrayReader(java.io.CharArrayReader) CSVReader(au.com.bytecode.opencsv.CSVReader) Text(org.apache.hadoop.io.Text) IOException(java.io.IOException)

Example 17 with CSVReader

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

the class BiocacheLegendObject method loadFromCsv.

private void loadFromCsv(String legend) {
    List<String[]> csv = null;
    try {
        CSVReader csvReader = new CSVReader(new StringReader(legend));
        csv = csvReader.readAll();
        csvReader.close();
    } catch (IOException ex) {
        LOGGER.error("error reading legend: ", ex);
    }
    boolean isDecade = colourMode.startsWith(StringConstants.OCCURRENCE_YEAR_DECADE) || StringConstants.DECADE.equals(colourMode);
    boolean isYear = colourMode.contains(StringConstants.OCCURRENCE_YEAR) && !isDecade;
    int count = 0;
    int sum = 0;
    String colour = null;
    String line = null;
    StringBuilder sb = new StringBuilder();
    sb.append("name,red,green,blue,count");
    categoryNameOrder = new String[csv.size() - 1];
    for (int i = 1; i < csv.size(); i++) {
        String[] c = csv.get(i);
        String[] p = (i > 1) ? csv.get(i - 1) : null;
        if (isYear) {
            c[0] = c[0].replace(StringConstants.DATE_TIME_BEGINNING_OF_YEAR, "");
            c[0] = c[0].replace(StringConstants.DATE_TIME_END_OF_YEAR, "");
        } else if (isDecade) {
            for (int j = 0; j <= 9; j++) {
                c[0] = c[0].replace(j + StringConstants.DATE_TIME_BEGINNING_OF_YEAR, "0");
                c[0] = c[0].replace(j + StringConstants.DATE_TIME_END_OF_YEAR, "0");
            }
        }
        int rc = Integer.parseInt(c[4]);
        if (rc == 0) {
            continue;
        }
        int[] value = { readColour(c[1], c[2], c[3]), rc };
        categories.put(c[0], value);
        categoryNameOrder[i - 1] = c[0];
        double d = Double.NaN;
        try {
            d = Double.parseDouble(c[0]);
        } catch (Exception e) {
        //will use default Double.NaN if parse fails
        }
        categoriesNumeric.put((float) d, value);
        //check for endpoint (repitition of colour)
        if (p != null && c.length > 4 && p.length > 4 && p[1].equals(c[1]) && p[2].equals(c[2]) && p[3].equals(c[3]) && !isDecade && !isYear) {
            if (count == 0) {
                count = 1;
                sum = Integer.parseInt(p[4]);
            }
            count++;
            sum += Integer.parseInt(c[4]);
        } else {
            sb.append("\n");
            colour = c[1] + "," + c[2] + "," + c[3];
            line = "\"" + c[0].replace("\"", "\"\"") + "\"," + colour + "," + c[4];
            sb.append(line);
        }
    }
    //replace last line
    if (count > 0) {
        csvLegend = sb.toString().replace(line, count + " more" + "," + colour + "," + sum);
    } else {
        csvLegend = sb.toString();
    }
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) StringReader(java.io.StringReader) IOException(java.io.IOException) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException)

Example 18 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 19 with CSVReader

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

the class ToolComposer method loadLayerList.

private void loadLayerList(Reader r) throws Exception {
    CSVReader reader = new CSVReader(r);
    // one line, read it
    StringBuilder sb = new StringBuilder();
    for (String s : reader.readNext()) {
        if (sb.length() > 0) {
            sb.append(",");
        }
        sb.append(s);
    }
    reader.close();
    selectLayerFromList(sb.toString());
    updateLayerSelectionCount();
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader)

Example 20 with CSVReader

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

the class Util method getAreaChecklists.

public static String[] getAreaChecklists(String[] records) {
    String[] lines = null;
    try {
        if (records != null && records.length > 0) {
            String[][] data = new String[records.length - 1][];
            // header
            for (int i = 1; i < records.length; i++) {
                CSVReader csv = new CSVReader(new StringReader(records[i]));
                data[i - 1] = csv.readNext();
                csv.close();
            }
            java.util.Arrays.sort(data, new Comparator<String[]>() {

                @Override
                public int compare(String[] o1, String[] o2) {
                    // compare WMS urls
                    String s1 = CommonData.getSpeciesChecklistWMSFromSpcode(o1[0])[1];
                    String s2 = CommonData.getSpeciesChecklistWMSFromSpcode(o2[0])[1];
                    if (s1 == null && s2 == null) {
                        return 0;
                    } else if (s1 != null && s2 != null) {
                        return s1.compareTo(s2);
                    } else if (s1 == null) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            });
            lines = new String[records.length];
            lines[0] = lines[0] = "SPCODE,SCIENTIFIC_NAME,AUTHORITY_FULL,COMMON_NAME,FAMILY,GENUS_NAME,SPECIFIC_NAME,MIN_DEPTH,MAX_DEPTH,METADATA_URL,LSID,AREA_NAME,AREA_SQ_KM,SPECIES_COUNT";
            int len = 1;
            int thisCount = 0;
            for (int i = 0; i < data.length; i++) {
                thisCount++;
                String s1 = CommonData.getSpeciesChecklistWMSFromSpcode(data[i][0])[1];
                String s2 = i + 1 < data.length ? CommonData.getSpeciesChecklistWMSFromSpcode(data[i + 1][0])[1] : null;
                if (i == data.length - 1 || (s1 == null && s2 != null) || (s1 != null && s2 == null) || (s1 != null && s2 != null && !s1.equals(s2))) {
                    StringBuilder sb = new StringBuilder();
                    for (int j = 0; j < data[i].length; j++) {
                        if (j > 0) {
                            sb.append(",");
                        }
                        if (j == 0 || (j >= 9 && j != 10)) {
                            sb.append(Util.wrap(data[i][j]));
                        }
                    }
                    sb.append(",").append(thisCount);
                    lines[len] = sb.toString();
                    len++;
                    thisCount = 0;
                }
            }
            lines = java.util.Arrays.copyOf(lines, len);
        }
    } catch (Exception e) {
        LOGGER.error("error building species checklist", e);
        lines = null;
    }
    return lines;
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) StringReader(java.io.StringReader) ParseException(com.vividsolutions.jts.io.ParseException)

Aggregations

CSVReader (au.com.bytecode.opencsv.CSVReader)32 StringReader (java.io.StringReader)12 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)6 HttpClient (org.apache.commons.httpclient.HttpClient)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 InputStreamReader (java.io.InputStreamReader)4 HashMap (java.util.HashMap)4 JSONObject (org.json.simple.JSONObject)4 JSONParser (org.json.simple.parser.JSONParser)4 Query (au.org.ala.spatial.util.Query)3 FileReader (java.io.FileReader)3 ZipEntry (java.util.zip.ZipEntry)3 ZipInputStream (java.util.zip.ZipInputStream)3 JSONArray (org.json.simple.JSONArray)3 ParseException (org.json.simple.parser.ParseException)3 Event (org.zkoss.zk.ui.event.Event)3 Facet (au.org.ala.legend.Facet)2 LegendObject (au.org.ala.legend.LegendObject)2 SelectedArea (au.org.emii.portal.menu.SelectedArea)2