open ecw files offline as base map

5075
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
RavindraSingh
Occasional Contributor

you can add these line before adding to the JMap. and then see.

StretchRenderer stretchRenderer = new StretchRenderer();

StdDevStretchParameters stretchParams = new StretchParameters.StdDevStretchParameters();

stretchParams.setStdDev(2.5f);

stretchRenderer .setStretchParameters(stretchParams);

RasterLayer rasterLayer = new RasterLayer(rasterSource);

rasterLayer.setRenderer(stretchRenderer );

rasterLayer.setName(fileName); // Here file name is the your raster file name. e.g.= "k5521.ecw".

map.getLayers().add(rasterLayer);

It should work.

0 Kudos
idozoar
New Contributor

what is the "rasterSource" you mentioned on the fifth line of the code?

0 Kudos
RavindraSingh
Occasional Contributor

Yes,

e.g.

FileRasterSource rasterSource = new FileRasterSource(rasterPath);

0 Kudos
idozoar
New Contributor

it doesn't work, here is the whole code, what am I getting wrong?

what needs to be the value in "rasterPath"? again: "k5221.ecw"?

import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileNotFoundException;
import com.esri.runtime.ArcGISRuntime;
import com.esri.core.raster.FileRasterSource;
import com.esri.core.renderer.StretchParameters;
import com.esri.core.renderer.StretchParameters.StdDevStretchParameters;
import com.esri.core.renderer.StretchRenderer;
import com.esri.map.JMap;
import com.esri.map.MapOptions;
import com.esri.map.MapOptions.MapType;
import com.esri.map.RasterLayer;

public class newclass {


  private JFrame window;
  private JMap map;


  public newclass() throws Exception {
    window = new JFrame();
    window.setSize(800, 600);
    window.setLocationRelativeTo(null); // center on screen
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setLayout(new BorderLayout(0, 0));


    // dispose map just before application window is closed.
    window.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent windowEvent) {
        super.windowClosing(windowEvent);
        map.dispose();
      }
    });
   
    map = new JMap();
    FileRasterSource rasterSource = new FileRasterSource("k5521.ecw");
    StretchRenderer stretchRenderer = new StretchRenderer();
    StdDevStretchParameters stretchParams = new StretchParameters.StdDevStretchParameters();
    stretchParams.setStdDev(2.5f);
    stretchRenderer .setStretchParameters(stretchParams);
    RasterLayer rasterLayer = new RasterLayer(rasterSource);
    rasterLayer.setRenderer(stretchRenderer );
    rasterLayer.setName("k5521.ecw"); // Here file name is the your raster file name. e.g.= "k5521.ecw".
   
    map.getLayers().add(rasterLayer);
    window.getContentPane().add(map);


  }


  /**
   * Starting point of this application.
   * @param args
   */
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {


      @Override
      public void run() {
        try {
          newclass application = new newclass();
          application.window.setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }
}
0 Kudos
SachinKanaujia
Occasional Contributor III

FileRasterSource should have the full path of the .ecw file. So if your ecw file in under C:\GIS\data\

then change this

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

to

FileRasterSource rasterSource = new FileRasterSource("C:\GIS\data\k5521.ecw");

0 Kudos
idozoar
New Contributor

its still doesn't work.
I don't think it has anything to do with the full file path because the file is inside the folder of the project.

0 Kudos
SachinKanaujia
Occasional Contributor III

As a good practice whenever in doubt always use the full path. Can you print the exception message you are getting?

I don't have a sample ecw file to test but it may well be a BUG. This method already has several issues. I dont know if they have added support for ECW for this method. Please check with ESRI support !!!

idozoar
New Contributor

I can send you a .ecw file if you'll write your email...
as far as I understood it suppose to support .ecw.
more than that- the full software support it for certain.


the exception I keep getting:


Java version : 1.7.0_45 (Oracle Corporation) x86

Rendering engine : DirectX

java.lang.RuntimeException: java.lang.RuntimeException: Failed to open raster dataset: k5521.ecw. - Failed to open raster dataset: k5521.ecw.

  at com.esri.core.raster.FileRasterSource.<init>(Unknown Source)

  at NewMapUI.<init>(NewMapUI.java:42)

  at NewMapUI$2.run(NewMapUI.java:66)

  at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)

  at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)

  at java.awt.EventQueue.access$200(EventQueue.java:103)

  at java.awt.EventQueue$3.run(EventQueue.java:694)

  at java.awt.EventQueue$3.run(EventQueue.java:692)

  at java.security.AccessController.doPrivileged(Native Method)

  at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)

  at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)

  at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)

  at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)

  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)

  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)

  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)

  at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Caused by: java.lang.RuntimeException: Failed to open raster dataset: k5521.ecw. - Failed to open raster dataset: k5521.ecw.

  at com.esri.core.raster.FileRasterSource.nativeCreate(Native Method)

  at com.esri.core.raster.FileRasterSource.<init>(Unknown Source)

  ... 16 more

0 Kudos
EricBader
Regular Contributor II

This raster format is actually not supported in the Java API.

A definitive list of supported raster data types are found here: Add raster data—ArcGIS Runtime SDK for Android | ArcGIS for Developers

0 Kudos