Example #1
0
  private FeatureCollection convexHhull(TaskMonitor monitor, FeatureCollection fc) {
    monitor.allowCancellationRequests();
    monitor.report(I18N.get("ui.plugin.analysis.ConvexHullPlugIn.Computing-Convex-Hull") + "...");

    int size = fc.size();
    GeometryFactory geomFact = null;

    if (size == 0) {
      return null;
    }
    int count = 0;
    Geometry[] geoms = new Geometry[size];

    for (Iterator i = fc.iterator(); i.hasNext(); ) {
      Feature f = (Feature) i.next();
      Geometry geom = f.getGeometry();
      if (geom == null) {
        continue;
      }
      if (geomFact == null) {
        geomFact = geom.getFactory();
      }

      geoms[count++] = geom;
    }
    GeometryCollection gc = geomFact.createGeometryCollection(geoms);
    Geometry hull = gc.convexHull();
    List hullList = new ArrayList();
    hullList.add(hull);

    return FeatureDatasetFactory.createFromGeometry(hullList);
  }
 @Override
 public void visit(GeometryCollection geom) {
   writeByteOrder(output);
   DimensionalFlag dimension = DimensionalFlag.valueOf(geom.is3D(), geom.isMeasured());
   writeTypeCodeAndSrid(geom, dimension, output);
   output.putUInt(geom.getNumGeometries());
 }
Example #3
0
 protected Geometry transformGeometryCollection(GeometryCollection geom, Geometry parent) {
   List transGeomList = new ArrayList();
   for (int i = 0; i < geom.getNumGeometries(); i++) {
     Geometry transformGeom = transform(geom.getGeometryN(i));
     if (transformGeom == null) continue;
     if (pruneEmptyGeometry && transformGeom.isEmpty()) continue;
     transGeomList.add(transformGeom);
   }
   if (preserveGeometryCollectionType)
     return factory.createGeometryCollection(GeometryFactory.toGeometryArray(transGeomList));
   return factory.buildGeometry(transGeomList);
 }
Example #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();
  }
Example #5
0
  private GeometryCollection editGeometryCollection(
      GeometryCollection collection, GeometryEditorOperation operation) {
    // first edit the entire collection
    // MD - not sure why this is done - could just check original collection?
    GeometryCollection collectionForType = (GeometryCollection) operation.edit(collection, factory);

    // edit the component geometries
    ArrayList geometries = new ArrayList();
    for (int i = 0; i < collectionForType.getNumGeometries(); i++) {
      Geometry geometry = edit(collectionForType.getGeometryN(i), operation);
      if (geometry == null || geometry.isEmpty()) {
        continue;
      }
      geometries.add(geometry);
    }

    if (collectionForType.getClass() == MultiPoint.class) {
      return factory.createMultiPoint((Point[]) geometries.toArray(new Point[] {}));
    }
    if (collectionForType.getClass() == MultiLineString.class) {
      return factory.createMultiLineString((LineString[]) geometries.toArray(new LineString[] {}));
    }
    if (collectionForType.getClass() == MultiPolygon.class) {
      return factory.createMultiPolygon((Polygon[]) geometries.toArray(new Polygon[] {}));
    }
    return factory.createGeometryCollection((Geometry[]) geometries.toArray(new Geometry[] {}));
  }
Example #6
0
 @Override
 public <G extends Geometry<P>> void visit(GeometryCollection<P, G> geom) {
   writeByteOrder(output);
   writeTypeCodeAndSrid(geom, output);
   output.putUInt(geom.getNumGeometries());
 }
Example #7
0
  /**
   * Main method - write the featurecollection to a shapefile (2d, 3d or 4d).
   *
   * @param featureCollection collection to write
   * @param dp 'OutputFile' or 'DefaultValue' to specify where to write, and 'ShapeType' to specify
   *     dimentionality.
   */
  public void write(FeatureCollection featureCollection, DriverProperties dp)
      throws IllegalParametersException, Exception {
    String shpfileName;
    String dbffname;
    String shxfname;

    String path;
    String fname;
    String fname_withoutextention;
    int shapeType;
    int loc;

    GeometryCollection gc;

    shpfileName = dp.getProperty(FILE_PROPERTY_KEY);

    if (shpfileName == null) {
      shpfileName = dp.getProperty(DEFAULT_VALUE_PROPERTY_KEY);
    }

    if (shpfileName == null) {
      throw new IllegalParametersException("no output filename specified");
    }

    loc = shpfileName.lastIndexOf(File.separatorChar);

    if (loc == -1) {
      // loc = 0; // no path - ie. "hills.shp"
      // path = "";
      // fname = shpfileName;
      // probably using the wrong path separator character.
      throw new Exception(
          "couldn't find the path separator character '"
              + File.separatorChar
              + "' in your shape file name. This you're probably using the unix (or dos) one.");
    } else {
      path = shpfileName.substring(0, loc + 1); // ie. "/data1/hills.shp" -> "/data1/"
      fname = shpfileName.substring(loc + 1); // ie. "/data1/hills.shp" -> "hills.shp"
    }

    loc = fname.lastIndexOf(".");

    if (loc == -1) {
      throw new IllegalParametersException("Filename must end in '.shp'");
    }

    fname_withoutextention = fname.substring(0, loc); // ie. "hills.shp" -> "hills."
    dbffname = path + fname_withoutextention + ".dbf";

    writeDbf(featureCollection, dbffname);

    // this gc will be a collection of either multi-points, multi-polygons, or multi-linestrings
    // polygons will have the rings in the correct order
    gc = makeSHAPEGeometryCollection(featureCollection);

    shapeType = 2; // x,y

    if (dp.getProperty(SHAPE_TYPE_PROPERTY_KEY) != null) {
      String st = dp.getProperty(SHAPE_TYPE_PROPERTY_KEY);

      if (st.equalsIgnoreCase("xy")) {
        shapeType = 2;
      } else if (st.equalsIgnoreCase("xym")) {
        shapeType = 3;
      } else if (st.equalsIgnoreCase("xymz")) {
        shapeType = 4;
      } else if (st.equalsIgnoreCase("xyzm")) {
        shapeType = 4;
      } else if (st.equalsIgnoreCase("xyz")) {
        shapeType = 4;
      } else {
        throw new IllegalParametersException(
            "ShapefileWriter.write() - dataproperties has a 'ShapeType' that isnt 'xy', 'xym', or 'xymz'");
      }
    } else {
      if (gc.getNumGeometries() > 0) {
        shapeType = guessCoorinateDims(gc.getGeometryN(0));
      }
    }

    URL url = new URL("file", "localhost", shpfileName);
    Shapefile myshape = new Shapefile(url);
    myshape.write(gc, shapeType);

    shxfname = path + fname_withoutextention + ".shx";

    BufferedOutputStream in = new BufferedOutputStream(new FileOutputStream(shxfname));
    EndianDataOutputStream sfile = new EndianDataOutputStream(in);

    myshape.writeIndex(gc, sfile, shapeType);
  }