001: package org.geotools.data.store;
002:
003: import java.io.IOException;
004: import java.util.Iterator;
005:
006: import org.geotools.factory.FactoryRegistryException;
007: import org.geotools.feature.Feature;
008: import org.geotools.feature.FeatureType;
009: import org.geotools.feature.IllegalAttributeException;
010: import org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer;
011: import org.geotools.referencing.ReferencingFactoryFinder;
012: import org.opengis.referencing.FactoryException;
013: import org.opengis.referencing.crs.CoordinateReferenceSystem;
014: import org.opengis.referencing.operation.MathTransform;
015: import org.opengis.referencing.operation.MathTransform2D;
016: import org.opengis.referencing.operation.OperationNotFoundException;
017: import org.opengis.referencing.operation.TransformException;
018:
019: import com.vividsolutions.jts.geom.Geometry;
020:
021: public class ReprojectingIterator implements Iterator {
022:
023: /**
024: * decorated iterator
025: */
026: Iterator delegate;
027:
028: /**
029: * The target coordinate reference system
030: */
031: CoordinateReferenceSystem target;
032:
033: /**
034: * schema of reprojected features
035: */
036: FeatureType schema;
037:
038: /**
039: * Transformer
040: */
041: GeometryCoordinateSequenceTransformer tx;
042:
043: public ReprojectingIterator(Iterator delegate,
044: MathTransform transform, FeatureType schema,
045: GeometryCoordinateSequenceTransformer transformer)
046: throws OperationNotFoundException,
047: FactoryRegistryException, FactoryException {
048: this .delegate = delegate;
049:
050: this .schema = schema;
051:
052: tx = transformer;
053: tx.setMathTransform((MathTransform2D) transform);
054: }
055:
056: public ReprojectingIterator(Iterator delegate,
057: CoordinateReferenceSystem source,
058: CoordinateReferenceSystem target, FeatureType schema,
059: GeometryCoordinateSequenceTransformer transformer)
060: throws OperationNotFoundException,
061: FactoryRegistryException, FactoryException {
062: this .delegate = delegate;
063: this .target = target;
064: this .schema = schema;
065: tx = transformer;
066:
067: MathTransform transform = ReferencingFactoryFinder
068: .getCoordinateOperationFactory(null).createOperation(
069: source, target).getMathTransform();
070: tx.setMathTransform((MathTransform2D) transform);
071: }
072:
073: public Iterator getDelegate() {
074: return delegate;
075: }
076:
077: public void remove() {
078: delegate.remove();
079: }
080:
081: public boolean hasNext() {
082: return delegate.hasNext();
083: }
084:
085: public Object next() {
086: Feature feature = (Feature) delegate.next();
087: try {
088: return reproject(feature);
089: } catch (IOException e) {
090: throw new RuntimeException(e);
091: }
092: }
093:
094: Feature reproject(Feature feature) throws IOException {
095:
096: Object[] attributes = feature.getAttributes(null);
097:
098: for (int i = 0; i < attributes.length; i++) {
099: Object object = attributes[i];
100: if (object instanceof Geometry) {
101: // do the transformation
102: Geometry geometry = (Geometry) object;
103: try {
104: attributes[i] = tx.transform(geometry);
105: } catch (TransformException e) {
106: String msg = "Error occured transforming "
107: + geometry.toString();
108: throw (IOException) new IOException(msg)
109: .initCause(e);
110: }
111: }
112: }
113:
114: try {
115: return schema.create(attributes, feature.getID());
116: } catch (IllegalAttributeException e) {
117: String msg = "Error creating reprojeced feature";
118: throw (IOException) new IOException(msg).initCause(e);
119: }
120: }
121:
122: }
|