Search in sources :

Example 31 with JSONArray

use of org.json.simple.JSONArray in project spatial-portal by AtlasOfLivingAustralia.

the class CommonData method dynamicSpeciesListColumns.

private static String dynamicSpeciesListColumns() {
    StringBuilder sb = new StringBuilder();
    try {
        JSONParser jp = new JSONParser();
        JSONObject threatened = (JSONObject) jp.parse(Util.readUrl(settings.getProperty("species_list_url", "") + "/ws/speciesList/?isThreatened=eq:true&isAuthoritative=eq:true"));
        JSONObject invasive = (JSONObject) jp.parse(Util.readUrl(settings.getProperty("species_list_url", "") + "/ws/speciesList/?isInvasive=eq:true&isAuthoritative=eq:true"));
        JSONObject[] lists = { threatened, invasive };
        for (JSONObject o : lists) {
            if (sb.length() == 0)
                sb.append("Conservation");
            else
                sb.append("|Invasive");
            JSONArray ja = (JSONArray) o.get("lists");
            for (int i = 0; i < ja.size(); i++) {
                sb.append(",").append(((JSONObject) ja.get(i)).get("dataResourceUid"));
            }
        }
    } catch (Exception e) {
        LOGGER.error("failed to get species lists for threatened or invasive species", e);
    }
    return sb.toString();
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) Point(com.vividsolutions.jts.geom.Point)

Example 32 with JSONArray

use of org.json.simple.JSONArray in project spatial-portal by AtlasOfLivingAustralia.

the class EnvLayersCombobox method refresh.

public void refresh(String val) {
    //don't do autocomplete when < 1 characters
    if (val.length() < 0) {
        return;
    }
    String baseUrl = CommonData.getLayersServer() + "/fields/";
    try {
        Iterator it = getItems().iterator();
        JSONArray results;
        String lsurl = baseUrl;
        if (val.length() == 0) {
            results = CommonData.getLayerListJSONArray();
        } else {
            lsurl += "search/?q=" + URLEncoder.encode(val, StringConstants.UTF_8);
            LOGGER.debug("nsurl: " + lsurl);
            HttpClient client = new HttpClient();
            GetMethod get = new GetMethod(lsurl);
            get.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
            client.executeMethod(get);
            String slist = get.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            results = (JSONArray) jp.parse(slist);
        }
        LOGGER.debug("got " + results.size() + " layers");
        Sessions.getCurrent().setAttribute("layerlist", results);
        if (!results.isEmpty()) {
            for (int i = 0; i < results.size(); i++) {
                JSONObject field = (JSONObject) results.get(i);
                JSONObject layer = (JSONObject) field.get("layer");
                if (!field.get(StringConstants.ENABLED).toString().equalsIgnoreCase("true") || !field.containsKey(StringConstants.NAME) || !layer.containsKey(StringConstants.TYPE) || !field.get(StringConstants.ADD_TO_MAP).toString().equalsIgnoreCase("true") || (!StringConstants.ENVIRONMENTAL.equalsIgnoreCase(layer.get(StringConstants.TYPE).toString()) && (includeLayers != null && !"AllLayers".equalsIgnoreCase(includeLayers) && !"MixLayers".equalsIgnoreCase(includeLayers)))) {
                    continue;
                }
                String displayName = field.get(StringConstants.NAME).toString();
                String type = layer.get(StringConstants.TYPE).toString();
                String name = field.get(StringConstants.ID).toString();
                Comboitem myci;
                if (it != null && it.hasNext()) {
                    myci = ((Comboitem) it.next());
                    myci.setLabel(displayName);
                } else {
                    it = null;
                    myci = new Comboitem(displayName);
                    myci.setParent(this);
                }
                String c2 = "";
                if (layer.containsKey(StringConstants.CLASSIFICATION2) && !StringConstants.NULL.equals(layer.get(StringConstants.CLASSIFICATION2))) {
                    c2 = layer.get(StringConstants.CLASSIFICATION2) + ": ";
                }
                String c1 = "";
                if (layer.containsKey(StringConstants.CLASSIFICATION1) && !StringConstants.NULL.equals(layer.get(StringConstants.CLASSIFICATION1))) {
                    c1 = layer.get(StringConstants.CLASSIFICATION1) + ": ";
                }
                myci.setDescription(c1 + c2 + type);
                myci.setValue(field);
            }
        }
        if (includeAnalysisLayers) {
            for (MapLayer ml : getMapComposer().getAnalysisLayers()) {
                String displayName = ml.getDisplayName();
                String type;
                String name;
                String classification1;
                String classification2;
                if (ml.getSubType() == LayerUtilitiesImpl.MAXENT) {
                    type = StringConstants.ENVIRONMENTAL;
                    classification1 = StringConstants.ANALYSIS;
                    classification2 = StringConstants.PREDICTION;
                    name = ml.getName();
                } else if (ml.getSubType() == LayerUtilitiesImpl.GDM) {
                    type = StringConstants.ENVIRONMENTAL;
                    classification1 = StringConstants.ANALYSIS;
                    classification2 = StringConstants.GDM;
                    name = ml.getName();
                } else if (ml.getSubType() == LayerUtilitiesImpl.ODENSITY) {
                    type = StringConstants.ENVIRONMENTAL;
                    classification1 = StringConstants.ANALYSIS;
                    classification2 = StringConstants.OCCURRENCE_DENSITY;
                    name = ml.getName();
                } else if (ml.getSubType() == LayerUtilitiesImpl.SRICHNESS) {
                    type = StringConstants.ENVIRONMENTAL;
                    classification1 = StringConstants.ANALYSIS;
                    classification2 = StringConstants.SPECIES_RICHNESS;
                    name = ml.getName();
                } else {
                    continue;
                }
                Comboitem myci;
                if (it != null && it.hasNext()) {
                    myci = ((Comboitem) it.next());
                    myci.setLabel(displayName);
                } else {
                    it = null;
                    myci = new Comboitem(displayName);
                    myci.setParent(this);
                }
                myci.setDescription(classification1 + ": " + classification2 + " " + type);
                myci.setDisabled(false);
                JSONObject jo = new JSONObject();
                jo.put(StringConstants.NAME, name);
                myci.setValue(jo);
            }
        }
        while (it != null && it.hasNext()) {
            it.next();
            it.remove();
        }
    } catch (Exception e) {
        LOGGER.error("Error searching for layers:", e);
    }
}
Also used : JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) MapLayer(au.org.emii.portal.menu.MapLayer) Iterator(java.util.Iterator) JSONArray(org.json.simple.JSONArray) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser) Comboitem(org.zkoss.zul.Comboitem)

