use of au.org.ala.legend.Facet in project spatial-portal by AtlasOfLivingAustralia.
the class AreaMerge method mergeAreas.
void mergeAreas() {
List<Facet> facets = new ArrayList<Facet>();
List<Geometry> wkt = new ArrayList<Geometry>();
WKTReader wktReader = new WKTReader();
String layerDisplayNames = "";
for (int i = 0; i < vboxAreas.getChildren().size(); i++) {
Checkbox cb = (Checkbox) vboxAreas.getChildren().get(i);
if (cb.isChecked()) {
MapLayer ml = cb.getValue();
if (layerDisplayNames.length() > 0) {
layerDisplayNames += ", ";
}
layerDisplayNames += ml.getDisplayName();
if (ml != null) {
if (ml.getFacets() != null) {
facets.addAll(ml.getFacets());
}
try {
//get actual WKT when 'envelope' is specified
String w = ml.getWKT();
if (w.startsWith(StringConstants.ENVELOPE)) {
//should only be one pid
String pid = w.substring(w.indexOf(',') + 1, w.length() - 1);
w = Util.readUrl(CommonData.getLayersServer() + "/shape/wkt/" + pid);
}
Geometry g = wktReader.read(w);
if (g != null) {
wkt.add(g);
}
} catch (ParseException e) {
LOGGER.error("cannot parse WKT for map layer: " + ml.getDisplayName() + " for WKT: " + ml.getWKT());
}
} else {
String swkt = null;
if (CommonData.getSettings().getProperty(CommonData.AUSTRALIA_NAME).equalsIgnoreCase(cb.getLabel())) {
swkt = CommonData.getSettings().getProperty(CommonData.AUSTRALIA_WKT);
} else if ("Current Extent".equalsIgnoreCase(cb.getLabel())) {
swkt = getMapComposer().getViewArea();
} else {
LOGGER.error("cannot determine what this checked area is: " + cb.getLabel());
}
if (swkt != null) {
try {
Geometry g = wktReader.read(swkt);
if (g != null) {
wkt.add(g);
}
} catch (ParseException e) {
LOGGER.error("cannot parse WKT for map layer: " + ml.getDisplayName() + " for WKT: " + swkt);
}
}
}
}
}
//produce single geometry
Geometry geometry = null;
if (!wkt.isEmpty()) {
geometry = wkt.get(0);
for (int i = 1; i < wkt.size(); i++) {
geometry = GeometryCombiner.combine(geometry, wkt.get(i));
}
}
String finalWkt = (geometry == null) ? null : geometry.toString();
MapComposer mc = getMapComposer();
layerName = (mc.getMapLayer(txtLayerName.getValue()) == null) ? txtLayerName.getValue() : mc.getNextAreaLayerName(txtLayerName.getValue());
MapLayer mapLayer = mc.addWKTLayer(finalWkt, layerName, txtLayerName.getValue());
//if possible, use facets instead of WKT with biocache
if (wkt.size() == facets.size()) {
//change to a single OR facet.
//Because all facet areas are single or multiple, ORs just need to OR for joining.
String fq = facets.get(0).toString();
for (int i = 1; i < facets.size(); i++) {
fq += " OR " + facets.get(i).toString();
}
List<Facet> array = new ArrayList<Facet>();
array.add(Facet.parseFacet(fq));
mapLayer.setFacets(array);
}
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
String metadata = "";
metadata += "Merged WKT layers\nLayers: " + layerDisplayNames + "\n";
metadata += "Name: " + layerName + " <br />\n";
metadata += "Date: " + formatter.format(calendar.getTime()) + " <br />\n";
mapLayer.getMapLayerMetadata().setMoreInfo(metadata);
//reapply layer name
getMapComposer().getMapLayer(layerName).setDisplayName(txtLayerName.getValue());
getMapComposer().redrawLayersList();
ok = true;
this.detach();
}
use of au.org.ala.legend.Facet in project spatial-portal by AtlasOfLivingAustralia.
the class AreaToolComposer method detach.
@Override
public void detach() {
super.detach();
String parentname = (String) winProps.get(StringConstants.PARENTNAME);
String areatype = (String) getMapComposer().getAttribute(StringConstants.ADDAREAWINDOW);
if (areatype == null) {
areatype = "";
} else {
if (areatype.startsWith("ci")) {
areatype = areatype.substring(2);
}
if (areatype.startsWith("Upload")) {
areatype = "Import - Area " + areatype.substring(6);
} else if (areatype.startsWith("WKT")) {
areatype = "Import - Area WKT";
} else {
areatype = "Area - " + areatype;
}
}
//always map the user defined area
MapLayer ml = getMapComposer().getMapLayer(layerName);
if (ok && ml != null) {
String displayName = ml.getDisplayName();
String fromLayer = (String) getMapComposer().getAttribute("mappolygonlayer");
String activeLayerName = (String) getMapComposer().getAttribute("activeLayerName");
if (fromLayer == null) {
fromLayer = "";
} else {
getMapComposer().removeAttribute("mappolygonlayer");
}
if (activeLayerName == null) {
activeLayerName = "";
} else {
getMapComposer().removeAttribute("activeLayerName");
}
remoteLogger.logMapArea(layerName + ((!layerName.equalsIgnoreCase(displayName)) ? " (" + displayName + ")" : ""), areatype, ml.testWKT(), activeLayerName, fromLayer);
//warn user when reduced WKT may be used for analysis
getMapComposer().warnForLargeWKT(ml);
//upload this area and replace with WMS
getMapComposer().replaceWKTwithWMS(ml);
}
if (isAnalysisChild) {
analysisParent.resetWindow(ok ? layerName : null);
try {
remoteLogger.logMapArea(layerName, areatype, getMapComposer().getMapLayer(layerName).testWKT());
} catch (Exception e) {
LOGGER.error("error with remote logging", e);
}
} else if (isFacetChild) {
facetParent.resetWindow(ok ? layerName : null);
try {
remoteLogger.logMapArea(layerName, areatype, getMapComposer().getMapLayer(layerName).testWKT());
} catch (Exception e) {
LOGGER.error("error with remote logging", e);
}
} else if (parentname != null && "AddSpeciesInArea".equals(parentname)) {
//was OK clicked?
if (ok) {
//map
// String wkt = null;
// try {
// wkt = getMapComposer().getMapLayer(layerName).getWKT();
// } catch (Exception e) {
// LOGGER.error("failed to get WKT for layer: " + layerName, e);
// }
MapLayer mapLayer = getMapComposer().getMapLayer(layerName);
Query q = null;
if (winProps.get(StringConstants.QUERY) != null) {
q = ((Query) winProps.get(StringConstants.QUERY));
List<Facet> facets = mapLayer.getFacets();
String wkt = facets == null ? mapLayer.getWKT() : null;
SelectedArea sa = new SelectedArea(getMapComposer().getMapLayer(layerName), wkt);
q = QueryUtil.queryFromSelectedArea(q, sa, true, null);
}
if (winProps.get(StringConstants.QUERY) == null) {
mapSpeciesInArea();
} else if (winProps.get(StringConstants.FILTER) != null && (Boolean) winProps.get(StringConstants.FILTER)) {
ml = getMapComposer().mapSpecies(q, (String) winProps.get(StringConstants.NAME), (String) winProps.get("s"), (Integer) winProps.get(StringConstants.FEATURE_COUNT), (Integer) winProps.get(StringConstants.TYPE), null, -1, MapComposer.DEFAULT_POINT_SIZE, MapComposer.DEFAULT_POINT_OPACITY, Util.nextColour(), false);
ml.getMapLayerMetadata().setMoreInfo((String) winProps.get("metadata"));
} else if (winProps.get("filterGrid") != null && (Boolean) winProps.get("filterGrid")) {
ml = getMapComposer().mapSpecies(q, (String) winProps.get(StringConstants.NAME), (String) winProps.get("s"), (Integer) winProps.get(StringConstants.FEATURE_COUNT), (Integer) winProps.get(StringConstants.TYPE), null, -1, MapComposer.DEFAULT_POINT_SIZE, MapComposer.DEFAULT_POINT_OPACITY, Util.nextColour(), false);
ml.getMapLayerMetadata().setMoreInfo((String) winProps.get("metadata"));
} else if (winProps.get("byLsid") != null && (Boolean) winProps.get("byLsid")) {
ml = getMapComposer().mapSpecies(q, (String) winProps.get(StringConstants.NAME), (String) winProps.get("s"), (Integer) winProps.get(StringConstants.FEATURE_COUNT), (Integer) winProps.get(StringConstants.TYPE), null, -1, MapComposer.DEFAULT_POINT_SIZE, MapComposer.DEFAULT_POINT_OPACITY, Util.nextColour(), false);
ml.getMapLayerMetadata().setMoreInfo((String) winProps.get("metadata"));
} else {
getMapComposer().mapSpecies(q, (String) winProps.get(StringConstants.TAXON), (String) winProps.get(StringConstants.RANK), 0, LayerUtilitiesImpl.SPECIES, null, -1, MapComposer.DEFAULT_POINT_SIZE, MapComposer.DEFAULT_POINT_OPACITY, Util.nextColour(), false);
}
if (getMapComposer().getMapLayer(layerName) != null) {
String displayName = getMapComposer().getMapLayer(layerName).getDisplayName();
remoteLogger.logMapArea(layerName + ((!layerName.equalsIgnoreCase(displayName)) ? " (" + displayName + ")" : ""), areatype, null);
}
}
}
}
use of au.org.ala.legend.Facet 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);
}
}
use of au.org.ala.legend.Facet in project spatial-portal by AtlasOfLivingAustralia.
the class AreaReportPDFComposer method onFinish.
@Override
public boolean onFinish() {
if (pool != null) {
return true;
}
//close any existing area report
Window w = (Window) getPage().getFellowIfAny("popupResults");
if (w != null) {
w.detach();
}
SelectedArea sa = getSelectedArea();
String areaName = getSelectedAreaName();
String areaDisplayName = getSelectedAreaDisplayName();
MapLayer ml = getMapComposer().getMapLayer(areaName);
double[] bbox = null;
if (ml != null && ml.getMapLayerMetadata().getBbox() != null && ml.getMapLayerMetadata().getBbox().size() == 4) {
bbox = new double[4];
bbox[0] = ml.getMapLayerMetadata().getBbox().get(0);
bbox[1] = ml.getMapLayerMetadata().getBbox().get(1);
bbox[2] = ml.getMapLayerMetadata().getBbox().get(2);
bbox[3] = ml.getMapLayerMetadata().getBbox().get(3);
}
String wktTmp = (ml == null ? sa.getWkt() : (ml.getFacets() == null ? ml.getWKT() : null));
if (wktTmp == null && ml != null && ml.getPid() != null) {
wktTmp = Util.readUrl(CommonData.getLayersServer() + "/shape/wkt/" + ml.getPid());
}
final String queryWkt = (ml == null ? sa.getWkt() : (ml.getFacets() == null ? ml.getWKT() : null));
final String area = areaDisplayName;
final String wkt = wktTmp;
final List<Facet> facets = (ml != null && ml.getFacets() != null ? ml.getFacets() : null);
progress = new ConcurrentHashMap();
progress.put("label", "Starting");
progress.put("percent", 0.0);
Callable pdfAreaReport = new Callable<AreaReportPDF>() {
@Override
public AreaReportPDF call() {
return new AreaReportPDF(wkt, area, facets, queryWkt, progress);
}
};
pool = Executors.newFixedThreadPool(1);
future = pool.submit(pdfAreaReport);
getMapComposer().getOpenLayersJavascript().execute("setTimeout('checkProgress()', 2000);");
divProgress.setVisible(true);
return true;
}
use of au.org.ala.legend.Facet in project spatial-portal by AtlasOfLivingAustralia.
the class MapComposer method setupMapLayerAsDistributionArea.
public void setupMapLayerAsDistributionArea(MapLayer mapLayer) {
try {
//identify the spcode from the url
String spcode = mapLayer.getSPCode();
String url = CommonData.getLayersServer() + "/distribution/" + spcode;
String jsontxt = Util.readUrl(url);
if (jsontxt == null || jsontxt.length() == 0) {
url = CommonData.getLayersServer() + "/checklist/" + spcode;
jsontxt = Util.readUrl(url);
}
if (jsontxt == null || jsontxt.length() == 0) {
LOGGER.debug("******** failed to find wkt for " + mapLayer.getUri() + " > " + spcode);
return;
}
JSONParser jp = new JSONParser();
JSONObject jo = (JSONObject) jp.parse(jsontxt);
if (!jo.containsKey(StringConstants.GEOMETRY)) {
return;
}
mapLayer.setWKT(jo.get(StringConstants.GEOMETRY).toString());
mapLayer.setPolygonLayer(true);
Facet facet = null;
if (jo.containsKey(StringConstants.PID) && jo.containsKey(StringConstants.AREA_NAME)) {
JSONObject object = (JSONObject) jp.parse(Util.readUrl(CommonData.getLayersServer() + "/object/" + jo.containsKey(StringConstants.PID)));
//only get field data if it is an intersected layer (to exclude layers containing points)
if (CommonData.getLayer((String) object.get(StringConstants.FID)) != null) {
facet = Util.getFacetForObject(jo.get(StringConstants.AREA_NAME).toString(), (String) object.get(StringConstants.FID));
}
}
if (facet != null) {
List<Facet> facets = new ArrayList<Facet>();
facets.add(facet);
mapLayer.setFacets(facets);
}
MapLayerMetadata md = mapLayer.getMapLayerMetadata();
try {
List<Double> bbox;
if (jo.containsKey("bounding_box")) {
bbox = Util.getBoundingBox(jo.get("bounding_box").toString());
} else {
bbox = Util.getBoundingBox(jo.get(StringConstants.GEOMETRY).toString());
}
md.setBbox(bbox);
} catch (Exception e) {
LOGGER.error("failed to parse wkt in : " + url, e);
}
//add colour!
int colour = Util.nextColour();
int r = (colour >> 16) & 0x000000ff;
int g = (colour >> 8) & 0x000000ff;
int b = (colour) & 0x000000ff;
mapLayer.setRedVal(r);
mapLayer.setGreenVal(g);
mapLayer.setBlueVal(b);
mapLayer.setDynamicStyle(true);
getMapComposer().applyChange(mapLayer);
warnForLargeWKT(mapLayer);
} catch (Exception e) {
LOGGER.error("error setting up distributions map layer", e);
}
}
Aggregations