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;
017:
018: // J2SE dependencies
019: import java.io.Serializable;
020: import java.util.NoSuchElementException;
021:
022: // OpenGIS dependencies
023: import org.opengis.referencing.FactoryException;
024: import org.opengis.referencing.crs.CoordinateReferenceSystem;
025: import org.opengis.referencing.operation.CoordinateOperationFactory;
026: import org.opengis.referencing.operation.TransformException;
027: import org.opengis.util.Cloneable;
028:
029: // Geotools dependencies
030: import org.geotools.referencing.ReferencingFactoryFinder;
031: import org.geotools.referencing.operation.TransformPathNotFoundException;
032:
033: /**
034: * Root class of the Geotools default implementation of geometric object. {@code Geometry}
035: * instances are sets of direct positions in a particular coordinate reference system.
036: *
037: * @since 2.0
038: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/geometry/Geometry.java $
039: * @deprecated Will be recreated in the ISO geometry module.
040: * @version $Id: Geometry.java 28965 2008-01-27 16:46:58Z acuster $
041: * @author Martin Desruisseaux
042: */
043: public abstract class Geometry implements
044: org.opengis.geometry.Geometry, Serializable {
045: /**
046: * Serial number for interoperability with different versions.
047: */
048: private static final long serialVersionUID = -601532429079649232L;
049:
050: /**
051: * The default {@link CoordinateOperationFactory} to uses for {@link #mathTransform}.
052: * Will be constructed only when first requested.
053: */
054: private static CoordinateOperationFactory coordinateOperationFactory;
055:
056: /**
057: * The coordinate reference system used in {@linkplain GeneralDirectPosition direct position}
058: * coordinates.
059: */
060: protected final CoordinateReferenceSystem crs;
061:
062: /**
063: * Constructs a geometry with the specified coordinate reference system.
064: *
065: * @param crs The coordinate reference system used in
066: * {@linkplain GeneralDirectPosition direct position} coordinates.
067: */
068: public Geometry(final CoordinateReferenceSystem crs) {
069: this .crs = crs;
070: }
071:
072: /**
073: * Returns the coordinate reference system used in {@linkplain GeneralDirectPosition
074: * direct position} coordinates.
075: *
076: * @return The coordinate reference system used in {@linkplain GeneralDirectPosition
077: * direct position} coordinates.
078: *
079: * @see #getCoordinateDimension
080: */
081: public CoordinateReferenceSystem getCoordinateReferenceSystem() {
082: return crs;
083: }
084:
085: /**
086: * Returns the dimension of the coordinates that define this {@code Geometry}, which must
087: * be the same as the coordinate dimension of the coordinate reference system for this
088: * {@code Geometry}.
089: *
090: * @return The coordinate dimension.
091: *
092: * @see #getDimension
093: * @see #getCoordinateReferenceSystem
094: */
095: public int getCoordinateDimension() {
096: return crs.getCoordinateSystem().getDimension();
097: }
098:
099: /**
100: * Returns a new {@code Geometry} that is the coordinate transformation of this
101: * {@code Geometry} into the passed coordinate reference system within the accuracy
102: * of the transformation.
103: *
104: * @param newCRS The new coordinate reference system.
105: * @return The transformed {@code Geometry}.
106: * @throws TransformException if the transformation failed.
107: */
108: public org.opengis.geometry.Geometry transform(
109: CoordinateReferenceSystem newCRS) throws TransformException {
110: if (coordinateOperationFactory == null) {
111: // No need to synchronize: this is not a problem if this method is invoked
112: // twice in two different threads.
113: try {
114: coordinateOperationFactory = ReferencingFactoryFinder
115: .getCoordinateOperationFactory(null);
116: } catch (NoSuchElementException exception) {
117: // TODO: localize the message
118: throw new TransformException(
119: "Can't transform the geometry", exception);
120: }
121: }
122: try {
123: return transform(newCRS, coordinateOperationFactory
124: .createOperation(crs, newCRS).getMathTransform());
125: } catch (FactoryException exception) {
126: // TODO: localize the message
127: throw new TransformPathNotFoundException(
128: "Can't transform the geometry", exception);
129: }
130: }
131:
132: /**
133: * Returns a clone of this geometry with <em>deep</em> copy semantic. Any change on this object
134: * will have no impact on the returned clone, and conversely. For big geometries, implementations
135: * are encouraged to share as much internal data as possible (as opposed to performing a real
136: * copy of the data), while preserving the deep copy semantic.
137: * <P>
138: * The default implementation throws {@link CloneNotSupportedException}. If subclasses
139: * implements {@link Cloneable}, then they should overrides this method.
140: *
141: * @throws CloneNotSupportedException if this object do not support clone. This exception is
142: * never throws if this object implements {@link Cloneable}.
143: */
144: public Object clone() throws CloneNotSupportedException {
145: return super.clone();
146: }
147: }
|