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.util.Iterator;
019: import java.util.NoSuchElementException;
020:
021: import org.geotools.feature.Feature;
022: import org.geotools.feature.FeatureIterator;
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/ForceCoordinateSystemIterator.java $
059: * @version $Id: ForceCoordinateSystemIterator.java 22755 2006-11-16 03:05:52Z jgarnett $
060: */
061: public class ForceCoordinateSystemIterator implements Iterator {
062: protected FeatureIterator reader;
063: protected FeatureType schema;
064:
065: /**
066: * Shortcut constructor that can be used if the new schema has already been computed
067: * @param reader
068: * @param schema
069: */
070: ForceCoordinateSystemIterator(FeatureIterator reader,
071: FeatureType schema) {
072: this .reader = reader;
073: this .schema = schema;
074: }
075:
076: /**
077: * Builds a new ForceCoordinateSystemFeatureReader
078: *
079: * @param reader
080: * @param cs
081: *
082: * @throws SchemaException
083: * @throws NullPointerException DOCUMENT ME!
084: * @throws IllegalArgumentException DOCUMENT ME!
085: */
086: public ForceCoordinateSystemIterator(FeatureIterator reader,
087: FeatureType type, CoordinateReferenceSystem cs)
088: throws SchemaException {
089: if (cs == null) {
090: throw new NullPointerException("CoordinateSystem required");
091: }
092: CoordinateReferenceSystem originalCs = type
093: .getDefaultGeometry().getCoordinateSystem();
094:
095: if (cs.equals(originalCs)) {
096: schema = FeatureTypes.transform(type, cs);
097: }
098:
099: this .reader = reader;
100: }
101:
102: /**
103: * @see org.geotools.data.FeatureReader#getFeatureType()
104: */
105: public FeatureType getFeatureType() {
106: if (reader == null || schema == null) {
107: throw new IllegalStateException(
108: "Reader has already been closed");
109: }
110: return schema;
111: }
112:
113: /**
114: * @see org.geotools.data.FeatureReader#next()
115: */
116: public Object next() throws NoSuchElementException {
117: if (reader == null) {
118: throw new IllegalStateException(
119: "Reader has already been closed");
120: }
121:
122: Feature next = reader.next();
123: if (schema == null)
124: return next;
125:
126: try {
127: return schema
128: .create(next.getAttributes(null), next.getID());
129: } catch (IllegalAttributeException eep) {
130: throw (IllegalStateException) new IllegalStateException(eep
131: .getMessage()).initCause(eep);
132: }
133: }
134:
135: /**
136: * @see org.geotools.data.FeatureReader#hasNext()
137: */
138: public boolean hasNext() {
139: if (reader == null) {
140: throw new IllegalStateException(
141: "Reader has already been closed");
142: }
143:
144: return reader.hasNext();
145: }
146:
147: public void remove() {
148: throw new UnsupportedOperationException();
149: }
150:
151: /**
152: * @see org.geotools.data.FeatureReader#close()
153: */
154: public void close() {
155: if (reader == null) {
156: return;
157: }
158: reader.close();
159: reader = null;
160: schema = null;
161: }
162: }
|