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.referencing.crs;
017:
018: import java.util.Arrays;
019:
020: import junit.framework.TestCase;
021:
022: import org.geotools.geometry.GeneralDirectPosition;
023: import org.geotools.geometry.jts.JTS;
024: import org.geotools.referencing.CRS;
025: import org.geotools.referencing.ReferencingFactoryFinder;
026: import org.opengis.referencing.NoSuchAuthorityCodeException;
027: import org.opengis.referencing.crs.CoordinateReferenceSystem;
028: import org.opengis.referencing.operation.CoordinateOperation;
029: import org.opengis.referencing.operation.MathTransform;
030: import org.opengis.referencing.operation.TransformException;
031: import org.opengis.geometry.DirectPosition;
032:
033: import com.vividsolutions.jts.JTSVersion;
034: import com.vividsolutions.jts.geom.Coordinate;
035: import com.vividsolutions.jts.geom.CoordinateSequence;
036: import com.vividsolutions.jts.geom.Envelope;
037: import com.vividsolutions.jts.geom.GeometryFactory;
038: import com.vividsolutions.jts.geom.LinearRing;
039: import com.vividsolutions.jts.geom.Polygon;
040:
041: /**
042: * @author Jody Garnett
043: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/epsg-wkt/src/test/java/org/geotools/referencing/crs/CRSTest.java $
044: */
045: public class CRSTest extends TestCase {
046:
047: public void testEPSG42102() throws Exception {
048: CoordinateReferenceSystem bc = CRS.decode("EPSG:42102");
049: assertNotNull("bc", bc);
050: }
051:
052: public void testAUTO4200() throws Exception {
053: CoordinateReferenceSystem utm = CRS
054: .decode("AUTO:42001,0.0,0.0");
055: assertNotNull("auto-utm", utm);
056: }
057:
058: public void test4269() throws Exception {
059: CoordinateReferenceSystem latlong = CRS.decode("EPSG:4269");
060: assertNotNull("latlong", latlong);
061: try {
062: latlong = CRS.decode("4269");
063: fail("Shoudl not be able to decode 4269 without EPSG authority");
064: } catch (NoSuchAuthorityCodeException e) {
065: // expected
066: }
067: assertNotNull("latlong", latlong);
068: }
069:
070: public void testManditoryTranform() throws Exception {
071: CoordinateReferenceSystem WGS84 = (CoordinateReferenceSystem) CRS
072: .decode("EPSG:4326"); // latlong
073: CoordinateReferenceSystem NAD83 = (CoordinateReferenceSystem) CRS
074: .decode("EPSG:4269");
075: CoordinateReferenceSystem NAD83_UTM10 = (CoordinateReferenceSystem) CRS
076: .decode("EPSG:26910");
077: CoordinateReferenceSystem BC_ALBERS = (CoordinateReferenceSystem) CRS
078: .decode("EPSG:42102");
079:
080: CoordinateOperation op = ReferencingFactoryFinder
081: .getCoordinateOperationFactory(null).createOperation(
082: WGS84, WGS84);
083: MathTransform math = op.getMathTransform();
084:
085: DirectPosition pt1 = new GeneralDirectPosition(0.0, 0.0);
086: DirectPosition pt2 = math.transform(pt1, null);
087: assertNotNull(pt2);
088:
089: double pts[] = new double[] { 1187128, 395268, 1187128, 396027,
090: 1188245, 396027, 1188245, 395268, 1187128, 395268 };
091: double tst[] = new double[pts.length];
092: math.transform(pts, 0, new double[pts.length], 0,
093: pts.length / 2);
094: for (int i = 0; i < pts.length; i++)
095: assertTrue("pts[" + i + "]", pts[i] != tst[i]);
096: }
097:
098: public void testReprojection() throws Exception {
099:
100: // origional bc alberts
101: Polygon poly1 = poly(new double[] { 1187128, 395268, 1187128,
102: 396027, 1188245, 396027, 1188245, 395268, 1187128,
103: 395268 });
104:
105: // transformed
106: Polygon poly3 = poly(new double[] { -123.47009555832284,
107: 48.543261561072285, -123.46972894676578,
108: 48.55009592117936, -123.45463828850829,
109: 48.54973520267305, -123.4550070827961,
110: 48.54290089070186, -123.47009555832284,
111: 48.543261561072285 });
112:
113: CoordinateReferenceSystem WGS84 = (CoordinateReferenceSystem) CRS
114: .decode("EPSG:4326"); // latlong
115: CoordinateReferenceSystem BC_ALBERS = (CoordinateReferenceSystem) CRS
116: .decode("EPSG:42102");
117:
118: MathTransform transform = CRS.findMathTransform(BC_ALBERS,
119: WGS84);
120:
121: Polygon polyAfter = (Polygon) JTS.transform(poly1, transform);
122: System.out.println(polyAfter);
123:
124: assertTrue(poly3.equals(polyAfter));
125:
126: Envelope before = poly1.getEnvelopeInternal();
127: Envelope expected = poly3.getEnvelopeInternal();
128:
129: Envelope after = JTS.transform(before, transform);
130: assertEquals(expected, after);
131: }
132:
133: public static GeometryFactory factory = new GeometryFactory();
134:
135: public static Polygon poly(double coords[]) {
136: return factory.createPolygon(ring(coords), null);
137: }
138:
139: public static LinearRing ring(double coords[]) {
140: return factory.createLinearRing(coords(coords));
141: }
142:
143: public static CoordinateSequence coords(double coords[]) {
144: Coordinate array[] = new Coordinate[coords.length / 2];
145: for (int i = 0; i < array.length; i++) {
146: array[i] = new Coordinate(coords[i * 2], coords[i * 2 + 1]);
147: }
148: return factory.getCoordinateSequenceFactory().create(array);
149: }
150:
151: }
|