001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * (C) 2005, Geotools Project Managment Committee (PMC)
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 org.geotools.demo.referencing;
016:
017: // J2SE and JAI dependencies
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.InputStreamReader;
021: import java.util.Collections;
022: import java.util.Map;
023:
024: // geoapi interfaces
025: import org.opengis.parameter.ParameterValueGroup;
026: import org.opengis.referencing.crs.CRSFactory;
027: import org.opengis.referencing.crs.GeographicCRS;
028: import org.opengis.referencing.crs.ProjectedCRS;
029: import org.opengis.referencing.cs.CSFactory;
030: import org.opengis.referencing.cs.EllipsoidalCS;
031: import org.opengis.referencing.cs.CartesianCS;
032: import org.opengis.referencing.cs.CoordinateSystemAxis;
033: import org.opengis.referencing.datum.GeodeticDatum;
034: import org.opengis.referencing.operation.MathTransformFactory;
035: import org.opengis.referencing.operation.MathTransform;
036: import org.opengis.referencing.operation.CoordinateOperationFactory;
037: import org.opengis.referencing.operation.CoordinateOperation;
038: import org.opengis.referencing.FactoryException;
039: import org.opengis.referencing.operation.TransformException;
040: import org.opengis.spatialschema.geometry.DirectPosition;
041:
042: // geotools dependancies
043: import org.geotools.referencing.FactoryFinder;
044: import org.geotools.referencing.factory.FactoryGroup;
045: import org.geotools.geometry.GeneralDirectPosition;
046:
047: /**
048: * An example of application reading points from the standard input, transforming
049: * them and writting the result to the standard output. This class can be run from
050: * the command-line using the following syntax:
051: *
052: * <blockquote><pre>
053: * java TransformationConsole [classification]
054: * </pre></blockquote>
055: *
056: * Where [classification] is the the classification name of the projection to perform.
057: * The default value is "Mercator_1SP". The list of supported classification name is
058: * available here:
059: *
060: * http://docs.codehaus.org/display/GEOTOOLS/Coordinate+Transformation+Parameters
061: *
062: * To exit from the application, enter "exit".
063: *
064: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/demo/referencing/src/main/java/org/geotools/demo/referencing/TransformationConsole.java $
065: * @version $Id: TransformationConsole.java 17699 2006-01-22 22:42:41Z desruisseaux $
066: * @author Martin Desruisseaux
067: */
068: public class TransformationConsole {
069: /**
070: * The program main entry point.
071: *
072: * @param args Array of command-line arguments. This small demo accept only
073: * one argument: the classification name of the projection to
074: * perform.
075: *
076: * @throws IOException if an error occured while reading the input stream.
077: * @throws FactoryException if a coordinate system can't be constructed.
078: * @throws TransformException if a transform failed.
079: */
080: public static void main(String[] args) throws IOException,
081: FactoryException, TransformException {
082: /*
083: * Check command-line arguments.
084: */
085: String classification;
086: switch (args.length) {
087: case 0:
088: classification = "Mercator_1SP";
089: break;
090: case 1:
091: classification = args[0];
092: break;
093: default:
094: System.err.println("Expected 0 or 1 argument");
095: return;
096: }
097: /*
098: * The factories to use for constructing coordinate systems.
099: */
100: CRSFactory crsFactory = FactoryFinder.getCRSFactory(null);
101: CSFactory csFactory = FactoryFinder.getCSFactory(null);
102: MathTransformFactory mtFactory = FactoryFinder
103: .getMathTransformFactory(null);
104: FactoryGroup factories = new FactoryGroup();
105: /*
106: * Construct the source CoordinateReferenceSystem. We will use a geographic coordinate
107: * system, i.e. one that use (latitude,longitude) coordinates. Latitude values
108: * are increasing north and longitude values area increasing east. Angular units
109: * are degrees and prime meridian is Greenwich. Datum is WGS 84 (a commonly
110: * used one for remote sensing data and GPS). Note that the Geotools library
111: * provides simpler ways to construct geographic coordinate systems using default
112: * values for some arguments. But we show here the complete way in order to show
113: * the range of possibilities and to stay closer to the OpenGIS's specification.
114: */
115: CoordinateSystemAxis longAxis = org.geotools.referencing.cs.DefaultCoordinateSystemAxis.GEODETIC_LONGITUDE;
116: CoordinateSystemAxis latAxis = org.geotools.referencing.cs.DefaultCoordinateSystemAxis.GEODETIC_LATITUDE;
117: EllipsoidalCS ellipseCS = csFactory.createEllipsoidalCS(
118: Collections.singletonMap("name", "Lat/Long"), latAxis,
119: longAxis);
120: GeodeticDatum datum = org.geotools.referencing.datum.DefaultGeodeticDatum.WGS84;
121: GeographicCRS sourceCRS = crsFactory.createGeographicCRS(
122: Collections.singletonMap("name", "WGS 84"), datum,
123: ellipseCS);
124: /*
125: * Construct the target CoordinateReferenceSystem. We will use a projected coordinate
126: * system, i.e. one that use linear (in metres) coordinates. We will use the
127: * same ellipsoid than the source geographic coordinate system (i.e. WGS84).
128: * Default parameters will be used for this projection, but false_easting and
129: * false_northing values could be set below.
130: *
131: * Note: The 'sourceCRS' argument below is the geographic coordinate system
132: * to base projection on. It is also the source coordinate system in
133: * this particular case, but this may not alway be the case.
134: */
135: ParameterValueGroup parameters = mtFactory
136: .getDefaultParameters(classification);
137: if (false) {
138: // Set optional parameters here. This example set the false
139: // easting and northing just for demonstration purpose.
140: parameters.parameter("false_easting").setValue(1000.0);
141: parameters.parameter("false_northing").setValue(1000.0);
142:
143: }
144: CartesianCS cartCS = org.geotools.referencing.cs.DefaultCartesianCS.GENERIC_2D;
145: Map properties = Collections.singletonMap("name",
146: classification);
147: ProjectedCRS targetCRS = factories.createProjectedCRS(
148: properties, sourceCRS, null, parameters, cartCS);
149:
150: /*
151: * Now, we have built source and destination coordinate referenc systems ('sourceCRS'
152: * and 'targetCRS'). Here are some observations about their relationships:
153: *
154: * * We use the same ellipsoid (WGS 84) for both, but it could as well be
155: * different. Using different ellipsoids would require a datum shift to
156: * transform between the two datums.
157: *
158: * * The axis order is inverted between the source (latitude,longitude)
159: * and the target (x,y). This is up to the user to choose the axis
160: * order he want; Geotools should correctly swap them as needed. User
161: * could as well reverse axis orientation (e.g. make longitude values
162: * increasing West); Geotools should handle that correctly.
163: *
164: * Now, get the transformation.
165: */
166: CoordinateOperationFactory coFactory = FactoryFinder
167: .getCoordinateOperationFactory(null);
168: CoordinateOperation co = coFactory.createOperation(sourceCRS,
169: targetCRS);
170:
171: /*
172: * The CoordinateOperation object contains information about
173: * the transformation. It does not actually perform the transform
174: * operations on points. In order to transform points, we must get
175: * the math transform.
176: *
177: * Because source and target coordinate reference systems are both two-dimensional,
178: * this transform object will actually be an instance of MathTransform2D.
179: * The MathTransform2D interface is a Geotools's extension that is not part
180: * of the OpenGIS's specification. This class provides additional methods
181: * for interoperability with Java2D. If the user want to use it, he have to
182: * cast the transform to MathTransform2D.
183: */
184: MathTransform transform = co.getMathTransform();
185: /*
186: * Now, read lines from the standard input, transform them,
187: * and write the result to the standard output. Note: Java
188: * is not very good for console application. See many bug
189: * reports (e.g. http://developer.java.sun.com/developer/bugParade/bugs/4071281.html).
190: */
191: System.out.print("Projection classification is ");
192: System.out.println(classification);
193: System.out.println("Source CRS is:");
194: System.out.println(" " + sourceCRS.toWKT());
195: System.out.println("Target CRS is:");
196: System.out.println(" " + targetCRS.toWKT());
197: System.out
198: .println("Enter (latitude longitude) coordinates separated by a space.");
199: System.out.println("Enter \"exit\" to finish.");
200: final BufferedReader in = new BufferedReader(
201: new InputStreamReader(System.in));
202: String line;
203: while ((line = in.readLine()) != null) {
204: line = line.trim();
205: if (line.equalsIgnoreCase("exit")) {
206: break;
207: }
208: int split = line.indexOf(' ');
209: if (split >= 0) {
210: double latitude = Double.parseDouble(line.substring(0,
211: split));
212: double longitude = Double.parseDouble(line
213: .substring(split));
214: DirectPosition point = new GeneralDirectPosition(
215: latitude, longitude);
216: point = transform.transform(point, point);
217: System.out.println(point);
218: }
219: }
220: }
221: }
|