Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given point (lat and long), find coordinates of corners of a square with given distance

I got a point with latitude 'x' and longitude 'y' using decimals. The point is centered in a square, the length to each side is 12 meters. How do I find the latitudes and longitudes of each corner of the square? I program in Java, but I appreciate any pseudo-code. :-)

After reading articles on the matter, I guess that a change of 'd'-meters is equal to 'e'-degrees (in decimal)..? If so, what is the "conversion-rate"?

I do not know if this helps, but given that it is 12 meters to each side, each corner has to be 16,97 meters from the point.

In advance, thanks :-)

like image 364
pecka85 Avatar asked Mar 03 '11 15:03

pecka85


2 Answers

Store your center point lat/lon, as well as the distance to the corner (use some trig to figure this), and the radius of the earth in meters. Your bearing in degrees will be 45 to give you the top-right, 115 for top-left, and so on. Use the fancy math below to find the lat/long of your desired corner in decimal format. Convert to degrees by multiplying each by 180/PI

lat1 = centerPoint.lat * ( Math.PI / 180 );  //Only do this if you need to convert from deg. to dec.
lon1 = centerPoint.lng * ( Math.PI / 180 );  //Only do this if you need to convert from deg. to dec.
d = distance;
R = EARTHS_RADIUS_IN_METERS;
brng = bearingInDegrees * ( Math.PI / 180 );
lat2 = Math.asin( Math.sin( lat1 ) * Math.cos( d / R ) + Math.cos( lat1 ) * Math.sin( d / R ) * Math.cos( brng ) );
lon2 = lon1 + Math.atan2( Math.sin( brng ) * Math.sin( d / R ) * Math.cos( lat1 ), Math.cos( d / R ) - Math.sin( lat1 ) * Math.sin( lat2 ) );

return new LatLong( lat2 * ( 180 / Math.PI ), lon2 * ( 180 / Math.PI ) );
like image 94
eterps Avatar answered Oct 21 '22 00:10

eterps


Here's a solution using GeographicLib, a library that I wrote:

// Assumes GeographicLib, http://geographiclib.sf.net, is installed
// 
// Compile and link with
//   g++ -o foo foo.cpp -L /usr/local/lib -Wl,-rpath=/usr/local/lib -lGeographic

#include <GeographicLib/Geodesic.hpp>
#include <iostream>
#include <iomanip>

using namespace GeographicLib;
using namespace std;

int main() {
  double lat1, lon1, side;
  cin >> lat1 >> lon1 >> side;
  cout << fixed << setprecision(14);
  double s12 = sqrt(0.5) * side;
  const Geodesic& g = Geodesic::WGS84;
  for (int i = 0; i < 4; ++i) {
    double azi1 = i * 90 - 135;
    double lat2, lon2;
    g.Direct(lat1, lon1, azi1, s12, lat2, lon2);
    cout << lat2 << " " << lon2 << "\n";
  }
  return 0;
}
like image 23
cffk Avatar answered Oct 21 '22 01:10

cffk