I am working with two KML input files and trying to determine if their coordinates overlap at any point. I am using the JTS Topology Suite (JTS) for spatial operations, but I am not getting accurate results.
Current Approach
Parse the KML files to extract coordinate points and polygons.
Convert the extracted coordinates into JTS Geometry objects, such as Point, LineString, or Polygon.
Use JTS spatial operations like intersects(), contains(), or covers() to detect overlaps.
import .locationtech.jts.geom.*;
import .locationtech.jts.io.WKTReader;
import java.util.List;
public class KMLOverlapDetector {
public static void main(String[] args) throws Exception {
GeometryFactory geometryFactory = new GeometryFactory();
WKTReader reader = new WKTReader(geometryFactory);
// Example: Converting KML coordinates to JTS geometries
Geometry geom1 = reader.read("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))");
Geometry geom2 = reader.read("POLYGON ((35 15, 45 45, 25 45, 15 25, 35 15))");
// Check if the two geometries overlap
if (geom1.intersects(geom2)) {
System.out.println("Overlap detected!");
} else {
System.out.println("No overlap.");
}
}
}
Issue Despite using intersects(), I am not getting expected results when dealing with large KML files or complex geometries. The overlap is sometimes missed or falsely detected.
Coordinates count: Expected from 200 to 2 million coordinates or more.
Questions
Is JTS the best library for this use case, or is there a better alternative?
Would a geospatial database like PostGIS or Neo4j perform better for detecting coordinate overlaps in large datasets?
Are there better methods or configurations within JTS that improve accuracy when checking overlapping points?
Is there an alternative Java library that works well for precise geospatial analysis with KML data?
Potential Alternatives I’m Considering
GeoTools: Does it provide better accuracy for spatial intersections?
PostGIS: Would storing KML data in a database and using ST_Intersects() be more efficient?
Neo4j (with Spatial plugin): Can graph-based spatial queries provide better results?
Note: Any insights or recommendations would be greatly appreciated! Let me know if Python and its libraries can help me resolve this issue. Thanks in advance!
I am working with two KML input files and trying to determine if their coordinates overlap at any point. I am using the JTS Topology Suite (JTS) for spatial operations, but I am not getting accurate results.
Current Approach
Parse the KML files to extract coordinate points and polygons.
Convert the extracted coordinates into JTS Geometry objects, such as Point, LineString, or Polygon.
Use JTS spatial operations like intersects(), contains(), or covers() to detect overlaps.
import .locationtech.jts.geom.*;
import .locationtech.jts.io.WKTReader;
import java.util.List;
public class KMLOverlapDetector {
public static void main(String[] args) throws Exception {
GeometryFactory geometryFactory = new GeometryFactory();
WKTReader reader = new WKTReader(geometryFactory);
// Example: Converting KML coordinates to JTS geometries
Geometry geom1 = reader.read("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))");
Geometry geom2 = reader.read("POLYGON ((35 15, 45 45, 25 45, 15 25, 35 15))");
// Check if the two geometries overlap
if (geom1.intersects(geom2)) {
System.out.println("Overlap detected!");
} else {
System.out.println("No overlap.");
}
}
}
Issue Despite using intersects(), I am not getting expected results when dealing with large KML files or complex geometries. The overlap is sometimes missed or falsely detected.
Coordinates count: Expected from 200 to 2 million coordinates or more.
Questions
Is JTS the best library for this use case, or is there a better alternative?
Would a geospatial database like PostGIS or Neo4j perform better for detecting coordinate overlaps in large datasets?
Are there better methods or configurations within JTS that improve accuracy when checking overlapping points?
Is there an alternative Java library that works well for precise geospatial analysis with KML data?
Potential Alternatives I’m Considering
GeoTools: Does it provide better accuracy for spatial intersections?
PostGIS: Would storing KML data in a database and using ST_Intersects() be more efficient?
Neo4j (with Spatial plugin): Can graph-based spatial queries provide better results?
Note: Any insights or recommendations would be greatly appreciated! Let me know if Python and its libraries can help me resolve this issue. Thanks in advance!
Share asked Mar 31 at 11:26 Eliza ColtEliza Colt 112 bronze badges New contributor Eliza Colt is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2- It would be helpful if you explained your expectations and provided the example KML spatial object pairs where the JTS results mismatch your expectation. It would help to diagnose the issue and suggest a solution. – Michael Entin Commented Mar 31 at 21:28
- 1 Can you share which version of JTS and GeoTools you have tried? – GeoJim Commented Mar 31 at 21:31
2 Answers
Reset to default 1As background information, JTS was ported to C/C++ as a project called GEOS (https://libgeos./**)**. Those libraries have over twenty years of existence handling topological issues and are used widely. If you use Python or PostGIS, you are using Geos. GeoTools uses JTS.
- Is JTS the best library for this use case, or is there a better alternative?
In my opinion, I believe JTS is the best library for this use case. The TestBuilder may be helpful for visualizing some of what you are doing once you have the KML in another format. This UI doesn't speak all formats; JTS is focused on topology. GeoTools is more focused on managing all the different ways of representing spatial data.
- Would a geospatial database like PostGIS or Neo4j perform better for detecting coordinate overlaps in large datasets?
Neo4j (with Spatial plugin): Can graph-based spatial queries provide better results?
An version of PostGIS would be using some version of GEOS. If you see different results between PostGIS and GeoTools, then it'd either be a difference in the KML handling or something about the specific version of the libraries in use.
I have no experience with Neo4j. From 5 seconds of searching it looks like there some Neo4j capabilities which use JTS and others which use their own implementations.
- Are there better methods or configurations within JTS that improve accuracy when checking overlapping points?
This is a good question. I suggest:
- Reading https://locationtech.github.io/jts/jts-faq.html#robustness
- Reading https://locationtech.github.io/jts/javadoc//locationtech/jts/geom/PrecisionModel.html
- Asking this question on the JTS Dev mailing list if you continue to have trouble.
- Is there an alternative Java library that works well for precise geospatial analysis with KML data?
Not sure. I am a JTS committer / user.
As @GeoJim says if you must use KML then GeoTools is probably the way to go as it has an unsupported (but pretty robust) KML parser that will create SimpleFeature
`s.
When working with GeoTools and JTS (and GEOS) you need to take care with geographically large polygons as JTS will treat the Earth as flat. You can use an AUTO projection in GeoTools to handle this provided that the polygons are small (<100km) see this question for an example. As an aside this is roughly what PostGIS does when you use the geography
type.