SVAPUtils.java [src/java/svap/utils] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2017, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package svap.utils;

import csip.ServiceException;
import gisobjects.GISObject;
import gisobjects.GISObjectException;
import gisobjects.vector.GIS_FeatureCollection;
import java.io.IOException;
import static java.lang.Math.abs;
import java.net.URISyntaxException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author rumpal
 */
public class SVAPUtils {

  public final static String[] MONTH_NAMES = {"January", "February", "March",
    "April", "May", "June", "July", "August", "September", "October", "November", "December"};


  public static JSONObject getHUC12Data(JSONObject assessmentLocation, String format) throws JSONException, URISyntaxException, IOException, ServiceException {
    String latitude = assessmentLocation.getJSONArray("coordinates").get(0).toString();
    String longitude = assessmentLocation.getJSONArray("coordinates").get(1).toString();
    HUC12 huc12 = new HUC12();
    huc12.setOutFields("HUC12, NAME, AREAACRES");
    huc12.setFormat(format);
    return huc12.getHUC12Data(latitude + ", " + longitude);
  }


  public static JSONObject getNHDFlowlineData(String geometry, String format, String whereClause) throws JSONException, URISyntaxException, IOException, ServiceException {
    NHDFlowline nhdFlowline = new NHDFlowline();
    nhdFlowline.setFormat(format);
    nhdFlowline.setWhereClause(whereClause);
    return nhdFlowline.getNHDFlowlineData(geometry);
  }


  public static HashMap<String, Double> computePropertyStreamMiles(JSONObject flowLine) throws JSONException {
    HashMap<String, Double> streamMiles = new HashMap();
    double perennialStreamMiles = 0, intermittentStreamMiles = 0, ephemeralStreamMiles = 0;
    JSONArray features = flowLine.getJSONArray("features");
    for (int i = 0; i < features.length(); i++) {
      JSONObject attrib = features.getJSONObject(i).getJSONObject("attributes");
      int fcode = attrib.getInt("FCODE");
      //The length is in kilometers; unit = km
      double length = attrib.getDouble("LENGTHKM");
      switch (fcode) {
        case 46003:
          perennialStreamMiles += length;
          break;
        case 46006:
          intermittentStreamMiles += length;
          break;
        case 46007:
          ephemeralStreamMiles += length;
          break;
      }
    }

    streamMiles.put("perennialStreamMiles", convertKMtoMiles(perennialStreamMiles));
    streamMiles.put("intermittentStreamMiles", convertKMtoMiles(intermittentStreamMiles));
    streamMiles.put("ephemeralStreamMiles", convertKMtoMiles(ephemeralStreamMiles));
    return streamMiles;
  }


  public static HashMap<String, Double> computePropertyStreamMiles(GISObject streams, GISObject boundary) throws JSONException, GISObjectException {
    HashMap<String, Double> streamMiles = new HashMap();
    double perennialStreamMiles = 0, intermittentStreamMiles = 0, ephemeralStreamMiles = 0;

    if (streams.getType() == GISObject.GISObjectType.featurecollection) {
      GIS_FeatureCollection streamFeatures = (GIS_FeatureCollection) streams;
      for (int i = 0; i < streamFeatures.getFeatureCount(); i++) {
        GISObject streamSection = streamFeatures.getGeometry(i);

        String streamIntersection = streamSection.intersection(boundary).toWKT();
        double length = streamSection.intersection(boundary).length();  //In Meters
        switch (Integer.parseInt(streamFeatures.getFeatureAttribute(i, "FCODE"))) {
          case 46003:
            perennialStreamMiles += length;
            break;
          case 46006:
            intermittentStreamMiles += length;
            break;
          case 46007:
            ephemeralStreamMiles += length;
            break;

        }
      }
    }

    streamMiles.put("perennialStreamMiles", convertKMtoMiles(perennialStreamMiles / 1000.0));
    streamMiles.put("intermittentStreamMiles", convertKMtoMiles(intermittentStreamMiles / 1000.0));
    streamMiles.put("ephemeralStreamMiles", convertKMtoMiles(ephemeralStreamMiles / 1000.0));
    return streamMiles;
  }


  public static double convertKMtoMiles(double value) {
    return value * 0.621371;
  }


  public synchronized static final LocalDate getDate(String sDate, int offset) {
    LocalDate date = LocalDate.parse(sDate, DateTimeFormatter.ISO_LOCAL_DATE);
    return date.minusDays(abs(offset));
  }
}