V2_0.java [src/java/d/soils/rse02_wepot] 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 d.soils.rse02_wepot;

import adb.DBResources;
import static adb.DBResources.EROSION_SQLSVR;
import static adb.DBResources.R2GIS_SQLSVR;
import csip.Config;
import csip.ModelDataService;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.Resource;
import static csip.annotations.ResourceType.FILE;
import csip.api.server.ServiceResources;
import d.util.WindWaterErosion;
import static d.util.WindWaterErosion.WWE_TIFF_FILE;
import java.sql.Connection;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.ws.rs.Path;
import static soils.db.DBResources.SDM;
import soils.db.SOILS_DATA;
import soils.db.SOILS_DB_Factory;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
@Name("wepot")
@Description("This service computes water and wind erodibility potentials for"
    + " an area of analysis (AoA).  The service clips SSURGO soil mapunits "
    + "with AoA geometry, determines the dominant soil component in the AoA, "
    + "gets parameters from the SSURGO component table, including a climate "
    + "factor from the C Factor layer, and computes the following equations:  "
    + "Wind Erosion Potential = C*I/T ;  Water Erosion Potential = K*(LS)/T")
@Path("d/wepot/2.0")

//@Resource(file = "/data/us_cvalues.zip", type = ARCHIVE)
@Resource(file = "/data/us_cvalues_topo2ras_masked.tif", type = FILE, id = WWE_TIFF_FILE)
@Resource(file = "/data/us_cvalues_topo2ras_masked.tfw", type = FILE, id = "tfw_file")
@Resource(file = "/data/us_cvalues_topo2ras_masked.tif.aux.xml", type = FILE, id = "tifxml_file")

@Resource(from = soils.db.DBResources.class)
@Resource(from = DBResources.class)
public class V2_0 extends ModelDataService {

  protected ServiceResources resources = resources();

  @Override
  protected Map<String, Object> getConfigInfo() {
    return new LinkedHashMap<String, Object>() {
      {
        put("soils.gis.database.source", resources().getResolved("soils.gis.database.source"));
        put(SDM, resources().getResolved(SDM));
        put(R2GIS_SQLSVR, resources().getResolved(R2GIS_SQLSVR));
        put("cfactor_gis_raster_file", resources().getResolved(WWE_TIFF_FILE));
        put("fpp.version", "wepot 2.0");
      }
    };
  }

  @Override
  protected void doProcess() throws Exception {
    try (SOILS_DATA soilsDb = SOILS_DB_Factory.createEngine(getClass(), LOG, Config.getString("soils.gis.database.source")); Connection gisDb = resources.getJDBC(EROSION_SQLSVR);) {
      soils.AoA.setMapUnitsRequired(false);
      WindWaterErosion.loadGisRaster(() -> resources.getFile(WWE_TIFF_FILE));

      WindWaterErosion aoaErosion = new WindWaterErosion(soilsDb, gisDb, getParamMap(), LOG);

      aoaErosion.getErosion(() -> resources.getJDBC(R2GIS_SQLSVR), false); //Will throw exception on failure

      aoaErosion.toJSON((String name, Object val, String descr, String unit) -> this.results().put(name, val, descr, unit));
    }
  }
}