Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.nasa; import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; public class Transformer { public static JsonObject weatherToMap(JsonObject jsonObject) { JsonObject result = new JsonObject(); JsonArray featuresArr = new JsonArray(); JsonArray items = jsonObject.getJsonObject("hits").getJsonArray("hits"); int idx = 0; for (Object hit : items) { JsonObject source = ((JsonObject) hit).getJsonObject("_source"); JsonObject feature = new JsonObject(); feature.put("type", "Feature"); feature.put("id", idx); JsonObject properties = new JsonObject(); float mag = source.getJsonObject("weather").getFloat("temp"); properties.put("mag", mag); feature.put("properties", properties); JsonObject geometry = new JsonObject(); geometry.put("type", "Point"); JsonArray coordinates = new JsonArray(); Float lon = source.getJsonObject("location").getFloat("lon"); Float lat = source.getJsonObject("location").getFloat("lat"); coordinates.add(lon); coordinates.add(lat); geometry.put("coordinates", coordinates); feature.put("geometry", geometry); featuresArr.add(feature); idx++; } result.put("type", "FeatureCollection"); result.put("features", featuresArr); return result; } public static JsonObject pollutionToMap(JsonObject jsonObject) { JsonObject result = new JsonObject(); JsonArray featuresArr = new JsonArray(); JsonArray items = jsonObject.getJsonObject("hits").getJsonArray("hits"); int idx = 0; for (Object hit : items) { JsonObject source = ((JsonObject) hit).getJsonObject("_source"); JsonObject feature = new JsonObject(); feature.put("type", "Feature"); feature.put("id", idx); JsonObject properties = new JsonObject(); float mag = source.getJsonObject("pollution").getFloat("aqi"); properties.put("mag", mag); feature.put("properties", properties); JsonObject geometry = new JsonObject(); geometry.put("type", "Point"); JsonArray coordinates = new JsonArray(); Float lon = source.getJsonObject("location").getFloat("lon"); Float lat = source.getJsonObject("location").getFloat("lat"); coordinates.add(lon); coordinates.add(lat); geometry.put("coordinates", coordinates); feature.put("geometry", geometry); featuresArr.add(feature); idx++; } result.put("type", "FeatureCollection"); result.put("features", featuresArr); return result; } public static JsonObject conditionToMap(JsonObject jsonObject) { // private String conditionsMap = "{\n" // + " \"Diseases\": [\n" // + " {\n" // + " \"lat\": 46.77029284,\n" // + " \"lng\": 23.57641889,\n" // + " \"type\": \"something good\",\n" // + " \"description\": \"good\",\n" // + " \"rating\": 1.23\n" // + " },\n" // + " {\n" // + " \"lat\": 46.78185201,\n" // + " \"lng\": 23.68522613,\n" // + " \"type\": \"something not bad\",\n" // + " \"description\": \"not bad\",\n" // + " \"rating\": 3.99\n" // + " },\n" // + " {\n" // + " \"lat\": 46.7558097,\n" // + " \"lng\": 23.5940353,\n" // + " \"type\": \"something bad\",\n" // + " \"description\": \"bad\",\n" // + " \"rating\": 4.75\n" // + " }\n" // + " ]\n" // + "}"; JsonObject result = new JsonObject(); JsonArray featuresArr = new JsonArray(); JsonArray items = jsonObject.getJsonObject("hits").getJsonArray("hits"); for (Object hit : items) { JsonObject source = ((JsonObject) hit).getJsonObject("_source"); JsonObject feature = new JsonObject(); Float lon = source.getJsonObject("location").getFloat("lon"); Float lat = source.getJsonObject("location").getFloat("lat"); feature.put("lat", lat); feature.put("lng", lon); feature.put("type", source.getString("condition")); String symptoms = ""; int ratingSum = 0; int ratingCount = 0; JsonArray symptomsJ = source.getJsonArray("symptoms"); for (Object sym : symptomsJ) { JsonObject symJ = (JsonObject) sym; symptoms += symJ.getString("name") + ", "; ratingSum += symJ.getInteger("rating"); ratingCount++; } feature.put("description", symptoms); if (ratingCount > 0) { feature.put("rating", ratingSum / ratingCount); } else { feature.put("rating", 0); } featuresArr.add(feature); } result.put("Diseases", featuresArr); return result; } }