Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How do I compute the longitude and latitude of a point at a certain distance?

by themaetrix (Acolyte)
on Apr 21, 2003 at 23:36 UTC ( [id://252153]=perlquestion: print w/replies, xml ) Need Help??

themaetrix has asked for the wisdom of the Perl Monks concerning the following question:

How do I compute the longitude and latitude of a point at a certain distance, say 5 miles, from a known point at a given directions, say NW?
  • Comment on How do I compute the longitude and latitude of a point at a certain distance?

Replies are listed 'Best First'.
Re: How do I compute the longitude and latitude of a point at a certain distance?
by Enlil (Parson) on Apr 22, 2003 at 00:49 UTC
Re: How do I compute the longitude and latitude of a point at a certain distance?
by zengargoyle (Deacon) on Apr 22, 2003 at 00:43 UTC

    if you want rough answer not taking into account non-spherosity of the planet on the large scale nor the curvature of the planet on the small scale i would think something like this might be close enough.

    • break your distance into EW and NS components.
    • find circumference of planetoid. --> c
    • your loc = x,y (longitude,latitude)
    • find circumference at original latitude. --> c' = c * cos(y); (c' = c at equator; c' = 0 at 90deg)
    • x' = x +- ( c' / 360 ) * EW
    • y' = y +- ( c / 360 ) * NS

    be carefull near the poles!

      I think that this is almost right, but not quite. c/360 gives the distance north-south per degree, whereas what we actually want is degrees per unit distance NS, ie 360/c. Similarly for the distance EW. We want the final result to have dimensions of an angle but the equations that you give have inconsistent dimensions (angle + distance^2/angle).

      The two equations should be:

      • x' = x +- ( 360 / c' ) * EW
      • y' = y +- ( 360 / c ) * NS

      The mean radius of the earth is 6371 km (NASA), so c = 40030 km.

        you are so correct! i should have made a units thingy.

        
        
        360 deg  |  EW m    360*EW deg  
        ---------|------- = ----------
         c' m    |    1         c'
        
        <
Re: How do I compute the longitude and latitude of a point at a certain distance?
by I0 (Priest) on Apr 22, 2003 at 08:37 UTC
    ($latitude,$longitude)=ll( MILES=>5, LATITUDE=>51+29/60, LONGITUDE=>0+0/60, DIRECTION=>315, ); if( $latitude < 0 ){ $latitude *= -1; $ns='S'; }else{ $ns='N'; } if( $longitude < 0 ){ $longitude *= -1; $ew='W'; }else{ $ew='E'; } print int $latitude,"°",60*($latitude-int $latitude),"' $ns\n", int $longitude,"°",60*($longitude-int $longitude),"' $ew\n"; use Math::Trig; sub ll{ my %l=@_; use constant PI=>4*atan2(1,1); my $nm = $l{MILES}*.87; my $A=$l{DIRECTION}*PI/180; my $b=(90-$l{LATITUDE})*PI/180; my $c=$nm/60/180*PI; my $a=acos(cos($b)*cos($c)+sin($b)*sin($c)*cos($A)); my $latitude=90-180/PI*$a; my $C=asin(sin($c)*sin($A)/sin($a)); my $longitude=$l{LONGITUDE}+180/PI*$C; return $latitude,$longitude; }
Re: How do I compute the longitude and latitude of a point at a certain distance?
by agentv (Friar) on Apr 21, 2003 at 23:53 UTC
    ...that's an excellent question.

    The simple answer is, "With trigonometry."

    I don't know the answer off the top of my head but I ++'ed because I think this will yield an excellent response. (And although it isn't a Perl question, Perl can be used to solve it.)

    From what I do remember, you'll need the distance and bearing to the landmark. (And you postulate that as the starting information.) That line segment becomes hypoteneuse of a right triangle between your target and the landmark.

    The length of the hypoteneuse would not be enough to solve for the triangle's sides normally, but with the bearing, (presuming that you limit yourself to 8 or 16 cardinal directions, (ie. only N, NW, and maybe NNW and so forth...) you could estimate the ratio between the length of the sides. You'd still only narrow your solution set down to four possibilities this way, but the direction also allows you to know if the East-West delta is positive or negative. Same for North-South.

    The last thing you'll have to account for is the latitude. You see, the formula for converting a degree of longitude into distance will be driven by your distance from the equator. (Damn this oblate spheroid anyway. Our algorithms were much simpler when we kept current on our dues to the Flat Earth Society.)

    Sorry I couldn't pause long enough to cobble something up in Perl. I promised myself that I'd only stop to read one more node before returning to life. I just couldn't resist enumerating the algorithmic considerations here. Perhaps it will help another puzzle-solver.

    ...All the world looks like -well- all the world, when your hammer is Perl.
    ---v

      ...well, this reinvented wheel provided at no charge by a constantly humbled fellow-seeker of wisdom. :-)

      ...All the world looks like -well- all the world, when your hammer is Perl.
      ---v

