/** * This program is partly based on Ken Been's TigerReader. * This program reads the tiger dataset directly from www.census.gov * Require : File links.txt containing all the url information of the counties. * Usage: * java TigerDowloader state outputfile * e.g. java TigerDowloader NY nylines.txt * the first four lines of the output are the min and max of latitude and the min and max of longitude. * each line correponds to a polyline boudary. */ import java.net.*; import java.io.*; import java.util.*; import java.util.zip.*; import java.math.*; public class TigerDownloader { String baseurl="http://www.census.gov/geo/www/tiger/rd_2ktiger/"; final static private int LINE_RECORD_SIZE = 230; final static private int SHAPE_RECORD_SIZE = 210; long XMIN,YMIN,XMAX,YMAX; int numSeg; int numChain; TreeSet lineSet = new TreeSet(); TigerDownloader(){ XMAX=YMAX=Long.MIN_VALUE; XMIN=YMIN=Long.MAX_VALUE; } private static int readIntoArray(InputStream inputStream, byte[] buf)throws IOException{ int numRead = inputStream.read(buf, 0, buf.length); int totalRead = numRead; while (numRead >= 0 && totalRead < buf.length) { numRead = inputStream.read(buf, totalRead, buf.length - totalRead); if (numRead >= 0) totalRead += numRead; } return totalRead; } public void read(String state) { numSeg=0; numChain=0; //int oldSize=0; try{ HashMap idMap =new HashMap(); InputStream iin=new FileInputStream("links.txt"); BufferedReader iins= new BufferedReader(new InputStreamReader(iin)); String subname; while((subname=iins.readLine())!=null){ if(!subname.startsWith(state))continue; System.out.println("downloading file "+subname+"."); URL ur = new URL(baseurl+subname); InputStream in=ur.openStream(); ZipInputStream zis=new ZipInputStream(in); ZipEntry entry = zis.getNextEntry(); int recordCount = (int)entry.getSize() / LINE_RECORD_SIZE; for(int i=0;i42)continue;//more than two points there, means there are details in it. String detail=""; for(int k=18;k<=189;k+=19) { String pp=new String(buf,k,19); if (pp.equals("+000000000+00000000"))break; detail+=pp; numChain++; } detail=endpoints.substring(0,19)+detail+endpoints.substring(19); idMap.put(lineID,detail); lineSet.remove(endpoints); lineSet.add(detail); } in.close();zis.close(); } iin.close();iins.close(); }catch(IOException e){System.out.println(e);} System.out.println("Number of polygon lines : "+numSeg+"\nNumber of segments : "+(numSeg+numChain)); } private boolean LeftOf(String p1,String p2){ //System.out.println(p1+"--"+p2); long x1=new Integer(p1.substring(1,10)).longValue(); long x2=new Integer(p1.substring(1,10)).longValue(); return(x1<=x2); } private void updateMinMax(String s){ long x=new Long(s.substring(1,10)).longValue(); long y=new Long(s.substring(11)).longValue(); if(xXMAX)XMAX=x; if(yYMAX)YMAX=y; return; } public void output(String filename){ try{ OutputStreamWriter outs= new OutputStreamWriter(new FileOutputStream(filename)); Object[] lines=(Object[])lineSet.toArray(); outs.write(XMIN+"\n"+XMAX+"\n"+YMIN+"\n"+YMAX+"\n"); for(int i=0;i