Adam Carter
Adam Carter

Reputation: 4844

Creating a bounding box from a latitude/longitude and a 'radius'

I have been looking for a piece of code (preferably in PHP) which can take a latitude, longitude and a plus/minus value (in Km) and return the minimum and maximum latitude and longitudes.

I can't seem to find anything which both explains what it does in simplistic terms as well as takes into account the 'bloated' curvature of the earth.

Can anyone help?

Upvotes: 0

Views: 1460

Answers (3)

Adam Carter
Adam Carter

Reputation: 4844

In order to make things easier, I have switched to MongoDB's system and am currently using MongoHQ's REST API to access the data with their geo APIs.

Upvotes: 0

acraig5075
acraig5075

Reputation: 10756

This is tricky because latitude/longitude is an angular quantity, whereas your radius distance is an incompatible linear quantity.

Here are some methods of solving it, in decreasing order of accuracy and increasing order of simplicity of coding.

  1. Use a projection library to first do the calculations in projected coordinates (x,y) and then re-project back to (lat,lon). There is a php port of the well-respected proj4 library, and I see PEAR has a projection library too.
  2. Approximate the Earth by an ellipsoid with WGS84 parameters. See the accepted answer of this question.
  3. Approximate the Earth by a sphere and use spherical trigonometry. See the accepted answer of this question.
  4. Use simple proportion of approximate conversion factors for your area of interest. See the accepted answer of this question.

I recommend no. 1.

Upvotes: 1

Baba
Baba

Reputation: 95111

Not sure why why you want to do this in pure PHP but MySQL and MongoDB support geospatial indexing which has huge performance advantage over any pure PHP implementation.

I personal prefer the MongoDB version because it installed by default and you can easily serach by

  • Near
  • Radius
  • Box
  • Polygon
  • Distance

Please See

Upvotes: 0

Related Questions