
package test;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import org.geotools.gui.swing.StyledMapPane;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.renderer.j2d.RenderedMapScale;
import org.geotools.styling.ColorMap;
import org.geotools.styling.RasterSymbolizer;
import org.geotools.styling.Style;
import org.mbari.vdm.MapBuilder;
import org.opengis.coverage.grid.GridCoverage;
import org.opengis.spatialschema.geometry.DirectPosition;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;

/**
 * Class to demonstrate use of ascii grid file.
 * 
 * @author achase
 */
public class MinimalTest2 {

    public void spearfishtest() throws Exception {
        long timeAtStart = System.currentTimeMillis();
        MapBuilder builder = new MapBuilder();
        
        GridCoverage gcDem = builder.getBaseMap();
        
        ColorMap cm = builder.getBathyColorMap();
        RasterSymbolizer rsDem = builder.sb.createRasterSymbolizer(cm, 1);
        Style demStyle = builder.sb.createStyle(rsDem);

        GridCoverage gcData = builder.createGridCoverage("test.asc.gz");
        ColorMap cmData = builder.getDataColorMap();

        RasterSymbolizer rsData = builder.sb.createRasterSymbolizer(cmData, .5);
        Style dataStyle = builder.sb.createStyle(rsData);

        // Build the map
        final MapContext map = new DefaultMapContext();
        
        map.addLayer(gcDem, demStyle);
//
        map.addLayer(gcData, dataStyle);

        // Show the map
        final StyledMapPane mapPane = new StyledMapPane();
        DirectPosition lowerCorner = gcDem.getEnvelope().getLowerCorner();
        DirectPosition upperCorner = gcDem.getEnvelope().getUpperCorner();
        Coordinate lowerCoord = new Coordinate(lowerCorner.getCoordinates()[0], lowerCorner.getCoordinates()[1]);
        Coordinate upperCoord = new Coordinate(upperCorner.getCoordinates()[0], upperCorner.getCoordinates()[1]);
        Envelope envelope = new Envelope(lowerCoord, upperCoord);
        map.setAreaOfInterest(envelope, gcDem.getCoordinateReferenceSystem());        
        mapPane.setMapContext(map);
        mapPane.getRenderer().addLayer(new RenderedMapScale());
        
        JFrame frame = new JFrame();
        frame.setTitle("Test map 2");
        frame.setContentPane(mapPane.createScrollPane());
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setBounds(800, 200, 640, 480);
        frame.setVisible(true);
        long totalTime = System.currentTimeMillis() - timeAtStart;
        System.out.println("time to complete: " + totalTime);
    }

    public static void main(String[] args) {
        MapBuilder mb = new MapBuilder();
        try {
            JFrame frame = mb.createFrameToViewGrid("test.asc");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}