Example 33 with JSONArray

use of org.json.simple.JSONArray in project spatial-portal by AtlasOfLivingAustralia.

the class EnvironmentalList method setupListEntries.

void setupListEntries() {
    listEntries = new ArrayList<ListEntryDTO>();
    JSONArray ja = CommonData.getLayerListJSONArray();
    for (int i = 0; i < ja.size(); i++) {
        JSONObject field = (JSONObject) ja.get(i);
        JSONObject layer = (JSONObject) field.get("layer");
        listEntries.add(new ListEntryDTO(field.get(StringConstants.ID).toString(), field.containsKey(StringConstants.NAME) ? field.get(StringConstants.NAME).toString() : field.get(StringConstants.ID).toString(), layer.containsKey(StringConstants.CLASSIFICATION1) ? layer.get(StringConstants.CLASSIFICATION1).toString() : "", layer.containsKey(StringConstants.CLASSIFICATION2) ? layer.get(StringConstants.CLASSIFICATION2).toString() : "", layer.containsKey(StringConstants.TYPE) ? layer.get(StringConstants.TYPE).toString() : "", layer.containsKey(StringConstants.DOMAIN) ? layer.get(StringConstants.DOMAIN).toString() : "", layer));
    }
    if (includeAnalysisLayers) {
        for (MapLayer ml : mapComposer.getAnalysisLayers()) {
            ListEntryDTO le = null;
            if (ml.getSubType() == LayerUtilitiesImpl.ALOC) {
                le = new ListEntryDTO(ml.getName(), ml.getDisplayName(), StringConstants.ANALYSIS, StringConstants.CLASSIFICATION, "Contextual", null, null);
            } else if (ml.getSubType() == LayerUtilitiesImpl.MAXENT) {
                le = new ListEntryDTO(ml.getName(), ml.getDisplayName(), StringConstants.ANALYSIS, StringConstants.PREDICTION, StringConstants.ENVIRONMENTAL, null, null);
            } else if (ml.getSubType() == LayerUtilitiesImpl.GDM) {
                le = new ListEntryDTO(ml.getName(), ml.getDisplayName(), StringConstants.ANALYSIS, StringConstants.GDM, StringConstants.ENVIRONMENTAL, null, null);
            } else if (ml.getSubType() == LayerUtilitiesImpl.ODENSITY) {
                le = new ListEntryDTO(ml.getName(), ml.getDisplayName(), StringConstants.ANALYSIS, StringConstants.OCCURRENCE_DENSITY, StringConstants.ENVIRONMENTAL, null, null);
            } else if (ml.getSubType() == LayerUtilitiesImpl.SRICHNESS) {
                le = new ListEntryDTO(ml.getName(), ml.getDisplayName(), StringConstants.ANALYSIS, StringConstants.SPECIES_RICHNESS, StringConstants.ENVIRONMENTAL, null, null);
            }
            if (le != null) {
                listEntries.add(le);
            }
        }
    }
    java.util.Collections.sort(listEntries, new Comparator<ListEntryDTO>() {

        @Override
        public int compare(ListEntryDTO e1, ListEntryDTO e2) {
            return (e1.getCatagory1() + " " + e1.getCatagory2() + " " + e1.getDisplayName()).compareTo(e2.getCatagory1() + " " + e2.getCatagory2() + " " + e2.getDisplayName());
        }
    });
}
Also used : ListEntryDTO(au.org.ala.spatial.dto.ListEntryDTO) JSONObject(org.json.simple.JSONObject) MapLayer(au.org.emii.portal.menu.MapLayer) JSONArray(org.json.simple.JSONArray)

