if (CGAlgorithms.signedArea(ring.getCoordinateSequence()) > 0.0) holes.add(ring); else
public static Geometry minimumCircle(Geometry g) { if (g == null) return null; MinimumBoundingCircle circle = new MinimumBoundingCircle(g); return circle.getCircle(); }
public static Geometry minimumRectangle(Geometry g) { if (g == null) return null; MinimumDiameter min = new MinimumDiameter(g); return min.getMinimumRectangle(); }
Coordinate c3 = new Coordinate(); Coordinate c4 = new Coordinate(); LineIntersector intersector = new RobustLineIntersector(); c1.x = data[0]; c1.y = data[1]; intersector.computeIntersection(c1, c2, c3, c4); if (intersector.hasIntersection()) { Coordinate intersection = intersector.getIntersection(0); c2.x = intersection.x; c2.y = intersection.y; } else if (j == max - 2 && !closed) { double distancePointLine = CGAlgorithms.distancePointLine(c4, c1, c2); if (distancePointLine < abs(offset) / 10) { c2.x = c4.x;
public static Geometry minimumDiameter(Geometry arg0) { if (arg0 == null) return null; MinimumDiameter minDiameter = new MinimumDiameter(arg0); return minDiameter.getDiameter(); }
/** * Ensure Ring of Coordinates are in a counter Orientationwise order. * * <p>If the Coordinate need to be reversed a copy will be returned. * * @param factory Factory to used to reverse CoordinateSequence * @param ring Ring of Coordinates * @return coords in a CCW order */ public static CoordinateSequence counterClockWise( CoordinateSequenceFactory factory, CoordinateSequence ring) { if (Orientation.isCCW(ring.toCoordinateArray())) { return ring; } return Coordinates.reverse(factory, ring); }
/** * Create a nice Polygon from the given Polygon. Will ensure that shells are clockwise and holes * are counter-clockwise. Capiche? * * @param p The Polygon to make "nice". * @return The "nice" Polygon. */ public static final Polygon makeGoodShapePolygon(Polygon p) { GeometryFactory factory = p.getFactory(); LinearRing outer; LinearRing[] holes = new LinearRing[p.getNumInteriorRing()]; Coordinate[] coords; coords = p.getExteriorRing().getCoordinates(); if (CGAlgorithms.isCCW(coords)) { outer = reverseRing((LinearRing) p.getExteriorRing()); } else { outer = (LinearRing) p.getExteriorRing(); } for (int t = 0, tt = p.getNumInteriorRing(); t < tt; t++) { coords = p.getInteriorRingN(t).getCoordinates(); if (!(CGAlgorithms.isCCW(coords))) { holes[t] = reverseRing((LinearRing) p.getInteriorRingN(t)); } else { holes[t] = (LinearRing) p.getInteriorRingN(t); } } return factory.createPolygon(outer, holes); }
public static int computeOrientation(CoordinateSequence cs, int p1, int p2, int q) { // travelling along p1->p2, turn counter clockwise to get to q return 1, // travelling along p1->p2, turn clockwise to get to q return -1, // p1, p2 and q are colinear return 0. double p1x = cs.getOrdinate(p1, 0); double p1y = cs.getOrdinate(p1, 1); double p2x = cs.getOrdinate(p2, 0); double p2y = cs.getOrdinate(p2, 1); double qx = cs.getOrdinate(q, 0); double qy = cs.getOrdinate(q, 1); double dx1 = p2x - p1x; double dy1 = p2y - p1y; double dx2 = qx - p2x; double dy2 = qy - p2y; return RobustDeterminant.signOfDet2x2(dx1, dy1, dx2, dy2); }
&& (CGAlgorithms.isPointInRing(testPt, coordList) || (pointInList(testPt, coordList)))) { isContained = true;
private int getOrientationIndex(LineString ls) { return CGAlgorithms.orientationIndex( ls.getCoordinateN(0), ls.getCoordinateN(1), ls.getCoordinateN(2)); }
CGAlgorithms.computeOrientation(firstCoord, midCoord, lastCoord);
private Geometry getConflictBounds(Geometry bounds, int spaceAround) { // apply the space around (with a negative one we might end up with nothing as the result) Geometry conflictBounds = bounds; if (spaceAround != 0) { conflictBounds = bounds.buffer(spaceAround); if (conflictBounds.isEmpty() || conflictBounds.getArea() == 0) { conflictBounds = null; } else { conflictBounds = new MinimumDiameter(conflictBounds).getMinimumRectangle(); } } return conflictBounds; }
/** Test of evaluate method, of class FilterFunction_minimumDiameter. */ @Test public void testEvaluate() throws Exception { SimpleFeatureCollection featureCollection = FunctionTestFixture.polygons(); // Test the Function FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints()); Function exp = ff.function("minimumdiameter", ff.property("geom")); SimpleFeatureIterator iter = featureCollection.features(); while (iter.hasNext()) { SimpleFeature feature = iter.next(); Geometry geom = (Geometry) feature.getDefaultGeometry(); Geometry minimumDiameter = new MinimumDiameter(geom).getDiameter(); Object value = exp.evaluate(feature); assertTrue(value instanceof LineString); assertTrue(minimumDiameter.equalsExact((Geometry) value, 0.1)); } iter.close(); // Check for null safeness assertNull(exp.evaluate(null)); } }
/** Test of evaluate method, of class FilterFunction_minimumCircle. */ @Test public void testEvaluate() throws Exception { SimpleFeatureCollection featureCollection = FunctionTestFixture.polygons(); // Test the Function FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints()); Function exp = ff.function("mincircle", ff.property("geom")); SimpleFeatureIterator iter = featureCollection.features(); while (iter.hasNext()) { SimpleFeature feature = iter.next(); Geometry geom = (Geometry) feature.getDefaultGeometry(); Geometry circle = new MinimumBoundingCircle(geom).getCircle(); Object value = exp.evaluate(feature); assertTrue(value instanceof Polygon); assertTrue(circle.equals((Geometry) value)); } iter.close(); // Check for null safeness assertNull(exp.evaluate(null)); } }
/** * Ensure Ring of Coordinates are in a Orientationwise order. * * <p>If the Coordinate need to be reversed a copy will be returned. * * @param factory Factory used to reverse CoordinateSequence * @param ring Ring of Coordinates * @return coords in a CW order */ private static CoordinateSequence clockWise( CoordinateSequenceFactory factory, CoordinateSequence ring) { if (!Orientation.isCCW(ring.toCoordinateArray())) { return ring; } return Coordinates.reverse(factory, ring); }
List<?> toList(Polygon p) { BasicDBList l = new BasicDBList(); if (!CGAlgorithms.isCCW(p.getExteriorRing().getCoordinates())) { l.add(toList(p.getExteriorRing().reverse().getCoordinates())); } else { l.add(toList(p.getExteriorRing().getCoordinateSequence())); } for (int i = 0; i < p.getNumInteriorRing(); i++) { l.add(toList(p.getInteriorRingN(i).getCoordinateSequence())); } return l; } }
double calcPolygonAlignMBRAngle(Geometry geometry) { // use JTS MinimumDiameter class to calc MBR Geometry mbr = new MinimumDiameter(geometry).getMinimumRectangle(); // calc angle from the longest side of the MBR Coordinate[] coordinates = mbr.getCoordinates(); double dx, dy; if (coordinates[0].distance(coordinates[1]) > coordinates[1].distance(coordinates[2])) { dx = coordinates[1].x - coordinates[0].x; dy = coordinates[1].y - coordinates[0].y; } else { dx = coordinates[2].x - coordinates[1].x; dy = coordinates[2].y - coordinates[1].y; } double angle = Math.atan(dy / dx); // make sure we turn PI/2 into -PI/2, we don't want some labels looking straight up // and some others straight down, when almost vertical they should all be oriented // on the same side if (Math.abs(angle - Math.PI / 2) < Math.PI / 180.0) { angle = -Math.PI / 2 + Math.abs(angle - Math.PI / 2); } return angle; } }
/** * INTERNAL: Returns a Rectangle of the JTS {@link Envelope} (bounding box) of the given {@code geom}. This asserts * that {@link Geometry#isRectangle()} is true. This method reacts to the {@link DatelineRule} setting. * @param geom non-null * @return the equivalent Rectangle. */ public Rectangle makeRectFromRectangularPoly(Geometry geom) { // TODO although, might want to never convert if there's a semantic difference (e.g. // geodetically)? Should have a setting for that. assert geom.isRectangle(); Envelope env = geom.getEnvelopeInternal(); boolean crossesDateline = false; if (ctx.isGeo() && getDatelineRule() != DatelineRule.none) { if (getDatelineRule() == DatelineRule.ccwRect) { // If JTS says it is clockwise, then it's actually a dateline crossing rectangle. crossesDateline = !CGAlgorithms.isCCW(geom.getCoordinates()); } else { crossesDateline = env.getWidth() > 180; } } if (crossesDateline) return rect(env.getMaxX(), env.getMinX(), env.getMinY(), env.getMaxY()); else return rect(env.getMinX(), env.getMaxX(), env.getMinY(), env.getMaxY()); }
private Geometry getGeometryBounds( Icon icon, Mark mark, Shape shape, double markSize, Object feature) { Geometry bounds; if (icon != null) { bounds = new GeometryBuilder().box(0, 0, icon.getIconWidth(), icon.getIconHeight()); } else { // the shape can be very complicated, go for the MBR. Wanted to use ShapeReader, but it // blindly assumes the shape is a polygon, while it may not be. Building a multipoint // instead AffineTransform at = AffineTransform.getScaleInstance(markSize, -markSize); Shape ts = at.createTransformedShape(shape); Geometry shapeGeometry = JTS.toGeometry(ts); bounds = new MinimumDiameter(shapeGeometry).getMinimumRectangle(); } // grow by the stroke size, if this is a mark if (icon == null && mark != null) { Stroke stroke = factory.getStroke(mark.getStroke(), feature); if (stroke instanceof BasicStroke) { float width = ((BasicStroke) stroke).getLineWidth() / 2 + 1; if (width > 0) { Geometry buffered = bounds.buffer(width); bounds = new MinimumDiameter(buffered).getMinimumRectangle(); } } } return bounds; }
/** Test of evaluate method, of class FilterFunction_minimumCircle. */ @Test public void testEvaluate() throws Exception { SimpleFeatureCollection featureCollection = FunctionTestFixture.polygons(); // Test the Function FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(GeoTools.getDefaultHints()); Function exp = ff.function("minrectangle", ff.property("geom")); SimpleFeatureIterator iter = featureCollection.features(); while (iter.hasNext()) { SimpleFeature feature = iter.next(); Geometry geom = (Geometry) feature.getDefaultGeometry(); Geometry rectangle = new MinimumDiameter(geom).getMinimumRectangle(); Object value = exp.evaluate(feature); assertTrue(value instanceof Polygon); assertTrue(rectangle.equals((Geometry) value)); } iter.close(); // Check for null safeness assertNull(exp.evaluate(null)); } }