V2_0.java [src/java/m/watershed/outlet] 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 m.watershed.outlet;

import csip.api.server.Executable;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.Resource;
import static csip.annotations.ResourceType.EXECUTABLE;
import static csip.annotations.ResourceType.OUTPUT;
import javax.ws.rs.Path;
import csip.annotations.Description;
import csip.annotations.Name;
import java.io.*;

/**
 * outlet
 *
 * @author @author HK (using JK's template (using OD's template))
 */
@Name("outlet")
@Description("Example of outlet")
@Path("m/outlet/2.0")
@Resource(file = "/bin/lin-amd64_TD537/moveoutletstostrm", type = EXECUTABLE, id = "moveoutletstostrm")
@Resource(file = "*.txt", type = OUTPUT)

public class V2_0 extends ModelDataService {

    static final String KEY_FDIR = "flow_8_dir";
    static final String KEY_OUT = "outlet_shp";
    static final String KEY_STREAM = "streams";
    static final String KEY_DIST = "distance";

    // 2) watershed
    @Override
    protected void doProcess() throws Exception {

        String file_string1 = "-";
        String file_string2 = "-";
        String file_string3 = "-";

        if (attachments().hasFile(parameter().getString(KEY_FDIR)) && attachments().hasFile(parameter().getString(KEY_OUT)) && attachments().hasFile(parameter().getString(KEY_STREAM))) {

            file_string1 = parameter().getString(KEY_FDIR);
            File file_dir = attachments().getFile(file_string1);
            String flow_8dir_path = file_dir.getPath();

            file_string2 = parameter().getString(KEY_OUT);
            File file_outlet = attachments().getFile(file_string2);
            String outlet_pt_path = file_outlet.getPath();

            file_string3 = parameter().getString(KEY_STREAM);
            File file_stream = attachments().getFile(file_string3);
            String stream_path = file_stream.getPath();

            String wd = workspace().getDir().toString();

            int distance_in_cells = parameter().getInt(KEY_DIST);

            System.out.println("---->>>>> " + distance_in_cells);
            System.out.println("---->>>>> " + flow_8dir_path);
            System.out.println("---->>>>> " + outlet_pt_path);
            System.out.println("---->>>>> " + stream_path);

            Executable e = resources().getExe("moveoutletstostrm");
            e.addArguments("-p", flow_8dir_path, "-src", stream_path, "-o", outlet_pt_path, "-om", wd + "/outlet_moved.shp", "-md", 50, "-omlyr", "layer");
            StringWriter err = new StringWriter();
            e.redirectError(err);

            e.stderr();
            e.stdout();

            int ret = e.exec();

            if (ret != 0) {
                String error = err.toString();
                throw new ServiceException("Error : " + error);
            }
        } else {
            throw new ServiceException("No Files listet!");
        }
    }

    // 3) provide the result.
    @Override
    protected void postProcess() throws Exception {
        File ws = workspace().getDir();
        results().put(new File(ws, "outlet_moved.shx"), "move outlets shx to stream");
        results().put(new File(ws, "outlet_moved.dbf"), "move outlets dbf to stream");
        results().put(new File(ws, "outlet_moved.shp"), "move outlets shp to stream");
        //results().put(new File(ws, "outlet_moved.prj"), "move outlets shp to stream");
    }
}