use of au.org.ala.legend.LegendObject in project spatial-portal by AtlasOfLivingAustralia.
the class AooEooComposer method onFinish.
@Override
public boolean onFinish() {
SelectedArea sa = getSelectedArea();
Query q = getSelectedSpecies();
q = q.newFacet(new Facet("occurrence_status_s", "absent", false), false);
Facet f = new Facet("occurrence_status_s", "absent", false);
q = q.newFacet(f, false);
Query newQ = QueryUtil.queryFromSelectedArea(q, sa, false, null);
double gridSize = dResolution.doubleValue();
// eoo, use actual points
LegendObject legend = newQ.getLegend("lat_long");
StringBuilder sb = new StringBuilder();
int pointCount = processLegend(legend, sb);
String aooWkt = aooWkt(legend, gridSize);
// aoo = gridSize * gridSize * number of gridSize by gridSize cells with an occurrence * (approx sq degrees to sq km)
double aoo = gridSize * gridSize * aooProcess(legend, gridSize) * 10000;
double eoo = 0;
WKTReader reader = new WKTReader();
String metadata = null;
try {
Geometry g = reader.read(sb.toString());
Geometry convexHull = g.convexHull();
String wkt = convexHull.toText().replace(" (", "(").replace(", ", ",");
eoo = SpatialUtil.calculateArea(wkt);
// aoo area
Geometry a = reader.read(aooWkt(legend, gridSize));
Geometry aUnion = a.union();
String aWkt = aUnion.toText().replace(" (", "(").replace(", ", ",");
if (eoo > 0) {
String name = "Extent of occurrence (area): " + q.getName();
MapLayer ml = getMapComposer().addWKTLayer(wkt, name, name);
name = "Area of occupancy (area): " + q.getName();
MapLayer mla = getMapComposer().addWKTLayer(aWkt, name, name);
metadata = "<html><body>" + "<div class='aooeoo'>" + "<div>The Sensitive Data Service may have changed the location of taxa that have a sensitive status." + " It is wise to first map the taxa and examine each record, then filter these records to create the " + "desired subset, then run the tool on the new filtered taxa layer.</div><br />" + "<table >" + "<tr><td>Number of records used for the calculations</td><td>" + newQ.getOccurrenceCount() + "</td></tr>" + "<tr><td>Species</td><td>" + q.getName() + "</td></tr>" + "<tr><td>Area of Occupancy (AOO: " + gridSize + " degree grid)</td><td>" + String.format("%.0f", aoo) + " sq km</td></tr>" + "<tr><td>Extent of Occurrence (EOO: Minimum convex hull)</td><td>" + (String.format("%.2f", eoo / 1000.0 / 1000.0)) + " sq km</td></tr></table></body></html>" + "</div>";
ml.getMapLayerMetadata().setMoreInfo("Area of Occupancy and Extent of Occurrence\n" + metadata);
name = "Extent of occurrence (points): " + q.getName();
MapLayer ml2 = getMapComposer().mapSpecies(newQ, name, StringConstants.SPECIES, newQ.getOccurrenceCount(), LayerUtilitiesImpl.SPECIES, null, 0, MapComposer.DEFAULT_POINT_SIZE, MapComposer.DEFAULT_POINT_OPACITY, Util.nextColour(), false);
ml2.getMapLayerMetadata().setMoreInfo("Area of Occupancy and Extent of Occurrence\n" + metadata);
} else {
// trigger eoo unavailable message
pointCount = 2;
}
} catch (Exception e) {
e.printStackTrace();
}
// show results
String message = "The Sensitive Data Service may have changed the location of taxa that have a sensitive status. " + "It is wise to first map the taxa and examine each record, then filter these records to create the " + "desired subset, then run the tool on the new filtered taxa layer.\r\n" + "\r\nNumber of records used for the calculations: " + newQ.getOccurrenceCount() + "\r\nSpecies: " + q.getName() + "\r\nArea of Occupancy: " + String.format("%.0f", aoo) + " sq km" + "\r\nExtent of Occurrence: " + (pointCount < 3 ? "insufficient unique occurrence locations" : (String.format("%.2f", eoo / 1000.0 / 1000.0) + " sq km"));
if (metadata != null) {
Event ev = new Event(StringConstants.ONCLICK, null, "Area of Occupancy and Extent of Occurrence\n" + metadata);
getMapComposer().openHTML(ev);
} else {
getMapComposer().showMessage(message);
}
// download metadata as text
Filedownload.save(message, "text/plain", "Calculated AOO and EOO.txt");
this.detach();
return true;
}
use of au.org.ala.legend.LegendObject in project spatial-portal by AtlasOfLivingAustralia.
the class ClassificationLegend method setupForNumericalList.
private void setupForNumericalList(String facetString) {
String h = facetString;
legendFacets = new HashMap<String, String>();
try {
divContinous.setVisible(!disableselection);
LegendObject lo = mapLayer.getLegendObject();
if (lo.getFieldType() == QueryField.FieldType.INT || lo.getFieldType() == QueryField.FieldType.LONG) {
intContinous = true;
}
gMinValue = minValue = lo.getMinMax()[0];
gMaxValue = maxValue = lo.getMinMax()[1];
double rng = gMaxValue - gMinValue;
if (intContinous && rng < sliderMax && rng > 0) {
sliderMax = (int) rng;
}
try {
if (facet != null) {
// count OR and test for null
boolean nulls = h.startsWith("-") || (h.length() > 1 && h.charAt(1) == '-') || (h.length() > 2 && h.charAt(2) == '-');
int countOr = h.split(" OR ").length;
if (countOr == 1 || (nulls && countOr == 2)) {
minValue = facet.getMin();
maxValue = facet.getMax();
if (Double.isInfinite(minValue)) {
minValue = gMinValue;
}
if (Double.isInfinite(maxValue)) {
maxValue = gMaxValue;
}
}
dunknown.setChecked(nulls);
}
} catch (Exception e) {
LOGGER.error("error setting up the numerical listing", e);
}
for (int j = 0; j < legendLines.size(); j++) {
String label = legendLines.get(j)[0];
if (label.charAt(0) == '-') {
legendFacets.put(StringConstants.UNKNOWN, legendLines.get(j)[0]);
legendLines.get(j)[0] = StringConstants.UNKNOWN;
} else {
String s = legendLines.get(j)[0];
String[] ss = s.substring(s.indexOf('[')).replace("[", "").replace("]", "").replace(StringConstants.DATE_TIME_END_OF_YEAR, "").replace(StringConstants.DATE_TIME_BEGINNING_OF_YEAR, "").split(" TO ");
float[] cutoffs = lo.getNumericLegend().getCutoffFloats();
float[] cutoffMins = lo.getNumericLegend().getCutoffMinFloats();
String range, strFacet;
double min;
double max;
double nextmin;
if (ss.length > 1) {
if ("*".equals(ss[0])) {
min = cutoffMins[0];
max = cutoffs[0];
nextmin = cutoffMins.length > 1 ? cutoffMins[1] : cutoffMins[0];
} else if ("*".equals(ss[1])) {
min = cutoffMins[cutoffMins.length - 1];
max = gMaxValue;
nextmin = gMaxValue;
} else {
double v = Double.parseDouble(ss[1]);
int pos = 0;
while (v > cutoffs[pos]) {
pos++;
}
min = cutoffMins[pos];
max = cutoffs[pos];
nextmin = cutoffMins.length > pos + 1 ? cutoffMins[pos + 1] : cutoffMins[pos];
}
if (intContinous) {
if (min == gMinValue && max == gMaxValue) {
range = String.format(">= %d and <= %d", (int) min, (int) max);
} else if (min == gMinValue) {
range = String.format(">= %d and < %d", (int) min, (int) nextmin);
} else if (max == gMaxValue) {
range = String.format("<= %d", (int) max);
} else {
range = String.format("< %d", (int) nextmin);
}
strFacet = colourmode + ":[" + (int) min + " TO " + (int) max + "]";
} else {
if (min == gMinValue && max == gMaxValue) {
range = String.format(">= %g and <= %g", min, max);
} else if (min == gMinValue) {
range = String.format(">= %g and < %g", min, nextmin);
} else if (max == gMaxValue) {
range = String.format("<= %g", max);
} else {
range = String.format("< %g", nextmin);
}
strFacet = colourmode + ":[" + min + " TO " + max + "]";
}
legendLines.get(j)[0] = range;
legendFacets.put(range, strFacet);
}
}
}
checkmarks = true;
} catch (Exception e) {
divContinous.setVisible(false);
LOGGER.error("error setting up continous legend listing", e);
}
}
use of au.org.ala.legend.LegendObject in project spatial-portal by AtlasOfLivingAustralia.
the class ClassificationLegend method setupForBiocacheDecade.
private void setupForBiocacheDecade() {
isNumber = false;
intContinous = true;
legendFacets = new HashMap<String, String>();
LegendObject lo = mapLayer.getLegendObject();
if (lo != null) {
// update text in legend lines
for (int j = 0; j < legendLines.size(); j++) {
String label = legendLines.get(j)[0];
if (label.charAt(0) == '-') {
legendFacets.put(StringConstants.UNKNOWN, legendLines.get(j)[0]);
legendLines.get(j)[0] = StringConstants.UNKNOWN;
} else {
String[] ss = legendLines.get(j)[0].split(" TO ");
String range, strFacet;
if (ss.length > 1) {
String yr = ss[1].substring(0, ss[1].length() - 2);
range = String.format("%s0s", yr);
strFacet = "occurrence_year:[" + yr + "0-01-01T00:00:00Z TO " + yr + "9-12-31T00:00:00Z]";
legendLines.get(j)[0] = range;
legendFacets.put(range, strFacet);
}
}
}
}
}
use of au.org.ala.legend.LegendObject in project spatial-portal by AtlasOfLivingAustralia.
the class BiocacheQuery method getLegend.
/**
* Get legend for a facet field.
*
* @param colourmode
* @return
*/
@Override
public LegendObject getLegend(String colourmode) {
if ("-1".equals(colourmode) || StringConstants.GRID.equals(colourmode)) {
return null;
}
LegendObject lo = legends.get(colourmode);
if (lo != null && lo.getColourMode() != null && !lo.getColourMode().equals(colourmode)) {
LOGGER.debug("lo not empty and lo=" + lo);
lo = legends.get(lo.getColourMode());
}
if (lo == null && getOccurrenceCount() > 0) {
HttpClient client = new HttpClient();
String facetToColourBy = StringConstants.OCCURRENCE_YEAR_DECADE.equals(colourmode) ? StringConstants.OCCURRENCE_YEAR : translateFieldForSolr(colourmode);
try {
String url = biocacheServer + LEGEND_SERVICE_CSV + DEFAULT_ROWS + "&q=" + getQ() + "&cm=" + URLEncoder.encode(facetToColourBy, StringConstants.UTF_8) + getQc();
LOGGER.debug(url);
GetMethod get = new GetMethod(url);
// NQ: Set the header type to JSON so that we can parse JSON instead of CSV (CSV has issue with quoted field where a quote is the escape character)
get.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
client.executeMethod(get);
String s = get.getResponseBodyAsString();
// in the first line do field name replacement
String t = translateFieldForSolr(colourmode);
if (!colourmode.equals(t)) {
s = s.replaceFirst(t, colourmode);
}
lo = new BiocacheLegendObject(colourmode, s);
// test for exceptions
if (!colourmode.contains(",") && (StringConstants.UNCERTAINTY.equals(colourmode) || StringConstants.DECADE.equals(colourmode) || StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.COORDINATE_UNCERTAINTY.equals(colourmode))) {
lo = ((BiocacheLegendObject) lo).getAsIntegerLegend();
// apply cutpoints to colourMode string
Legend l = lo.getNumericLegend();
if (l == null || l.getCutoffFloats() == null) {
return null;
}
float[] cutpoints = l.getCutoffFloats();
float[] cutpointmins = l.getCutoffMinFloats();
StringBuilder sb = new StringBuilder();
// NQ 20140109: use the translated SOLR field as the colour mode so that Constants.DECADE does not cause an issue
String newFacet = StringConstants.DECADE.equals(colourmode) ? StringConstants.OCCURRENCE_YEAR : colourmode;
sb.append(newFacet);
int i = 0;
while (i < cutpoints.length) {
if (i == cutpoints.length - 1 || cutpoints[i] != cutpoints[i + 1]) {
if (i > 0) {
sb.append(",").append(cutpointmins[i]);
if (StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.DECADE.equals(colourmode)) {
sb.append(StringConstants.DATE_TIME_BEGINNING_OF_YEAR);
}
} else {
sb.append(",*");
}
sb.append(",").append(cutpoints[i]);
if (StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.DECADE.equals(colourmode)) {
sb.append(StringConstants.DATE_TIME_END_OF_YEAR);
}
} else if (i < cutpoints.length - 1 && cutpoints[i] == cutpoints[i + 1]) {
cutpointmins[i + 1] = cutpointmins[i];
}
i++;
}
String newColourMode = sb.toString();
if (StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.DECADE.equals(colourmode)) {
newColourMode = newColourMode.replace(".0", "");
}
lo.setColourMode(newColourMode);
legends.put(colourmode, lo);
LegendObject newlo = getLegend(newColourMode);
newlo.setColourMode(newColourMode);
newlo.setNumericLegend(lo.getNumericLegend());
legends.put(newColourMode, newlo);
lo = newlo;
} else if (StringConstants.MONTH.equals(colourmode)) {
String newColourMode = "month,00,00,01,01,02,02,03,03,04,04,05,05,06,06,07,07,08,08,09,09,10,10,11,11,12,12";
lo.setColourMode(newColourMode);
legends.put(colourmode, lo);
LegendObject newlo = getLegend(newColourMode);
newlo.setColourMode(newColourMode);
newlo.setNumericLegend(lo.getNumericLegend());
legends.put(newColourMode, newlo);
lo = newlo;
} else if (!colourmode.contains(",") && (StringConstants.OCCURRENCE_YEAR_DECADE.equals(colourmode) || StringConstants.DECADE.equals(colourmode))) {
Set<Integer> decades = new TreeSet<Integer>();
for (double d : ((BiocacheLegendObject) lo).categoriesNumeric.keySet()) {
decades.add((int) (d / 10));
}
List<Integer> d = new ArrayList<Integer>(decades);
StringBuilder sb = new StringBuilder();
sb.append(StringConstants.OCCURRENCE_YEAR);
for (int i = !d.isEmpty() && d.get(0) > 0 ? 0 : 1; i < d.size(); i++) {
if (i > 0) {
sb.append(",").append(d.get(i));
sb.append("0-01-01T00:00:00Z");
} else {
sb.append(",*");
}
sb.append(",").append(d.get(i));
sb.append("9-12-31T00:00:00Z");
}
String newColourMode = sb.toString();
lo.setColourMode(newColourMode);
legends.put(colourmode, lo);
LegendObject newlo = getLegend(newColourMode);
newlo.setColourMode(newColourMode);
newlo.setNumericLegend(lo.getNumericLegend());
legends.put(newColourMode, newlo);
lo = newlo;
} else {
legends.put(colourmode, lo);
}
} catch (Exception e) {
LOGGER.error("error getting legend for : " + colourmode, e);
}
}
return lo;
}
use of au.org.ala.legend.LegendObject in project spatial-portal by AtlasOfLivingAustralia.
the class LayerLegendGeneralComposer method showPointsColourModeLegend.
private void showPointsColourModeLegend(MapLayer m) {
// remove all
while (!legendHtml.getChildren().isEmpty()) {
legendHtml.removeChild(legendHtml.getFirstChild());
}
// 1. register legend
String colourMode = cbColour.getSelectedItem().getValue();
if (pointtype.getSelectedItem() == rGrid) {
colourMode = StringConstants.GRID;
}
// put any parameters into map
Map map = new HashMap();
map.put(StringConstants.QUERY, m.getSpeciesQuery());
map.put(StringConstants.LAYER, m);
map.put(StringConstants.READONLY, StringConstants.TRUE);
map.put(StringConstants.COLOURMODE, colourMode);
String colourmode = cbColour.getSelectedItem().getValue();
if (!StringConstants.GRID.equals(m.getColourMode()) && query.getLegend(colourmode).getCategoryNameOrder() != null) {
map.put("checkmarks", StringConstants.TRUE);
}
try {
LegendObject lo = m.getSpeciesQuery().getLegend(colourmode);
if (lo != null) {
m.setLegendObject(lo);
}
} catch (Exception e) {
LOGGER.error("error getting legend for map layer: " + m.getName(), e);
}
try {
Executions.createComponents("/WEB-INF/zul/legend/LayerLegendClassification.zul", legendHtml, map);
} catch (Exception e) {
LOGGER.error("error opening classification legend: " + legendHtml, e);
}
}
Aggregations