Skip to content

How to correctly calculate distances #164

@MoeweX

Description

@MoeweX

Hi,

I am currently testing spatial4j and struggle with correctly calculating distances. For example, I tried to calculate the distance between Berlin and Hamburg with this:

SpatialContext ctx = SpatialContext.GEO;
Point berlin = ctx.getShapeFactory().pointXY(52.5200, 13.4050);
Point hamburg = ctx.getShapeFactory().pointXY(53.511, 9.9937);
logger.info(ctx.getDistCalc().distance(berlin, hamburg) * DEG_TO_KM);

Unfortunately, this results in ~394km when the real value is 253.4km.

As I could not find out what I was doing wrong, I looked into your test cases to see whether I can reproduce the results of the "testSomeDistances" test. So I tried this code snippet:

Point ctr = ctx.getShapeFactory().pointXY(0, 100);
logger.info(ctx.getDistCalc().distance(ctr, GEO.getShapeFactory().pointXY(10, 0)));

Unfortunately, this throws an InvalidShapeException (Bad Y value 100.0 is not in boundary Rect(minX=-180.0,maxX=180.0,minY=-90.0,maxY=90.0)), so I wonder: why is the test working?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions