Geoprocessing CopyRaster_management fails to convert ASCII to GeoTIFF

335
3
05-12-2023 12:01 PM
JoeMadrigal
New Contributor III

In ArcGIS Pro SDK, I'm converting ascii files to GeoTiff using the Geoprocessing tool with "CopyRaster_management".  The funny thing is, it works 4 out of 5 times, but occasionally I get an internal System.ArgumentNullException 'Key cannot be null' error that I cannot capture with a try/catch.  It just crashes ArcGIS Pro.  I'll paste the full exception below.  I'm not sure what to do here besides go back to the deprecated ASCIIToRaster tool.

I'm using ArcGIS Pro 2.9

Here is the code:

 

var srEnv = Geoprocessing.MakeEnvironmentArray(outputCoordinateSystem: map.SpatialReference/* , parallelProcessingFactor: 0*/);
List<object> arguments = new List<object>
{
@"C:\files\myfile.asc", // raster dataset you want to copy
@"C:\files\myfile.tif", // The name and format for the raster dataset you are creating.
};

IGPResult gpResult = await Geoprocessing.ExecuteToolAsync("CopyRaster_management", Geoprocessing.MakeValueArray(arguments.ToArray()),
srEnv);

 

 

 

 

The exception:

System.ArgumentNullException
HResult=0x80004003
Message=Key cannot be null.
Parameter name: key
Source=mscorlib
StackTrace:
at System.Collections.Hashtable.ContainsKey(Object key)
at MS.Internal.WeakDictionary`2.TryGetValue(TKey key, TValue& value)
at MS.Internal.Helper.SetItemValuesOnContainer(DependencyObject owner, DependencyObject container, Object item)
at System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item)
at System.Windows.Controls.VirtualizingStackPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled)
at System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized, Boolean isBeforeViewport)
at System.Windows.Controls.VirtualizingStackPanel.MeasureChild(IItemContainerGenerator& generator, IContainItemStorage& itemStorageProvider, IContainItemStorage& parentItemStorageProvider, Object& parentItem, Boolean& hasUniformOrAverageContainerSizeBeenSet, Double& computedUniformOrAverageContainerSize, Double& computedUniformOrAverageContainerPixelSize, Boolean& computedAreContainersUniformlySized, Boolean& hasAnyContainerSpanChanged, IList& items, Object& item, IList& children, Int32& childIndex, Boolean& visualOrderChanged, Boolean& isHorizontal, Size& childConstraint, Rect& viewport, VirtualizationCacheLength& cacheSize, VirtualizationCacheLengthUnit& cacheUnit, Int64& scrollGeneration, Boolean& foundFirstItemInViewport, Double& firstItemInViewportOffset, Size& stackPixelSize, Size& stackPixelSizeInViewport, Size& stackPixelSizeInCacheBeforeViewport, Size& stackPixelSizeInCacheAfterViewport, Size& stackLogicalSize, Size& stackLogicalSizeInViewport, Size& stackLogicalSizeInCacheBeforeViewport, Size& stackLogicalSizeInCacheAfterViewport, Boolean& mustDisableVirtualization, Boolean isBeforeFirstItem, Boolean isAfterFirstItem, Boolean isAfterLastItem, Boolean skipActualMeasure, Boolean skipGeneration, Boolean isAncestorLookingForFirstItem, Boolean& hasBringIntoViewContainerBeenMeasured, Boolean& hasVirtualizingChildren)
at System.Windows.Controls.VirtualizingStackPanel.MeasureOverrideImpl(Size constraint, Nullable`1& lastPageSafeOffset, List`1& previouslyMeasuredOffsets, Nullable`1& lastPagePixelSize, Boolean remeasure)
at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at main(String[] args)

This exception was originally thrown at this call stack:
System.Collections.Hashtable.ContainsKey(object)
MS.Internal.WeakDictionary<TKey, TValue>.TryGetValue(TKey, out TValue)
MS.Internal.Helper.SetItemValuesOnContainer(System.Windows.DependencyObject, System.Windows.DependencyObject, object)
System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(System.Windows.DependencyObject, object)
System.Windows.Controls.VirtualizingStackPanel.InsertContainer(int, System.Windows.UIElement, bool)
System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(int, System.Windows.UIElement, bool, bool)
System.Windows.Controls.VirtualizingStackPanel.MeasureChild(ref System.Windows.Controls.Primitives.IItemContainerGenerator, ref System.Windows.Controls.Primitives.IContainItemStorage, ref System.Windows.Controls.Primitives.IContainItemStorage, ref object, ref bool, ref double, ref double, ref bool, ref bool, ref System.Collections.IList, ref object, ref System.Collections.IList, ref int, ref bool, ref bool, ref System.Windows.Size, ref System.Windows.Rect, ref System.Windows.Controls.VirtualizationCacheLength, ref System.Windows.Controls.VirtualizationCacheLengthUnit, ref long, ref bool, ref double, ref System.Windows.Size, ref System.Windows.Size, ref System.Windows.Size, ref System.Windows.Size, ref System.Windows.Size, ref System.Windows.Size, ref System.Windows.Size, ref System.Windows.Size, ref bool, bool, bool, bool, bool, bool, bool, ref bool, ref bool)
System.Windows.Controls.VirtualizingStackPanel.MeasureOverrideImpl(System.Windows.Size, ref double?, ref System.Collections.Generic.List<double>, ref double?, bool)
System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(System.Windows.Size)
System.Windows.FrameworkElement.MeasureCore(System.Windows.Size)
...
[Call Stack Truncated]

0 Kudos
3 Replies
JoeMadrigal
New Contributor III

One thing I didn't mention is that I'm doing multiple calls to Geoprocessing CopyRaster in a row.  Out of curiosity, I put in Thread.sleep() between each call for about a half second and so far I'm not getting the error anymore.  I'm crossing my fingers.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

The error that you are getting indicates that one of the items in a collection that you pass into the ExecuteTool function has expired.  This can happen if you assign a reference to a collection that is set to null while the GP Tool is running.  There are two collections in your snippet above the environment settings and the arguments.  Since the argument items are all hard coded (and hence can't become null) i would suggest changing the environment settings:

var srEnv = Geoprocessing.MakeEnvironmentArray(outputCoordinateSystem: map.SpatialReference.Wkt);

This should keep the spatial reference setting alive even is map gets set to null.

However, since we don't see the complete code snippet that fails it's impossible to guess the cause of the error.

0 Kudos
JoeMadrigal
New Contributor III

Thanks Wolf.  My code is doing a bunch of things (creating an ascii file first), but I put a print before and after the Geoprocessing CopyRaster tool and the error appears before the second print, so I know it was the line that was the offending line.  The only difference between your environment is the ".wkt" in the map.spatialReference assuming you mean to include that.  I'll use that also.  I'll post back up if I get the error again.

0 Kudos