Select to view content in your preferred language

Contour GP tool crashes after renaming raster

836
7
Jump to solution
07-27-2022 09:16 AM
StephenRhea_NV5
Occasional Contributor

When called through the SDK, the Contour tool is failing after renaming a raster, but ONLY if a layer in the map uses the raster as its data source. If I run the GP tools (Rename_management & Contour_sa) directly in Pro, everything works fine, but running the code below makes the Contour tool fail with message "ERROR 010213: Error in reading raster %1." If the raster isn't referenced by a layer, everything works fine as well. The processes are run by separate tools within the Add-in, but I combined the code for quick repro. Any thoughts on why it would fail through the SDK but not through the UI?

It's a file geodatabase raster, and I'm using Pro 2.9.2.

internal static async Task RenameRasterAndGenerateContours()
{
    var oldRasterPath = @"directPathToGDB.gdb\test";
    var newRasterPath = @"directPathToGDB.gdb\test1";

    await QueuedTask.Run(async () =>
    {
        var progressor = new CancelableProgressorSource().Progressor;

        var parameters = new Dictionary<string, object>
        {
            { "in_data", oldRasterPath },
            { "out_data", newRasterPath },
            { "data_type", "RasterDataset" }
        };

        var values = Geoprocessing.MakeValueArray(parameters.Values.ToArray());

        var results = await Geoprocessing.ExecuteToolAsync("Rename_management", values, null, progressor, GPExecuteToolFlags.None).ConfigureAwait(true);
        if (results.ErrorCode != 0)
        {
            var messages = new StringBuilder("The Rename geoprocessing tool errored with the following messages:");
            messages.AppendLine();

            foreach (var message in results.ErrorMessages)
                messages.AppendLine($" - {message.Text}");

            throw new InvalidOperationException(messages.ToString());
        }
    }).ConfigureAwait(true);

    var contourInterval = "42";
    var contourPath = $"{newRasterPath}_contour_{contourInterval}";

    await QueuedTask.Run(async () =>
    {
        var progressor = new CancelableProgressorSource().Progressor;

        var parameters = new Dictionary<string, object>
        {
            { "in_raster", newRasterPath },
            { "out_polyline_features", contourPath },
            { "contour_interval", contourInterval },
        };

        var values = Geoprocessing.MakeValueArray(parameters.Values.ToArray());

        var results = await Geoprocessing.ExecuteToolAsync("Contour_sa", values, null, progressor, GPExecuteToolFlags.None).ConfigureAwait(true);
        if (results.ErrorCode != 0)
        {
            var messages = new StringBuilder("The Contour geoprocessing tool errored with the following messages:");
            messages.AppendLine();

            foreach (var message in results.ErrorMessages)
                messages.AppendLine($" - {message.Text}");

            throw new InvalidOperationException(messages.ToString());
        }
    }).ConfigureAwait(true);
}

 

0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Hi,

You can't rename raster which is loaded to map. It is locked. You need to check map for loaded raster and remove it from map first.

View solution in original post

0 Kudos
7 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

First, geoprocessing tools don't need QueuedTask.Run.

Second, your input parameters of geoprocessing tools organized in a wrong way. Code below works fine

        internal static async Task RenameRasterAndGenerateContours()
        {
            var oldRasterPath = @"d:\Temp\Contours\contours.gdb\test";
            var newRasterPath = @"d:\Temp\Contours\contours.gdb\test1";

            var progressor = new CancelableProgressorSource().Progressor;

            var parameters = Geoprocessing.MakeValueArray(oldRasterPath, newRasterPath, "RasterDataset");

            var results = await Geoprocessing.ExecuteToolAsync("Rename_management", parameters, null, progressor, GPExecuteToolFlags.None).ConfigureAwait(true);
            if (results.ErrorCode != 0)
            {
                var messages = new StringBuilder("The Rename geoprocessing tool errored with the following messages:");
                messages.AppendLine();

                foreach (var message in results.ErrorMessages)
                    messages.AppendLine($" - {message.Text}");

                throw new InvalidOperationException(messages.ToString());
            }

            var contourInterval = "42";
            var contourPath = $"{newRasterPath}_contour_{contourInterval}";


            parameters = Geoprocessing.MakeValueArray(newRasterPath, contourPath, contourInterval);

            results = await Geoprocessing.ExecuteToolAsync("Contour_sa", parameters, null, progressor, GPExecuteToolFlags.None).ConfigureAwait(true);
            if (results.ErrorCode != 0)
            {
                var messages = new StringBuilder("The Contour geoprocessing tool errored with the following messages:");
                messages.AppendLine();

                foreach (var message in results.ErrorMessages)
                    messages.AppendLine($" - {message.Text}");

                throw new InvalidOperationException(messages.ToString());
            }
        }
StephenRhea_NV5
Occasional Contributor

Thanks, @GKmieliauskas. I copied your code directly in (minus the paths), and I still get the same error. Which version of Pro are you using?

0 Kudos
GKmieliauskas
Esri Regular Contributor

I am using ArcGIS Pro 2.9.1

P.s. I have installed 2.9.2 and it works. Check your database or paths

0 Kudos
StephenRhea_NV5
Occasional Contributor

Apologies for the long delay. I'm just now getting back to this and am still having the same issue; still on 2.9.2 per client requirements. Your code works fine as long as the raster isn't in the map, but if I add the raster as a layer then run the code, it crashes with the original error. Are you able to run the process successfully if you add the raster to the map first?

0 Kudos
GKmieliauskas
Esri Regular Contributor

Hi,

You can't rename raster which is loaded to map. It is locked. You need to check map for loaded raster and remove it from map first.

0 Kudos
StephenRhea_NV5
Occasional Contributor

Ah, that's helpful to know. Thanks! Why would it work running the GP tools manually versus calling them from code? That feels like a bug in the SDK.

Do you happen to know if the layer removal is documented anywhere?

0 Kudos
GKmieliauskas
Esri Regular Contributor

Situation is the same from ArcObjects times. You can't delete, rename if dataset is loaded to map.

There are different types of locks depending on locked data usage.

How to check locks on geodatabases look here:

https://pro.arcgis.com/en/pro-app/latest/help/data/geodatabases/overview/manage-geodatabase-locks.ht... 

For file system datasets (TIF, SHP and etc.) you will see ".lock" file if dataset is locked.

0 Kudos