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.Iterator;
020:
021: import org.geotools.feature.FeatureCollection;
022: import org.geotools.feature.FeatureType;
023: import org.geotools.feature.FeatureTypes;
024: import org.geotools.feature.SchemaException;
025: import org.geotools.feature.collection.AbstractFeatureCollection;
026: import org.geotools.geometry.jts.ReferencedEnvelope;
027: import org.opengis.referencing.crs.CoordinateReferenceSystem;
028:
029: /**
030: * ForceCoordinateSystemFeatureResults 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: * ForceCoordinateSystemFeatureResults results =
043: * new ForceCoordinateSystemFeatureResults( originalResults, forceCS );
044: *
045: * CoordinateReferenceSystem originalCS =
046: * originalResults.getFeatureType().getDefaultGeometry().getCoordinateSystem();
047: *
048: * CoordinateReferenceSystem newCS =
049: * reader.getFeatureType().getDefaultGeometry().getCoordinateSystem();
050: *
051: * assertEquals( forceCS, newCS );
052: * </code></pre>
053: * </p>
054: *
055: * @author aaime
056: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/crs/ForceCoordinateSystemFeatureResults.java $
057: * @version $Id: ForceCoordinateSystemFeatureResults.java 26186 2007-07-10 02:18:59Z jdeolive $
058: */
059: public class ForceCoordinateSystemFeatureResults extends
060: AbstractFeatureCollection {
061: FeatureCollection results;
062:
063: //FeatureType schema;
064:
065: public ForceCoordinateSystemFeatureResults(
066: FeatureCollection results,
067: CoordinateReferenceSystem forcedCS) throws IOException,
068: SchemaException {
069: this (results, forcedCS, false);
070: }
071:
072: public ForceCoordinateSystemFeatureResults(
073: FeatureCollection results,
074: CoordinateReferenceSystem forcedCS, boolean forceOnlyMissing)
075: throws IOException, SchemaException {
076: super (forceType(origionalType(results), forcedCS,
077: forceOnlyMissing));
078: this .results = results;
079: }
080:
081: private static FeatureType origionalType(FeatureCollection results) {
082: while (true) {
083: if (results instanceof ReprojectFeatureResults) {
084: results = ((ReprojectFeatureResults) results)
085: .getOrigin();
086: }
087: if (results instanceof ForceCoordinateSystemFeatureResults) {
088: results = ((ForceCoordinateSystemFeatureResults) results)
089: .getOrigin();
090: }
091: break;
092: }
093: return results.getSchema();
094: }
095:
096: private static FeatureType forceType(FeatureType startingType,
097: CoordinateReferenceSystem forcedCS, boolean forceOnlyMissing)
098: throws SchemaException {
099: if (forcedCS == null) {
100: throw new NullPointerException("CoordinateSystem required");
101: }
102: CoordinateReferenceSystem originalCs = startingType
103: .getDefaultGeometry() != null ? startingType
104: .getDefaultGeometry().getCoordinateSystem() : null;
105:
106: if (forcedCS.equals(originalCs)) {
107: return startingType;
108: } else {
109: return FeatureTypes.transform(startingType, forcedCS,
110: forceOnlyMissing);
111: }
112: }
113:
114: protected Iterator openIterator() {
115: return new ForceCoordinateSystemIterator(results.features(),
116: getSchema());
117: }
118:
119: protected void closeIterator(Iterator close) {
120: if (close == null)
121: return;
122: if (close instanceof ForceCoordinateSystemIterator) {
123: ForceCoordinateSystemIterator iterator = (ForceCoordinateSystemIterator) close;
124: iterator.close();
125: }
126: }
127:
128: /**
129: * @see org.geotools.data.FeatureResults#getBounds()
130: */
131: public ReferencedEnvelope getBounds() {
132: return ReferencedEnvelope.reference(results.getBounds());
133: }
134:
135: public int size() {
136: return results.size();
137: }
138:
139: /**
140: * @see org.geotools.data.FeatureResults#collection()
141: */
142: // public FeatureCollection collection() throws IOException {
143: // FeatureCollection collection = FeatureCollections.newCollection();
144: //
145: // try {
146: // FeatureReader reader = reader();
147: //
148: // while (reader.hasNext()) {
149: // collection.add(reader.next());
150: // }
151: // } catch (NoSuchElementException e) {
152: // throw new DataSourceException("This should not happen", e);
153: // } catch (IllegalAttributeException e) {
154: // throw new DataSourceException("This should not happen", e);
155: // }
156: //
157: // return collection;
158: // }
159: /**
160: * Returns the feature results wrapped by this
161: * ForceCoordinateSystemFeatureResults
162: *
163: */
164: public FeatureCollection getOrigin() {
165: return results;
166: }
167: }
|