Point in Circle with JTS
Asked Answered
W

3

7

I have a huge set of points already loaded within a plane I need to draw a circle/ellipse starting from a given point and a radius distance in meters then check which points are inside the circle.

I've already done this with a polygon with the within() method, but I can't find a way to draw a circle/ellipse without having to specify every point around the polygon.

Is there a way to do this on JTS or do I need another java library?

Woodall answered 10/1, 2012 at 17:2 Comment(0)
E
6

If I understood correctly you have the radius and the center, so you can draw a circle with JTS like this:

public static Geometry createCircle(double x, double y, final double RADIUS) {
    GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
    shapeFactory.setNumPoints(32);
    shapeFactory.setCentre(new Coordinate(x, y));
    shapeFactory.setSize(RADIUS * 2);
    return shapeFactory.createCircle();
}
Exegetic answered 1/9, 2015 at 1:37 Comment(0)
U
2

You can just verify that the distance from the point is less than the radius. No need to draw the circle to know which points are inside it. For faster run times, compare the square of the distance with the square of the radius; this saves unnecessary square root operations.

For ellipses, the problem is only slightly harder, involving a quadratic form x^2 + k y^2.

Unseemly answered 20/11, 2012 at 6:52 Comment(1)
why shapeFactory.setNumPoints(32);? how did you come up with 32?Cyton
N
1

You can simply buffer the circle center with a positive value like so:

Point centerPoint = ...;
Polygon circle = (Polygon) centerPoint.buffer(0.1);
Nolasco answered 11/11, 2018 at 16:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.