1

I am testing the intersection method of JTS / GeoTools

I intersected 2 lines. Then I test if the intersection point intersects with one of the lines. The result should be true. But it is false if I use coordinates with many digits. I get true if I use coordinates with no digits.

Could anyone explain this to me?

GeometryFactory gf = new GeometryFactory();

double x1 = 10.342211513452, y1 = 11.342211514232, x2 = 0.342211513926, y2 = 1.342211513111,
            x3 = 0.342211513898, y3 = 13.342211513101, x4 = 11.342211513921, y4 = 2.342211513878;

//double x1 = 10, y1 = 11, x2 = 0, y2 = 1, x3 = 0, y3 = 13, x4 = 11, y4 = 2;

LineString line1 = gf.createLineString(new Coordinate[] { new Coordinate(x1, y1), new Coordinate(x2, y2) });
LineString line2 = gf.createLineString(new Coordinate[] { new Coordinate(x3, y3), new Coordinate(x4, y4) });
Point intersectionPoint = (Point) line2.intersection(line1);

System.out.println(line1.intersects(intersectionPoint)); // true for no digits, false for many digits
System.out.println(line2.intersects(intersectionPoint)); // true for no digits, false for many digits
  • You asked about the precision model in another question. I suppose that this problem has to do with precision as well. – user30184 Jun 22 '16 at 15:36
1

The coordinates of the calculated intersection point are more accurate than the other coordinates. This is why the intersection result is not true. A workaround is to use a very small buffer:

GeometryFactory gf = new GeometryFactory();

double x1 = 10.342211513452, y1 = 11.342211514232, x2 = 0.342211513926, y2 = 1.342211513111,
            x3 = 0.342211513898, y3 = 13.342211513101, x4 = 11.342211513921, y4 = 2.342211513878;

LineString line1 = gf.createLineString(new Coordinate[] { new Coordinate(x1, y1), new Coordinate(x2, y2) });
LineString line2 = gf.createLineString(new Coordinate[] { new Coordinate(x3, y3), new Coordinate(x4, y4) });
Point intersectionPoint = (Point) line2.intersection(line1);

// define very small buffer
double bufferDistance = 0.0001 * 0.0001 * 0.0001;
// use buffer when looking for intersection
System.out.println(line1.buffer(bufferDistance).intersects(intersectionPoint)); // true
System.out.println(line2.buffer(bufferDistance).intersects(intersectionPoint)); // true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.