views:

1366

answers:

6

Given a coordinate (lat, long), I am trying to calculate a square bounding box that is a given distance (e.g. 50km) away from the coordinate. So as input I have lat, long and distance and as output I would like two coordinates; one being the south-west (bottom-left) corner and one being the north-east (top-right) corner. I have seen a couple of answers on here that try to address this question in Python, but I am looking for a Java implementation in particular.

Just to be clear, I intend on using the algorithm on Earth only and so I don't need to accommodate a variable radius.

It doesn't have to be hugely accurate (+/-20% is fine) and it'll only be used to calculate bounding boxes over small distances (no more than 150km). So I'm happy to sacrifice some accuracy for an efficient algorithm. Any help is much appreciated.

Edit: I should have been clearer, I really am after a square, not a circle. I understand that the distance between the center of a square and various points along the square's perimeter is not a constant value like it is with a circle. I guess what I mean is a square where if you draw a line from the center to any one of the four points on the perimeter that results in a line perpendicular to a side of the perimeter, then those 4 lines have the same length.

+1  A: 

A square bounding box for a distance from a point? Sounds more like a circle to me.

For distances much less than an Earth radius you can easily approximate by discarding the curvature of the Earth and just say X miles in either direction from the spot you have.

Thorbjørn Ravn Andersen
the question then becomes how does one calculate lat and long of a point X miles away from a given lat and long?
Peter Recore
Peter - exactly.
Bryce Thomas
+2  A: 

If you have a Python implementation I don't see why you couldn't convert it to Java.

The algorithm should be the same.

Alexandru Luchian
The best Python example I found had been marked as untested by the author. I imagine given that I'm after something that is fairly forgiving on accuracy that a different algorithm might be appropriate also.
Bryce Thomas
It's just a formula you need; I'm sure someone will provide it for you and hopefully give you the reference link.
Kevin Bourrillion
A: 
import com.vividsolutions.jts.geom.Envelope;

...
Envelope env = new Envelope(centerPoint.getCoordinate());
env.expandBy(distance_in_degrees); 
...

Now env contains your envelope. It's not actually a "square" (whatever that means on the surface of a sphere), but it should do.

You should note that the distance in degrees will depend on the latitude of the center point. At the equator, 1 degree of latitude is about 111km, but in New York, it's only about 75km.

The really cool thing is that you can toss all your points into a com.vividsolutions.jts.index.strtree.STRtree and then use it to quickly calculate points inside that Envelope.

novalis
A: 

double R = 6371; // earth radius in km

double radius = 50; // km

double x1 = lon - Math.toDegrees(radius/R/Math.cos(Math.toRadians(lat)));

double x2 = lon + Math.toDegrees(radius/R/Math.cos(Math.toRadians(lat)));

double y1 = lat + Math.toDegrees(radius/R);

double y2 = lat - Math.toDegrees(radius/R);

Although I would also recommend JTS.

IronMan
+3  A: 

I wrote an article about finding the bounding coordinates:

http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates

The article explains the formulae and also provides a Java implementation. (It also shows why IronMan's formula for the min/max longitude is inaccurate.)

Jan Philip Matuschek
Thank you for writing this; I especially appreciate the SQL implementation details.
Dave Jarvis
A: 
susheel