// Converts COxx.BNA files to USPS.txt // Rename ST_LOW48.BNA to CO00.BNA // % java Convert public class Convert { public static void main(String[] args) { // parse states.txt file int STATES = 57; String[] states = new String[STATES]; String[] uspss = new String[STATES]; In statesin = new In("states.txt"); while (!statesin.isEmpty()) { String line = statesin.readLine(); String[] fields = line.split(","); int fips = Integer.parseInt(fields[2]); states[fips] = fields[0]; uspss[fips] = fields[1]; } /*************************************************************** * Create the polygons, but not the bounding box ***************************************************************/ // for each state for (int fips = 0; fips < STATES; fips++) { String state = states[fips]; String usps = uspss[fips]; if (state == null) continue; // read state county file String filename = "CO" + String.format("%02d", fips) + ".BNA"; In in = new In(filename); Out out = new Out(usps + ".txt.temp"); // process the points while (!in.isEmpty()) { String line = in.readLine(); String[] fields = line.split(","); String county = fields[2].replaceAll("\"", ""); // special case if (usps.equals("USA")) county = states[Integer.parseInt(county)]; int N = Integer.parseInt(fields[3]); Point p0 = new Point(in.readDouble(), in.readDouble()); Polygon poly = new Polygon(); poly.add(p0); for (int i = 1; i < N - 1; i++) { Point p = new Point(in.readDouble(), in.readDouble()); if (p.eq(p0)) { // separate polygon out.println(county); out.println(usps); out.print(poly); poly = new Polygon(); p = new Point(in.readDouble(), in.readDouble()); i++; } poly.add(p); } out.println(county); out.println(usps); out.print(poly); in.readLine(); // ignore last point - it is same as first in.readLine(); // eat up rest of whitespace on current line } out.close(); } /*************************************************************** * Add in the bounding box information and number of polygons ***************************************************************/ // for each state for (int fips = 0; fips < STATES; fips++) { String state = states[fips]; String usps = uspss[fips]; if (state == null) continue; // read state county file In in = new In(usps + ".txt.temp"); // process data double xmin = Double.POSITIVE_INFINITY; double xmax = Double.NEGATIVE_INFINITY; double ymin = Double.POSITIVE_INFINITY; double ymax = Double.NEGATIVE_INFINITY; int numberOfPolygons = 0; while (!in.isEmpty()) { numberOfPolygons++; in.readLine(); in.readLine(); int N = in.readInt(); for (int i = 0; i < N; i++) { double x = in.readDouble(); double y = in.readDouble(); if (x < xmin) xmin = x; if (y < ymin) ymin = y; if (x > xmax) xmax = x; if (y > ymax) ymax = y; } in.readLine(); // ignore } in.close(); // now add the bounding box information to usps.txt in = new In(usps + ".txt.temp"); Out out = new Out(usps + ".txt"); out.println(xmin + " " + ymin + " " + xmax + " " + ymax); out.println(numberOfPolygons); while (!in.isEmpty()) { out.println(in.readLine()); } out.println(); out.close(); } } }