use of org.json.simple.parser.ParseException in project Dragonet-Legacy by DragonetMC.
the class ChatMessageTranslator method handleSpecific.
@Override
public PEPacket[] handleSpecific(ChatMessage packet) {
String msg = "";
try {
//String msg = ((ChatMessage) message).text.asPlaintext();
Object json = new JSONParser().parse(packet.text.encode());
if (json instanceof JSONObject) {
msg = this.getTranslator().translateChatMessage((JSONObject) json);
} else {
msg = packet.text.asPlaintext();
}
} catch (ParseException ex) {
return null;
}
//if(json)
ChatPacket pkMessage = new ChatPacket();
pkMessage.source = "";
pkMessage.type = ChatPacket.TextType.RAW;
pkMessage.message = msg;
return new PEPacket[] { pkMessage };
}
use of org.json.simple.parser.ParseException in project hadoop by apache.
the class HttpFSFileSystem method createXAttrNames.
/** Convert xAttr names json to names list */
private List<String> createXAttrNames(String xattrNamesStr) throws IOException {
JSONParser parser = new JSONParser();
JSONArray jsonArray;
try {
jsonArray = (JSONArray) parser.parse(xattrNamesStr);
List<String> names = Lists.newArrayListWithCapacity(jsonArray.size());
for (Object name : jsonArray) {
names.add((String) name);
}
return names;
} catch (ParseException e) {
throw new IOException("JSON parser error, " + e.getMessage(), e);
}
}
use of org.json.simple.parser.ParseException in project spatial-portal by AtlasOfLivingAustralia.
the class ContextualLayerSelection method onChange$autoCompleteLayers.
public void onChange$autoCompleteLayers(Event event) {
treeName = null;
ContextualLayerListComposer llc = (ContextualLayerListComposer) getFellow("layerTree").getFellow("contextuallayerlistwindow");
if (autoCompleteLayers.getItemCount() > 0 && autoCompleteLayers.getSelectedItem() != null) {
JSONObject jo = autoCompleteLayers.getSelectedItem().getValue();
String metadata;
metadata = CommonData.getLayersServer() + "/layers/view/more/" + jo.get(StringConstants.ID);
setLayer(jo.get(StringConstants.NAME).toString(), ((JSONObject) jo.get("layer")).get("displaypath").toString(), metadata, StringConstants.ENVIRONMENTAL.equalsIgnoreCase(((JSONObject) jo.get("layer")).get(StringConstants.TYPE).toString()) ? LayerUtilitiesImpl.GRID : LayerUtilitiesImpl.CONTEXTUAL);
} else {
// it generates an error. This should fix it.
if (llc.tree.getSelectedItem() == null) {
return;
}
JSONParser jp = new JSONParser();
JSONObject joLayer = null;
try {
joLayer = (JSONObject) jp.parse(llc.tree.getSelectedItem().getTreerow().getAttribute("lyr").toString());
} catch (ParseException e) {
}
if (!StringConstants.CLASS.equals(joLayer.get(StringConstants.TYPE))) {
String metadata = CommonData.getLayersServer() + "/layers/view/more/" + joLayer.get(StringConstants.ID);
setLayer(joLayer.get(StringConstants.NAME).toString(), ((JSONObject) joLayer.get("layer")).get("displaypath").toString(), metadata, StringConstants.ENVIRONMENTAL.equalsIgnoreCase(((JSONObject) joLayer.get("layer")).get(StringConstants.TYPE).toString()) ? LayerUtilitiesImpl.GRID : LayerUtilitiesImpl.CONTEXTUAL);
} else {
String classValue = joLayer.get(StringConstants.NAME).toString();
String layer = joLayer.get(StringConstants.LAYERNAME).toString();
String displaypath = CommonData.getGeoServer() + "/wms?service=WMS&version=1.1.0&request=GetMap&layers=ALA:Objects&format=image/png&viewparams=s:" + joLayer.get("displaypath");
displaypath = displaypath.replace("gwc/service/", "");
String metadata = CommonData.getLayersServer() + "/layers/view/more/" + joLayer.get(StringConstants.ID);
setLayer(layer + " - " + classValue, displaypath, metadata, StringConstants.ENVIRONMENTAL.equalsIgnoreCase(joLayer.get(StringConstants.TYPE).toString()) ? LayerUtilitiesImpl.GRID : LayerUtilitiesImpl.CONTEXTUAL);
}
//close parent if it is 'addlayerwindow'
if (getRoot().hasFellow("addlayerwindow")) {
getRoot().getFellow("addlayerwindow").detach();
}
}
}
use of org.json.simple.parser.ParseException in project spatial-portal by AtlasOfLivingAustralia.
the class MapComposer method addObjectByPid.
/**
* Adds a object as a layer to the map.
*
* @param pid
* @param displayName
* @param radiusKm for use when the pid refers to a point not a polygon
* @return
*/
public MapLayer addObjectByPid(String pid, String displayName, double radiusKm) {
JSONParser jp = new JSONParser();
JSONObject obj = null;
try {
obj = (JSONObject) jp.parse(Util.readUrl(CommonData.getLayersServer() + "/object/" + pid));
} catch (ParseException e) {
LOGGER.error("failed to parse for object: " + pid);
}
//add feature to the map as a new layer
String areaName = obj.get(StringConstants.NAME).toString();
MapLayer mapLayer = null;
boolean pointLayer = false;
List<Double> dbb = null;
try {
dbb = Util.getBoundingBox(obj.get(StringConstants.BBOX).toString());
//if the layer is a point create a radius
if (dbb.get(0).floatValue() == dbb.get(2).floatValue() && dbb.get(1).floatValue() == dbb.get(3).floatValue()) {
double radius = radiusKm * 1000.0;
String wkt = Util.createCircleJs(dbb.get(0).floatValue(), dbb.get(1).floatValue(), radius);
mapLayer = getMapComposer().addWKTLayer(wkt, displayName, displayName);
pointLayer = true;
}
} catch (Exception e) {
}
if (mapLayer == null) {
//not a point layer
mapLayer = getMapComposer().addWMSLayer("PID:" + pid, displayName == null ? areaName : displayName, obj.get(StringConstants.WMSURL).toString(), 0.6f, null, null, LayerUtilitiesImpl.WKT, null, null);
mapLayer.setAreaSqKm(obj.get(StringConstants.AREA_KM).toString());
}
if (mapLayer == null) {
return null;
}
if (dbb != null) {
mapLayer.getMapLayerMetadata().setBbox(dbb);
}
mapLayer.setPid(pid);
mapLayer.setPolygonLayer(true);
//if the layer is a point create a radius
String fid = obj.get(StringConstants.FID).toString();
MapLayerMetadata md = mapLayer.getMapLayerMetadata();
try {
md.setBbox(Util.getBoundingBox(obj.get(StringConstants.BBOX).toString()));
} catch (Exception e) {
}
Facet facet = null;
if (!pointLayer && CommonData.getLayer(fid) != null && CommonData.getFacetLayerNameDefault(fid) != null) {
JSONObject field = CommonData.getLayer(fid);
if (field.containsKey("indb") && StringConstants.TRUE.equalsIgnoreCase(field.get("indb").toString())) {
String spid = field.get("spid").toString();
md.setMoreInfo(CommonData.getLayersServer() + "/layers/view/more/" + spid);
facet = Util.getFacetForObject(areaName, fid);
}
}
if (facet != null) {
List<Facet> facets = new ArrayList<Facet>();
facets.add(facet);
mapLayer.setFacets(facets);
//do not set WKT for grids as shapefiles
if (!((JSONObject) CommonData.getLayer(fid).get("layer")).get("path_orig").toString().contains("diva")) {
mapLayer.setWktUrl(CommonData.getLayersServer() + "/shape/wkt/" + pid);
}
} else if (!pointLayer) {
//not in biocache, so add as WKT
mapLayer.setWKT(Util.readUrl(CommonData.getLayersServer() + "/shape/wkt/" + pid));
}
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);
getMapComposer().updateLayerControls();
return mapLayer;
}
use of org.json.simple.parser.ParseException 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);
}
Aggregations