Repository for Ideas & Research
Open Source GIS, Hydrologic Modeling, Optimization

How to use the geoprocessor using ArcObjects

public IGeoProcessorResult ExecuteGeoProcessor(bool addOutputsToMap, string extent, IRaster snapRaster,
    string gpToolName, params object[] args)
{
    IGeoProcessor gp;
    IVariantArray gpParams;
    IGeoProcessorResult gpResult;
    string warningMessages;
    string errorMessages;
    
    // create a new geoprocessor instance
    gp = new GeoProcessorClass();
    
    // don't add outputs to TOC
    gp.AddOutputsToMap = addOutputsToMap;
    
    // set the extent
    gp.SetEnvironmentValue("extent", extent);
    
    // set the snap raster
    gp.SetEnvironmentValue("snapRaster", ((IRaster2)snapRaster).RasterDataset.CompleteName);
    
    // create a parameter array
    gpParams = new VarArrayClass();
    for (int i = 0; i < args.Length; ++i)
    {
        gpParams.Add(args[i]);
    }
    
    // run the geoprocessing tool
    gpResult = _geoProcessor.Execute(gpToolName, gpParams, null);
    Application.DoEvents();
    
    warningMessages = gpResult.GetMessages(1);
    errorMessages = gpResult.GetMessages(2);
    
    if (errorMessages != "")
    {
        throw new Exception(errorMessages);
    }

    return gpResult;
}

ExecuteGeoProcessor(true, "MAXOF", snapRaster,
    "Watershed", fdrLayer.Name, outletFCPath, watershedRasterPath, "OutletOID");

Wed Sep 6 13:51:02 2017 EDT by Huidae Cho
Parsing time: 0.014 seconds
XHTML . CSS . Powered by Uniqki!