wood burning stoves 2.0*
The moose likes Java in General and the fly likes crow flies distance between two location Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "crow flies distance between two location " Watch "crow flies distance between two location " New topic
Author

crow flies distance between two location

Habeeb Shaikh
Ranch Hand

Joined: Nov 23, 2008
Posts: 48
hi,
I want to calculate distance between 2 zipcodes using crow flies. let me know how i can achieve this. we can use any api like google,yahoo etc..
I really do not know exactly where to write.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41106
    
  45
The difficulty is that a zipcode is not a geometric point, but a geometric area. The distance could be just about zero (for two adjacent points), or several miles.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
Have a look at this old thread, where somebody else had a related problem.
Habeeb Shaikh
Ranch Hand

Joined: Nov 23, 2008
Posts: 48
Thanks for reply.
I have got latitude and longitude through our database.i am calculating distance using 2 ways
1. Spherical Law of Cosines
2. haversine formula.


In second type it is giving me proper answer. but it first type it is giving me result like 9170.1352. Although i am expecting is 377.84 miles. I do not understanding in which unit it is returning value. once i get know its unit then i can convert it in miles.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Ulf Dittmer wrote:The distance could be just about zero (for two adjacent points), or several miles.

Several? I guess you've never been to the States . The whole of Montana only has 1,000. Alaska just 500.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
Please show us the cosine algorithm. Are you using degrees or radians?
Habeeb Shaikh
Ranch Hand

Joined: Nov 23, 2008
Posts: 48
hi,
i have implement this formula.

acos(sin(lat1).sin(lat2)+cos(lat1).cos(lat2).cos(long2−long1)).R
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
That formula looks correct. I have got to go now, but please show us how you have implemented it, and somebody else will doubtless find you are using radians for part of it and degrees for the rest
Habeeb Shaikh
Ranch Hand

Joined: Nov 23, 2008
Posts: 48
hi we have implemented in that way
It's a query inwhich we a have passed zip1 and zip2 to get latitude and longitude..
Let me know in which unit its output.


sql_stmt = " SELECT 3959 * ACOS(( SIN((select z.latitude ",
" FROM zipdata_wrk z ",
" WHERE z.zipcode = '", zip1 CLIPPED, "') / 57.3) ",
" * SIN((select z.latitude",
" FROM zipdata_wrk z ",
" WHERE z.zipcode = '", zip2 CLIPPED,"') / 57.3) ) ",
" + (COS((select z.latitude ",
" FROM zipdata_wrk z ",
" WHERE z.zipcode = '", zip1 clipped, "' ) / 57.3) ",
" * COS((select z.latitude ",
" FROM zipdata_wrk z ",
" WHERE z.zipcode = '", zip2 clipped, "') / 57.3) ",
" * COS(((SELECT z.longitude from zipdata_wrk z ",
" WHERE z.zipcode = '", zip2 clipped, "' ) ",
" - (SELECT z.longitude ",
" FROM zipdata_wrk z ",
" WHERE z.zipcode = '", zip1 clipped, " ')) / 57.3))) ",
" FROM zipdata_wrk where zipcode = '", zip1, "' "
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38003
    
  22
Where did you get 57.3 from? I made it 57.2957795130823208768. Why don’t you use that? Similarly, 3959 is only an approximate radius for the earth, and which varies with latitude. That radius is of course in statute miles.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Most folks calculate it as nautical miles, and then do a conversion. Nautical miles are nice and handy, since they directly relate to degrees, minutes and seconds of lat/long.

Here are some good sample locations to use for testing:


Its about 800 miles from the Whitehouse to Daytona International Speedway

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Habeeb Shaikh wrote:It's a query inwhich we a have passed zip1 and zip2 to get latitude and longitude..
Let me know in which unit its output.


So that means you're asking a question about your database, which you didn't tell us what that database is. Does its SIN() function require a number in degrees or radians? (The database's documentation should tell you that.) And are your database columns in those units?

And by the way you should seriously consider changing that query to use a PreparedStatement. If only so that then it wouldn't be totally unreadable because of all the quotes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: crow flies distance between two location
 
Similar Threads
how to use distanceBetween built in methog
Measure distance between two cities ?
operator instanceof confusion
Your native place and your working city
How far am I from directly opposite the Ranch?