0

I want to execute the Visibility Analysis Tool using ArcObjects 10.3. I tested the tool in ArcMap and it works great with the test data I provided, hence it should work as well in ArcObjects code. The only difference is that I don't want to write the output raster to file, I would prefer to have it stored in memory for some immediate pixel analysis. So, please take a look at my code:

private IFeatureClass OpenShapeFile(string shapeFilePath)
{
    string shapeFile = System.IO.Path.GetFileNameWithoutExtension(shapeFilePath);
    string fileFolder = System.IO.Path.GetDirectoryName(shapeFilePath);
    IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactory();

    IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(fileFolder, 0);
    IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(shapeFile);
    return featureClass;
}
public bool PerformAnalysis(string inputRaster, string observerLocationShp)
{
    if (InitLicense())
    {
        //var inputRaster = @"C:\Test\kand_lidar_dem_sub_products.dat";
        //var observerLocationShp= @"C:\Test\VisTest_ObserverPoint.shp"; //feature class with observer location
        try
        {
            ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
            gp.ResetEnvironments();
            gp.OverwriteOutput = true;
            gp.RegisterGeoProcessorEvents((IGeoProcessorEvents)new GPEvents());

            var test2 = new RasterDataset();
            test2.OpenFromFile(inputRaster);

            var testShp = OpenShapeFile(observerLocationShp);

            var output = new RasterDataset();
            output.CreateDefaultRaster();

            Viewshed viewshed = new Viewshed(test2, testShp, output);
            IGeoProcessorResult2 result = gp.Execute(viewshed, null) as IGeoProcessorResult2;
            IGPMessages msgs = result.GetResultMessages();
            var gpInfo = string.Empty;
            for (int i = 0; i < msgs.Count; i++)
            {
                //Console.WriteLine("{0} {1}", msgs.GetMessage(i).Description, msgs.GetMessage(i).Type);
                gpInfo+=msgs.GetMessage(i).Description+" "+ msgs.GetMessage(i).Type+"\n";
            }
        }
        catch (COMException ex)
        {
            Console.WriteLine(ex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    return false;
}

The code fails on line: IGeoProcessorResult2 result = gp.Execute(viewshed, null) as IGeoProcessorResult2;

The problem is, the GeoProcessor crashes on Execute function giving me the error code -2147467259. I can't see this error code on the list provided by Esri http://help.arcgis.com/en/sdk/10.0/java_ao_adf/conceptualhelp/engine/index.html#//000100000312000000.

In the console output I can see "ToolboxChange" message written two times. Since there are no Console.Writeline functions used in my code, I assume that is what GeoProcessor outputs.

Update: As Hornbydd suggested, I changed the output to file and it works. The problem is, I would like to get a result in memory without writing to file. Is it possible?

  • 1
    For the output try specifying a full string path like c:\Test\vs.tif? – Hornbydd Dec 7 '15 at 17:03
  • Thanks, that did the trick - it works now. But the thing is, I would prefer to get the result in memory without writing to file. Is it possible? – Konrad Dec 8 '15 at 9:46
  • This is a wild guess try "IN_MEMORY\myrastername"? – Hornbydd Dec 8 '15 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.