I have some spatial data that has all of its coordinates as lat/lon pairs (with about 10 digits decimal precision), it's stored in a database as WGS84 data. Some of the data is represented as polygons which are the resulting union of some smaller polygons whose boundaries are stored. I also have a number of points from which I build line segments (just 2 points in each segment) which I use later for intersection tests with the polygons.
I'm using a SpatialIndex to improve my queries so I insert the envelopes of all polygons in a tree (tested with both QuadTree and STRtree). Then, I connect two points into a line segment and I'm using its envelope to query the tree for possible intersections. The problem is that I get pretty much all the polygons as a result which is clearly wrong. To give you some idea about the real scale of my data, I have about 100 polygons that cover the whole North America, each line covers a very very small part of a single polygon. Ideally, I would expect no more than 2 polygons as a result.
I'm using JTS to do this calculation and I'm aware that it's not really suited for spherical data so can you suggest me another library/tool to achieve the desired behaviour or possible a workaround (for example, projecting before using JTS)?
EDIT: I'm using postgresql + postgis extension, storing the all spatial data as geometry in 4326.