RasterMap Class

package javaxt.cartography;

import java.io.*;

import java.awt.image.*;
import java.awt.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import javaxt.geospatial.geometry.Point;

//******************************************************************************
//**  CreateMap Class - By Peter Borissow
//******************************************************************************
/**
 *   Enter class description
 *
 ******************************************************************************/

public class RasterMap {
    
    private double ULx = 0;
    private double ULy = 0;
    private double resX = 1;
    private double resY = 1;
    
    
    private BufferedImage bufferedImage = null;
    Graphics2D g2d = null; 
    
  //**************************************************************************
  //** Creates a new instance of CreateMap
  //**************************************************************************
    
    public RasterMap(double minX, double minY, 
                     double maxX, double maxY, 
                     int width, int height){

      //Create BufferedImage
        this.bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        
      //Initialize Variables
        init(minX, minY, maxX, maxY, bufferedImage);
       
      //Set default background and line color
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0,0,width,height);
        g2d.setColor(Color.BLACK);
        
    }
    
  //**************************************************************************
  //** Creates a new instance of CreateMap
  //**************************************************************************
    
    public RasterMap(double minX, double minY, 
                     double maxX, double maxY, 
                     BufferedImage bufferedImage){
        
        this.bufferedImage = bufferedImage;
        
      //Initialize Variables
        init(minX, minY, maxX, maxY, bufferedImage);
    }
    

    
  //**************************************************************************
  //** Init
  //**************************************************************************
    
    private void init(double minX, double minY, double maxX, double maxY, 
                      BufferedImage bufferedImage){
        
      //Validate Coordinates
        if (validate(minX, minY, maxX, maxY)==false) return; //error?
        
      //Get Width/Height
        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();
        
      //Update min/max coordinates
        minX = x(minX);
        minY = y(minY);
        maxX = x(maxX);
        maxY = y(maxY);
        
        
      //Update Local Variables using updated values
        this.ULx = minX;
        this.ULy = maxY;
        
        
      //Compute pixelsPerDeg
        this.resX = ((double) width)  / (maxX-minX);
        this.resY = ((double) height) / (minY-maxY);//(maxY-minY);
        //System.out.println("pixelsPerDeg = " + resX + ", " + resY);
        
        


      //Instantiate 2D Drawing Class
        this.g2d = bufferedImage.createGraphics();
        g2d.setColor(Color.BLACK);
    }
    
    
  //**************************************************************************
  //** Validate
  //**************************************************************************
  /**  Used to validate coordinates used to invoke this class */
    
    private boolean validate(double minX, double minY, double maxX, double maxY){
        if (minX > maxX || minY > maxY) return false;
        if (minX < -180 || maxX < -180 || maxX > 180 || minX > 180) return false;
        if (minY < -90 || maxY < -90 || maxY > 90 || minY > 90) return false;
        return true;
    }
    
  //**************************************************************************
  //** X
  //**************************************************************************
  /**  Used to convert longitude to pixel coordinates */
    
    private double x(double pt){    
        pt += 180;
        double x = (pt - ULx) * resX;        
        //System.out.println("X = " + x);
        return x;
    }
    
    private double x(String pt){
        return x(cdbl(pt));
    }
    
    
  //**************************************************************************
  //** Y
  //**************************************************************************
  /**  Used to convert latitude to pixel coordinates */
    
    private double y(double pt){
        
       //System.out.print("Y = " + pt + " (");
        pt = -pt;
        if (pt<=0) pt = 90 + -pt;
        else pt = 90 - pt;
        
        pt = 180-pt;
        //System.out.print(pt + ")" + " (");
        
        
        double y = (pt - ULy) * resY; 
        
        if (cint(y)==0 || cint(y)==-0) y = 0;
        //else y = -y;
        
        //System.out.println(y + ")");
        return y;
    }
       
    private double y(String pt){
        return y(cdbl(pt));
    }
    
    
    public void AddGraticule(double SpacingInDegrees){
        
        int spacing = 15; //degrees
        
        for (int i=0; i<=360; i++){
             int x = -(180-i);
             Insert(x + ",90 " + x + ",-90",null);
             i+=spacing-1;
        }
        
        for (int i=0; i<=90; i++){
             int y = i;
             Insert("-180," + y + " 180," + y,null);
             Insert("-180," + -y + " 180," + -y,null);
             i+=spacing-1;
        }
    }
    
  //**************************************************************************
  //** Insert
  //**************************************************************************
  /**  Used to insert a "feature" into the map */
    
    public void Insert(Point[] Coordinates, Style style){
        
    }

  //**************************************************************************
  //** Insert
  //**************************************************************************
  /**  Used to insert a "feature" into the map */
    public void Insert(String Coordinates, Style style){
        String cs = ",";
        String ts = " ";
        Insert(Coordinates, style, cs, ts);
    }
    
    public void Insert(String Coordinates, Style style, String cs, String ts){        
        if (Coordinates==null) return;
        
        Coordinates = Coordinates.trim();
        
        String[] coords = Coordinates.split(ts);
        
        int numPoints = coords.length;
        int[] x = new int[numPoints];
        int[] y = new int[numPoints];
        for (int i=0; i<numPoints; i++){
             String[] pt = coords[i].split(cs);
             x[i] = cint(x(pt[0]));
             y[i] = cint(y(pt[1]));
        }
        
        if (coords.length==1){
            g2d.fillOval(cint(x(x[0])), cint(y(y[0])), 5, 5); 
        }
        else{
            if (coords[0].equals(coords[coords.length-1])){
                g2d.drawPolygon(x, y, numPoints);
            }
            else{
                g2d.drawPolyline(x, y, numPoints);
            }
        }
    }
    
    
  //**************************************************************************
  //** getBufferedImage
  //**************************************************************************
    
    public BufferedImage getBufferedImage(double minX, double minY, 
                                 double maxX, double maxY, 
                                 int width, int height){


        
        minX = x(minX);
        minY = y(minY);
        maxX = x(maxX);
        maxY = y(maxY);
        
        int x = cint(minX);
        int y = cint(maxY);
        int w = cint(maxX)-x;
        int h = cint(minY)-y;
        
      //Crop the Map
        BufferedImage croppedImage = bufferedImage.getSubimage(x,y,w,h);

        return croppedImage;
/*
      //Resize Cropped Map
        java.awt.Image scaledImage = croppedImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
        BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = outputImage.createGraphics();
        g2d.drawImage(scaledImage, 0, 0, null);
        g2d.dispose();
       
      //Return Output
        return outputImage;
 */
    }
    
  //**************************************************************************
  //** Save
  //**************************************************************************
  /**  Used to save the map to an image file */
    
    public void Save(String PathToFile){
        try{
            //g2d.dispose();
            
            File File = new File(PathToFile);
            File.getParentFile().mkdirs();
            String FileName = File.getName();
            String FileExt = FileName.substring(FileName.lastIndexOf(".")+1,FileName.length());
            RenderedImage rendImage = bufferedImage;
            ImageIO.write(rendImage,FileExt,File);
        }
        catch (Exception e){
            //System.out.println(e.toString());
        }
    }
    
    
    public Style getStyle(){
        return new Style(g2d);
    }
    
    public BufferedImage getImage(){
        return bufferedImage;
    }
    
    
  //**************************************************************************
  //** String Conversions
  //**************************************************************************  
    private int cint(String str){return Integer.valueOf(str).intValue(); }
    private int cint(Double d){ return (int)Math.round(d); }
    private double cdbl(String str){return Double.valueOf(str).doubleValue(); }
}