davidmoten / grumpy

OGC WMS server allowing custom rendered layers in java
Apache License 2.0
31 stars 14 forks source link

Problem with creating Rectangle based on 4 coordinates from predicted Positions #10

Closed KrazyD closed 2 years ago

KrazyD commented 3 years ago

I'm trying to create com.github.davidmoten.rtree2.geometry.Geometries.rectangle from coordinates com.github.davidmoten.grumpy.core.Position with following code:

Position from = Position.create(90D, 179D);
int DIR_NORTH = 0;
int DIR_SOUTH = 180;
int DIR_EAST = 90;
int DIR_WEST = 270;
double distanceKm = 2.0D;
Position north = from.predict(distanceKm, DIR_NORTH);
Position south = from.predict(distanceKm, DIR_SOUTH);
Position east = from.predict(distanceKm, DIR_EAST);
Position west = from.predict(distanceKm, DIR_WEST);
return rectangle(south.getLat(), west.getLon(), north.getLat(), east.getLon());

But, when create Position.from(90D, -179D), east.getLon() give -89, while west.getLon() give 90.9. In this case rectangle creation throw IllegalArgumentException() in constructor of RectangleDouble(). A similar exception i am getting when create Position with positive longitude. I have not yet been able to figure out which formula is used in the predict() method. This seems to be the formula for finding bearing.

Could you, please, help me to figure out, how to fix my problem creating rectangle given longitude and latitude.

davidmoten commented 3 years ago

Sorry @KrazyD I missed this. I'll have a look.

davidmoten commented 3 years ago

by setting latitude to 90 you are on the North Pole. That is a singularity for predictions based on direction because north, south, east, west are meaningless or ambiguous at that point. The relevant code is in https://github.com/davidmoten/grumpy/blob/master/grumpy-core/src/main/java/com/github/davidmoten/grumpy/core/Position.java:

https://github.com/davidmoten/grumpy/blob/61619ba7b389b223099c5ac604d8ff86b334ce36/grumpy-core/src/main/java/com/github/davidmoten/grumpy/core/Position.java#L104-L115