Re: How do I compute the longitude and latitude of a point at a certain distance?
by rinceWind (Monsignor) on Apr 22, 2003 at 10:02 UTC
    Before reinventing any wheels, I recommend a good look at the CPAN modules in the Geo:: namespace, in particular Geo::Distance and Geo::Coordinates.

    However, most of the pre-supplied routines and methods tend to do the opposite of what you want to do, i.e. "What's the distance between A and B where the lat and long are known.

    My $0.02 --rinceWind

Re: How do I compute the longitude and latitude of a point at a certain distance?
by bart (Canon) on Apr 21, 2003 at 23:52 UTC
    Well I've been looking at the documentation for the module Math::Trig on CPAN — well, actually, it's a standard module — and it includes calculations related to "great circle distances". That's quite the opposite of what you want... but it might be a direction to look into.
Re: How do I compute the longitude and latitude of a point at a certain distance?
by The Mad Hatter (Priest) on Apr 21, 2003 at 23:48 UTC
Re: How do I compute the longitude and latitude of a point at a certain distance?
by hawtin (Prior) on Apr 22, 2003 at 10:50 UTC

    There are a number of things that you need to be clear about to answer your question:

    • There are three directions commonly referred to as "North". Magnetic North, Geodetic North and Grid North. You need to be clear which one you are using
    • Presumably your starting point is a geodetic location (i.e. Lat/ Long) not a projected one (i.e. X/Y like UK National Grid or US State Plane)
    • What level of precision do you need?

    Here are some hints:

    • Assuming a spherical earth makes calculations much simpler but less accurate
    • Things get more distorted near the poles, if you are only interested in locations below the artic circle you can simplify
    • Things get more distorted as distances increase, if you never want any distance more than 10km then simple geometry will probably be good enough

    You can go all the way from doing a simple bit of trig (e.g. how long is 1 degree East-West, how long is 1 degree North-South and interpolate) to using a full blown coordinate conversion system. You have to decide how much error you are prepared to tolerate and how much time you want to invest.

    A few months ago I looked into location conversion modules in CPAN and did not find any that met my needs.

      All good points, especially the stuff regarding grids -v- geodetic locations and accuracy.

      However, it's worth pointing out that whilst 1 deg N-S stays constant (~70 miles), 1 deg E-W gets longer as you move from the equator towards the poles.

      1 deg. longitude ~ 70 miles/111 km at the equator, but the same distance covers an arc of around 57 degs. when your at 89 degs. latitude (north or south).

      Hence the old conundrum of the bear walking South for 10 miles, then East for 10 miles then North for 10 miles and ending up back where he started. What color was the bear?


      Examine what is said, not who speaks.
      1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
      2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
      3) Any sufficiently advanced technology is indistinguishable from magic.
      Arthur C. Clarke.

        The bear of course was probably White. Additionally he was probably quite dizzy.

        Diziness ~ 1/(10 - DPF) where DFP = Distance from pole.

        Further questions. Is there a finite or an infinite number of solutions to this problem? What are the possible values for DFP. If there is an infinite number of solutions what restriction(s) do you need to place on the problem to yeild a finite set such that there would be a sufficient population of bears to physically test the problem space say before the expiry of the Unix epoch? Assumptions such as the MPBPD (Mean Polar Bear Packing Density) and the MPBSL (Mean Polar Bear Step Length) will need to be made. Bonus marks for a regression analysis on...... cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: How do I compute the longitude and latitude of a point at a certain distance?
