001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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.geometry.jts.coordinatesequence;
017:
018: import org.geotools.geometry.jts.CoordinateSequenceTransformer;
019: import org.opengis.referencing.crs.CoordinateReferenceSystem;
020: import org.opengis.referencing.operation.MathTransform;
021: import org.opengis.referencing.operation.TransformException;
022: import org.opengis.geometry.DirectPosition;
023: import org.opengis.geometry.MismatchedDimensionException;
024:
025: import com.vividsolutions.jts.geom.CoordinateSequence;
026: import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
027: import com.vividsolutions.jts.geom.impl.PackedCoordinateSequence;
028:
029: /**
030: * A JTS CoordinateSequenceTransformer which transforms the values in place.
031: * <p>
032: * Paragraph ...
033: * </p><p>
034: * Responsibilities:
035: * <ul>
036: * <li>
037: * <li>
038: * </ul>
039: * </p><p>
040: * Example:<pre><code>
041: * InPlaceCoordinateSequenceTransformer x = new InPlaceCoordinateSequenceTransformer( ... );
042: * TODO code example
043: * </code></pre>
044: * </p>
045: * @author jeichar
046: * @since 0.6.0
047: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/geometry/jts/coordinatesequence/InPlaceCoordinateSequenceTransformer.java $
048: */
049: public class InPlaceCoordinateSequenceTransformer implements
050: CoordinateSequenceTransformer {
051:
052: /**
053: * @see org.geotools.geometry.jts.CoordinateSequenceTransformer#transform(com.vividsolutions.jts.geom.CoordinateSequence, org.opengis.referencing.operation.MathTransform)
054: */
055: public CoordinateSequence transform(CoordinateSequence cs,
056: MathTransform transform) throws TransformException {
057: if (cs instanceof PackedCoordinateSequence) {
058: return transformInternal((PackedCoordinateSequence) cs,
059: transform);
060: }
061: throw new TransformException(
062: cs.getClass().getName()
063: + " is not a implementation that is known to be transformable in place");
064: }
065:
066: FlyWeightDirectPosition start = new FlyWeightDirectPosition(2);
067:
068: private CoordinateSequence transformInternal(
069: PackedCoordinateSequence sequence, MathTransform transform)
070: throws TransformException {
071:
072: start.setSequence(sequence);
073: for (int i = 0; i < sequence.size(); i++) {
074: start.setOffset(i);
075: try {
076: transform.transform(start, start);
077: } catch (MismatchedDimensionException e) {
078: throw new TransformException("", e);
079: }
080: }
081: return sequence;
082: }
083:
084: private class FlyWeightDirectPosition implements DirectPosition {
085: PackedCoordinateSequence sequence;
086: int offset = 0;
087: private int dimension;
088:
089: /**
090: * Construct <code>InPlaceCoordinateSequenceTransformer.FlyWeightDirectPosition</code>.
091: *
092: */
093: public FlyWeightDirectPosition(int dim) {
094: dimension = dim;
095: }
096:
097: /**
098: * @param offset The offset to set.
099: */
100: public void setOffset(int offset) {
101: this .offset = offset;
102: }
103:
104: /**
105: * @param sequence The sequence to set.
106: */
107: public void setSequence(PackedCoordinateSequence sequence) {
108: this .sequence = sequence;
109: }
110:
111: /**
112: * @see org.opengis.geometry.coordinate.DirectPosition#getDimension()
113: */
114: public int getDimension() {
115: return dimension;
116: }
117:
118: /**
119: * @see org.opengis.geometry.coordinate.DirectPosition#getCoordinates()
120: */
121: public double[] getCoordinates() {
122: return new double[] { sequence.getX(offset),
123: sequence.getY(offset),
124: sequence.getOrdinate(offset, CoordinateSequence.Z) };
125: }
126:
127: /**
128: * @see org.opengis.geometry.coordinate.DirectPosition#getOrdinate(int)
129: */
130: public double getOrdinate(int arg0)
131: throws IndexOutOfBoundsException {
132: return sequence.getOrdinate(offset, arg0);
133: }
134:
135: /**
136: * @see org.opengis.geometry.coordinate.DirectPosition#setOrdinate(int, double)
137: */
138: public void setOrdinate(int arg0, double arg1)
139: throws IndexOutOfBoundsException {
140: sequence.setOrdinate(offset, arg0, arg1);
141: }
142:
143: /**
144: * @see org.opengis.geometry.coordinate.DirectPosition#getCoordinateReferenceSystem()
145: */
146: public CoordinateReferenceSystem getCoordinateReferenceSystem() {
147: //TODO implement method body
148: throw new UnsupportedOperationException();
149: }
150:
151: /**
152: * @see org.opengis.geometry.coordinate.DirectPosition#clone()
153: */
154: public Object clone() {
155: throw new UnsupportedOperationException();
156: }
157:
158: /**
159: * @see org.opengis.geometry.coordinate.Position#getPosition()
160: */
161: public DirectPosition getPosition() {
162: return this;
163: }
164:
165: }
166:
167: }
|