Class MultiPolygon

  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 java.util.ArrayList;
 17  
 18 /**
 19  * Models a collection of {@link Polygon}s.
 20  * <p>
 21  * As per the OGC SFS specification,
 22  * the Polygons in a MultiPolygon may not overlap,
 23  * and may only touch at single points.
 24  * This allows the topological point-set semantics
 25  * to be well-defined.
 26  *
 27  *
 28  *@version 1.7
 29  */
 30 public class MultiPolygon
 31     extends GeometryCollection
 32     implements Polygonal
 33 {
 34   private static final long serialVersionUID = -551033529766975875L;
 35   /**
 36    *  Constructs a <code>MultiPolygon</code>.
 37    *
 38    *@param  polygons        the <code>Polygon</code>s for this <code>MultiPolygon</code>
 39    *      , or <code>null</code> or an empty array to create the empty geometry.
 40    *      Elements may be empty <code>Polygon</code>s, but not <code>null</code>
 41    *      s. The polygons must conform to the assertions specified in the <A
 42    *      HREF="http://www.opengis.org/techno/specs.htm">OpenGIS Simple Features
 43    *      Specification for SQL</A> .
 44    *@param  precisionModel  the specification of the grid of allowable points
 45    *      for this <code>MultiPolygon</code>
 46    *@param  SRID            the ID of the Spatial Reference System used by this
 47    *      <code>MultiPolygon</code>
 48    * @deprecated Use GeometryFactory instead
 49    */
 50   public MultiPolygon(Polygon[] polygons, PrecisionModel precisionModel, int SRID) {
 51     this(polygons, new GeometryFactory(precisionModel, SRID));
 52   }
 53  
 54  
 55   /**
 56    * @param polygons
 57    *            the <code>Polygon</code>s for this <code>MultiPolygon</code>,
 58    *            or <code>null</code> or an empty array to create the empty
 59    *            geometry. Elements may be empty <code>Polygon</code>s, but
 60    *            not <code>null</code>s. The polygons must conform to the
 61    *            assertions specified in the <A
 62    *            HREF="http://www.opengis.org/techno/specs.htm">OpenGIS Simple
 63    *            Features Specification for SQL</A>.
 64    */
 65   public MultiPolygon(Polygon[] polygons, GeometryFactory factory) {
 66     super(polygons, factory);
 67   }
 68  
 69   public int getDimension() {
 70     return 2;
 71   }
 72  
 73   public int getBoundaryDimension() {
 74     return 1;
 75   }
 76  
 77   public String getGeometryType() {
 78     return Geometry.TYPENAME_MULTIPOLYGON;
 79   }
 80  
 81   /*
 82   public boolean isSimple() {
 83     return true;
 84   }
 85 */
 86  
 87   /**
 88    * Computes the boundary of this geometry
 89    *
 90    * @return a lineal geometry (which may be empty)
 91    * @see Geometry#getBoundary
 92    */
 93   public Geometry getBoundary() {
 94     if (isEmpty()) {
 95       return getFactory().createMultiLineString();
 96     }
 97     ArrayList allRings = new ArrayList();
 98     for (int i = 0; i < geometries.length; i++) {
 99       Polygon polygon = (Polygon) geometries[i];
100       Geometry rings = polygon.getBoundary();
101       for (int j = 0; j < rings.getNumGeometries(); j++) {
102         allRings.add(rings.getGeometryN(j));
103       }
104     }
105     LineString[] allRingsArray = new LineString[allRings.size()];
106     return getFactory().createMultiLineString((LineString[]) allRings.toArray(allRingsArray));
107   }
108  
109   public boolean equalsExact(Geometry other, double tolerance) {
110     if (!isEquivalentClass(other)) {
111       return false;
112     }
113     return super.equalsExact(other, tolerance);
114   }
115  
116   /**
117    * Creates a {@link MultiPolygon} with
118    * every component reversed.
119    * The order of the components in the collection are not reversed.
120    *
121    * @return a MultiPolygon in the reverse order
122    */
123   public MultiPolygon reverse() {
124     return (MultiPolygon) super.reverse();
125   }
126  
127   protected MultiPolygon copyInternal() {
128     Polygon[] polygons = new Polygon[this.geometries.length];
129     for (int i = 0; i < polygons.length; i++) {
130       polygons[i] = (Polygon) this.geometries[i].copy();
131     }
132     return new MultiPolygon(polygons, factory);
133   }
134  
135   protected int getTypeCode() {
136     return Geometry.TYPECODE_MULTIPOLYGON;
137   }
138 }
139  
140  
141