001:
002: /*
003: * The JTS Topology Suite is a collection of Java classes that
004: * implement the fundamental operations required to validate a given
005: * geo-spatial data set to a known topological specification.
006: *
007: * Copyright (C) 2001 Vivid Solutions
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: *
023: * For more information, contact:
024: *
025: * Vivid Solutions
026: * Suite #1A
027: * 2328 Government Street
028: * Victoria BC V8T 5G5
029: * Canada
030: *
031: * (250)385-6040
032: * www.vividsolutions.com
033: */
034: package com.vividsolutions.jts.geom;
035:
036: import com.vividsolutions.jts.util.Assert;
037:
038: /**
039: * Basic implementation of <code>Point</code>.
040: *
041: *@version 1.7
042: */
043: public class Point extends Geometry {
044: private static final long serialVersionUID = 4902022702746614570L;
045: /**
046: * The <code>Coordinate</code> wrapped by this <code>Point</code>.
047: */
048: private CoordinateSequence coordinates;
049:
050: /**
051: * Constructs a <code>Point</code> with the given coordinate.
052: *
053: *@param coordinate the coordinate on which to base this <code>Point</code>
054: * , or <code>null</code> to create the empty geometry.
055: *@param precisionModel the specification of the grid of allowable points
056: * for this <code>Point</code>
057: *@param SRID the ID of the Spatial Reference System used by this
058: * <code>Point</code>
059: * @deprecated Use GeometryFactory instead
060: */
061: public Point(Coordinate coordinate, PrecisionModel precisionModel,
062: int SRID) {
063: super (new GeometryFactory(precisionModel, SRID));
064: init(getFactory().getCoordinateSequenceFactory().create(
065: coordinate != null ? new Coordinate[] { coordinate }
066: : new Coordinate[] {}));
067: }
068:
069: /**
070: *@param coordinates contains the single coordinate on which to base this <code>Point</code>
071: * , or <code>null</code> to create the empty geometry.
072: */
073: public Point(CoordinateSequence coordinates, GeometryFactory factory) {
074: super (factory);
075: init(coordinates);
076: }
077:
078: private void init(CoordinateSequence coordinates) {
079: if (coordinates == null) {
080: coordinates = getFactory().getCoordinateSequenceFactory()
081: .create(new Coordinate[] {});
082: }
083: Assert.isTrue(coordinates.size() <= 1);
084: this .coordinates = coordinates;
085: }
086:
087: public Coordinate[] getCoordinates() {
088: return isEmpty() ? new Coordinate[] {}
089: : new Coordinate[] { getCoordinate() };
090: }
091:
092: public int getNumPoints() {
093: return isEmpty() ? 0 : 1;
094: }
095:
096: public boolean isEmpty() {
097: return getCoordinate() == null;
098: }
099:
100: public boolean isSimple() {
101: return true;
102: }
103:
104: public boolean isValid() {
105: return true;
106: }
107:
108: public int getDimension() {
109: return 0;
110: }
111:
112: public int getBoundaryDimension() {
113: return Dimension.FALSE;
114: }
115:
116: public double getX() {
117: if (getCoordinate() == null) {
118: throw new IllegalStateException(
119: "getX called on empty Point");
120: }
121: return getCoordinate().x;
122: }
123:
124: public double getY() {
125: if (getCoordinate() == null) {
126: throw new IllegalStateException(
127: "getY called on empty Point");
128: }
129: return getCoordinate().y;
130: }
131:
132: public Coordinate getCoordinate() {
133: return coordinates.size() != 0 ? coordinates.getCoordinate(0)
134: : null;
135: }
136:
137: public String getGeometryType() {
138: return "Point";
139: }
140:
141: /**
142: * Gets the boundary of this geometry.
143: * Zero-dimensional geometries have no boundary by definition,
144: * so an empty GeometryCollection is returned.
145: *
146: * @return an empty GeometryCollection
147: * @see Geometry#getBoundary
148: */
149: public Geometry getBoundary() {
150: return getFactory().createGeometryCollection(null);
151: }
152:
153: protected Envelope computeEnvelopeInternal() {
154: if (isEmpty()) {
155: return new Envelope();
156: }
157: Envelope env = new Envelope();
158: env.expandToInclude(coordinates.getX(0), coordinates.getY(0));
159: return env;
160: }
161:
162: public boolean equalsExact(Geometry other, double tolerance) {
163: if (!isEquivalentClass(other)) {
164: return false;
165: }
166: if (isEmpty() && other.isEmpty()) {
167: return true;
168: }
169: return equal(((Point) other).getCoordinate(), this
170: .getCoordinate(), tolerance);
171: }
172:
173: public void apply(CoordinateFilter filter) {
174: if (isEmpty()) {
175: return;
176: }
177: filter.filter(getCoordinate());
178: }
179:
180: public void apply(CoordinateSequenceFilter filter) {
181: if (isEmpty())
182: return;
183: filter.filter(coordinates, 0);
184: if (filter.isGeometryChanged())
185: geometryChanged();
186: }
187:
188: public void apply(GeometryFilter filter) {
189: filter.filter(this );
190: }
191:
192: public void apply(GeometryComponentFilter filter) {
193: filter.filter(this );
194: }
195:
196: /**
197: * Creates and returns a full copy of this {@link Point} object.
198: * (including all coordinates contained by it).
199: *
200: * @return a clone of this instance
201: */
202: public Object clone() {
203: Point p = (Point) super .clone();
204: p.coordinates = (CoordinateSequence) coordinates.clone();
205: return p;// return the clone
206: }
207:
208: public void normalize() {
209: }
210:
211: protected int compareToSameClass(Object other) {
212: Point point = (Point) other;
213: return getCoordinate().compareTo(point.getCoordinate());
214: }
215:
216: protected int compareToSameClass(Object other,
217: CoordinateSequenceComparator comp) {
218: Point point = (Point) other;
219: return comp.compare(this .coordinates, point.coordinates);
220: }
221:
222: public CoordinateSequence getCoordinateSequence() {
223: return coordinates;
224: }
225: }
|