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();
}
}
use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.
the class CommonData method getSpeciesListCounts.
public static Map getSpeciesListCounts(boolean refresh) {
if (speciesListCounts == null || refresh) {
Map m = new HashMap();
HttpClient client = new HttpClient();
String url = biocacheServer + "/occurrences/facets/download?facets=species_guid&count=true" + "&q=geospatial_kosher:*";
LOGGER.debug(url);
GetMethod get = new GetMethod(url);
try {
client.executeMethod(get);
CSVReader csv = new CSVReader(new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream())));
String[] row;
// skip header
csv.readNext();
while ((row = csv.readNext()) != null) {
try {
m.put(row[0], Long.parseLong(row[1]));
} catch (Exception e) {
LOGGER.error("error getting species_guid,count: " + url, e);
}
}
} catch (Exception e) {
LOGGER.error("error getting species list from: " + url);
}
speciesListCountsUpdated = System.currentTimeMillis();
speciesListCounts = m;
}
return speciesListCounts;
}
use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.
the class CommonData method getSpeciesListCountsKosher.
public static Map getSpeciesListCountsKosher(boolean refresh) {
if (speciesListCountsKosher == null || refresh) {
Map m = new HashMap();
HttpClient client = new HttpClient();
String url = biocacheServer + "/occurrences/facets/download?facets=species_guid&count=true" + "&q=geospatial_kosher:true";
LOGGER.debug(url);
GetMethod get = new GetMethod(url);
try {
client.executeMethod(get);
CSVReader csv = new CSVReader(new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream())));
String[] row;
// skip header
csv.readNext();
while ((row = csv.readNext()) != null) {
try {
m.put(row[0], Long.parseLong(row[1]));
} catch (Exception e) {
LOGGER.error("error getting species_guid,count (kosher): " + url, e);
}
}
} catch (Exception e) {
LOGGER.error("error getting species list from: " + url);
}
speciesListCountsUpdatedKosher = System.currentTimeMillis();
speciesListCountsKosher = m;
}
return speciesListCountsKosher;
}
use of au.com.bytecode.opencsv.CSVReader in project neo4j-apoc-procedures by neo4j-contrib.
the class LoadCsv method csv.
@Procedure
@Description("apoc.load.csv('url',{config}) YIELD lineNo, list, map - load CSV fom URL as stream of values,\n config contains any of: {skip:1,limit:5,header:false,sep:'TAB',ignore:['tmp'],arraySep:';',mapping:{years:{type:'int',arraySep:'-',array:false,name:'age',ignore:false}}")
public Stream<CSVResult> csv(@Name("url") String url, @Name("config") Map<String, Object> config) {
boolean failOnError = booleanValue(config, "failOnError", true);
try {
CountingReader reader = FileUtils.readerFor(url);
char separator = separator(config, "sep", DEFAULT_SEP);
char arraySep = separator(config, "arraySep", DEFAULT_ARRAY_SEP);
long skip = longValue(config, "skip", 0L);
boolean hasHeader = booleanValue(config, "header", true);
long limit = longValue(config, "limit", Long.MAX_VALUE);
EnumSet<Results> results = EnumSet.noneOf(Results.class);
for (String result : value(config, "results", asList("map", "list"))) {
results.add(Results.valueOf(result));
}
List<String> ignore = value(config, "ignore", emptyList());
List<String> nullValues = value(config, "nullValues", emptyList());
Map<String, Map<String, Object>> mapping = value(config, "mapping", Collections.emptyMap());
Map<String, Mapping> mappings = createMapping(mapping, arraySep, ignore);
CSVReader csv = new CSVReader(reader, separator);
String[] header = getHeader(hasHeader, csv, ignore, mappings);
boolean checkIgnore = !ignore.isEmpty() || mappings.values().stream().anyMatch(m -> m.ignore);
return StreamSupport.stream(new CSVSpliterator(csv, header, url, skip, limit, checkIgnore, mappings, nullValues, results), false);
} catch (IOException e) {
if (!failOnError)
return Stream.of(new CSVResult(new String[0], new String[0], 0, true, Collections.emptyMap(), emptyList(), EnumSet.noneOf(Results.class)));
else
throw new RuntimeException("Can't read CSV from URL " + cleanUrl(url), e);
}
}
use of au.com.bytecode.opencsv.CSVReader in project dbeaver by dbeaver.
the class ConfigImportWizardPageCustomConnections method importCSV.
private void importCSV(ImportData importData, ImportDriverInfo driver, Reader reader) throws IOException {
final CSVReader csvReader = new CSVReader(reader);
final String[] header = csvReader.readNext();
if (ArrayUtils.isEmpty(header)) {
setErrorMessage("No connection found");
return;
}
for (; ; ) {
final String[] line = csvReader.readNext();
if (ArrayUtils.isEmpty(line)) {
break;
}
Map<String, String> conProps = new HashMap<>();
for (int i = 0; i < line.length; i++) {
if (i >= header.length) {
break;
}
conProps.put(header[i], line[i]);
}
makeConnectionFromProps(importData, driver, conProps);
}
}
Aggregations