NLCD_Clip.java [src/java/svap/utils] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package svap.utils;

import static csip.ModelDataService.KEY_METAINFO;
import static csip.ModelDataService.KEY_PARAMETER;
import csip.ServiceException;
import csip.utils.JSONUtils;
import gisobjects.raster.GISRaster;
import java.io.File;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class NLCD_Clip extends ServiceCall {

    private JSONObject geometryData;
    private JSONObject metaInfo;

    public NLCD_Clip(JSONObject geometryData, String URI, JSONObject metaInfo) {
        super(URI);
        this.geometryData = geometryData;
        this.metaInfo = metaInfo;
        errorPrefix = "SVAP-03";
    }

    @Override
    protected void createRequest() throws ServiceException {
        JSONArray dataArray;

        requestMetainfoObject = new JSONObject();
        request = new JSONObject();

        dataArray = new JSONArray();
        try {
            requestMetainfoObject.put("MultipartRequest", "Bundled Service Request WQM-21 wqmnutrientslpsrp");
            if (null != metaInfo) {
                requestMetainfoObject.put("WQMNutrientSLPSRP_Metainfo", metaInfo);
            }
            request.put(KEY_METAINFO, requestMetainfoObject);

            dataArray.put(JSONUtils.dataDesc("year", "2011", "the year of NLCD data to pull (2001 | 2006 | 2011)"));

            //For some reason the NLCD_Clip Service expects an array for the value of boundary...
            JSONArray boundary = new JSONArray();
            boundary.put(geometryData);
            dataArray.put(JSONUtils.data("boundary", boundary));

            request.put(KEY_PARAMETER, dataArray);
        } catch (JSONException ex) {
            throwServiceCallException("Cannot create the JSON request.", ex);
        }
    }

    public GISRaster getRaster() throws ServiceException, JSONException, Exception {
        JSONArray tifResult = getResultSection();

        if (tifResult.length() > 0) {
            String filename = tifResult.getJSONObject(0).getString("name");
            String url = tifResult.getJSONObject(0).getString("value");
            File file = new File(filename);
            doGET(url, file);
            return new GISRaster(file);
        } else {
            throw new ServiceException("No file information returned from NLCD Clip service.");
        }
    }
}