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.crs;
017:
018: import java.io.IOException;
019: import java.util.NoSuchElementException;
020:
021: import org.geotools.data.FeatureReader;
022: import org.geotools.feature.Feature;
023: import org.geotools.feature.FeatureType;
024: import org.geotools.feature.FeatureTypes;
025: import org.geotools.feature.IllegalAttributeException;
026: import org.geotools.feature.SchemaException;
027: import org.opengis.referencing.crs.CoordinateReferenceSystem;
028:
029: /**
030: * ForceCoordinateSystemFeatureReader provides a CoordinateReferenceSystem for
031: * FeatureTypes.
032: *
033: * <p>
034: * ForceCoordinateSystemFeatureReader is a wrapper used to force
035: * GeometryAttributes to a user supplied CoordinateReferenceSystem rather then
036: * the default supplied by the DataStore.
037: * </p>
038: *
039: * <p>
040: * Example Use:
041: * <pre><code>
042: * ForceCoordinateSystemFeatureReader reader =
043: * new ForceCoordinateSystemFeatureReader( originalReader, forceCS );
044: *
045: * CoordinateReferenceSystem originalCS =
046: * originalReader.getFeatureType().getDefaultGeometry().getCoordianteSystem();
047: *
048: * CoordinateReferenceSystem newCS =
049: * reader.getFeatureType().getDefaultGeometry().getCoordianteSystem();
050: *
051: * assertEquals( forceCS, newCS );
052: * </code></pre>
053: * </p>
054: *
055: * @author jgarnett, Refractions Research, Inc.
056: * @author aaime
057: * @author $Author: jive $ (last modification)
058: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/crs/ForceCoordinateSystemFeatureReader.java $
059: * @version $Id: ForceCoordinateSystemFeatureReader.java 24278 2007-02-08 16:35:47Z aaime $
060: */
061: public class ForceCoordinateSystemFeatureReader implements
062: FeatureReader {
063: protected FeatureReader reader;
064: protected FeatureType schema;
065:
066: /**
067: * Shortcut constructor that can be used if the new schema has already been computed
068: * @param reader
069: * @param schema
070: */
071: ForceCoordinateSystemFeatureReader(FeatureReader reader,
072: FeatureType schema) {
073: this .reader = reader;
074: this .schema = schema;
075: }
076:
077: /**
078: * Builds a new ForceCoordinateSystemFeatureReader
079: *
080: * @param reader
081: * @param cs
082: *
083: * @throws SchemaException
084: * @throws NullPointerException DOCUMENT ME!
085: * @throws IllegalArgumentException DOCUMENT ME!
086: */
087: public ForceCoordinateSystemFeatureReader(FeatureReader reader,
088: CoordinateReferenceSystem cs) throws SchemaException {
089: this (reader, cs, false);
090: }
091:
092: /**
093: * Builds a new ForceCoordinateSystemFeatureReader
094: *
095: * @param reader
096: * @param cs
097: *
098: * @throws SchemaException
099: * @throws NullPointerException DOCUMENT ME!
100: * @throws IllegalArgumentException DOCUMENT ME!
101: */
102: public ForceCoordinateSystemFeatureReader(FeatureReader reader,
103: CoordinateReferenceSystem cs, boolean forceOnlyMissing)
104: throws SchemaException {
105: if (cs == null) {
106: throw new NullPointerException("CoordinateSystem required");
107: }
108:
109: FeatureType type = reader.getFeatureType();
110: CoordinateReferenceSystem originalCs = type
111: .getDefaultGeometry().getCoordinateSystem();
112:
113: if (!cs.equals(originalCs)) {
114: schema = FeatureTypes.transform(type, cs, forceOnlyMissing);
115: }
116:
117: this .reader = reader;
118: }
119:
120: /**
121: * @see org.geotools.data.FeatureReader#getFeatureType()
122: */
123: public FeatureType getFeatureType() {
124: if (reader == null) {
125: throw new IllegalStateException(
126: "Reader has already been closed");
127: }
128:
129: if (schema == null)
130: return reader.getFeatureType();
131:
132: return schema;
133: }
134:
135: /**
136: * @see org.geotools.data.FeatureReader#next()
137: */
138: public Feature next() throws IOException,
139: IllegalAttributeException, NoSuchElementException {
140: if (reader == null) {
141: throw new IllegalStateException(
142: "Reader has already been closed");
143: }
144:
145: Feature next = reader.next();
146: if (schema == null)
147: return next;
148: return schema.create(next.getAttributes(null), next.getID());
149: }
150:
151: /**
152: * @see org.geotools.data.FeatureReader#hasNext()
153: */
154: public boolean hasNext() throws IOException {
155: if (reader == null) {
156: throw new IllegalStateException(
157: "Reader has already been closed");
158: }
159:
160: return reader.hasNext();
161: }
162:
163: /**
164: * @see org.geotools.data.FeatureReader#close()
165: */
166: public void close() throws IOException {
167: if (reader == null) {
168: throw new IllegalStateException(
169: "Reader has already been closed");
170: }
171:
172: reader.close();
173: reader = null;
174: schema = null;
175: }
176: }
|