Class Point

  1  
  2  
  3 /*
  4  * Copyright (c) 2016 Vivid Solutions.
  5  *
  6  * All rights reserved. This program and the accompanying materials
  7  * are made available under the terms of the Eclipse Public License 2.0
  8  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  9  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 10  * and the Eclipse Distribution License is available at
 11  *
 12  * http://www.eclipse.org/org/documents/edl-v10.php.
 13  */
 14 package org.locationtech.jts.geom;
 15  
 16 import org.locationtech.jts.util.Assert;
 17  
 18 /**
 19  * Represents a single point.
 20  *
 21  * A <code>Point</code> is topologically valid if and only if:
 22  * <ul>
 23  * <li>the coordinate which defines it (if any) is a valid coordinate
 24  * (i.e. does not have an <code>NaN</code> X or Y ordinate)
 25  * </ul>
 26  *
 27  *@version 1.7
 28  */
 29 public class Point
 30     extends Geometry
 31     implements Puntal
 32 {
 33   private static final long serialVersionUID = 4902022702746614570L;
 34   /**
 35    *  The <code>Coordinate</code> wrapped by this <code>Point</code>.
 36    */
 37   private CoordinateSequence coordinates;
 38  
 39   /**
 40    *  Constructs a <code>Point</code> with the given coordinate.
 41    *
 42    *@param  coordinate      the coordinate on which to base this <code>Point</code>
 43    *      , or <code>null</code> to create the empty geometry.
 44    *@param  precisionModel  the specification of the grid of allowable points
 45    *      for this <code>Point</code>
 46    *@param  SRID            the ID of the Spatial Reference System used by this
 47    *      <code>Point</code>
 48    * @deprecated Use GeometryFactory instead
 49    */
 50   public Point(Coordinate coordinate, PrecisionModel precisionModel, int SRID) {
 51     super(new GeometryFactory(precisionModel, SRID));
 52     init(getFactory().getCoordinateSequenceFactory().create(
 53           coordinate != null ? new Coordinate[]{coordinate} : new Coordinate[]{}));
 54   }
 55  
 56   /**
 57    *@param  coordinates      contains the single coordinate on which to base this <code>Point</code>
 58    *      , or <code>null</code> to create the empty geometry.
 59    */
 60   public Point(CoordinateSequence coordinates, GeometryFactory factory) {
 61     super(factory);
 62     init(coordinates);
 63   }
 64  
 65   private void init(CoordinateSequence coordinates)
 66   {
 67     if (coordinates == null) {
 68       coordinates = getFactory().getCoordinateSequenceFactory().create(new Coordinate[]{});
 69     }
 70     Assert.isTrue(coordinates.size() <= 1);
 71     this.coordinates = coordinates;
 72   }
 73  
 74   public Coordinate[] getCoordinates() {
 75     return isEmpty() ? new Coordinate[]{} : new Coordinate[]{
 76         getCoordinate()
 77         };
 78   }
 79  
 80   public int getNumPoints() {
 81     return isEmpty() ? 0 : 1;
 82   }
 83  
 84   public boolean isEmpty() {
 85     return coordinates.size() == 0;
 86   }
 87  
 88   public boolean isSimple() {
 89     return true;
 90   }
 91  
 92   public int getDimension() {
 93     return 0;
 94   }
 95  
 96   public int getBoundaryDimension() {
 97     return Dimension.FALSE;
 98   }
 99  
100   public double getX() {
101     if (getCoordinate() == null) {
102       throw new IllegalStateException("getX called on empty Point");
103     }
104     return getCoordinate().x;
105   }
106  
107   public double getY() {
108     if (getCoordinate() == null) {
109       throw new IllegalStateException("getY called on empty Point");
110     }
111     return getCoordinate().y;
112   }
113  
114   public Coordinate getCoordinate() {
115     return coordinates.size() != 0 ? coordinates.getCoordinate(0): null;
116   }
117  
118   public String getGeometryType() {
119     return Geometry.TYPENAME_POINT;
120   }
121  
122   /**
123    * Gets the boundary of this geometry.
124    * Zero-dimensional geometries have no boundary by definition,
125    * so an empty GeometryCollection is returned.
126    *
127    * @return an empty GeometryCollection
128    * @see Geometry#getBoundary
129    */
130   public Geometry getBoundary() {
131     return getFactory().createGeometryCollection();
132   }
133  
134   protected Envelope computeEnvelopeInternal() {
135     if (isEmpty()) {
136       return new Envelope();
137     }
138     Envelope env = new Envelope();
139     env.expandToInclude(coordinates.getX(0), coordinates.getY(0));
140     return env;
141   }
142  
143   public boolean equalsExact(Geometry other, double tolerance) {
144     if (!isEquivalentClass(other)) {
145       return false;
146     }
147     if (isEmpty() && other.isEmpty()) {
148       return true;
149     }
150     if (isEmpty() != other.isEmpty()) {
151       return false;
152     }
153     return equal(((Point) other).getCoordinate(), this.getCoordinate(), tolerance);
154   }
155  
156   public void apply(CoordinateFilter filter) {
157         if (isEmpty()) { return; }
158         filter.filter(getCoordinate());
159       }
160  
161   public void apply(CoordinateSequenceFilter filter)
162   {
163         if (isEmpty())
164         return;
165         filter.filter(coordinates, 0);
166       if (filter.isGeometryChanged())
167         geometryChanged();
168       }
169  
170   public void apply(GeometryFilter filter) {
171     filter.filter(this);
172   }
173  
174   public void apply(GeometryComponentFilter filter) {
175     filter.filter(this);
176   }
177  
178   /**
179    * Creates and returns a full copy of this {@link Point} object.
180    * (including all coordinates contained by it).
181    *
182    * @return a clone of this instance
183    * @deprecated
184    */
185   public Object clone() {
186     return copy();
187   }
188  
189   protected Point copyInternal() {
190     return new Point(coordinates.copy(), factory);
191   }
192  
193   public Point reverse() {
194     return (Point) super.reverse();
195   }
196  
197   protected Point reverseInternal()
198   {
199     return getFactory().createPoint(coordinates.copy());
200   }
201  
202   public void normalize()
203   {
204     // a Point is always in normalized form
205   }
206  
207   protected int compareToSameClass(Object other) {
208     Point point = (Point) other;
209     return getCoordinate().compareTo(point.getCoordinate());
210   }
211  
212   protected int compareToSameClass(Object other, CoordinateSequenceComparator comp)
213   {
214     Point point = (Point) other;
215     return comp.compare(this.coordinates, point.coordinates);
216   }
217   
218   protected int getTypeCode() {
219     return Geometry.TYPECODE_POINT;
220   }
221  
222   public CoordinateSequence getCoordinateSequence() {
223     return coordinates;
224   }
225 }
226  
227