GetLatLonCokey.java [tools/GetLatLonCokey/src/getlatloncokey] Revision: cd8d9383764d483b896f2fcbccf324614b63af3b Date: Thu Oct 17 16:18:19 MDT 2019
/*
* 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 getlatloncokey;
import static csip.ModelDataService.KEY_VALUE;
import csip.ServiceException;
import csip.utils.JSONUtils;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import soils.Component;
import soils.MapUnit;
import soils.db.tables.TableComponent;
import soils.db.tables.TableMapUnit;
/**
*
* @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
*/
public class GetLatLonCokey {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
MapUnit.setDefaultUsedColumns(new ArrayList<>(Arrays.asList(TableMapUnit.MUKEY, TableMapUnit.MUACRES)));
Component.setDefaultUsedColumns(new ArrayList<>(Arrays.asList(TableComponent.COKEY, TableComponent.COMPPCT_R_NAME)));
FileOutputStream outFile = new FileOutputStream("latlon_cokey.csv");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outFile));
// Write header
writer.write("latitude, longitude, cokey" + System.lineSeparator());
File folder = new File(new java.io.File(".").getCanonicalPath());
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
if (file.getName().contains(".zip")) {
System.out.println(file.getName());
CliWindSoilFile cwsFile;
try {
cwsFile = new CliWindSoilFile(file);
cwsFile.writeCSV(writer);
} catch (JSONException | ServiceException ex) {
System.err.println(ex);
}
}
}
}
writer.close();
}
public static class CliWindSoilFile {
protected static final String SOILS_RETURN = "soils.json";
protected static final String SOILS_REQUEST = "request.json";
protected JSONArray soilsReturn;
protected JSONArray returnVal;
protected JSONObject request;
protected double latitude = Double.NaN, longitude = Double.NaN;
protected String cokey = "";
soils.AoA aoa;
public CliWindSoilFile(File filename) throws IOException, JSONException, ServiceException {
boolean gotResponse = false;
boolean gotRequest = false;
InputStream zipFile = new FileInputStream(filename);
try (ZipInputStream zin = new ZipInputStream(zipFile)) {
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
if (entry.getName().contains(SOILS_RETURN)) {
BufferedReader bReader = new BufferedReader(new InputStreamReader(zin));
StringBuilder fileContent = new StringBuilder();
String inputStr;
while ((inputStr = bReader.readLine()) != null) {
fileContent.append(inputStr);
}
returnVal = new JSONArray(fileContent.toString());
//soilsReturn = returnVal.getJSONArray(KEY_RESULT).getJSONArray(0);
Map<String, JSONObject> inputMap = JSONUtils.preprocess(returnVal);
aoa = new soils.AoA(inputMap);
if (aoa.getMapUnits().size() <= 0 ) {
throw new ServiceException("Mapunits returned for this AoA did not match expected number of 1. Mapunit size is: " + aoa.getMapUnits().size());
}
MapUnit maxMapUnit = null;
for (String mukey : aoa.getMapUnits().keySet()) {
MapUnit mapunit = aoa.getMapUnits().get(mukey);
if ( null == maxMapUnit){
maxMapUnit = mapunit;
}
else{
if ( mapunit.muacres() > maxMapUnit.muacres()){
maxMapUnit = mapunit;
}
}
}
Component maxComponent = null;
if( null != maxMapUnit ) {
for (String cokey : maxMapUnit.components().keySet()) {
Component component = maxMapUnit.components().get(cokey);
if (null == maxComponent) {
maxComponent = component;
} else {
if (component.area_pct() > maxComponent.area_pct()) {
maxComponent = component;
}
}
}
}else{
throw new ServiceException("No max mapunit was found.");
}
if (null != maxComponent) {
cokey = maxComponent.cokey();
gotResponse = true;
}
}
if (entry.getName().contains(SOILS_REQUEST)) {
BufferedReader bReader = new BufferedReader(new InputStreamReader(zin));
StringBuilder fileContent = new StringBuilder();
String inputStr;
while ((inputStr = bReader.readLine()) != null) {
fileContent.append(inputStr);
}
request = new JSONObject(fileContent.toString());
String coordinates = request.getString("coordinates");
String type = request.getString("type");
if (type.equalsIgnoreCase("point")) {
coordinates = coordinates.replace("[", " ");
coordinates = coordinates.replace("]", " ");
String[] coord = coordinates.split(",");
if (coord.length == 2) {
longitude = Double.parseDouble(coord[0]);
latitude = Double.parseDouble(coord[1]);
gotRequest = true;
}
}
}
if (gotRequest && gotResponse) {
break;
}
}
if (!gotRequest || !gotResponse) {
throw new ServiceException("Could not parse all of the service zip file properly: " + filename);
}
}
}
public double latitude() {
return latitude;
}
public double longitude() {
return longitude;
}
public String cokey() {
return cokey;
}
public void writeCSV(BufferedWriter writer) throws IOException {
if (null != writer) {
String outString = latitude + ", " + longitude + ", " + cokey + System.lineSeparator();
writer.write(outString);
writer.flush();
}
}
}
}