by tbone1 (Monsignor) on Apr 22, 2003 at 14:06 UTC
    As the resident astronomy dropout who's solved these problems more often than you've had hot callgirls, let me say that this problem is more complex than it first appears.

    First, recall that the earth is (nearly) spherical. Spherical trigonometry is required, as others have pointed out, which is not always the simplest of math. Amongst other things, it takes into account that one degree east-west is a lot greater distance near the equator than near the pole.

    However, the earth is not exactly a sphere. It is slightly flatted at the poles; technically, the shape is an 'oblate spheroid', kind of like what you get when you sit on a basketball or soccer ball for a while. And what's more, the earth really isn't an oblate spheroid, either. There is something known as The South Atlantic Anomoly, where the earth's crust bulges a bit, whether in or out I can't recall.

    This brings up another issue, that while the earth is mostly at the same distance from the center, it isn't exactly all at the same distance. In the Himalayas you are dealing with a larger distance from the center, and thus 5 miles takes less of an angular distance than, say, next to The Dead Sea.

    Normally, these differences are small, but the distance you are 'moving' is also small, only five miles, so those small differences can be a significant percent of what you calculate.

    Hope this helps.

    --
    tbone1
    Ain't enough 'O's in 'stoopid' to describe that guy.
    - Dave "the King" Wilson

Re: How do I compute the longitude and latitude of a point at a certain distance?
by aquarium (Curate) on Apr 22, 2003 at 05:57 UTC
    check out "coords" module..i think its what you want Chris
Re: How do I compute the longitude and latitude of a point at a certain distance?
by tall_man (Parson) on Apr 22, 2003 at 16:02 UTC
    To do it more accurately, you need to pick ellipsoid parameters, which measure the distortion of the oblate spheroid. For North America, the Clarke 1866 parameters are often used. The Clarke 1866 parameters are:
    $a = 6378206.4; $b = 6356583.8;
    $a corresponds to a North/South radius, and $b corresponds to an East/West radius. Then you can compute the earth radius at a given latitude. I only have the calculation in C++ code at the moment (Latitude and LatLon are classes holding the numeric values):
    // Reduced or Parametric Latitude Latitude Ellipsoid:: Eta(const Latitude & lat) const { // NOTE: sqrt(1-e^2) == b/a return Latitude(atan(b/a*tan(lat*deg2rad))*rad2deg); } // Eta // Radius of Ellipsoid at Latitude. double Ellipsoid:: Radius(const Latitude & lat) const { Latitude eta = Eta(lat); double x = a * cos(eta*deg2rad); double y = b * sin(eta*deg2rad); return sqrt(x*x + y*y); } // Radius
    Then you can compute a new location from a lat/lon point and an azimuth:
    // Given a Lat,Lon point, a distance (in meters), and an azimuth (in d +egrees), // this routine returns a new Lat,Lon point. LatLon Ellipsoid:: NewLatLon(const LatLon & lat_lon, double c, double Az) const { // Convert to radians double lat = lat_lon.lat * deg2rad; // We do not need lon in radians. Az *= deg2rad; // Equations (5-5) and (5-6) in "Map Projections--A Working Manual" +, Page 31 double cosAz = cos(Az); double sinAz = sin(Az); double sinlat = sin(lat); double coslat = cos(lat); c /= Radius(lat_lon.lat); double cosc = cos(c); double sinc = sin(c); double y = sinc*sinAz; double x = coslat*cosc - sinlat*sinc*cosAz; double at = (x == 0.0 && y == 0.0) ? 0.0 : atan2(y,x); lat = asin(sinlat*cosc + coslat*sinc*cosAz) * rad2deg; double lon = lat_lon.lon + at * rad2deg; return LatLon(lat, lon); } // NewLatLon
Re: How do I compute the longitude and latitude of a point at a certain distance? Geocaching! (Slightly OT)
by Acolyte (Hermit) on Apr 23, 2003 at 00:04 UTC

    There is also the empirical method...

    With all of the affordable handheld GPS systems on the market right now you could figure out the answer the fun way - by walking there. In fact, there is a whole eco/geek-friendly sport centered around it called Geocaching.

    I've found it to be a great excuse to get away from the computer every once in a while. I spend far too much time with a keyboard under my fingers...

    Acolyte
    Studying at the feet of the masters

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://252153]
Approved by bart
Front-paged by nothingmuch
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found