Open Filesystem Raster Dataset in ArcGIS using ArcObjects 10.2 in Java

1.2k views Asked by At

SOLVED: see my answer below

I am trying to open .tif, .img, and .dat Raster files to a new layer in ArcMap.

I have tried all the methods I have found in the documentation and a few implementations on StackOverflow.

This is the current code:

    File file = new File(output);
    IWorkspaceFactory wsFactory = (IWorkspaceFactory)new RasterWorkspaceFactory();
    IRasterWorkspace2 rasterWS = (IRasterWorkspace2)wsFactory.openFromFile(file.getParentFile().getAbsolutePath(),0);
    IRasterDataset rds = rasterWS.openRasterDataset(output);    
    IRasterLayer rasterLayer = new RasterLayer();
    rasterLayer.createFromDataset(rds);
    IActiveView activeView = mxDocument.getActiveView();
    IMap _map = activeView.getFocusMap();
    _map.addLayer(rasterLayer);

This code throws no error messages, but it does not open the layer. Previous implementations that I took from sample programs and documentation for arcObjects 10.2 throw exceptions on this line:

RasterDataset rasterDataset = (RasterDataset) rasterWorkspace.openRasterDataset(file.getAbsolutePath());

The only lead I have right now is the ControlsAddDataCommand class to possibly call the command for opening the files and give the filepath as input. Build a custom command to open the file?

Note: The files open fine by clicking the addData option on the Layer menu.

1

There are 1 answers

0
Vok250 On

SOLVED

    IRasterLayer rasterLayer = new RasterLayer();
    rasterLayer.createFromFilePath(output);
    mxDocument.addLayer(rasterLayer);

Found this code by browsing the JavaDoc Tooltips in Eclipse.

Just type in the object and a "." Eclipse will show all the options available.

I'm amazed this code snippet wasn't listed anywhere online (that I found in about 8 hours of searching). Anyways, that's the simple 3 line solution!