private static GeometryCollection geometryCollectionFromVertices(Graph graph) { GeometryFactory gf = GeometryUtils.getGeometryFactory(); Collection<Vertex> vertices = graph.getVertices(); Geometry[] points = new Geometry[vertices.size()]; int i = 0; for (Vertex v : vertices) { points[i++] = gf.createPoint(v.getCoordinate()); } GeometryCollection geometries = new GeometryCollection(points, gf); return geometries; }
/** * Transform into GeometryCollection. * * @param geom * input geometry * @return * a geometry collection */ private static GeometryCollection transformIntoPointGeometryCollection(Geometry geom) { UniqueCoordinateArrayFilter filter = new UniqueCoordinateArrayFilter(); geom.apply(filter); Coordinate[] coord = filter.getCoordinates(); Geometry[] geometries = new Geometry[coord.length]; for (int i = 0 ; i < coord.length ; i++) { Coordinate[] c = new Coordinate[] { coord[i] }; CoordinateArraySequence cs = new CoordinateArraySequence(c); geometries[i] = new Point(cs, geom.getFactory()); } return new GeometryCollection(geometries, geom.getFactory()); }
/** * Transform into GeometryCollection. * * @param geom * input geometry * @return * a geometry collection */ private static GeometryCollection transformIntoPointGeometryCollection(GeometryCollection gc) { UniqueCoordinateArrayFilter filter = new UniqueCoordinateArrayFilter(); gc.apply(filter); Coordinate[] coord = filter.getCoordinates(); Geometry[] geometries = new Geometry[coord.length]; for (int i = 0 ; i < coord.length ; i++) { Coordinate[] c = new Coordinate[] { coord[i] }; CoordinateArraySequence cs = new CoordinateArraySequence(c); geometries[i] = new Point(cs, gc.getFactory()); } return new GeometryCollection(geometries, gc.getFactory()); }
/** * Creates a GeometryCollection using the given Geometries; a null or empty * array will create an empty GeometryCollection. * @param geometries Geometries, each of which may be empty but not null */ public GeometryCollection createGeometryCollection(Geometry[] geometries) { return new GeometryCollection(geometries, this); }
/** * Creates a GeometryCollection using the given Geometries; a null or empty * array will create an empty GeometryCollection. * * @param geometries an array of Geometries, each of which may be empty but not null, or null * @return the created GeometryCollection */ public GeometryCollection createGeometryCollection(Geometry[] geometries) { return new GeometryCollection(geometries, this); }
protected static Geometry findIntersectedGeoms(Geometry srcGeom, List<Geometry> geometries) { List<Geometry> intersected = new ArrayList<>(); // find all geometries which intersect with with srcGeom Iterator<Geometry> geomIter = geometries.iterator(); while (geomIter.hasNext()) { Geometry geom = geomIter.next(); if (geom.intersects(srcGeom)) { geomIter.remove(); intersected.add(geom); } } // compute the envelope for the intersected geometries and the source geometry, and look for more intersections if (!intersected.isEmpty()) { intersected.add(srcGeom); Geometry mergedGeom = new GeometryCollection(intersected.toArray(new Geometry[intersected.size()]), new GeometryFactory()).getEnvelope(); return findIntersectedGeoms(mergedGeom, geometries); } return srcGeom; }
/** Creates a point where there is black color.*/ public static Geometry createGeometry(BufferedImage image) { ArrayList<Point> points = new ArrayList<Point>(128 * 128); image = ImageUtilities.rotateImage(image, FastMath.PI); for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { Color color = ImageUtilities.getColor(image, i, j, true); boolean opaque = (color.getAlpha() > BLACK_COLOR_THRESHOLD); boolean black = ((color.getRed() + color.getGreen() + color.getBlue()) / 3 < BLACK_COLOR_THRESHOLD); if (opaque && black) { points.add(createPoint(new Coordinate(i, j))); } } } GeometryCollection g = new GeometryCollection(points.toArray(new Point[points.size()]), GEOMETRY_FACTORY); return g; }
/** Creates a point where there is black color.*/ public static Geometry createGeometry(BufferedImage image) { ArrayList<Point> points = new ArrayList<Point>(128 * 128); image = ImageUtilities.rotateImage(image, FastMath.PI); for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { Color color = ImageUtilities.getColor(image, i, j, true); boolean opaque = (color.getAlpha() > BLACK_COLOR_THRESHOLD); boolean black = ((color.getRed() + color.getGreen() + color.getBlue()) / 3 < BLACK_COLOR_THRESHOLD); if (opaque && black) { points.add(createPoint(new Coordinate(i, j))); } } } GeometryCollection g = new GeometryCollection(points.toArray(new Point[points.size()]), GEOMETRY_FACTORY); return g; }
@Override public Point getCentroid() { Geometry[] geometries = new Geometry[shapes.size()]; int counter = 0; for (Shape shape : shapes) { geometries[counter] = shape.getGeometry(); counter++; } GeometryCollection collection = new GeometryCollection(geometries, geometries[0].getFactory()); return collection.getCentroid(); }
@Override public Geometry getBoundingBox() { Geometry[] geometries = new Geometry[shapes.size()]; int counter = 0; for (Shape shape : shapes) { geometries[counter] = shape.getGeometry(); counter++; } GeometryCollection collection = new GeometryCollection(geometries, geometries[0].getFactory()); return collection.getEnvelope(); }
@Override public Point getCentroid() { Geometry[] geometries = new Geometry[shapes.size()]; int counter = 0; for (Shape shape : shapes) { geometries[counter] = shape.getGeometry(); counter++; } GeometryCollection collection = new GeometryCollection(geometries, geometries[0].getFactory()); return collection.getCentroid(); }
@Override public Geometry getBoundingBox() { Geometry[] geometries = new Geometry[shapes.size()]; int counter = 0; for (Shape shape : shapes) { geometries[counter] = shape.getGeometry(); counter++; } GeometryCollection collection = new GeometryCollection(geometries, geometries[0].getFactory()); return collection.getEnvelope(); }
/** * Implementation of the concave hull algorithm. Every iteration, starting * with the zero-th iteration, will be written to file. Both the triangles, * and the border will be written to file. * @param args two arguments required, and in the following order: * <ol> * <li> <b>coordinate file</b> the absolute path of the coordinates to be * be read in. It is assumed that the first column of the CSV file * will be the longitude, and the second column the latitude. Also, * the coordinate file is assumed to have a header line. * <li> <b>threshold</b> the edge length threshold that will be used for * the concave hull algorithm. */ public static void main(String[] args) { /* Read in the coordinates from file. */ List<Coordinate> coordinates = ConcaveHull.getClusterCoords(args[0]); /* Convert the coordinates to a GeometryCollection. */ GeometryFactory gf = new GeometryFactory(); Geometry[] points = new Geometry[coordinates.size()]; for(int i = 0; i < coordinates.size(); i++){ points[i] = gf.createPoint(coordinates.get(i)); } GeometryCollection gc = new GeometryCollection(points, gf); /* Instantiate and run the concave hull algorithm. */ ConcaveHull ch = new ConcaveHull(gc, Double.parseDouble(args[1]), true); Geometry g = ch.getConcaveHull(); }
public Object evaluate(Object object) { List geometries = getParameters().get(0).evaluate(object, List.class); if (geometries == null || geometries.size() == 0) { return new GeometryCollection(null, new GeometryFactory()); } // collect but don't clone, unfortunately we're already stuck with a list, by cloning // we'd just increase memory usage GeometryCollector collector = new GeometryCollector(); collector.setFactory(null); collector.setMaxCoordinates(maxCoordinates); for (Iterator it = geometries.iterator(); it.hasNext();) { Geometry geometry = (Geometry) it.next(); collector.add(geometry); } return collector.collect(); }
/** * Calculates the enveloper of the specified treatmentZones as a whole * * @param treatmentZones TreatmentZone to calculate envelope from * @return The envelope as a Geometry */ public static Geometry getEnvelope(Collection<TreatmentZone> treatmentZones) { // Determine total envelope Geometry[] geometries = new Geometry[treatmentZones.size()]; int counter = 0; for (TreatmentZone treatmentZone : treatmentZones) { geometries[counter] = treatmentZone.getGeometry(); counter++; } GeometryCollection collection = new GeometryCollection(geometries, geometries[0].getFactory()); return collection.getEnvelope(); }
/** * Creates a similar {@link GeometryCollection} as in setup() but have * triplicates of points 7 & 8. * @return */ private GeometryCollection setupWithDuplicates(){ GeometryFactory gf = new GeometryFactory(); Geometry[] ga = new Geometry[12]; ga[0] = gf.createPoint(new Coordinate(0, 0)); // 1 ga[1] = gf.createPoint(new Coordinate(4, 0)); // 2 ga[2] = gf.createPoint(new Coordinate(0, 4)); // 3 ga[3] = gf.createPoint(new Coordinate(4, 4)); // 4 ga[4] = gf.createPoint(new Coordinate(2, 1)); // 5 ga[5] = gf.createPoint(new Coordinate(3, 2)); // 6 ga[6] = gf.createPoint(new Coordinate(2, 3)); // 7 ga[7] = gf.createPoint(new Coordinate(1, 2)); // 8 ga[8] = gf.createPoint(new Coordinate(2, 3)); // 7 ga[9] = gf.createPoint(new Coordinate(2, 3)); // 7 ga[10] = gf.createPoint(new Coordinate(1, 2)); // 8 ga[11] = gf.createPoint(new Coordinate(1, 2)); // 8 return new GeometryCollection(ga, gf); }
/** * Set up a small test case: * * ^ * | * 3 4 * | 7 * | 8 6 * | 5 * 1_________2___> * */ private GeometryCollection setup(){ GeometryFactory gf = new GeometryFactory(); Geometry[] ga = new Geometry[8]; ga[0] = gf.createPoint(new Coordinate(0, 0)); ga[1] = gf.createPoint(new Coordinate(4, 0)); ga[2] = gf.createPoint(new Coordinate(0, 4)); ga[3] = gf.createPoint(new Coordinate(4, 4)); ga[4] = gf.createPoint(new Coordinate(2, 1)); ga[5] = gf.createPoint(new Coordinate(3, 2)); ga[6] = gf.createPoint(new Coordinate(2, 3)); ga[7] = gf.createPoint(new Coordinate(1, 2)); return new GeometryCollection(ga, gf); }
/** * Convert MultiPolygons to Geometry collections as MySQL does not seem to like them. */ private Geometry fixUpGeometry(Geometry geometry) { if(geometry instanceof MultiPolygon) { MultiPolygon multiPolygon = (MultiPolygon) geometry; if(multiPolygon.getNumGeometries() == 1) { return multiPolygon.getGeometryN(0); } else { Geometry[] polygons = new Geometry[multiPolygon.getNumGeometries()]; for (int i = 0; i < polygons.length; i++) { polygons[i] = multiPolygon.getGeometryN(i); } return new GeometryCollection(polygons, multiPolygon.getFactory()); } } return geometry; }
GeometryCollection geoCollection = new GeometryCollection(g, g[0].getFactory());
GeometryCollection geoCollection = new GeometryCollection(g, g[0].getFactory());