ServiceCall.java [src/java/m/wqm/wqmsoilattributes] Revision: 75536cb4a3a1cec75d1e239cd980034e2d3ee091 Date: Sat Jun 27 19:50:42 MDT 2015
package m.wqm.wqmsoilattributes;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
/**
*
* @author RUMPAL SIDHU
*/
public class ServiceCall {
private HashMap<String, Double> aoa_mukeyList;
private final String SSURGO_INTERSECT = "http://csip.engr.colostate.edu:8081/csip/d/soils/1.1";
/*Calls the csip soil service to intersect AoA and SSURGO layers producing
set of AoA x mapunit polygons */
public HashMap intersect(String aoaGeometry) {
aoa_mukeyList = new HashMap();
try {
HttpPost post = new HttpPost(SSURGO_INTERSECT);
HttpClient client = HttpClientBuilder.create().build();
String geo = "{\n"
+ " \"metainfo\": {},\n"
+ " \"parameter\": [\n"
+ " {\n"
+ " \"name\": \"AoI\",\n"
+ " \"value\": " + aoaGeometry + "\n"
+ " }\n"
+ " ]\n"
+ "}";
StringEntity stringEntity = new StringEntity(geo, ContentType.APPLICATION_JSON);
post.setEntity(stringEntity);
HttpResponse response = client.execute(post);
System.out.println(response.getStatusLine());
StringBuilder result;
try (BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) {
result = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
result.append('\n');
}
HashMap tempMap = new HashMap();
JSONArray r = new JSONObject(result.toString()).getJSONArray("result").getJSONArray(1).getJSONArray(0);
String mukey;
double aoaArea;
ArrayList<String> keys = new ArrayList<String>();
for (int i = 0; i < r.length(); i++) {
mukey = r.getJSONArray(i).getJSONObject(1).getString("value");
aoaArea = r.getJSONArray(i).getJSONObject(15).getDouble("value");
boolean contains = tempMap.containsKey(geo);
if (!contains) {
tempMap.put(mukey, aoaArea);
keys.add(mukey);
} else {
aoaArea = aoaArea + (Double) tempMap.get(mukey);
tempMap.remove(mukey);
tempMap.put(mukey, aoaArea);
}
}
Collections.sort(keys);
keys.stream().forEach((i) -> {
aoa_mukeyList.put(i, (Double) tempMap.get(i));
});
}
} catch (UnsupportedCharsetException | IOException | IllegalStateException | JSONException e) {
} finally {
}
return aoa_mukeyList;
}
}