CMZ1_1.java [src/java/d/rusle2] Revision: 57d2b78912cf87f99bd814df3c90cb61873e5fe4  Date: Mon Apr 10 21:28:56 MDT 2017
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package d.rusle2;

import c.SqlGIS;
import javax.ws.rs.Path;
import oms3.annotations.Description;
import oms3.annotations.Name;
import oms3.annotations.VersionInfo;
import csip.ModelDataService;
import csip.ServiceException;
import csip.annotations.Resource;
import static csip.annotations.ResourceType.JDBC;

import static d.rusle2.CMZ1_1.DB;

/**
 * CMZ REST Web Service
 *
 * @author od
 */
@Name("CMZ")
@Description("Crop Management Zone (CMZ) service. The service returns "
        + "the CMZ id for a given lat/lon.")
@VersionInfo("1.1")
@Path("d/cmz/1.1")
@Resource(type = JDBC, file = "${conservation_resources.db}", id = DB)
public class CMZ1_1 extends ModelDataService {

    static final String DB = "csip.erosion.sqlsvr";


    @Override
    protected void doProcess() throws Exception {
        double lat = getDoubleParam("latitude");
        double lon = getDoubleParam("longitude");

        SqlGIS db = new SqlGIS(getResourceJDBC(DB));
        try {
            if (!db.IsValidCMZCoord(lat, lon)) {
                throw new ServiceException("Invalid lat/lon for CONUS: " + lat + "/" + lon);
            }
            String cmz = db.findCmz(lat, lon);
            if ("0".equals(cmz)) {
                throw new ServiceException("no CMZ for lat/long :" + lat + "/" + lon);
            }
            putResult("cmz", cmz);
            LOG.info("CMZ found=" + cmz);
        } finally {
            db.close();
        }
    }


    /**
     * Map-extend of the continental US.
     *
     * Extent: (-124.848974, 24.396308) - (-66.885444, 49.384358)
     * @param lat
     * @param lon
     * @throws IllegalArgumentException
     */
    private static boolean inContinentalUS(double lat, double lon) throws IllegalArgumentException {
        if (lat > 49.384358 || lat < 24.396308
                || lon > -124.848974 || lon < -66.885444) {
            return false;
        }
        return true;
    }
}