Displaying differences for changeset
 
display as  

tools/GetLatLonCokey/src/getlatloncokey/GetLatLonCokey.java

@@ -5,28 +5,15 @@
  */
 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;
@@ -42,6 +29,7 @@
      * @param args the command line arguments
      */
     public static void main(String[] args) throws IOException {
+        ArrayList<String> fields = new ArrayList<>();
 
         MapUnit.setDefaultUsedColumns(new ArrayList<>(Arrays.asList(TableMapUnit.MUKEY, TableMapUnit.MUACRES)));
         Component.setDefaultUsedColumns(new ArrayList<>(Arrays.asList(TableComponent.COKEY, TableComponent.COMPPCT_R_NAME)));
@@ -50,8 +38,21 @@
         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outFile));
 
         //  Write header
-        writer.write("latitude, longitude, cokey" + System.lineSeparator());
+        if (args.length >= 1) {
+            String tFields = args[0].trim();
+            String[] fieldList = tFields.split(",");
 
+            for (String field : fieldList) {
+                fields.add(field.trim().toLowerCase());
+            }
+        } else {
+            fields.add("latitude");
+            fields.add("longitude");
+            fields.add("cokey");
+        }
+
+        writer.write(fields + System.lineSeparator());
+        
         File folder = new File(new java.io.File(".").getCanonicalPath());
         File[] listOfFiles = folder.listFiles();
 
@@ -62,7 +63,7 @@
 
                     CliWindSoilFile cwsFile;
                     try {
-                        cwsFile = new CliWindSoilFile(file);
+                        cwsFile = new CliWindSoilFile(file, fields);
                         cwsFile.writeCSV(writer);
                     } catch (JSONException | ServiceException ex) {
                         System.err.println(ex);
@@ -74,133 +75,4 @@
 
         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();
-            }
-        }
-    }
 }