0

I need to display a large raster with ArcGIS. To do so, I'm using pyramids, and it is working, but they can be long to build. So I would like to build the pyramids in a separate thread. When doing so, the layer is added in the TOC, but not on the map.

Here is a sample code :

private class Worker {

    private delegate void RasterLoadedCallback(IRasterDataset rasterDataset);

    public void DoWork() {

        // Set the workspace and file names :
        string workspacePath = "C:\\Path\\To\\My\\Folder";
        string fileName = "filename.tif";

        // Open a workspace :
        IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
        IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(workspacePath, 0);

        // Open a file in the workspace :
        IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(fileName);

        // Create the pyramids :
        IRasterPyramid rasterPyramids = (IRasterPyramid)rasterDataset;
        rasterPyramids.Create();

        // Run on the main thread :
        RasterLoadedCallback callback = AddRasterToMap;
        Dispatcher.FromThread(mainThread).Invoke(callback, new object[] { rasterDataset });
    }

    private static void AddRasterToMap(IRasterDataset rasterDataset) {

        // Create a raster layer from a raster dataset :
        IRasterLayer rasterLayer = new RasterLayer();
        rasterLayer.CreateFromDataset(rasterDataset);

        // Add it to a map if the layer is valid.
        if (rasterLayer != null) {

            IMap map = MyAxMapControl.ActiveView.FocusMap;
            map.AddLayer((ILayer)rasterLayer);
        }
    }
}

Does anyone know what could the problem be ?

  • This is a very specific technical question about ArcObjects, I would recommend you ask this on Geonet. – Hornbydd Oct 28 '14 at 14:10
  • Thanks for pointing this forum, it may help me in the future, but I found an answer (see my answer). – Tim Autin Nov 3 '14 at 16:43
0

I found a solution : re-open the raster in the main thread when the pyramids creation is done, instead of passing the dataset as a parameter.

So the my code is :

private class Worker {

    private delegate void RasterLoadedCallback();

    public void DoWork() {

        // Set the workspace and file names :
        string workspacePath = "C:\\Path\\To\\My\\Folder";
        string fileName = "filename.tif";

        // Open a workspace :
        IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
        IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(workspacePath, 0);

        // Open a file in the workspace :
        IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(fileName);

        // Create the pyramids :
        IRasterPyramid rasterPyramids = (IRasterPyramid)rasterDataset;
        rasterPyramids.Create();

        // Run on the main thread :
        RasterLoadedCallback callback = AddRasterToMap;
        Dispatcher.FromThread(mainThread).Invoke(callback, new object[] { });
    }

    private static void AddRasterToMap() {

        // Open a workspace :
        IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
        IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(workspacePath, 0);

        // Open a file in the workspace :
        IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(fileName);

        // Create a raster layer from a raster dataset :
        IRasterLayer rasterLayer = new RasterLayer();
        rasterLayer.CreateFromDataset(rasterDataset);

        //rasterLayer.PyramidsPresent = true

        // Add it to a map if the layer is valid.
        if (rasterLayer != null) {

            IMap map = MyAxMapControl.ActiveView.FocusMap;
            map.AddLayer((ILayer)rasterLayer);
        }
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.