001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2003-2004, Julian J. Ray, All Rights Reserved
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.data.tiger;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.util.NoSuchElementException;
022:
023: import org.geotools.data.FeatureReader;
024: import org.geotools.feature.Feature;
025: import org.geotools.feature.FeatureType;
026: import org.geotools.feature.IllegalAttributeException;
027:
028: /**
029: * <p>
030: * Title: GeoTools2 Development
031: * </p>
032: *
033: * <p>
034: * Description:
035: * </p>
036: *
037: * <p>
038: * Copyright: Copyright (c) 2003
039: * </p>
040: *
041: * <p>
042: * Company:
043: * </p>
044: *
045: * @author Julian J. Ray
046: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/tiger/src/main/java/org/geotools/data/tiger/TigerFeatureReader.java $
047: * @version 1.0
048: */
049: public class TigerFeatureReader implements FeatureReader {
050: /** DOCUMENT ME! */
051: private TigerAttributeReader reader;
052:
053: /** DOCUMENT ME! */
054: private String namespace;
055:
056: /** DOCUMENT ME! */
057: private String typeName;
058:
059: /**
060: * Creates a new TigerFeatureReader object.
061: *
062: * @param dir DOCUMENT ME!
063: * @param typeName DOCUMENT ME!
064: *
065: * @throws IOException DOCUMENT ME!
066: */
067: public TigerFeatureReader(File dir, String typeName)
068: throws IOException {
069: // Typename contains both the file prefix and the subtype to be read
070: this .namespace = typeName.substring(0, typeName.indexOf("_"));
071: this .typeName = typeName.substring(typeName.indexOf("_") + 1);
072:
073: // here we deal with case sensetivity. A little clumsy but what can you do...
074: // Try lower case extension first
075: File file = new File(dir, this .namespace + ".rt1");
076: if (!file.exists()) {
077: file = new File(dir, this .namespace + ".RT1");
078: }
079: if (!file.exists()) {
080: throw new IOException("Can't read RT1 file.");
081: }
082:
083: reader = new TigerAttributeReader(file, this .namespace,
084: this .typeName);
085: }
086:
087: /**
088: * getFeatureType
089: *
090: * @return FeatureType
091: */
092: public FeatureType getFeatureType() {
093: return reader.getFeatureType();
094: }
095:
096: /**
097: * next
098: *
099: * @return Feature
100: *
101: * @throws IOException
102: * @throws IllegalAttributeException
103: * @throws NoSuchElementException
104: */
105: public Feature next() throws IOException,
106: IllegalAttributeException, NoSuchElementException {
107: reader.next();
108:
109: FeatureType type = reader.getFeatureType();
110: String fid = reader.getFeatureID();
111:
112: int numAtts = reader.getAttributeCount();
113: Object[] values = new Object[numAtts];
114:
115: for (int i = 0; i < numAtts; i++) {
116: values[i] = reader.read(i);
117: }
118:
119: return type.create(values, fid);
120: }
121:
122: /**
123: * hasNext
124: *
125: * @return boolean
126: *
127: * @throws IOException
128: */
129: public boolean hasNext() throws IOException {
130: return reader.hasNext();
131: }
132:
133: /**
134: * close
135: *
136: * @throws IOException
137: */
138: public void close() throws IOException {
139: reader.close();
140: reader = null;
141: }
142: }
|