Class LinearRing

  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 /**
 17  * Models an OGC SFS <code>LinearRing</code>.
 18  * A <code>LinearRing</code> is a {@link LineString} which is both closed and simple.
 19  * In other words,
 20  * the first and last coordinate in the ring must be equal,
 21  * and the interior of the ring must not self-intersect.
 22  * Either orientation of the ring is allowed.
 23  * <p>
 24  * A ring must have either 0 or 4 or more points.
 25  * The first and last points must be equal (in 2D).
 26  * If these conditions are not met, the constructors throw
 27  * an {@link IllegalArgumentException}
 28  *
 29  * @version 1.7
 30  */
 31 public class LinearRing extends LineString
 32 {
 33   /**
 34    * The minimum number of vertices allowed in a valid non-empty ring (= 4).
 35    * Empty rings with 0 vertices are also valid.
 36    */
 37   public static final int MINIMUM_VALID_SIZE = 4;
 38  
 39   private static final long serialVersionUID = -4261142084085851829L;
 40  
 41   /**
 42    * Constructs a <code>LinearRing</code> with the given points.
 43    *
 44    *@param  points          points forming a closed and simple linestring, or
 45    *      <code>null</code> or an empty array to create the empty geometry.
 46    *      This array must not contain <code>null</code> elements.
 47    *
 48    *@param  precisionModel  the specification of the grid of allowable points
 49    *      for this <code>LinearRing</code>
 50    *@param  SRID            the ID of the Spatial Reference System used by this
 51    *      <code>LinearRing</code>
 52    * @throws IllegalArgumentException if the ring is not closed, or has too few points
 53    *
 54    * @deprecated Use GeometryFactory instead
 55    */
 56   public LinearRing(Coordinate points[], PrecisionModel precisionModel,
 57                     int SRID) {
 58     this(points, new GeometryFactory(precisionModel, SRID));
 59     validateConstruction();
 60   }
 61  
 62   /**
 63    * This method is ONLY used to avoid deprecation warnings.
 64    * @param points
 65    * @param factory
 66    * @throws IllegalArgumentException if the ring is not closed, or has too few points
 67    */
 68   private LinearRing(Coordinate points[], GeometryFactory factory) {
 69     this(factory.getCoordinateSequenceFactory().create(points), factory);
 70   }
 71  
 72  
 73   /**
 74    * Constructs a <code>LinearRing</code> with the vertices
 75    * specified by the given {@link CoordinateSequence}.
 76    *
 77    *@param  points  a sequence points forming a closed and simple linestring, or
 78    *      <code>null</code> to create the empty geometry.
 79    *
 80    * @throws IllegalArgumentException if the ring is not closed, or has too few points
 81    *
 82    */
 83   public LinearRing(CoordinateSequence points, GeometryFactory factory) {
 84     super(points, factory);
 85     validateConstruction();
 86   }
 87  
 88   private void validateConstruction() {
 89     if (!isEmpty() && ! super.isClosed()) {
 90       throw new IllegalArgumentException("Points of LinearRing do not form a closed linestring");
 91     }
 92     if (getCoordinateSequence().size() >= 1 && getCoordinateSequence().size() < MINIMUM_VALID_SIZE) {
 93       throw new IllegalArgumentException("Invalid number of points in LinearRing (found "
 94               + getCoordinateSequence().size() + " - must be 0 or >= 4)");
 95     }
 96   }
 97  
 98   /**
 99    * Returns <code>Dimension.FALSE</code>, since by definition LinearRings do
100    * not have a boundary.
101    *
102    * @return Dimension.FALSE
103    */
104   public int getBoundaryDimension() {
105     return Dimension.FALSE;
106   }
107  
108   /**
109    * Tests whether this ring is closed.
110    * Empty rings are closed by definition.
111    *
112    * @return true if this ring is closed
113    */
114   public boolean isClosed() {
115     if (isEmpty()) {
116         // empty LinearRings are closed by definition
117       return true;
118     }
119     return super.isClosed();
120   }
121  
122  
123   public String getGeometryType() {
124     return Geometry.TYPENAME_LINEARRING;
125   }
126   
127   protected int getTypeCode() {
128     return Geometry.TYPECODE_LINEARRING;
129   }
130  
131   protected LinearRing copyInternal() {
132     return new LinearRing(points.copy(), factory);
133   }
134  
135   public LinearRing reverse()
136   {
137     return (LinearRing) super.reverse();
138   }
139  
140   public LinearRing reverseInternal() {
141     CoordinateSequence seq = points.copy();
142     CoordinateSequences.reverse(seq);
143     return getFactory().createLinearRing(seq);
144   }
145 }
146