protected int getGeometryType(Geometry geometry) {
   WkbGeometryType type = WkbGeometryType.forClass(geometry.getClass());
   if (type == null)
     throw new UnsupportedConversionException(
         String.format(
             "Can't convert geometries of type %s", geometry.getClass().getCanonicalName()));
   return type.getTypeCode();
 }
예제 #2
0
 protected int getGeometryType(Geometry<P> geometry) {
   // empty geometries have the same representation as an empty geometry collection
   if (geometry.isEmpty()) {
     return WkbGeometryType.GEOMETRY_COLLECTION.getTypeCode();
   }
   WkbGeometryType type = WkbGeometryType.forClass(geometry.getClass());
   if (type == null) {
     throw new UnsupportedConversionException(
         String.format(
             "Can't convert geometries of type %s", geometry.getClass().getCanonicalName()));
   }
   return type.getTypeCode();
 }
예제 #3
0
  /**
   * Build an appropriate <code>Geometry</code>, <code>MultiGeometry</code>, or <code>
   * GeometryCollection</code> to contain the <code>Geometry</code>s in it. For example:<br>
   *
   * <ul>
   *   <li>If <code>geomList</code> contains a single <code>Polygon</code>, the <code>Polygon</code>
   *       is returned.
   *   <li>If <code>geomList</code> contains several <code>Polygon</code>s, a <code>MultiPolygon
   *       </code> is returned.
   *   <li>If <code>geomList</code> contains some <code>Polygon</code>s and some <code>LineString
   *       </code>s, a <code>GeometryCollection</code> is returned.
   *   <li>If <code>geomList</code> is empty, an empty <code>GeometryCollection</code> is returned
   * </ul>
   *
   * Note that this method does not "flatten" Geometries in the input, and hence if any
   * MultiGeometries are contained in the input a GeometryCollection containing them will be
   * returned.
   *
   * @param geomList the <code>Geometry</code>s to combine
   * @return a <code>Geometry</code> of the "smallest", "most type-specific" class that can contain
   *     the elements of <code>geomList</code> .
   */
  public Geometry buildGeometry(Collection geomList) {

    /** Determine some facts about the geometries in the list */
    Class geomClass = null;
    boolean isHeterogeneous = false;
    boolean hasGeometryCollection = false;
    for (Iterator i = geomList.iterator(); i.hasNext(); ) {
      Geometry geom = (Geometry) i.next();
      Class partClass = geom.getClass();
      if (geomClass == null) {
        geomClass = partClass;
      }
      if (partClass != geomClass) {
        isHeterogeneous = true;
      }
      if (geom instanceof GeometryCollection) hasGeometryCollection = true;
    }

    /** Now construct an appropriate geometry to return */
    // for the empty geometry, return an empty GeometryCollection
    if (geomClass == null) {
      return createGeometryCollection(null);
    }
    if (isHeterogeneous || hasGeometryCollection) {
      return createGeometryCollection(toGeometryArray(geomList));
    }
    // at this point we know the collection is hetereogenous.
    // Determine the type of the result from the first Geometry in the list
    // this should always return a geometry, since otherwise an empty collection would have already
    // been returned
    Geometry geom0 = (Geometry) geomList.iterator().next();
    boolean isCollection = geomList.size() > 1;
    if (isCollection) {
      if (geom0 instanceof Polygon) {
        return createMultiPolygon(toPolygonArray(geomList));
      } else if (geom0 instanceof LineString) {
        return createMultiLineString(toLineStringArray(geomList));
      } else if (geom0 instanceof Point) {
        return createMultiPoint(toPointArray(geomList));
      }
      Assert.shouldNeverReachHere("Unhandled class: " + geom0.getClass().getName());
    }
    return geom0;
  }
예제 #4
0
  public static String toWkt(GeometryCollection gc) {
    StringBuffer result;
    if (gc.isValid() == false) {
      result = new StringBuffer("GEOMETRYCOLLECTION EMPTY");
    } else {
      // 1st figure out if all the collection is the same type
      Geometry type = null;
      for (Geometry g : gc.getGeometries()) {
        if (type == null) {
          type = g;
        } else if (!type.getClass().isInstance(g)) {
          type = null;
          break;
        }
      }

      if (type == null) {
        result = new StringBuffer("GEOMETRYCOLLECTION EMPTY");
      } else {
        String strip = null;
        if (GeometryCollection.class.isInstance(type)) {
          result = new StringBuffer("GEOMETRYCOLLECTION(");
        } else if (Point.class.isInstance(type)) {
          result = new StringBuffer("MULTIPOINT(");
          strip = "POINT";
        } else if (LineString.class.isInstance(type)) {
          result = new StringBuffer("MULTILINESTRING(");
          strip = "LINESTRING";
        } else if (Polygon.class.isInstance(type)) {
          result = new StringBuffer("MULTIPOLYGON(");
          strip = "POLYGON";
        } else {
          result = new StringBuffer("GEOMETRYCOLLECTION EMPTY");
        }

        String sep = "";
        for (Geometry g : gc.getGeometries()) {
          result.append(sep);

          // strip the name if it a multi... type
          result.append(StringUtils.removeStartIgnoreCase(toWkt(g), strip));

          sep = ",";
        }
        result.append(")");
      }
    }

    return result.toString();
  }
예제 #5
0
  public final Geometry transform(Geometry inputGeom) {
    this.inputGeom = inputGeom;
    this.factory = inputGeom.getFactory();

    if (inputGeom instanceof Point) return transformPoint((Point) inputGeom, null);
    if (inputGeom instanceof MultiPoint) return transformMultiPoint((MultiPoint) inputGeom, null);
    if (inputGeom instanceof LinearRing) return transformLinearRing((LinearRing) inputGeom, null);
    if (inputGeom instanceof LineString) return transformLineString((LineString) inputGeom, null);
    if (inputGeom instanceof MultiLineString)
      return transformMultiLineString((MultiLineString) inputGeom, null);
    if (inputGeom instanceof Polygon) return transformPolygon((Polygon) inputGeom, null);
    if (inputGeom instanceof MultiPolygon)
      return transformMultiPolygon((MultiPolygon) inputGeom, null);
    if (inputGeom instanceof GeometryCollection)
      return transformGeometryCollection((GeometryCollection) inputGeom, null);

    throw new IllegalArgumentException(
        "Unknown Geometry subtype: " + inputGeom.getClass().getName());
  }
예제 #6
0
  private Geometry editInternal(Geometry geometry, GeometryEditorOperation operation) {
    // if client did not supply a GeometryFactory, use the one from the input Geometry
    if (factory == null) factory = geometry.getFactory();

    if (geometry instanceof GeometryCollection) {
      return editGeometryCollection((GeometryCollection) geometry, operation);
    }

    if (geometry instanceof Polygon) {
      return editPolygon((Polygon) geometry, operation);
    }

    if (geometry instanceof Point) {
      return operation.edit(geometry, factory);
    }

    if (geometry instanceof LineString) {
      return operation.edit(geometry, factory);
    }

    Assert.shouldNeverReachHere("Unsupported Geometry class: " + geometry.getClass().getName());
    return null;
  }