Search in sources :

Example 16 with MapLayerMetadata

use of au.org.emii.portal.menu.MapLayerMetadata in project spatial-portal by AtlasOfLivingAustralia.

the class LayerLegendGeneralComposer method onClick$createInGroup.

public void onClick$createInGroup(Event e) {
    StringBuilder sb = new StringBuilder();
    String layer = ((JSONObject) mapLayer.getClassificationObjects().get(0)).get("fid").toString();
    //build facet
    for (Object n : selectedList) {
        if (sb.length() > 0) {
            sb.append(" OR ");
        }
        sb.append(layer).append(":\"").append(n).append("\"");
    }
    //get pids
    String pids = "";
    String anyPid = "";
    Integer groupCount = mapLayer.getClassificationGroupCount();
    JSONArray groupObjects = mapLayer.getClassificationObjects();
    for (Object name : selectedList) {
        for (int i = 0; i < groupCount; i++) {
            if (name.equals(((JSONObject) groupObjects.get(i)).get("name"))) {
                if (!pids.isEmpty()) {
                    pids += "~";
                }
                anyPid = ((JSONObject) groupObjects.get(i)).get("pid").toString();
                pids += anyPid;
                break;
            }
        }
    }
    JSONParser jp = new JSONParser();
    JSONObject obj = null;
    Double[] bbox = new Double[4];
    boolean firstPid = true;
    Map<Integer, String> urlParts = new HashMap<Integer, String>();
    try {
        obj = (JSONObject) jp.parse(Util.readUrl(CommonData.getLayersServer() + "/object/" + anyPid));
        for (String p : pids.split("~")) {
            JSONObject jo = (JSONObject) jp.parse(Util.readUrl(CommonData.getLayersServer() + "/object/" + p));
            String bbString = jo.get(StringConstants.BBOX).toString();
            bbString = bbString.replace(StringConstants.POLYGON + "((", "").replace("))", "").replace(",", " ");
            String[] split = bbString.split(" ");
            String u = jo.get(StringConstants.WMSURL).toString();
            if (!u.contains("viewparams")) {
                //grid as contextual
                //extract colour map entries
                String s = u.split("ColorMap%3E")[1];
                int pos = 0;
                for (String c : s.split("%3CColorMapEntry")) {
                    int start = c.indexOf("quantity") + 14;
                    if (start > 14) {
                        Integer qty = Integer.parseInt(c.substring(start, c.indexOf("%", start)));
                        //do no overwrite existing quantities if this is the first or last entry
                        if (pos == 1 || pos == 2 || !urlParts.containsKey(qty))
                            urlParts.put(qty, c.replace("%3C%2F", ""));
                    }
                }
            }
            if (firstPid || Double.parseDouble(split[0]) < bbox[0])
                bbox[0] = Double.parseDouble(split[0]);
            if (firstPid || Double.parseDouble(split[1]) < bbox[1])
                bbox[1] = Double.parseDouble(split[1]);
            if (firstPid || Double.parseDouble(split[4]) > bbox[2])
                bbox[2] = Double.parseDouble(split[4]);
            if (firstPid || Double.parseDouble(split[5]) > bbox[3])
                bbox[3] = Double.parseDouble(split[5]);
            firstPid = false;
        }
    } catch (ParseException er) {
        LOGGER.error("failed to parse for object: " + anyPid);
    }
    String bboxString = "POLYGON((" + bbox[0] + " " + bbox[1] + "," + bbox[0] + " " + bbox[3] + "," + bbox[2] + " " + bbox[3] + "," + bbox[2] + " " + bbox[1] + "," + bbox[0] + " " + bbox[1] + "))";
    String url = obj.get(StringConstants.WMSURL).toString();
    if (!url.contains("s:" + anyPid)) {
        //grid as contextual layer
        String[] split = url.split("ColorMap%3E");
        String colours = "";
        List<Integer> sorted = new ArrayList<Integer>(urlParts.keySet());
        Collections.sort(sorted);
        for (int i = 0; i < sorted.size(); i++) {
            colours += "%3CColorMapEntry" + urlParts.get(sorted.get(i));
        }
        colours += "%3C%2F";
        url = split[0] + "ColorMap%3E" + colours + "ColorMap%3E" + split[2];
    } else {
        url = url.replace("s:" + anyPid, "s:" + pids);
    }
    String name = selectedList.size() + " areas: " + mapLayer.getDisplayName();
    MapLayer ml = getMapComposer().addWMSLayer(getMapComposer().getNextAreaLayerName(name), name, url, 0.6f, /*metadata url*/
    null, null, LayerUtilitiesImpl.WKT, null, null);
    String lname = ml.getName();
    //add colour!
    int colour = Util.nextColour();
    int r = (colour >> 16) & 0x000000ff;
    int g = (colour >> 8) & 0x000000ff;
    int b = (colour) & 0x000000ff;
    ml.setRedVal(r);
    ml.setGreenVal(g);
    ml.setBlueVal(b);
    ml.setDynamicStyle(true);
    ml.setPolygonLayer(true);
    Facet facet = Facet.parseFacet(sb.toString());
    //only get field data if it is an intersected layer (to exclude layers containing points)
    JSONObject field = CommonData.getLayer((String) obj.get(StringConstants.FID));
    List<Facet> facets = new ArrayList<Facet>();
    facets.add(facet);
    ml.setFacets(facets);
    ml.setWKT(bboxString);
    MapLayerMetadata md = ml.getMapLayerMetadata();
    md.setBbox(Arrays.asList(bbox));
    try {
        md.setMoreInfo(CommonData.getLayersServer() + "/layers/view/more/" + field.get("spid").toString());
    } catch (Exception er) {
        LOGGER.error("error setting map layer moreInfo: " + (field != null ? field.toString() : "layerObj is null"), er);
    }
    getMapComposer().applyChange(ml);
    getMapComposer().updateLayerControls();
    getMapComposer().reloadMapLayerNowAndIndexes(ml);
}
Also used : MapLayer(au.org.emii.portal.menu.MapLayer) JSONArray(org.json.simple.JSONArray) ParseException(org.json.simple.parser.ParseException) MapLayerMetadata(au.org.emii.portal.menu.MapLayerMetadata) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject) LegendObject(au.org.ala.legend.LegendObject) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) Facet(au.org.ala.legend.Facet)

Aggregations

MapLayerMetadata (au.org.emii.portal.menu.MapLayerMetadata)16 MapLayer (au.org.emii.portal.menu.MapLayer)15 URL (java.net.URL)7 ParseException (org.json.simple.parser.ParseException)5 JSONObject (org.json.simple.JSONObject)4 JSONParser (org.json.simple.parser.JSONParser)4 Facet (au.org.ala.legend.Facet)3 HasMapLayer (au.org.emii.portal.menu.HasMapLayer)2 WMSStyle (au.org.emii.portal.wms.WMSStyle)2 XmlArrayList (com.thoughtworks.xstream.persistence.XmlArrayList)2 LegendObject (au.org.ala.legend.LegendObject)1 CRSEnvelope (org.geotools.data.ows.CRSEnvelope)1 Layer (org.geotools.data.ows.Layer)1 WebMapServer (org.geotools.data.wms.WebMapServer)1 GetMapRequest (org.geotools.data.wms.request.GetMapRequest)1 MetadataURL (org.geotools.data.wms.xml.MetadataURL)1 JSONArray (org.json.simple.JSONArray)1