001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.catalog.ui.export;
016:
017: import java.util.Iterator;
018:
019: import net.refractions.udig.catalog.ui.internal.Messages;
020: import net.refractions.udig.ui.ProgressFeatureCollection;
021:
022: import org.eclipse.core.runtime.IProgressMonitor;
023: import org.geotools.feature.Feature;
024: import org.geotools.feature.FeatureCollection;
025: import org.geotools.feature.FeatureType;
026: import org.geotools.geometry.jts.JTS;
027: import org.opengis.referencing.operation.MathTransform;
028: import org.opengis.referencing.operation.TransformException;
029: import org.opengis.spatialschema.geometry.MismatchedDimensionException;
030:
031: import com.vividsolutions.jts.geom.Geometry;
032:
033: /**
034: * Reprojects the features that as they are read from the collection. The features are read only so don't try to attempt to
035: * set any values on the features.
036: *
037: * @author Jesse
038: * @since 1.1.0
039: */
040: public class ReprojectingFeatureCollection extends
041: ProgressFeatureCollection implements FeatureCollection {
042:
043: private FeatureType featureType;
044: private MathTransform mt;
045:
046: /**
047: * new instance
048: * @param delegate the feature collection to transform
049: * @param monitor the monitor to update
050: * @param featureType the featureType of the <em>final</em> featureType. Which means that the default geometry attribute
051: * type declares the projection <em>after</em> the transformation.
052: * @param mt
053: */
054: public ReprojectingFeatureCollection(FeatureCollection delegate,
055: IProgressMonitor monitor, FeatureType featureType,
056: MathTransform mt) {
057: super (delegate, monitor);
058: this .mt = mt;
059: this .featureType = featureType;
060: }
061:
062: @Override
063: protected Iterator openIterator() {
064: final Iterator iterator = delegate.iterator();
065: return new Iterator() {
066:
067: private FeatureWrapper feature;
068:
069: public boolean hasNext() {
070: while (feature == null) {
071: if (!iterator.hasNext())
072: return false;
073: Feature next = (Feature) iterator.next();
074: if (next == null)
075: continue;
076: Geometry geometry = next.getDefaultGeometry();
077: if (geometry != null) {
078: try {
079: geometry = JTS.transform(geometry, mt);
080: } catch (MismatchedDimensionException e) {
081: throw (RuntimeException) new RuntimeException()
082: .initCause(e);
083: } catch (TransformException e) {
084: throw (RuntimeException) new RuntimeException(
085: Messages.ReprojectingFeatureCollection_transformationError
086: + next.getID())
087: .initCause(e);
088: }
089: }
090: feature = new FeatureWrapper(next, featureType,
091: new Geometry[] { geometry },
092: new String[] { featureType
093: .getDefaultGeometry().getName() });
094: }
095: return feature != null;
096: }
097:
098: public Object next() {
099: monitor.worked(1);
100: FeatureWrapper tmp = feature;
101: feature = null;
102: return tmp;
103: }
104:
105: public void remove() {
106: iterator.next();
107: }
108:
109: };
110: }
111:
112: }
|