001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
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;
017:
018: // J2SE dependencies
019: import java.awt.geom.AffineTransform;
020:
021: // JTS dependencies
022: import com.vividsolutions.jts.geom.Coordinate;
023: import com.vividsolutions.jts.geom.Envelope;
024:
025: // OpenGIS dependencies
026: import org.opengis.referencing.FactoryException;
027: import org.opengis.referencing.operation.MathTransform2D;
028: import org.opengis.referencing.operation.TransformException;
029: import org.opengis.referencing.crs.CoordinateReferenceSystem;
030:
031: // Geotools dependencies
032: import org.geotools.referencing.wkt.Parser;
033: import org.geotools.referencing.ReferencingFactoryFinder;
034: import org.geotools.referencing.crs.DefaultProjectedCRS;
035: import org.geotools.referencing.crs.DefaultGeographicCRS;
036: import org.geotools.referencing.operation.matrix.GeneralMatrix;
037: import org.geotools.referencing.operation.projection.PointOutsideEnvelopeException;
038:
039: // JUnit dependencies
040: import junit.framework.Test;
041: import junit.framework.TestCase;
042: import junit.framework.TestSuite;
043:
044: /**
045: * Tests the {@link JTS} implementation.
046: *
047: * @since 2.2
048: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/geometry/jts/JTSTest.java $
049: * @version $Id: JTSTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
050: * @author Jess Eichar
051: * @author Martin Desruisseaux
052: */
053: public class JTSTest extends TestCase {
054: /**
055: * The tolerance factor.
056: */
057: private static final double EPS = 0.000001;
058:
059: /**
060: * A CRS for testing purpose.
061: */
062: static final String UTM_ZONE_10N = "PROJCS[\"NAD_1983_UTM_Zone_10N\",\n"
063: + " GEOGCS[\"GCS_North_American_1983\",\n"
064: + " DATUM[\"D_North_American_1983\",\n"
065: + " TOWGS84[0,0,0,0,0,0,0],\n"
066: + " SPHEROID[\"GRS_1980\",6378137,298.257222101]],\n"
067: + " PRIMEM[\"Greenwich\",0],\n"
068: + " UNIT[\"Degree\",0.017453292519943295]],\n"
069: + " PROJECTION[\"Transverse_Mercator\"],\n"
070: + " PARAMETER[\"False_Easting\",500000],\n"
071: + " PARAMETER[\"False_Northing\",0],\n"
072: + " PARAMETER[\"Central_Meridian\",-123],\n"
073: + " PARAMETER[\"Scale_Factor\",0.9996],\n"
074: + " PARAMETER[\"Latitude_Of_Origin\",0],\n"
075: + " UNIT[\"Meter\",1]]";
076: /**
077: * A CRS for testing purpose.
078: */
079: static final String NAD83_BC = "PROJCS[\"NAD83 / BC Albers\",\n"
080: + " GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\",\n"
081: + " SPHEROID[\"GRS 1980\",6378137,298.257222101,AUTHORITY[\"EPSG\",\"7019\"]],\n"
082: + " TOWGS84[0,0,0],AUTHORITY[\"EPSG\",\"6269\"]],\n"
083: + " PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],\n"
084: + " UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],\n"
085: + " AUTHORITY[\"EPSG\",\"4269\"]],\n"
086: + " PROJECTION[\"Albers_Conic_Equal_Area\"],\n"
087: + " PARAMETER[\"standard_parallel_1\",50],\n"
088: + " PARAMETER[\"standard_parallel_2\",58.5],\n"
089: + " PARAMETER[\"latitude_of_center\",45],\n"
090: + " PARAMETER[\"longitude_of_center\",-126],\n"
091: + " PARAMETER[\"false_easting\",1000000],\n"
092: + " PARAMETER[\"false_northing\",0],\n"
093: + " UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],\n"
094: + " AUTHORITY[\"EPSG\",\"3005\"]]";
095:
096: /**
097: * Run the suite from the command line.
098: */
099: public static void main(String[] args) {
100: org.geotools.util.logging.Logging.GEOTOOLS
101: .forceMonolineConsoleOutput();
102: junit.textui.TestRunner.run(suite());
103: }
104:
105: /**
106: * Returns the test suite.
107: */
108: public static Test suite() {
109: return new TestSuite(JTSTest.class);
110: }
111:
112: /**
113: * Tests the transformation of a single coordinate.
114: */
115: public void testTransformCoordinate() throws FactoryException,
116: TransformException {
117: Coordinate coord = new Coordinate(10, 10);
118: AffineTransform at = AffineTransform.getScaleInstance(0.5, 1);
119: MathTransform2D t = (MathTransform2D) ReferencingFactoryFinder
120: .getMathTransformFactory(null).createAffineTransform(
121: new GeneralMatrix(at));
122: coord = JTS.transform(coord, coord, t);
123: assertEquals(new Coordinate(5, 10), coord);
124: coord = JTS.transform(coord, coord, t.inverse());
125: assertEquals(new Coordinate(10, 10), coord);
126:
127: CoordinateReferenceSystem crs = ReferencingFactoryFinder
128: .getCRSFactory(null).createFromWKT(UTM_ZONE_10N);
129: t = (MathTransform2D) ReferencingFactoryFinder
130: .getCoordinateOperationFactory(null).createOperation(
131: DefaultGeographicCRS.WGS84, crs)
132: .getMathTransform();
133: coord = new Coordinate(-123, 55);
134: coord = JTS.transform(coord, coord, t);
135: coord = JTS.transform(coord, coord, t.inverse());
136: assertEquals(-123, coord.x, EPS);
137: assertEquals(55, coord.y, EPS);
138: }
139:
140: /*
141: * Tests the transformation of an envelope.
142: */
143: public void testTransformEnvelopeMathTransform()
144: throws FactoryException, TransformException {
145: Envelope envelope = new Envelope(0, 10, 0, 10);
146: AffineTransform at = AffineTransform.getScaleInstance(0.5, 1);
147: MathTransform2D t = (MathTransform2D) ReferencingFactoryFinder
148: .getMathTransformFactory(null).createAffineTransform(
149: new GeneralMatrix(at));
150: envelope = JTS.transform(envelope, t);
151: assertEquals(new Envelope(0, 5, 0, 10), envelope);
152: envelope = JTS.transform(envelope, t.inverse());
153: assertEquals(new Envelope(0, 10, 0, 10), envelope);
154:
155: envelope = JTS.transform(envelope, null, t, 10);
156: assertEquals(new Envelope(0, 5, 0, 10), envelope);
157: envelope = JTS.transform(envelope, null, t.inverse(), 10);
158: assertEquals(new Envelope(0, 10, 0, 10), envelope);
159:
160: CoordinateReferenceSystem crs = ReferencingFactoryFinder
161: .getCRSFactory(null).createFromWKT(UTM_ZONE_10N);
162: t = (MathTransform2D) ReferencingFactoryFinder
163: .getCoordinateOperationFactory(null).createOperation(
164: DefaultGeographicCRS.WGS84, crs)
165: .getMathTransform();
166: envelope = new Envelope(-123, -133, 55, 60);
167: envelope = JTS.transform(envelope, t);
168: envelope = JTS.transform(envelope, t.inverse());
169: /*
170: * Use a large tolerance factory for comparaisons because an accurate transformed envelope
171: * is bigger than the envelope that we get if we transformed only the 4 corners, and the
172: * inverse envelope way expand yet again the envelope for exactly the same reason.
173: */
174: assertEquals(-133, envelope.getMinX(), 1.5);
175: assertEquals(-123, envelope.getMaxX(), EPS);
176: assertEquals(55, envelope.getMinY(), 0.5);
177: assertEquals(60, envelope.getMaxY(), 0.5);
178: }
179:
180: /**
181: * Tests the distance between points function
182: */
183: public void testOrthodromicDistance() throws Exception {
184: final Parser parser = new Parser();
185: final DefaultProjectedCRS crs = (DefaultProjectedCRS) parser
186: .parseObject(NAD83_BC);
187: double d = JTS.orthodromicDistance(new Coordinate(
188: 1402848.1938534670, 651571.1729878788), new Coordinate(
189: 1389481.3104009738, 641990.9430108378), crs);
190: double realValue = 16451.33114;
191: assertEquals(realValue, d, 0.1);
192: }
193:
194: public void testCheckCoordinateRange() throws Exception {
195: DefaultGeographicCRS crs = DefaultGeographicCRS.WGS84;
196:
197: // valid
198: JTS.checkCoordinatesRange(JTS.toGeometry(new Envelope(-10, 10,
199: -10, 10)), crs);
200:
201: // invalid lat
202: try {
203: JTS.checkCoordinatesRange(JTS.toGeometry(new Envelope(-10,
204: 10, -100, 10)), crs);
205: fail("Provided invalid coordinates, yet check did not throw an exception");
206: } catch (PointOutsideEnvelopeException e) {
207: // fine
208: }
209:
210: // invalid lon
211: try {
212: JTS.checkCoordinatesRange(JTS.toGeometry(new Envelope(-190,
213: 10, -10, 10)), crs);
214: fail("Provided invalid coordinates, yet check did not throw an exception");
215: } catch (PointOutsideEnvelopeException e) {
216: // fine
217: }
218:
219: }
220: }
|