Example 34 with JSONArray

use of org.json.simple.JSONArray in project spatial-portal by AtlasOfLivingAustralia.

the class GazetteerAutoComplete method refresh.

/**
     * Refresh comboitem based on the specified value.
     */
private void refresh(String val) {
    String searchString = val.trim().replaceAll("\\s+", "+");
    searchString = (searchString.isEmpty()) ? "a" : searchString;
    try {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(CommonData.getLayersServer() + "/search?limit=40&q=" + searchString + "&userObjects=false");
        get.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
        client.executeMethod(get);
        String slist = get.getResponseBodyAsString();
        JSONParser jp = new JSONParser();
        JSONArray ja = (JSONArray) jp.parse(slist);
        if (ja == null) {
            return;
        }
        Iterator it = getItems().iterator();
        for (int i = 0; i < ja.size(); i++) {
            JSONObject jo = (JSONObject) ja.get(i);
            String itemString = jo.get(StringConstants.NAME).toString();
            String description = (jo.containsKey(StringConstants.DESCRIPTION) ? jo.get(StringConstants.DESCRIPTION).toString() : "") + " (" + jo.get("fieldname") + ")";
            if (it != null && it.hasNext()) {
                Comboitem ci = (Comboitem) it.next();
                ci.setLabel(itemString);
                ci.setValue(jo);
                ci.setDescription(description);
            } else {
                it = null;
                Comboitem ci = new Comboitem();
                ci.setLabel(itemString);
                ci.setValue(jo);
                ci.setDescription(description);
                ci.setParent(this);
            }
        }
        while (it != null && it.hasNext()) {
            it.next();
            it.remove();
        }
    } catch (Exception e) {
        LOGGER.error("error selecting gaz autocomplete item", e);
    }
}
Also used : JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONArray(org.json.simple.JSONArray) Iterator(java.util.Iterator) JSONParser(org.json.simple.parser.JSONParser) Comboitem(org.zkoss.zul.Comboitem)

Example 35 with JSONArray

use of org.json.simple.JSONArray in project spatial-portal by AtlasOfLivingAustralia.

the class GazetteerPointSearch method pointSearch.

/**
     * *
     * Given a lon,lat and layer - queries the gaz for a polygon
     *
     * @param lon   longitude
     * @param lat   latitude
     * @param layer geoserver layer to search
     * @return returns a link to a geojson feature in the gaz
     */
public static Map<String, String> pointSearch(String lon, String lat, String layer, String geoserver) {
    try {
        String uri = CommonData.getLayersServer() + "/intersect/" + layer + "/" + lat + "/" + lon;
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(uri);
        get.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
        int result = client.executeMethod(get);
        String slist = get.getResponseBodyAsString();
        LOGGER.debug("URI: " + uri);
        LOGGER.debug("result: " + result);
        LOGGER.debug("slist: " + slist);
        JSONParser jp = new JSONParser();
        JSONArray ja = (JSONArray) jp.parse(slist);
        if (ja != null && !ja.isEmpty()) {
            JSONObject jo = (JSONObject) ja.get(0);
            Map<String, String> map = new HashMap<String, String>();
            for (Object k : jo.keySet()) {
                map.put((String) k, jo.get((String) k) == null ? "" : jo.get((String) k).toString());
            }
            return map;
        }
    } catch (Exception e1) {
        LOGGER.error("error with gaz point search", e1);
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject)

Aggregations

JSONArray (org.json.simple.JSONArray)267 JSONObject (org.json.simple.JSONObject)238 JSONParser (org.json.simple.parser.JSONParser)73 Test (org.junit.Test)40 ParseException (org.json.simple.parser.ParseException)23 ArrayList (java.util.ArrayList)21 HttpClient (org.apache.commons.httpclient.HttpClient)21 IOException (java.io.IOException)17 HashMap (java.util.HashMap)17 GetMethod (org.apache.commons.httpclient.methods.GetMethod)17 Transaction (org.xel.Transaction)12 File (java.io.File)11 List (java.util.List)10 HttpResponse (org.apache.http.HttpResponse)10 BlockchainTest (org.xel.BlockchainTest)10 APICall (org.xel.http.APICall)10 Block (org.xel.Block)9 MapLayer (au.org.emii.portal.menu.MapLayer)8 Map (java.util.Map)8 FileReader (java.io.FileReader)6