open ecw files offline as base map

5127
10
02-26-2015 03:12 AM
idozoar
New Contributor

Hi,

I'm new to ArcGIS runtime sdk for java.
I need my app to open my own base map that represented by about 6-7 .ecw files (including of course matching .aux files).
How do I add a layer from .ecw file to JMap?
I  tried:
JMap map = new JMap(); ... String rasterPath = "k5521.ecw"; //the file is in the folder of the project
FileRasterSource rasterSource;

try {

     rasterSource = new FileRasterSource(rasterPath);

     RasterLayer rasterLayer = new RasterLayer(rasterSource);

     map.getLayers().add(rasterLayer);

} catch (IllegalArgumentException iex) {

// handle exception

} catch (FileNotFoundException fe) {

// handle exception

} catch (RuntimeException re) { // handle exception }

and also:

FileRasterSource rasterSource = new FileRasterSource("k5521.ecw");

rasterSource.project(map.getSpatialReference());

RasterLayer rasterLayer = new RasterLayer(rasterSource);

map.getLayers().add(rasterLayer);

what am I doing wrong?

Tags (3)
0 Kudos
10 Replies
SachinKanaujia
Occasional Contributor III

ido zoar​ So it turns out the .ecw is not supported with Java API. You can instead use the approach to add local Raster files to a Local Map service created from a blank mpk. The only drawback there is that for Raster local server doesn't support Renderers.

Below is a small code snippet. You can make modifications as per your requirements

    final String workspaceId = ""+count++; // an arbitrary unique string

    // create a local map service and enable dynamic layers
    LocalMapService localMapService = new LocalMapService("C:\\mpk_blank.mpk");

    if(!localMapService.getEnableDynamicLayers()) {
        localMapService.setEnableDynamicLayers(true);
    }

    if(localMapService.getStatus() == LocalServiceStatus.STARTED) {
        localMapService.stop();
    }

    // get dynamic workspaces from service
    WorkspaceInfoSet workspaceInfoSet = localMapService.getDynamicWorkspaces();

    // create a workspace info via the static method according to data type
    // e.g. shapefile folder connection
    WorkspaceInfo workspaceInfo = WorkspaceInfo.CreateShapefileFolderConnection(
            workspaceId, fileDir);
    // also:
    // --> WorkspaceInfo.CreateSDEConnection
    // --> WorkspaceInfo.CreateRasterFolderConnection
    // --> WorkspaceInfo.CreateFileGeoDatabaseConnection

    // set dynamic workspaces for our local map service
    workspaceInfoSet.add(workspaceInfo);
    localMapService.setDynamicWorkspaces(workspaceInfoSet);

    // now start service...
    localMapService.start();

    // set up a local dynamic layer
    final ArcGISDynamicMapServiceLayer localDynamicLayer = new ArcGISDynamicMapServiceLayer(localMapService.getUrlMapService());

    // add the layer to the map
    jMap.getLayers().add(localDynamicLayer);

    localDynamicLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener()
    {
        @Override
        public void layerInitializeComplete(LayerInitializeCompleteEvent arg0)
        {

            DynamicLayerInfoCollection layerInfos = localDynamicLayer.getDynamicLayerInfos();
            DynamicLayerInfo layerInfo = layerInfos.get(0);
            DrawingInfo drawingInfo = new DrawingInfo(simpleRenderer, TRANSPARENCY);
            layerInfo.setDrawingInfo(drawingInfo);

            // Create the data source
            TableDataSource dataSource = new TableDataSource();
            dataSource.setWorkspaceId(workspaceId);
            dataSource.setDataSourceName(fileName);

            // Set the data source
            LayerDataSource layerDataSource = new LayerDataSource();
            layerDataSource.setDataSource(dataSource);
            layerInfo.setLayerSource(layerDataSource);

            localDynamicLayer.refresh();
        }
   

});

0 Kudos