阅读:1852回复:0
[转帖]ArcInfoReader.java(源码共享)
import java.io.*;
import java.util.Hashtable; public class ArcInfoReader extends BufferedReader { public ArcInfoReader(Reader in) { super(in); ignoreIDs = false; sequentialID = 0; } public void setIgnoreIDs(boolean flag) { ignoreIDs = flag; } public final synchronized GeoPolygon readGeoPolygon() throws IOException { GeoPolygon poly = null; int id = 0; float xcent = 0.0F; float ycent = 0.0F; String line = readLine(); String marker = line.substring(0, 3); if(marker.equals("END")) return null; try { id = Integer.parseInt(line.substring(4, 10).trim()); if(id != 0xfffe7961) { sequentialID++; if(ignoreIDs) id = sequentialID; xcent = (new Float(line.substring(11, 28).trim())).floatValue(); ycent = (new Float(line.substring(29, 44).trim())).floatValue(); } else { id = 0xfffe7961; xcent = 0.0F; ycent = 0.0F; System.out.println("!Null Polygon!"); } double xpoints[] = new double[500]; double ypoints[] = new double[500]; int npoints = -1; int limit = 500; int grow = 20; do { line = readLine(); if(line.substring(0, 3).trim().equals("END")) break; if(++npoints >= limit) { double xtemp[] = xpoints; double ytemp[] = ypoints; xpoints = new double[limit + grow]; ypoints = new double[limit + grow]; limit += grow; System.arraycopy(xtemp, 0, xpoints, 0, npoints); System.arraycopy(ytemp, 0, ypoints, 0, npoints); } float x = (new Float(line.substring(4, 18).trim())).floatValue(); float y = (new Float(line.substring(19, line.length()).trim())).floatValue(); xpoints[npoints] = x; ypoints[npoints] = y; } while(true); if(npoints < limit) { double xtemp[] = xpoints; double ytemp[] = ypoints; xpoints = new double[npoints]; ypoints = new double[npoints]; limit += grow; System.arraycopy(xtemp, 0, xpoints, 0, npoints); System.arraycopy(ytemp, 0, ypoints, 0, npoints); } return new GeoPolygon(id, xcent, ycent, xpoints, ypoints, npoints); } catch(NumberFormatException _ex) { System.err.println("Invalid Polygon ID"); } return null; } public final synchronized PolygonLayer readUngenerateFile() throws IOException { PolygonLayer map = new PolygonLayer(); do { GeoPolygon poly = readGeoPolygon(); if(poly != null) map.addGeoPolygon(poly); else return map; } while(true); } public final synchronized Hashtable readAttributes() throws IOException { Hashtable col = new Hashtable(); StreamTokenizer st = new StreamTokenizer(this); st.eolIsSignificant(false); st.whitespaceChars(44, 44); for(boolean done = false; !done;) { int c = -1; c = st.nextToken(); switch(c) { default: break; case -1: done = true; break; case -2: int id = (int)st.nval; c = st.nextToken(); double value = (int)st.nval; if(id > 0) col.put(new Integer(id), new Double(value)); break; } } return col; } public final synchronized GeoData readGeoData() throws IOException { SimpleGeoData store = new SimpleGeoData(); StreamTokenizer st = new StreamTokenizer(this); st.eolIsSignificant(false); st.whitespaceChars(44, 44); for(boolean done = false; !done;) { int c = -1; c = st.nextToken(); switch(c) { default: break; case -1: done = true; break; case -2: int id = (int)st.nval; c = st.nextToken(); if(c == -2) { double value = (int)st.nval; store.setValue(id, value); } else { String text = st.sval; store.setText(id, text); } break; } } return store; } public final synchronized CircleLayer readCircles() throws IOException { CircleLayer cl = new CircleLayer(); StreamTokenizer st = new StreamTokenizer(this); st.eolIsSignificant(false); st.whitespaceChars(44, 44); for(boolean done = false; !done;) { int c = -1; c = st.nextToken(); switch(c) { default: break; case -1: done = true; break; case -2: int id = (int)st.nval; st.nextToken(); double x = st.nval; st.nextToken(); double y = st.nval; st.nextToken(); double r = st.nval; if(id > 0) cl.addGeoCircle(new GeoCircle(id, x, y, r)); break; } } return cl; } public final synchronized Table readZdes() throws IOException { Table t = new Table(); StreamTokenizer st = new StreamTokenizer(this); st.eolIsSignificant(false); st.whitespaceChars(44, 44); boolean done = false; st.nextToken(); int zones = (int)st.nval; int j = 1; while(!done) { j++; Hashtable col = new Hashtable(); st.nextToken(); for(int i = 1; i < zones && !done; i++) { int c = -1; c = st.nextToken(); switch(c) { case -1: done = true; break; case -2: double value = st.nval; col.put(new Integer(i), new Double(value)); break; } } t.addCol("" + j + " zones", col); } return t; } public final synchronized PolygonLayer readUngenerateFile2() throws IOException { int id = 0; float xcent = 0.0F; float ycent = 0.0F; double point[] = { 0.0D, 0.0D }; int pair = 1; double header[] = { 0.0D, 0.0D, 0.0D }; int headerSegment = 0; PolygonLayer map = new PolygonLayer(); GeoPolygon poly = null; StreamTokenizer st = new StreamTokenizer(this); st.eolIsSignificant(true); boolean done = false; boolean readingHeader = true; while(!done) { int c = -1; try { c = st.nextToken(); } catch(IOException _ex) { break; } switch(c) { default: break; case -1: done = true; break; case 10: // '\n' if(readingHeader) { poly = new GeoPolygon(0xfffe7961, 0.0D, 0.0D); readingHeader = false; } else { poly.addPoint(point[0], point[1]); } break; case -3: if(readingHeader) { done = true; } else { readingHeader = true; headerSegment = 0; map.addGeoPolygon(poly); } break; case -2: if(readingHeader) { header[headerSegment] = st.nval; if(++headerSegment > 2) { poly = new GeoPolygon((int)header[0], (float)header[1], (float)header[2]); readingHeader = false; } } else { point[pair - 1] = st.nval; pair = 3 - pair; } break; } } return map; } boolean ignoreIDs; int sequentialID; } |
|