Dissolve using In Memory Feature Class and In Memory Output

1183
4
03-09-2014 02:05 PM
SanjayRana
New Contributor III
Hi Everyone,

For some reasons the following code is failing to run the dissolve tool on a feature class that I store in a in-memory workspace. 
I have checked that the inFClass is valid by querying the number of features inside it, and whether it contains the dissolve field. They appear okay. Everything else seems pretty standard.

So what might be incorrect with the code?

Thanks,
Sanj.

private IFeatureClass DissolveAdvancedToMemory(IFeatureWorkspace inMemWS, string InputName, string DissolveField, string OutputName)
        {
            try
            {
                Dissolve DissolveDS = new Dissolve();
                ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult Result;
                IFeatureClass inFClass = inMemWS.OpenFeatureClass(InputName);                
                DissolveDS.in_features = inFClass;
                DissolveDS.dissolve_field = DissolveField;
                DissolveDS.multi_part = "MULTI_PART";
                DissolveDS.unsplit_lines = "DISSOLVE_LINES";
                DissolveDS.out_feature_class = OutputName ; // output name is a simple string like "Dissolve_Run"

                Result = RunTool(DissolveDS, null); 

                if (Result == null)
                {
                    Debug.WriteLine("Could not dissolve dataset");
                    return null;
                }

                return ReturnFeatureClassfromResult(Result);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString() + " Dissolve error");
                return null;
            }
        }


        private static ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult RunTool(ESRI.ArcGIS.Geoprocessor.IGPProcess Process,
             ESRI.ArcGIS.esriSystem.ITrackCancel2 TC)
        {
            ESRI.ArcGIS.Geoprocessor.Geoprocessor GP = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();            
            GP.AddOutputsToMap = false;

            ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult Result = new GeoProcessorResult();

            try
            { 
                Result = GP.Execute(Process, null) as GeoProcessorResult; // code crashes here

                if (Result.Status != ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)
                    Debug.WriteLine("Geoprocessing Error");

                GP.ClearMessages();
                return Result;
            }
            catch (Exception ex)
            {
                if (Result.MessageCount > 0)
                {
                    for (int count = 0; count < Result.MessageCount; count++)
                    {
                        Debug.WriteLine(Result.GetMessage(count));
                    }
                    Debug.WriteLine("Geoprocessing Failed");
                }
                GP.AddOutputsToMap = true;
                return null;
            }
        }

        private static ESRI.ArcGIS.Geodatabase.IFeatureClass ReturnFeatureClassfromResult(ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult result)
        {
            ESRI.ArcGIS.Geodatabase.IGPValue GPVal;
            string InMemFC;
            ESRI.ArcGIS.Geoprocessing.IGPUtilities3 GPUtil =
                new ESRI.ArcGIS.Geoprocessing.GPUtilities() as ESRI.ArcGIS.Geoprocessing.IGPUtilities3;
            ESRI.ArcGIS.Geodatabase.IFeatureClass pOutputFC;

            try
            {
                GPVal = result.GetOutput(0);
                InMemFC = GPVal.GetAsText();
                pOutputFC = GPUtil.OpenFeatureClassFromString(InMemFC);
                return pOutputFC;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Return FeatureClass error " + ex.ToString());
                return null;
            }
        }
0 Kudos
4 Replies
AhmedEl-Sisi
Occasional Contributor III
You can check results window from Geoprocessing menu in Arcmap to get descriptive error.

also you can set Overwrite Output property to True as the default output is your Default.gdb
ESRI.ArcGIS.Geoprocessor.Geoprocessor GP = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
            GP.AddOutputsToMap = false; 
            GP.OverwriteOutput = true;


To get the result in InMemory FC you should add "in_memory" before your FC name.

 DissolveDS.out_feature_class = "in_memory\\" + OutputName;
0 Kudos
SanjayRana
New Contributor III
Hi Ahmed,
Thanks a lot for the response. Looking at the geoprocessor result revealed that the dissolveds.in_features was being read as "GPL0" instead of the actual name. I have tried placing the gp execution inside the first method ie dissolveadanced.. But the error remains. The input feature classs does exist in memory and contains features, fields so not sure how dissolve/gp is not able to read. I was thinking perhaps I should give the full pathname of the feature class in inmemory workspace because it would contain the uid of the inmemory workspace but no luck so far in creating the full path name..
Cheers,
Sanj.
0 Kudos
AhmedEl-Sisi
Occasional Contributor III
Hi Ahmed,
Thanks a lot for the response. Looking at the geoprocessor result revealed that the dissolveds.in_features was being read as "GPL0" instead of the actual name. I have tried placing the gp execution inside the first method ie dissolveadanced.. But the error remains. The input feature classs does exist in memory and contains features, fields so not sure how dissolve/gp is not able to read. I was thinking perhaps I should give the full pathname of the feature class in inmemory workspace because it would contain the uid of the inmemory workspace but no luck so far in creating the full path name..
Cheers,
Sanj.


Hi Sanjay,
GPL0 will not cause any problems.
You can try to add this layer to map and run dissolve tool from toolbox and check if it's working as I tried the code and it works fine.
If dissolve tool runs on your layer in Arcmap you should debug your code and check if the layer is not locked by another operation.
0 Kudos
SanjayRana
New Contributor III
Hi Sanjay,
GPL0 will not cause any problems.
You can try to add this layer to map and run dissolve tool from toolbox and check if it's working as I tried the code and it works fine.
If dissolve tool runs on your layer in Arcmap you should debug your code and check if the layer is not locked by another operation.


Hi Ahmed,

That's interesting. The error in the GP results in my case stated something like "Dataset GPLO doesn't exist or is unsuitable". As I wrote previously, I have checked that the feature class does exist in the in-memory workspace by checking on the number of features, fields. I didn't worry about the lock because layer is not used anywhere else.  My understanding was the files can multiple read-only locks?

Cheers,
Sanj.
0 Kudos