I'm trying to check if two polygons are next to each other and touch eachother. For that purpose I use the method ".intersects()" from the JTS library but it returns false even though visually the two polygons touch eachother. Here is my code that I test the two polygons. (Keep in mind this is only for testing purposes and the actual code will be rewritten.)
String testCoords1 = "M149.9 373.5l-3-3.4-9.4-7.2-5-2-0.7-7.6-1.1-4.1-1.7-3.6-3.3-1.7-2.8-2.3-3.8-3.8-4.2-2.8 5-6.3 6.7-0.7 3.3 2.3 3.8 0.6 2.8-1.6 2.4-2.9 0.4-4.3-2-3.9 7.8-6.5 0.2-10 3.8-7.6 2.5 1 2 2.4 8 5.3 7.7 2.2 8-3.4 7.3-1.6 6.2 5.7 6 0.4 5.7-4-2 5.7-3.3 6.2-8.6 19-3 13.3 2.4 4.7 6.8 16.5 8.4 14.9 5 4.8 1.6 7.2-4 4.1-4.5-2-6.7 1.1-4.3-2.9 1.8-7.1-3.2-6-1.1-3.7 0-2.6-6.5-1.4-3 6.3-4 6.2-5.7-1.4-5.7-4.5-13-7z";
String testCoords2 = "M114.9 335l4.2 2.8 3.8 3.8 2.8 2.3 3.3 1.7 1.7 3.6 1.1 4.1 0.7 7.6 5 2 9.4 7.2 3 3.4-5.4 8.2-3.6 9.7 1.7 9.4-2.1 3.2-0.7 3.9-4.4 0.6-4.2-2.3-4.8-0.8-4.6-4-2 5.9-2.7 0.4-3-2-8.6-2.9-3.6 1-9.6 0.8-6.4 5.3-4.4 0.8-3.7-2.3-2.7-4.6-3.5-3.5-3.6-2.2-2.5-4.1-3.3-4.1-2.5-3.8 0.5-6.9-2.8-4.2-4.7 1.4-2.7-3.2-0.6-4.5-2.8-2.9-3-5.2-2.1-6.2-4.9-2.6-6 0.6-7.8-1.2-6.4 0.5 1.2-7.6-1.1-3 4.1-7.9 2.2-2.1 0-2.6-2.4-1.9-2.2-3.3-3.8-9.2-0.3-1.7 1.5-2.3 1.4-1.3 4.1-2.1 1.7-1.5 2.4-3.8 1.5-1.3 5 0.4 2-1.9 2.2-1.5 4.3-0.6 12 3.3 4.9-1.2 4.8-2.1 2-2.6 4.4 4 2.7 4.3 0.9 2.7 1.4 2 1.5-0.2 1.2 1.1 2.6-0.5 2.2-3.9 2.3 1.3-1 6.9 3.2 2.2 3.9-2.4 3.1 3.8 2.7 5.4 4.9 0 1.7 2.9 3 0.7 4.8-0.7 3.1 5 2.4 6.5z";
GeometryFactory geometryFactory = new GeometryFactory();
ArrayList<Coordinate> coordinatesShape1 = calcGeo(testCoords1);
ArrayList<Coordinate> coordinatesShape2 = calcGeo(testCoords2);
Polygon poly1 = geometryFactory.createPolygon(coordinatesShape1.toArray(new Coordinate[] {}));
boolean valid = poly1.isValid();
System.out.println(valid);
Polygon poly2 = geometryFactory.createPolygon(coordinatesShape2.toArray(new Coordinate[] {}));
boolean valid2 = poly2.isValid();
System.out.println(valid2);
boolean intersects = poly1.intersects(poly2);
System.out.println(intersects);
This is the code for calcGeo() method that I use to seperate the coordinates and save them in an ArrayList (This will be rewritten as well)
private ArrayList<Coordinate> calcGeo(String coords)
{
Pattern p = Pattern.compile("\\([mzlhvcsqta]|[\\+\\-]?(\\d*\\.\\d+|\\d+\\.?)([eE][\\+\\-]?\\d+)*");
Matcher m = p.matcher(coords);
double X = 0;
double Y = 0;
int index = 0;
ArrayList<Coordinate> points = new ArrayList<>();
while(m.find()) {
//System.out.println(m.group());
switch (index)
{
case 0: X = Double.parseDouble(m.group()); break;
case 1: Y = Double.parseDouble(m.group()); break;
}
if (index % 2 == 0 && index != 0)
{
double calcX = X + Double.parseDouble(m.group());
//System.out.println(calcX);
X = calcX;
}else if(index % 2 == 1 && index != 1)
{
double calcY = Y + Double.parseDouble(m.group());
//System.out.println(calcY);
Y = calcY;
}
if (index % 2 == 1)
{
points.add(new Coordinate(X, Y)); //Add the coords to the list every second time so both X and Y are calculated.
}
index++;
}
points.add(points.get(0)); //After all the points are mapped complete the polygon by adding the first pair of coords to the end of the list.
return points;
}
Basically I have two Strings with SVG data coordinates which I pass to calcGeo method so I can extract only the coordinates. Then I save those to two ArrayLists and with those I construct the two polygons and check if they are valid. For those two exact polygons".intersects()" is returning "false" but clearly they touch each other(see the image). The two coordiante paths visualized
Is this the right approach for me to check if two polygons are next to each other and touch ? If it is how could I fix this bug ? If not whats the best approach to check that ?