/* * GeoTools java GIS tookit (c) The Centre for Computational Geography 2002 * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation version 2.1 */ package uk.ac.leeds.ccg.geotools; import java.lang.*; import cmp.LEDataStream.*; import java.awt.color.*; import java.io.*; import java.net.*; import java.util.*; import java.util.zip.*; import uk.ac.leeds.ccg.dbffile.*; /** * A class to load GeoPoints from a DBF (xBase) file without using a shapefile. * This class file reads a given DBF file and creates a Layer with GeoPoints. * The dbf file should contain (at least) the following fields: * X coordinate (numerical field) * Y coordinate (numerical field) * ID field (character field) * The column where each of these fields are located can be set by you, or it will be guessed. * The file can either be a .DBF file, or a .ZIP file containing a .DBF file. * * The class also has a series of getTheme() methods that encapsulate the process * of building Themes (complete with Layer and Shaders) as a very easy way of setting up a Theme * for inclusion in a Viewer. * * $Log: DBFFileReader.java,v $ * Revision 1.3 2002/03/09 19:48:27 loxnard * Fixed JavaDoc comments. * * * @author: Mathieu van Loon * @version $Revision: 1.3 $ $Date: 2002/03/09 19:48:27 $ * @since before 0.8.0 */ public class DBFFileReader implements FeatureReader{ private int xCol; // number of column where x coordinate is stored private int yCol; // number of column where y coordinate is stored private int idCol; // number of column where id is stored /** * The DBF object to read data from. */ public uk.ac.leeds.ccg.dbffile.Dbf dbf = null; // instance of class that will contain the representation of dbf file private java.lang.String name = "none"; // name of file without extension, also name of theme /** * Shows debug statements if set to true. */ public boolean debug = false; private int[] ids = null; /** * Constructs a new reader. * @param file DBF file to read from. * @param idCol Number of column where ID is stored. * @param xCol Number of column where x coordinate is stored. * @param yCol Number of column where y coordinate is stored. */ public DBFFileReader(String file, int idCol, int xCol, int yCol) { this.idCol=idCol; this.xCol = xCol; this.yCol = yCol; name = file; String sub=""; if(name.indexOf('?')>=0){sub = name.substring(name.indexOf( '?' ),name.lastIndexOf('/'));} if(debug)System.out.println("DBFFileReader: Sub "+sub); boolean dbfZip = false; /* try{ System.out.println("DBFileReader: Looking for .zip version of "+name); String ext = ".zip"; String noExt = file; if(debug)System.out.println("DBFileReader: No Ext "+noExt); if(noExt.toLowerCase().endsWith(".shp") || noExt.toLowerCase().endsWith(".zip")){ noExt = name.substring(0,name.length()-4); } if(debug)System.out.println("DBFileReader: No Ext "+noExt); if(debug)System.out.println("DBFileReader: Opening zis (ZipInputStream)"); int size = uc.getContentLength(); if(debug)System.out.println("DBFileReader: size equals :"+size); if(size!=-1){ byte [] buf1 = new byte [size]; byte [] got = new byte[size/10]; InputStream is = uc.getInputStream(); int grab=0; int total = 0; while ((grab = is.read(got)) > -1) { System.arraycopy(got, 0, buf1, total, grab); total+=grab; } got=null; ByteArrayInputStream zipfile = new ByteArrayInputStream(buf1); ZipInputStream zis = new ZipInputStream(zipfile); ZipEntry ze; if(debug)System.out.println("DBFileReader: Looking for entries"); while(((ze= zis.getNextEntry())!=null) && !dbfZip){ if(debug)System.out.println("DBFileReader: Found entry"); if(debug)System.out.println("DBFileReader: "+ze); if(debug)System.out.println("DBFileReader: Entry: "+ze.getName()); if(debug)System.out.println("DBFileReader: Getting entry"); if(ze.getName().toLowerCase().endsWith(".dbf")) { if(debug)System.out.println("DBFileReader: Found .dbf in zip file"); size = (int)ze.getSize(); byte [] buf = new byte [size]; byte [] in = new byte[size]; int n; int index = 0; while ((n = zis.read(in)) > -1) { System.arraycopy(in, 0, buf, index, n); index+=n; } zis.closeEntry(); ByteArrayInputStream bais = new ByteArrayInputStream(buf); dbf = new Dbf(bais); dbfZip = true; if(debug)System.out.println("DBFileReader: DBF done"); } } if(dbfZip){System.out.println("DBFileReader: Zip file version used "+name); return;} } } catch(ZipException ze){System.err.println("DBFileReader: zip read failed "+ze);} catch(IOException ie){System.err.println("DBFileReader: zip read not possible, looking for .dbf ");} catch(Exception e2){System.err.println("DBFileReader: General exception in zip read ");} */ if(!dbfZip) { if(debug)System.out.println("DBFFileReader: Looking for files outside of zip file"); try{ String noExt = file; if(noExt.toLowerCase().endsWith(".zip")){ noExt = name.substring(0,name.length()-4); } String ext = ".dbf"; File fl = new File(file+ext); dbf = new Dbf(fl); } catch(Exception e) { System.err.println("DBFFileReader: Unable to locate or load a .dbf file either in a zip file or the file itself"); System.err.println(e); } } this.init(); } /** * Constructs a new reader. * @param base URL from which to grab DBF file. * @param idCol Number of column where ID is stored. * @param xCol Number of column where x coordinate is stored. * @param yCol Number of column where y coordinate is stored. */ public DBFFileReader(URL base, int idCol, int xCol, int yCol) { this.idCol=idCol; this.xCol = xCol; this.yCol = yCol; name = base.getFile(); String sub=""; if(name.indexOf('?')>=0){sub = name.substring(name.indexOf( '?' ),name.lastIndexOf('/'));} if(debug)System.out.println("DBFFileReader: Sub "+sub); boolean dbfZip = false; try{ System.out.println("DBFFileReader: Looking for .zip version of "+name); String ext = ".zip"; String noExt = base.getFile(); if(debug)System.out.println("DBFFileReader: No Ext "+noExt); if(noExt.toLowerCase().endsWith(".shp") || noExt.toLowerCase().endsWith(".zip")){ noExt = name.substring(0,name.length()-4); } if(debug)System.out.println("DBFFileReader: No Ext "+noExt); URL zipURL = new URL(base.getProtocol(),base.getHost(),base.getPort(),noExt+ext); if(debug)System.out.println("DBFFileReader: zip url = "+zipURL); URLConnection uc = zipURL.openConnection(); uc.setUseCaches(false); uc.setDefaultUseCaches(false); if(debug)System.out.println("DBFFileReader: Opening zis (ZipInputStream)"); int size = uc.getContentLength(); if(debug)System.out.println("DBFFileReader: size equals :"+size); if(size!=-1){ byte [] buf1 = new byte [size]; byte [] got = new byte[size/10]; InputStream is = uc.getInputStream(); int grab=0; int total = 0; while ((grab = is.read(got)) > -1) { System.arraycopy(got, 0, buf1, total, grab); total+=grab; } got=null; ByteArrayInputStream zipfile = new ByteArrayInputStream(buf1); ZipInputStream zis = new ZipInputStream(zipfile); ZipEntry ze; if(debug)System.out.println("DBFFileReader: Looking for entries"); while(((ze= zis.getNextEntry())!=null) && !dbfZip){ if(debug)System.out.println("DBFFileReader: Found entry"); if(debug)System.out.println("DBFFileReader: "+ze); if(debug)System.out.println("DBFFileReader: Entry: "+ze.getName()); if(debug)System.out.println("DBFFileReader: Getting entry"); if(ze.getName().toLowerCase().endsWith(".dbf")) { if(debug)System.out.println("DBFFileReader: Found .dbf in zip file"); size = (int)ze.getSize(); byte [] buf = new byte [size]; byte [] in = new byte[size]; int n; int index = 0; while ((n = zis.read(in)) > -1) { System.arraycopy(in, 0, buf, index, n); index+=n; } zis.closeEntry(); ByteArrayInputStream bais = new ByteArrayInputStream(buf); dbf = new Dbf(bais); dbfZip = true; if(debug)System.out.println("DBFFileReader: DBF done"); } } if(dbfZip){System.out.println("DBFFileReader: Zip file version used "+name); return;} } } catch(ZipException ze){System.err.println("DBFFileReader: zip read failed "+ze);} catch(IOException ie){System.err.println("DBFFileReader: zip read not possible, looking for .dbf ");} catch(Exception e2){System.err.println("DBFFileReader: General exception in zip read ");} if(!dbfZip) { if(debug)System.out.println("DBFFileReader: Looking for files outside of zip file"); try{ String noExt = base.getFile(); if(noExt.toLowerCase().endsWith(".zip")){ noExt = name.substring(0,name.length()-4); } String ext = ".dbf"; URL dbfURL = new URL(base.getProtocol(),base.getHost(),base.getPort(),noExt+ext); dbf = new Dbf(dbfURL); } catch(Exception e) { System.err.println("DBFFileReader: Unable to locate or load a .dbf file either in a zip file or the file itself"); System.err.println(e); } } this.init(); } /** * TODO add descriptive comment * Creation date: (11/24/00 5:08:41 PM) * @return int[] */ private int[] getIds() throws IOException,DbfFileException{ Float fids[]; int ids[]; int count; if(dbf.getFieldType(idCol)=='N') { fids = dbf.getFloatCol(idCol); count = fids.length; ids = new int[count]; for(int i=0;i