001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data;
017:
018: import java.io.IOException;
019: import java.util.NoSuchElementException;
020: import java.util.logging.Logger;
021:
022: import org.geotools.feature.Feature;
023: import org.geotools.feature.FeatureType;
024: import org.geotools.feature.FeatureTypeFactory;
025: import org.geotools.feature.IllegalAttributeException;
026: import org.geotools.feature.SchemaException;
027:
028: /**
029: * Experimental FeatureReader that always takes the first column of the
030: * attributeReader as the FeatureID. I want to get this working with postgis,
031: * but then will consider other options, for those who want featureIDs created
032: * automatically. Perhaps a constructor param or a method to say that you
033: * would just like to have the FeatureReader increment one for each feature,
034: * prepending the typeName. I'm also don't really like the one argument
035: * constructor defaulting to the xxx typename. I feel that it should perhaps
036: * take a typename. If people deliberately set to null then we could use xxx
037: * or something. ch
038: *
039: * <p>
040: * This now feels sorta hacky, I'm not sure that I like it, but I'm going to
041: * commit as I need to go now and revisit it in a bit. I think the idea of
042: * passing in an FIDAttributeReader might be cleaner, and if none is provided
043: * then do an auto-increment one. This might then work as the
044: * DefaultFeatureReader.
045: * </p>
046: *
047: * @author Ian Schneider
048: * @author Chris Holmes, TOPP
049: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/FIDFeatureReader.java $
050: * @version $Id: FIDFeatureReader.java 27862 2007-11-12 19:51:19Z desruisseaux $
051: */
052: public class FIDFeatureReader implements FeatureReader {
053: /** The logger for the data module. */
054: private static final Logger LOGGER = org.geotools.util.logging.Logging
055: .getLogger("org.geotools.data");
056: private final AttributeReader attributeReader;
057: private final FeatureType schema;
058: private final FIDReader fidReader;
059: protected final Object[] attributes;
060:
061: /**
062: * Creates a new instance of AbstractFeatureReader
063: *
064: * @param attributeReader AttributeReader
065: * @param fidReader FIDReader used to ID Features
066: * @param schema FeatureType to use, may be <code>null</code>
067: *
068: * @throws SchemaException if we could not determine the correct
069: * FeatureType
070: */
071: public FIDFeatureReader(AttributeReader attributeReader,
072: FIDReader fidReader, FeatureType schema)
073: throws SchemaException {
074: this .attributeReader = attributeReader;
075: this .fidReader = fidReader;
076:
077: if (schema == null) {
078: schema = createSchema();
079: }
080:
081: this .schema = schema;
082: this .attributes = new Object[attributeReader
083: .getAttributeCount()];
084: }
085:
086: public FIDFeatureReader(AttributeReader attributeReader,
087: FIDReader fidReader) throws SchemaException {
088: this (attributeReader, fidReader, null);
089: }
090:
091: public Feature next() throws IOException,
092: IllegalAttributeException, NoSuchElementException {
093: if (attributeReader.hasNext()) {
094: attributeReader.next();
095:
096: return readFeature(attributeReader);
097: } else {
098: throw new NoSuchElementException(
099: "There are no more Features to be read");
100: }
101: }
102:
103: protected FeatureType createSchema() throws SchemaException {
104: FeatureTypeFactory factory = FeatureTypeFactory
105: .newInstance("xxx");
106:
107: for (int i = 0, ii = attributeReader.getAttributeCount(); i < ii; i++) {
108: factory.addType(attributeReader.getAttributeType(i));
109: }
110:
111: return factory.getFeatureType();
112: }
113:
114: protected Feature readFeature(AttributeReader atts)
115: throws IllegalAttributeException, IOException {
116:
117: //Seems like doing it here could be a bit expensive.
118: //The other option from this is to have this constructed with two
119: //attributeReaders, the FID one and real attributes one. Could then
120: //have default FIDAttributeReader.
121: String fid = fidReader.next();
122:
123: for (int i = 0, ii = atts.getAttributeCount(); i < ii; i++) {
124: attributes[i] = atts.read(i);
125: }
126:
127: return schema.create(attributes, fid);
128: }
129:
130: public void close() throws IOException {
131: fidReader.close();
132: attributeReader.close();
133: }
134:
135: public FeatureType getFeatureType() {
136: return schema;
137: }
138:
139: public boolean hasNext() throws IOException {
140: return attributeReader.hasNext();
141: }
142: }
|