package uk.ac.leeds.ccg.cluster; import java.awt.*; import java.io.*; import java.net.*; import java.util.*; import uk.ac.leeds.ccg.geotools.*; public class Database extends PointLayer { // Members boolean expected = false; Vector points = new Vector(); Vector cases = new Vector(); SimpleGeoData geoc = new SimpleGeoData(); double overrat; double minX = Double.POSITIVE_INFINITY; double minY = Double.POSITIVE_INFINITY; double maxX = Double.NEGATIVE_INFINITY; double maxY = Double.NEGATIVE_INFINITY; GeoRectangle b = new GeoRectangle(); String name = "unamed datafile"; // Constructors public Database() { //useQuadtree=true; geoc.setMissingValueCode(0.0); } public Database(String name) { this(new File(name)); } public Database(File name) { // useQuadtree=true; geoc.setMissingValueCode(0.0); loadText(name); } // Methods public Vector getCases() { return (Vector) cases.clone(); } public Vector getPoints() { return (Vector) points.clone(); } public GeoData getCaseVals() { return geoc; } /** * returns a point selected at random from the database since users have no knowlege of the * order that points are stored this is the best way to handle requests for any point * @return DOCUMENT ME! */ public MyPoint getRandomPoint() { int i = (int) (Math.random() * points.size()); return (MyPoint) points.elementAt(i); } public void setup(Vector pv, double overrate) { b = new GeoRectangle(); MyPoint p; int ncases = 0; int npop = 0; for (int i = 0; i < pv.size(); i++) { p = (MyPoint) pv.elementAt(i); maxX = Math.max(p.getX(), maxX); minX = Math.min(p.getX(), minX); maxY = Math.max(p.getY(), maxY); minY = Math.min(p.getY(), minY); npop += p.pop; p.setPop(p.getPop() * overrate); b.add(p); geoc.setValue(p.getID(), (double) p.cases); if (p.cases > 0) { cases.addElement(p); ncases += p.cases; } } overrat = (double) ncases / (double) npop; System.out.println("setup " + pv.size() + " pop= " + npop + " cases " + ncases + " Rate = " + overrat + " " + overrate); shapeTree.setBounds(b); for (int i = 0; i < pv.size(); i++) { addGeoPoint((MyPoint) pv.elementAt(i)); } points = pv; } public void loadText(String name) { loadText(new File(name)); } public void loadText(File name) { this.name = name.toString(); StreamTokenizer xyStrTok; BufferedReader xyInput; try { xyInput = new BufferedReader(new FileReader(name)); xyStrTok = new StreamTokenizer(xyInput); xyStrTok.parseNumbers(); xyStrTok.whitespaceChars(',', ','); xyStrTok.eolIsSignificant(false); int token; int id; double x; double y; int pop; int tcase; int npop = 0; int ncases = 0; MyPoint p; token = xyStrTok.nextToken(); while (token != xyStrTok.TT_EOF) { id = (int) xyStrTok.nval; token = xyStrTok.nextToken(); x = xyStrTok.nval; maxX = Math.max(x, maxX); minX = Math.min(x, minX); token = xyStrTok.nextToken(); y = xyStrTok.nval; maxY = Math.max(y, maxY); minY = Math.min(y, minY); token = xyStrTok.nextToken(); tcase = (int) xyStrTok.nval; ncases += tcase; token = xyStrTok.nextToken(); pop = (int) xyStrTok.nval; npop += pop; token = xyStrTok.nextToken(); p = new MyPoint(id, x, y, tcase, pop); b.add(p); points.addElement(p); geoc.setValue(id, (double) tcase); if (tcase > 0) { cases.addElement(p); } } shapeTree.setBounds(b); for (int i = 0; i < points.size(); i++) { addGeoPoint((MyPoint) points.elementAt(i)); } overrat = (double) ncases / (double) npop; System.out.println("Read " + points.size() + " pop= " + npop + " cases " + ncases + " Rate = " + overrat); } catch (Exception e) { System.err.println("Database load error:\n" + e); } } public void convertToRates() { if (expected) { System.out.println("double conversion!!!"); } expected = true; System.out.println("Converting to expected rates"); MyPoint p; for (int i = 0; i < points.size(); i++) { p = (MyPoint) points.elementAt(i); p.setPop(p.getPop() * overrat); } } public final int totalObs() { return points.size(); } public final int getTotalObs() { return points.size(); } public final Vector getPoints(GeoRectangle gb) { int[] m = getIDs(gb, 0); Vector results = new Vector(m.length); for (int i = 0; i < m.length; i++) { results.addElement(shapeList.elementAt(i)); } return results; } public final Vector getPoints(GeoCircle gc) { //System.out.println("checking "+gc); Vector results = getShapesIn(gc.getBounds(), 0); GeoPoint gp; Iterator i = results.iterator(); while (i.hasNext()) { gp = (GeoPoint) i.next(); if (!gc.contains(gp)) { i.remove(); } } //HashSet results = new HashSet(shapeTree.find(gc.getBounds())); if (results.size() == 0) { return new Vector(0); } Vector res = new Vector(results.size()); //if(results.size()>0)System.out.println("get Points - got "+results.size()+" "+gc.getRadius()); GeoPoint p; Iterator it = results.iterator(); while (it.hasNext()) { p = (GeoPoint) it.next(); if (gc.contains(p)) { res.addElement(p); } } //System.out.println("Size "+res.size()); return res; } public Vector getShapesIn(GeoRectangle box, int mode) { Vector list = new Vector(); //System.out.println(""+box); for (int i = 0; i < shapeList.size(); i++) { MyPoint temp = (MyPoint) shapeList.elementAt(i); //System.out.println("Checking "+temp.x+" "+temp.y); if (box.contains(temp)) { //System.out.println("hit "+temp); list.addElement(temp); } } return list; /*int[] IDs = new int[list.size()]; if(list.size()==0) return IDs; //System.out.println("size "+list.size()); for(int i=0;i 0) { id = dis.readInt(); x = dis.readDouble(); y = dis.readDouble(); c = dis.readInt(); p = dis.readDouble(); temp = new MyPoint(id, x, y, c, p); points.addElement(temp); b.add(temp); geoc.setValue(id, (double) c); if (c > 0) { cases.addElement(temp); } } } catch (IOException ie) { System.err.println("Error on database load\n" + ie); return; } expected = true; shapeTree.setBounds(b); for (int i = 0; i < points.size(); i++) { addGeoPoint((MyPoint) points.elementAt(i)); } } public int getSize() { return points.size(); } public int getNumberCases() { return cases.size(); } public void setName(String n) { name = n; } public String getName() { return name; } public void loadShapefile(String shp, Frame f) { int index = shp.lastIndexOf(".shp"); if (index > 0) { shp = shp.substring(0, index); } ShapefileReader sfr = new ShapefileReader(shp, 0); System.out.println("Shape type " + sfr.getShapeType()); GeoData[] dbs = sfr.readData(); ColPicker poppicker = new ColPicker(f, dbs, "Choose Population column"); int popCol = poppicker.getColumn(); ColPicker casepicker = new ColPicker(f, dbs, "Choose Cases column"); int caseCol = casepicker.getColumn(); } public void loadShapefile(File file, String pop, String casestr){ loadShapefile (file, -1, pop, casestr); } public void loadShapefile(File file, int idCol, String pop, String casestr){ try{ URL base = file.toURL (); ShapefileReader sfr = new ShapefileReader(base, idCol); GeoData[] dbs = sfr.readData(); loadShapefile (sfr,dbs,pop,casestr); }catch (MalformedURLException mue){ System.out.println("Unable to open shapefile " + file.toString ()+"\n"+mue.toString ()); } } public void loadShapefile(ShapefileReader sfr, GeoData[] dbs, String pop, String casestr){ int popid = -1,caseid=-1; try{ popid = Integer.parseInt (pop); } catch (NumberFormatException nfe){ /* intentionally blank */ } try{ caseid = Integer.parseInt (casestr); } catch (NumberFormatException nfe){ /* intentionally blank */ } if(popid>-1 && caseid > -1){ loadShapefile(sfr,dbs, popid, caseid); } for(int i = 0; i< dbs.length; i++){ if(dbs[i].getName ().equalsIgnoreCase (pop)){ popid = i; } if(dbs[i].getName ().equalsIgnoreCase (casestr)){ caseid = i; } } if(popid>-1 && caseid > -1){ loadShapefile(sfr,dbs, popid, caseid); }else{ System.out.println("Unable to find named shapefile columns\nPopulation:\t" + pop +"("+popid+")\nCases:\t"+ cases +"("+caseid+")"); System.exit(2); } } public void loadShapefile(ShapefileReader sfr, GeoData[] dbs, int popCol, int caseCol){ MyPoint p; int tcase; int pop; int ncases = 0; int npop = 0; MixedLayer pl = (MixedLayer)sfr.readPoints(); Vector shapes = null; boolean point = false, polygons = false, lines = false; if(pl.isHasPoints ()){ shapes = pl.getPointLayer ().getGeoShapes(); point = true; } else if (pl.isHasPolygons ()){ shapes = pl.getPolygonLayer ().getGeoShapes (); polygons = true; } else if (pl.isHasLines ()){ shapes = pl.getLineLayer ().getGeoShapes (); lines= true; } for (int i = 0; i < shapes.size (); i++) { GeoPoint tp = null; if(point){ tp = (GeoPoint) shapes.elementAt(i); } else if (polygons){ tp = ((GeoPolygon)shapes.elementAt (i)).getCentroidLocation (); } else if (lines){ tp = ((GeoLine)shapes.elementAt(i)).getCentroidLocation (); } tcase = (int) dbs[caseCol].getValue(tp.getID()); pop = (int) dbs[popCol].getValue(tp.getID()); ncases += tcase; npop += pop; p = new MyPoint(tp.getID(), tp.x, tp.y, tcase, pop); points.addElement(p); geoc.setValue(tp.getID(), (double) tcase); if (tcase > 0) { cases.addElement(p); } } for (int i = 0; i < points.size(); i++) { addGeoPoint((MyPoint) points.elementAt(i)); } overrat = (double) ncases / (double) npop; System.out.println("Read " + points.size() + " pop= " + npop + " cases " + ncases + " Rate = " + overrat); } }