package uk.ac.leeds.ccg.cluster; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; import uk.ac.leeds.ccg.geotools.*; import uk.ac.leeds.ccg.raster.*; import uk.ac.leeds.ccg.widgets.*; //import com.sun.java.util.collections.*; //change for 1.2! /* * ResViewer.java * * Created on 31 January 2001 */ /** * $Header: /export/cvsroot/spinsrc/src/uk/ac/leeds/ccg/cluster/ResViewer.java,v 1.3 2001/05/03 * 08:31:43 ian Exp $ * * @version A results viewer .... * @author alistair */ public class ResViewer extends Panel implements ActionListener { int i; private Database dbase = new Database(); private Results res = new Results(); FileDialog fd; FilenameFilter ff; Method method; RasterLayer rl; Shader sh = null; circleRaster rstr = null; Theme rt = null; Frame f; boolean visible = true; boolean app =false; // Variables declaration public java.awt.MenuBar menuBar1; private java.awt.Menu menu1; private java.awt.MenuItem menuItem1; private java.awt.MenuItem menuItem2; private java.awt.MenuItem menuItem3; private java.awt.MenuItem menuItem4; private java.awt.Menu menu2; private java.awt.MenuItem menuItem10; private uk.ac.leeds.ccg.geotools.Viewer viewer1; // End of variables declaration public ResViewer(){ visible=false; } /** * Creates new form ResViewer? * @param f DOCUMENT ME! */ public ResViewer(Frame f) { this.f = f; initComponents(); f.addWindowListener(new WindowAdapter(){ public void WindowClosing(WindowEvent e) {System.exit(0);} }); ToolBar tb = new ToolBar(viewer1); tb.setSize(200, 50); add(tb, "South"); fd = new FileDialog(new Frame(), "File to load "); ff = new FileFil(fd, ".shp"); } public ResViewer(Frame f, Results r) { this(f); setResults(r); } public ResViewer(Results r){ visible = false; setResults(r); } private void initComponents() { menuBar1 = new java.awt.MenuBar(); menu1 = new java.awt.Menu(); menuItem1 = new java.awt.MenuItem(); menuItem1.addActionListener(this); menuItem2 = new java.awt.MenuItem(); menuItem2.addActionListener(this); menuItem3 = new java.awt.MenuItem(); menuItem3.addActionListener(this); menuItem4 = new java.awt.MenuItem(); menuItem4.addActionListener(this); menu2 = new java.awt.Menu(); menuItem10 = new java.awt.MenuItem(); menuItem10.addActionListener(this); viewer1 = new uk.ac.leeds.ccg.geotools.Viewer(); menu1.setLabel("File"); menuItem1.setActionCommand("jpeg"); menuItem1.setLabel("Save as Jpeg"); menu1.add(menuItem1); menuItem2.setActionCommand("arcgrid"); menuItem2.setLabel("Save as ArcGrid"); menu1.add(menuItem2); menuItem3.setActionCommand("ascii"); menuItem3.setLabel("Save as ASCII"); menu1.add(menuItem3); menuItem4.setName("Close"); menuItem4.setLabel("Close"); menuItem4.setActionCommand("exit"); menu1.add(menuItem4); menuBar1.add(menu1); menu2.setLabel("Geography"); menuItem10.setActionCommand("sload"); menuItem10.setLabel("Load Shapefile"); menu2.add(menuItem10); menuBar1.add(menu2); setLayout(new java.awt.BorderLayout()); viewer1.setSize(250, 250); add(viewer1, java.awt.BorderLayout.CENTER); } /** * handles the actions of the menubar - feel free to use a different one if you prefer * * @param p1 the event */ public void actionPerformed(java.awt.event.ActionEvent p1) { String cmd = p1.getActionCommand(); // if its quit then quit! if (cmd.equalsIgnoreCase("quit")) { if(visible) f.dispose(); if(app) System.exit(0); } if (cmd.equalsIgnoreCase("exit")) { // System.exit(0); if(visible) f.dispose(); if(app) System.exit(0); } if (cmd.equalsIgnoreCase("jpeg")) { ff = new FileFil(fd, ".jpg"); fd.setTitle("File to save to"); fd.setFile(""); fd.setFilenameFilter(ff); fd.setVisible(true); if (fd.getFile() == null) { return; } File fs = new File(fd.getDirectory(), fd.getFile()); writeJpeg(fs); } if (cmd.equalsIgnoreCase("arcgrid")) { fd.setTitle("File to save to"); fd.setFile(""); fd.setVisible(true); if (fd.getFile() == null) { return; } File fs = new File(fd.getDirectory(), fd.getFile()); writeArcGrid(fs); } if (cmd.equalsIgnoreCase("ascii")) { ff = new FileFil(fd, ".dat"); fd.setTitle("File to save to"); fd.setFilenameFilter(ff); fd.setVisible(true); // work out which file they chose. if (fd.getFile() == null) { return; } File fs = new File(fd.getDirectory(), fd.getFile()); writeAscii(fs); } if (cmd.equalsIgnoreCase("sload")) { fd.setTitle("Shapefile to load"); ff = new FileFil(fd, ".shp"); fd.setFilenameFilter(ff); fd.setVisible(true); // work out which file they chose. if (fd.getFile() == null) { return; } File fs = new File(fd.getDirectory(), fd.getFile()); System.out.println("File = " + fs); //String name = fs.getCanonicalPath(); URL base = null; try{ base = fs.toURL(); } catch (MalformedURLException e){ return; } ShapefileReader sr = new ShapefileReader(base); Theme t = sr.getTheme(); t.setStyle(new ShadeStyle(false, true, null, Color.black, true)); System.out.println("" + t); addGeography(t); } } /** * allows the frame using the viewer to have the menu bar * * @return the menubar */ public MenuBar getMenuBar() { return menuBar1; } double numberOfCells = 100.0; public void setResults(Results r, double ncells) { numberOfCells=ncells; setResults(r); } public String getRasterBBox(){ GeoRectangle gr = rl.getBounds(); String ret = gr.getX()+","+gr.getY()+" "+(gr.getX()+gr.getWidth())+"," +(gr.getY()+gr.getHeight()); return ret; } public void setResults(Results r) { res = r; GeoRectangle gr = res.getBounds(); double len = Math.max(gr.width, gr.height); System.out.println("len " + len); double size = Math.ceil(len / numberOfCells); System.out.println("size " + size); rstr = new circleRaster(size, gr); rstr.setSparse(false); System.out.println("size = " + res.getSize() + " circles " + res.getCircles().size() + " excess " + res.getExcesses().size()); //Commented by andyt November 11 2003 //rstr.addGeoCircles(res.getCircles(), res.getExcesses()); rstr.setMissingValueCode(0.0d); sh = new ClassificationShader(rstr, 5, ClassificationShader.EQUAL_INTERVAL); //sh.setRange(1.0,2.0); double min = Math.max(0.0, Math.floor(rstr.getMin())); double max = Math.ceil(rstr.getMax()); sh.setRange(min, max); // sh.setRange(min, rstr.getMax()); Color bg = Color.white; sh.setMissingValueColor(bg); rl = new RasterLayer(rstr); if(visible){ viewer1.setBackground(bg); rt = new Theme(rl, sh); viewer1.addTheme(rt); Key key = sh.getKey(); key.setSize(50, 200); add(key, "West"); } } public void addGeography(Theme t) { viewer1.addTheme(t); ThemeStack ts = viewer1.getThemeStack(); ts.setToTop(t); //viewer1.removeTheme(rt); //viewer1.addTheme(t); //viewer1.invalidateSequenceBuffer(); //viewer1.addTheme(rt); } public void writeJpeg(File fs){ System.out.println("File = " + fs); Image i = rl.getImage(sh); if (i == null) { System.out.println("Image is null?"); return; } System.out.println("About to encode"); BufferedOutputStream bos; try { bos = new BufferedOutputStream(new FileOutputStream(fs)); JpegEncoder jp = new JpegEncoder(i, 50, bos); jp.Compress(); bos.close(); } catch (IOException ie) { System.err.println("Error opening file " + ie); return; } } public void writeArcGrid(File fs){ System.out.println("File = " + fs); rl.getRaster().writeArcGrid(fs); } public void writeAscii(File fs){ System.out.println("File = " + fs); Vector tmp = new Vector(); GeoCircle temp; Double val; // tmp = res.getCircles(); try { PrintWriter pw = new PrintWriter(new FileWriter(fs.toString())); Vector circles = res.getCircles(); Vector excesses = res.getExcesses(); for (i = 0; i < res.getSize(); i++) { // temp = (GeoCircle) tmp.elementAt(i); temp = (GeoCircle) circles.elementAt(i); val = (Double) excesses.elementAt(i); pw.println(temp.getID() + "\t" + temp.getX() + "\t" + temp.getY() + "\t" + temp.getRadius() + "\t" + val); } pw.close(); } catch (IOException ie) { System.err.println("Error in save\n" + ie); return; } } /** Getter for property numberOfCells. * @return Value of property numberOfCells. */ public double getNumberOfCells() { return numberOfCells; } /** Setter for property numberOfCells. * @param numberOfCells New value of property numberOfCells. */ public void setNumberOfCells(double numberOfCells) { this.numberOfCells = numberOfCells; } public static void main(String args[]) throws Exception{ if(args.length != 1 ){ System.out.println("Usage: ResViewer results_file"); System.exit(1); } File data = new File(System.getProperty("user.dir"),args[0]); BufferedReader in = new BufferedReader(new FileReader(data)); Results res = new Results(); String line; GeoCircle gc; Frame frame = new Frame(args[0]); while((line=in.readLine())!=null){ StringTokenizer stok = new StringTokenizer(line, "\t"); int id = Integer.parseInt(stok.nextToken()); double x = Double.parseDouble(stok.nextToken()); double y = Double.parseDouble(stok.nextToken()); double r = Double.parseDouble(stok.nextToken()); double e = Double.parseDouble(stok.nextToken()); gc = new GeoCircle(id,x, y, r); res.addResult(gc, e); } System.out.println("Read " + res.getCircles().size() + " results"); ResViewer rv = new ResViewer(frame); frame.add (rv); rv.setResults(res); frame.setMenuBar (rv.getMenuBar ()); frame.pack (); frame.setVisible(true); rv.setApp(true); } public void setApp(boolean a){ app = a; } }