Esempio n. 1
0
  /**
   * return a single geometry collection <br>
   * result.GeometryN(i) = the i-th feature in the FeatureCollection<br>
   * All the geometry types will be the same type (ie. all polygons) - or they will be set to<br>
   * NULL geometries<br>
   * <br>
   * GeometryN(i) = {Multipoint,Multilinestring, or Multipolygon)<br>
   *
   * @param fc feature collection to make homogeneous
   */
  public GeometryCollection makeSHAPEGeometryCollection(FeatureCollection fc) throws Exception {
    GeometryCollection result;
    Geometry[] allGeoms = new Geometry[fc.size()];

    int geomtype = findBestGeometryType(fc);

    if (geomtype == 0) {
      throw new Exception(
          "Could not determine shapefile type - data is either all GeometryCollections or empty");
    }

    List features = fc.getFeatures();

    for (int t = 0; t < features.size(); t++) {
      Geometry geom;
      geom = ((Feature) features.get(t)).getGeometry();

      switch (geomtype) {
        case 1: // point
          if ((geom instanceof Point)) {
            // good!
            Point[] p = new Point[1];
            p[0] = (Point) geom;

            allGeoms[t] = new MultiPoint(p, new PrecisionModel(), 0);
          } else if (geom instanceof MultiPoint) {
            allGeoms[t] = geom;
          } else {
            allGeoms[t] = new MultiPoint(null, new PrecisionModel(), 0);
          }

          break;

        case 2: // line
          if ((geom instanceof LineString)) {
            LineString[] l = new LineString[1];
            l[0] = (LineString) geom;

            allGeoms[t] = new MultiLineString(l, new PrecisionModel(), 0);
          } else if (geom instanceof MultiLineString) {
            allGeoms[t] = geom;
          } else {
            allGeoms[t] = new MultiLineString(null, new PrecisionModel(), 0);
          }

          break;

        case 3: // polygon
          if (geom instanceof Polygon) {
            // good!
            Polygon[] p = new Polygon[1];
            p[0] = (Polygon) geom;

            allGeoms[t] = makeGoodSHAPEMultiPolygon(new MultiPolygon(p, new PrecisionModel(), 0));
          } else if (geom instanceof MultiPolygon) {
            allGeoms[t] = makeGoodSHAPEMultiPolygon((MultiPolygon) geom);
          } else {
            allGeoms[t] = new MultiPolygon(null, new PrecisionModel(), 0);
          }

          break;
      }
    }

    result = new GeometryCollection(allGeoms, new PrecisionModel(), 0);

    return result;
  }