V1_0.java [src/java/m/nmpt/nmpt05_getmupct] 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 m.nmpt.nmpt05_getmupct;
import csip.annotations.Polling;
import csip.annotations.Resource;
import csip.ModelDataService;
import csip.ServiceException;
import csip.SessionLogger;
import csip.utils.JSONUtils;
import gisobjects.GISObject;
import gisobjects.GISObjectException;
import static gisobjects.GISObjectFactory.createGISObject;
import static gisobjects.db.GISEngineFactory.createGISEngine;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import javax.ws.rs.Path;
import nmpt.utils.DBResources;
import static nmpt.utils.DBResources.CRDB;
import nmpt.utils.NMPTUtils;
import csip.annotations.Description;
import csip.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import soils.MapUnit;
import soils.db.SOILS_DATA;
import soils.db.SOILS_DB_Factory;
import soils.db.tables.TableMapUnit;
/**
* NMPT-05: Get Map Units and Map Unit Percentages for AoA
*
* @author Rumpal Sidhu
* @version 1.0
*/
@Name("NMPT-05: Get Map Units and Map Unit Percentages for AoA")
@Description("Intersect AoA boundary with Soil Data Mart spatial layer to "
+ "retrieve a list of soil map units with percentages of each mapunit within AoA.")
@Path("m/nmpt/getmupct/1.0")
@Polling(first = 10000, next = 2000)
@Resource(from = DBResources.class)
public class V1_0 extends ModelDataService {
private int aoaId;
private JSONObject aoaGeometry;
private double aoaAcres;
private JSONArray result;
@Override
protected void preProcess() throws ServiceException {
aoaId = getIntParam("AoAId", 0);
aoaGeometry = getParam("aoa_geometry");
}
@Override
protected void doProcess() throws ServiceException, SQLException, Exception {
try (SOILS_DATA soilsDb = SOILS_DB_Factory.createEngine(getClass(), LOG, "SDM_REST");
Connection gisDb = getResourceJDBC(CRDB);) {
GISObject aoaGISGeo = createGISObject(aoaGeometry, createGISEngine(gisDb));
AoA aoaObject = new AoA(soilsDb, aoaGISGeo, LOG);
aoaAcres = aoaObject.getArea();
aoaObject.findIntersectedMapUnits();
result = aoaObject.toJSON();
}
}
@Override
protected void postProcess() {
putResult("AoAId", aoaId, "Area of Analysis Identifier");
putResult("aoa_acres", NMPTUtils.roundValues(aoaAcres, 2), "Area of Analysis acres", "acres");
putResult("Map Unit List", result);
}
private JSONObject getParam(String name) throws ServiceException {
JSONObject p = getParamMap().get(name);
if (p == null) {
throw new ServiceException("Parameter not found: '" + name + "'");
}
return p;
}
class AoA extends soils.AoA {
public AoA(SOILS_DATA soilsDb, GISObject gisShape, SessionLogger Log) throws GISObjectException, ServiceException, SQLException {
super(soilsDb, gisShape, Log);
}
public JSONArray toJSON() throws JSONException, ServiceException {
JSONArray mapunitData = new JSONArray();
for (MapUnit mapUnit : map_units.values()) {
JSONArray mapUnitArray = new JSONArray();
mapUnit.setOutputColumns(new ArrayList<>(Arrays.asList(TableMapUnit.MUKEY, TableMapUnit.MUSYM, TableMapUnit.MUNAME)));
mapUnit.mapUnitJSON(mapUnitArray);
mapUnitArray.put(JSONUtils.dataUnitDesc("mu_acres", NMPTUtils.roundValues(mapUnit.area(), 2), "acres", "Map Unit Acres"));
mapUnitArray.put(JSONUtils.dataDesc("mu_pct", NMPTUtils.roundValues(mapUnit.area_pct(), 2), "Map Unit Percent"));
mapunitData.put(mapUnitArray);
}
return mapunitData;
}
}
}