use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.
the class PhylogeneticDiversityListResults method evalArea.
private void evalArea(SelectedArea sa) {
try {
Query sq = QueryUtil.queryFromSelectedArea(selectedQuery, sa, null, false, null);
CSVReader r = new CSVReader(new StringReader(sq.speciesList()));
JSONArray ja = new JSONArray();
for (String[] s : r.readAll()) {
ja.add(s[1]);
}
//call pd with specieslist=ja.toString()
String url = CommonData.getSettings().getProperty(CommonData.PHYLOLIST_URL) + "/phylo/getPD";
NameValuePair[] params = new NameValuePair[2];
params[0] = new NameValuePair("noTreeText", StringConstants.TRUE);
params[1] = new NameValuePair("speciesList", ja.toString());
JSONParser jp = new JSONParser();
JSONArray pds = (JSONArray) jp.parse(Util.readUrlPost(url, params));
Map<String, String> pdrow = new HashMap<String, String>();
Map<String, JSONArray> speciesRow = new HashMap<String, JSONArray>();
for (int j = 0; j < pds.size(); j++) {
String tree = "" + ((JSONObject) pds.get(j)).get(StringConstants.STUDY_ID);
pdrow.put(tree, ((JSONObject) pds.get(j)).get("pd").toString());
speciesRow.put(tree, (JSONArray) ((JSONObject) pds.get(j)).get("taxaRecognised"));
//maxPD retrieval
String maxPd = ((JSONObject) pds.get(j)).get("maxPd").toString();
for (int k = 0; k < selectedTrees.size(); k++) {
if (((Map<String, String>) selectedTrees.get(k)).get(StringConstants.STUDY_ID).equals(tree)) {
((Map<String, String>) selectedTrees.get(k)).put("maxPd", maxPd);
}
}
}
areaPds.add(pdrow);
areaSpeciesMatches.add(speciesRow);
} catch (Exception e) {
LOGGER.error("failed processing a pd for a selected area.", e);
}
}
use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.
the class PhylogeneticDiversityListResults method makeCSV.
private void makeCSV() {
StringBuilder sb = new StringBuilder();
//header
sb.append("Area Name,Area (sq km),PD,Proportional PD (PD / Tree PD),");
sb.append("Species,Proportional Species (Species / Tree Species),Tree Name,Tree ID,DOI,Study Name,Notes,Tree PD");
//rows
for (int j = 0; j < selectedTrees.size(); j++) {
Map<String, String> map = (Map<String, String>) selectedTrees.get(j);
for (int i = 0; i < selectedAreas.size(); i++) {
sb.append("\n");
//numbers
double pd = 0;
double maxpd = 0;
int speciesFound = 0;
int studySpecieCount = 1;
try {
//area pd
pd = Double.parseDouble(areaPds.get(i).get(map.get(StringConstants.STUDY_ID)));
//tree pd
maxpd = Double.parseDouble(map.get("maxPd"));
//species found in tree
//Integer.parseInt();
speciesFound = areaSpeciesMatches.get(i).get(map.get(StringConstants.STUDY_ID)).size();
//tree species count
studySpecieCount = Integer.parseInt(map.get("numberOfLeaves"));
} catch (Exception e) {
}
//'current extent' does not have a map layer
if (selectedAreas.get(i).getMapLayer() == null) {
sb.append(toCSVString("Current extent")).append(",");
sb.append(selectedAreas.get(i).getKm2Area()).append(",");
} else {
sb.append(toCSVString(selectedAreas.get(i).getMapLayer().getDisplayName())).append(",");
sb.append(toCSVString(selectedAreas.get(i).getKm2Area())).append(",");
}
String s = areaPds.get(i).get(map.get(StringConstants.STUDY_ID));
if (s == null) {
s = "";
}
sb.append(s).append(",");
sb.append(String.format("%.4f,", pd / maxpd));
sb.append("" + speciesFound).append(",");
sb.append(String.format("%.4f,", speciesFound / (double) studySpecieCount));
//tree name
sb.append(toCSVString(map.get("authors"))).append(",");
//tree id
sb.append(toCSVString(map.get("studyId"))).append(",");
//doi
sb.append(toCSVString(map.get("doi"))).append(",");
//study name
sb.append(toCSVString(map.get("studyName"))).append(",");
//notes
sb.append(toCSVString(map.get("notes"))).append(",");
//tree pd
sb.append(toCSVString(map.get("maxPd")));
}
}
csv = sb.toString();
try {
csvCells = new CSVReader(new StringReader(csv)).readAll();
} catch (Exception e) {
LOGGER.error("failed to parse phylogenetic diversity list", e);
}
}
use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.
the class UploadLayerListController method loadLayerList.
private void loadLayerList(Reader r) throws IOException {
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();
if (callback != null) {
try {
callback.onEvent(new ForwardEvent("", this, null, sb.toString()));
} catch (Exception e) {
LOGGER.error("failed when calling ToolComposer callback", e);
}
}
}
use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.
the class InOutComposer method onFinish.
@Override
public boolean onFinish() {
Query query = getSelectedSpecies();
query = query.newFacet(new Facet("occurrence_status_s", "absent", false), false);
if (query == null) {
return false;
}
List<SelectedArea> sa = getSelectedAreas();
//check the number of selected areas
int countSelected = sa.size();
if (autoCompleteLayerSelection != null && cAreasFromLayer.isChecked() && autoCompleteLayerAreas == null) {
countSelected += 2;
}
if (countSelected != 2) {
getMapComposer().showMessage(countSelected + " areas are selected. Select only 2 areas.");
return false;
}
try {
StringBuilder results = new StringBuilder();
double inArea = 0;
Query in;
Query out;
double outArea = 0;
String outName;
String inName;
Map<String, String[]> onlyIn = new HashMap<String, String[]>();
Map<String, String[]> onlyOut = new HashMap<String, String[]>();
Map<String, String[]> both = new HashMap<String, String[]>();
String[] speciesListHeader = null;
//get area of the layer
if (autoCompleteLayerSelection != null && cAreasFromLayer.isChecked() && autoCompleteLayerAreas == null) {
// in/out for a selected layer and the marine/terrestrial complement
String fieldId = CommonData.getLayerFacetNameDefault(autoCompleteLayerSelection);
JSONParser jp = new JSONParser();
JSONObject jo = (JSONObject) jp.parse(Util.readUrl(CommonData.getLayersServer() + "/field/" + fieldId));
inName = "In area (" + jo.get("name") + ")";
JSONArray objects = (JSONArray) jo.get("objects");
for (int i = 0; i < objects.size(); i++) {
if (((JSONObject) objects.get(i)).containsKey("area_km")) {
inArea += (Double) ((JSONObject) objects.get(i)).get("area_km");
}
}
in = query.newFacet(new Facet(fieldId, "*", true), false);
//determine complement area
JSONObject fld = CommonData.getLayer(fieldId);
JSONObject layer = (JSONObject) fld.get("layer");
boolean isMarine = Util.isSameDomain(new String[] { "marine" }, Util.getDomain(layer));
boolean isTerrestrial = Util.isSameDomain(new String[] { "terrestrial" }, Util.getDomain(layer));
String terrestrialQuery = CommonData.getSettings().getProperty("in_out_report.terrestrial.query", "cl2013:*");
String terrestrialName = CommonData.getSettings().getProperty("in_out_report.terrestrial.name", "Other - ASGS Australian States and Territories");
Double terrestrialArea = Double.parseDouble(CommonData.getSettings().getProperty("in_out_report.terrestrial.area", "7719806.774"));
String marineQuery = CommonData.getSettings().getProperty("in_out_report.marine.query", "cl21:*");
String marineName = CommonData.getSettings().getProperty("in_out_report.marine.name", "Other - IMCRA 4");
Double marineArea = Double.parseDouble(CommonData.getSettings().getProperty("in_out_report.marine.area", "8669607.781"));
if (isMarine && isTerrestrial) {
outName = marineName + " AND " + terrestrialName;
outArea = terrestrialArea + marineArea;
out = query.newFacets(Arrays.asList(new Facet[] { new Facet(fieldId, "*", false), Facet.parseFacet("(" + terrestrialQuery + " OR " + marineQuery + ")") }), false);
} else if (isMarine) {
outName = marineName;
outArea = marineArea;
out = query.newFacets(Arrays.asList(new Facet[] { new Facet(fieldId, "*", false), Facet.parseFacet(marineQuery) }), false);
} else if (isTerrestrial) {
outName = terrestrialName;
outArea = terrestrialArea;
out = query.newFacets(Arrays.asList(new Facet[] { new Facet(fieldId, "*", false), Facet.parseFacet(terrestrialQuery) }), false);
} else {
//world
outName = "rest of the world";
outArea = 510000000.0;
out = query.newFacet(new Facet(fieldId, "*", false), false);
}
outArea -= inArea;
} else {
inName = sa.get(0).getMapLayer().getDisplayName();
outName = sa.get(1).getMapLayer().getDisplayName();
in = QueryUtil.queryFromSelectedArea(query, sa.get(0), false, null);
out = QueryUtil.queryFromSelectedArea(query, sa.get(1), false, null);
try {
inArea = Double.parseDouble(sa.get(0).getKm2Area().replace(",", ""));
outArea = Double.parseDouble(sa.get(1).getKm2Area().replace(",", ""));
} catch (Exception e) {
}
//build species lists for comparison
List<String[]> inSpeciesList = new CSVReader(new StringReader(in.speciesList())).readAll();
List<String[]> outSpeciesList = new CSVReader(new StringReader(out.speciesList())).readAll();
//used to exclude header
int row = 0;
for (String[] line : inSpeciesList) {
if (row == 0) {
speciesListHeader = line;
row++;
} else if (line.length > 0) {
onlyIn.put(line[0], line);
}
}
row = 0;
for (String[] line : outSpeciesList) {
if (row == 0) {
//header will be missing if no species in inArea
speciesListHeader = line;
row++;
} else if (line.length > 0) {
if (!onlyIn.containsKey(line[0])) {
onlyOut.put(line[0], line);
} else {
onlyIn.remove(line[0]);
both.put(line[0], line);
}
}
}
}
results.append("Species,Area name,Sq km,Occurrences,Species\n");
results.append(getSelectedSpeciesName()).append(",");
results.append(inName).append(",").append(inArea).append(",").append(in.getOccurrenceCount()).append(",").append(in.getSpeciesCount()).append("\n");
results.append(getSelectedSpeciesName()).append(",");
results.append(outName).append(" (").append(outName).append("),").append(outArea).append(",").append(out.getOccurrenceCount()).append(",").append(out.getSpeciesCount()).append("\n");
if (onlyIn.size() + onlyOut.size() + both.size() > 0) {
results.append("\n");
results.append("\nSpecies found only in ").append(inName).append(",").append(String.format("%d", onlyIn.size()));
results.append("\nSpecies found only in ").append(outName).append(",").append(String.format("%d", onlyOut.size()));
results.append("\nSpecies found in both areas,").append(String.format("%d", both.size()));
results.append("\n\n").append(StringUtils.join(speciesListHeader, ",", 0, speciesListHeader.length - 3)).append(",").append(inName).append(",").append(outName);
for (String[] line : both.values()) {
results.append("\n");
for (int i = 0; i < line.length - 3; i++) results.append("\"").append(line[i].replace("\"", "\"\",")).append("\",");
results.append("found,found");
}
for (String[] line : onlyIn.values()) {
results.append("\n");
for (int i = 0; i < line.length - 3; i++) results.append("\"").append(line[i].replace("\"", "\"\",")).append("\",");
results.append("found,not found");
}
for (String[] line : onlyOut.values()) {
results.append("\n");
for (int i = 0; i < line.length - 3; i++) results.append("\"").append(line[i].replace("\"", "\"\",")).append("\",");
results.append("not found,found");
}
}
//show results
String metadata = "<html><body>" + "<div class='aooeoo'>" + "<div>Report for: " + getSelectedSpeciesName() + "<br />" + inName + "<br />" + outName + "</div><br />" + "<table >" + "<tr><td>Area name</td><td>Sq km</td><td>Occurrences</td><td>Species</td></tr>" + "<tr><td>" + inName + "</td><td>" + String.format("%.2f", inArea) + "</td><td>" + in.getOccurrenceCount() + "</td><td>" + in.getSpeciesCount() + "</td></tr>" + "<tr><td>" + outName + "</td><td>" + String.format("%.2f", outArea) + "</td><td>" + out.getOccurrenceCount() + "</td><td>" + out.getSpeciesCount() + "</td></tr>";
if (onlyIn.size() + onlyOut.size() + both.size() > 0) {
metadata += "<tr><td> </td></tr>" + "<tr><td>Species found only in " + inName + "</td><td>" + String.format("%d", onlyIn.size()) + "</td></tr>" + "<tr><td>Species found only in " + outName + "</td><td>" + String.format("%d", onlyOut.size()) + "</td></tr>" + "<tr><td>Species found in both areas</td><td>" + String.format("%d", both.size()) + "</td></tr>";
}
metadata += "</table></div>";
Event ev = new Event(StringConstants.ONCLICK, null, "Compare Areas Report\n" + metadata);
getMapComposer().openHTML(ev);
//download metadata as text
Filedownload.save(results.toString(), "text/plain", "Compare Areas Report.csv");
this.detach();
return true;
} catch (Exception e) {
LOGGER.error("failed In Out finish", e);
}
return false;
}
use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.
the class ClassificationLegend method buildLegend.
void buildLegend() {
try {
StringBuilder slist = new StringBuilder();
if (query != null) {
if (StringConstants.GRID.equals(colourmode)) {
slist.append("name,red,green,blue,count");
for (int i = 0; i < 600; i += 100) {
slist.append(">");
slist.append("\n").append(i).append(",").append(LegendObject.getRGB(Legend.getLinearColour(i, 0, 500, 0xFFFFFF00, 0xFFFF0000))).append(",");
}
} else {
slist.append(query.getLegend(colourmode).getTable());
}
} else {
return;
}
try {
legendLines = new CSVReader(new StringReader(slist.toString())).readAll();
//reset selection when legendLines is rebuilt
uncheckAll();
} catch (IOException e) {
LOGGER.error("failed to read legend list as csv", e);
}
legendLines.remove(0);
String h = mapLayer.getHighlight();
facet = Facet.parseFacet(h);
divContinous.setVisible(false);
//test for range (user upload)
if (legendLines.size() > 1) {
String first = legendLines.get(0)[0];
if (first == null || first.length() == 0 || first.startsWith(StringConstants.UNKNOWN)) {
first = legendLines.get(1)[0];
}
if (!checkmarks && query.getLegend(colourmode) != null && query.getLegend(colourmode).getNumericLegend() != null) {
setupForNumericalList(h);
//test for manual range (solr query)
} else if (StringConstants.OCCURRENCE_YEAR.equals(colourmode)) {
setupForBiocacheNumber(h, colourmode, true);
} else if (StringConstants.OCCURRENCE_YEAR_DECADE.equals(colourmode) || StringConstants.DECADE.equals(colourmode)) {
setupForBiocacheDecade();
} else if (StringConstants.COORDINATE_UNCERTAINTY.equals(colourmode) || StringConstants.UNCERTAINTY.equals(colourmode)) {
setupForBiocacheNumber(h, colourmode, false);
} else if (StringConstants.MONTH.equals(colourmode)) {
setupForBiocacheMonth();
}
}
/* apply something to line onclick in lb */
legend.setItemRenderer(new ListitemRenderer() {
@Override
public void render(Listitem li, Object data, int itemIdx) {
String[] ss = (String[]) data;
if (ss == null) {
return;
}
Checkbox cb = null;
if (checkmarks) {
cb = new Checkbox();
cb.addEventListener("onCheck", new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
String v = ((Listcell) event.getTarget().getParent().getParent().getChildren().get(1)).getLabel();
if (((CheckEvent) event).isChecked()) {
selectedList.add(v);
} else {
selectedList.remove(v);
}
lblSelectedCount.setValue(selectedList.size() + " selected");
if (selectedList.size() > 0) {
createInGroup.setVisible(true);
if (createOutGroup != null)
createOutGroup.setVisible(true);
clearSelection.setVisible(true);
} else {
createInGroup.setVisible(false);
if (createOutGroup != null)
createOutGroup.setVisible(false);
clearSelection.setVisible(false);
}
checkboxClick(event);
}
});
determineCheckboxState(cb, ss[0]);
}
Listcell lc;
lc = new Listcell();
if (cb != null) {
cb.setParent(lc);
//Do not display checkboxes for facets that are not simple
cb.setVisible(!disableselection && !ss[0].endsWith(" more"));
}
lc.setParent(li);
if (readonly) {
lc = new Listcell(actualToDisplayLabel(ss[0]));
} else {
lc = new Listcell("group " + ss[0]);
}
lc.setParent(li);
int red = 0, green = 0, blue = 0;
try {
red = Integer.parseInt(ss[1]);
green = Integer.parseInt(ss[2]);
blue = Integer.parseInt(ss[3]);
} catch (Exception e) {
LOGGER.error("error parsing colours : " + ss[0], e);
}
lc = new Listcell(" ");
lc.setStyle("background-color: rgb(" + red + "," + green + "," + blue + "); color: rgb(" + red + "," + green + "," + blue + ")");
lc.setParent(li);
//count
try {
//don't display if it is not a number
Long.parseLong(ss[4]);
lhFourthColumn.setVisible(true);
lc = new Listcell(ss[4]);
lc.setParent(li);
} catch (NumberFormatException e) {
lhFourthColumn.setVisible(false);
lhThirdColumn.setWidth("100%");
dCreateButtons.setVisible(!readonly);
}
}
});
legend.setModel(new SimpleListModel(legendLines));
createInGroup.setVisible(!disableselection && mapLayer.getHighlight() != null && mapLayer.getHighlight().length() > 0);
if (createOutGroup != null)
createOutGroup.setVisible(!disableselection && mapLayer.getHighlight() != null && mapLayer.getHighlight().length() > 0);
clearSelection.setVisible(!disableselection && mapLayer.getHighlight() != null && mapLayer.getHighlight().length() > 0);
if (divContinous.isVisible()) {
int[] state = getState();
if (state[0] > 1) {
setEnableContinousControls(false);
} else {
updateD();
}
getFellow("txtSearch").setVisible(false);
} else {
getFellow("txtSearch").setVisible(true);
}
} catch (Exception e) {
LOGGER.error("error building classification legend, pid: " + pid, e);
}
}
Aggregations