cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar

Distance Calculation using Latitude and Longitude

Does anyone has idea how to calculate the distance between states in the US using either zip code or latitude and longitude?

Jenkins Macedo
13 REPLIES 13
mjvincent87
Level II

Re: Distance Calculation using Latitude and Longitude

Another thank you to ron_horne!  Just a couple questions:

 

Just want to confirm the output is statute miles, correct?

 

Also, do you have a script for bearing between two coordinates?

Craige_Hales
Super User

Re: Distance Calculation using Latitude and Longitude

Amazing collection of formulas:

http://www.movable-type.co.uk/scripts/latlong.html

Craige
ron_horne
Super User (Alumni)

Re: Distance Calculation using Latitude and Longitude

Hi @mjvincent87 

have a look at this script and let me know if it is the direction you are thinking of:


Clear Globals();
Names Default To Here( 1 );

// open data file (dots) - this includes the observation we want to update
dt = Open( "$SAMPLE_DATA/Hurricanes.jmp" );

// introduce the column with the distance to be updated in the dots table
dots << New column ( "Distance", Numeric );

// introduce the column with the distance to be updated in the dots table
Dots << New Column("Bearing", Numeric, "Continuous", Format("Latitude DMS", "PUNSGN", 15, 0));

// extract the longitude and the latitude values from the table - as radians
x = (Column( dots, "Longitude" ) << getValues) * Pi() / 180 ; 
y = (Column( dots, "Latitude" ) << getValues) * Pi() / 180 ; 


// declare the distance calculation function in KM based on world radius of 6371 KM.
// For statute miles multiply final distance d by 0.621371.
// For nautical miles multiply final distance d by 0.539957
haversine = Function( {long1, lat1, long2, lat2, R = 6371.009},
	{Default Local},
	a = Sin( (lat1 - lat2) / 2 ) ^ 2 + Cos( lat1 ) * Cos( lat2 ) * Sin( (long1 - long2) / 2 ) ^ 2;
	c = 2 * ATan( a ^ 0.5, (1 - a) ^ 0.5 );
	d = R * c //  *0.621371 For statute miles;
);

wait (0.01);

// run the function along the rows of the table
For( i = 1, i <= N Row( dots ) - 1, i++,
	dots:distance[i] = haversine( x[i], y[i], x[i + 1], y[i + 1] )
);


// Declare the Bearing calculation function
Bearing = function ({long1, lat1, long2, lat2 },
	{Default Local},
	abc = sin (long2 - long1) * cos (lat2);
	def = cos (lat1) * sin (lat2) - sin (lat1) *cos (lat2) * cos (long2 - long1) ;
	brng = mod (( (atan ( abc, def )) * 180 / pi() ) + 360 , 360)  ;

);
wait (0.01);

// run the Bearing function along the rows of the table
For( i = 1, i <= N Row( dots ) - 1, i++,
	dots:Bearing[i] = Bearing( x[i], y[i], x[i + 1], y[i + 1] )
);

Best,

ron

ron_horne
Super User (Alumni)

Re: Distance Calculation using Latitude and Longitude

Hi @mjvincent87 and @jenkins_macedo 

another way of doing this is with column formula in a data table. try the following script

Clear Globals();
Names Default To Here( 1 );

// Open data file
dt = Open( "$SAMPLE_DATA/Cities.jmp" );

// add column for distance
dt << New Column( "latrad", formula( :Latitude * (Pi() / 180) ) );
dt << New Column( "longrad", formula( :Longitude * (Pi() / 180) ) );

dt << New Column( "Distance", Numeric,
	formula(
		6371.00 * 2 * ATan(
			(Sin( (:latrad - Lag( :latrad, -1 )) / 2 ) ^ 2 + Cos( :latrad ) * Cos( Lag( :latrad, -1 ) ) * Sin( (:longrad - Lag( :longrad, -1 )) / 2 )
			 ^ 2) ^ 0.5,
			(1 - (Sin( (:latrad - Lag( :latrad, -1 )) / 2 ) ^ 2 + Cos( :latrad ) * Cos( Lag( :latrad, -1 ) ) * Sin(
				(:longrad - Lag( :longrad, -1 )) / 2
			) ^ 2)) ^ 0.5
		)
	)
);

dt << New Column( "Bearing", Numeric, "Continuous", Format( "Latitude DMS", "PUNSGN", 15, 0 ),
	formula(
		brng = Mod(
			((ATan(
				Sin( Lag( :longrad, -1 ) - :longrad ) * Cos( Lag( :latrad, -1 ) ),
				Cos( :latrad ) * Sin( Lag( :latrad, -1 ) ) - Sin( :latrad ) * Cos( Lag( :latrad, -1 ) ) * Cos( Lag( :longrad, -1 ) - :longrad )
			)) * 180 / Pi()) + 360,
			360
		)
	)
);