phpcoord Test Script

Calculate Surface Distance between two Latitudes/Longitudes

The LatLngDistance function takes two latitudes/longitudes and calculates the surface distance between the two in kilometres:

$lld1 = new LatLng(40.718119, -73.995667); // New York
echo "New York Lat/Long: " . $lld1->toString() . "<br />";
$lld2 = new LatLng(51.499981, -0.125313);  // London
$d = $lld1->distance($lld2);
echo "Surface Distance between New York and London: " . $d . "km";
New York Lat/Long: (40.718119, -73.995667)
London Lat/Long: (51.499981, -0.125313)
Surface Distance between New York and London: 5565.8427348131km

Convert OS Grid Reference to Latitude/Longitude

Note that the OSGB-Latitude/Longitude conversions use the OSGB36 datum by default. The majority of applications use the WGS84 datum, for which the appropriate conversions need to be added. See the examples below to see the difference between the two data.

Using OSGB36 (convert an OSGB grid reference to a latitude and longitude using the OSGB36 datum):

$os1 = new OSRef(651409.903, 313177.270);
echo "OS Grid Reference: " . $os1->toString() . " - " . $os1->toSixFigureString() . "<br />";
$ll1 = $os1->toLatLng();
echo "Converted to Lat/Long: " . $ll1->toString();
OS Grid Reference: (651409.903, 313177.27) - TG514132
Converted to Lat/Long: (52.657570301933, 1.7179215806451)

Using WGS84 (convert an OSGB grid reference to a latitude and longitude using the WGS84 datum):

$os1w = new OSRef(651409.903, 313177.270);
echo "OS Grid Reference: " . $os1w->toString() . " - " . $os1w->toSixFigureString() . "<br />";
$l1w = $os1w->toLatLng();
$l1w->OSGB36ToWGS84();
echo "Converted to Lat/Long: " . $ll1w->toString();
OS Grid Reference: (651409.903, 313177.27) - TG514132
Converted to Lat/Long: (52.657975599534, 1.7160665447978)

Convert Latitude/Longitude to OS Grid Reference

Note that the OSGB-Latitude/Longitude conversions use the OSGB36 datum by default. The majority of applications use the WGS84 datum, for which the appropriate conversions need to be added. See the examples below to see the difference between the two data.

Using OSGB36 (convert a latitude and longitude using the OSGB36 datum to an OSGB grid reference):

$ll2 = new LatLng(52.657570301933, 1.7179215806451);
echo "Latitude/Longitude: " . $ll2->toString() . "<br />";
$os2 = $ll2->toOSRef();
echo "Converted to OS Grid Ref: " . $os2->toString() . " - " . $os2->toSixFigureString();
Latitude/Longitude: (52.657570301933, 1.7179215806451)
Converted to OS Grid Ref: (651409.90280223, 313177.2699187) - TG514132

Using WGS84 (convert a latitude and longitude using the WGS84 datum to an OSGB grid reference):

$ll2w = new LatLng(52.657570301933, 1.7179215806451);
echo "Latitude/Longitude: " . $ll2->toString() . "<br />";
$ll2w->WGS84ToOSGB36();
$os2w = $ll2w->toOSRef();
echo "Converted to OS Grid Ref: " . $os2w->toString() . " - " . $os2w->toSixFigureString();
Latitude/Longitude: (52.657570301933, 1.7179215806451)
Converted to OS Grid Ref: (651537.63534073, 313138.68699872) - TG515131

Convert Six-Figure OS Grid Reference String to an OSRef Object

To convert a string representing a six-figure OSGB grid reference:

$os6 = "TG514131";
echo "Six figure string: " . $os6 . "<br />";
$os6x = getOSRefFromSixFigureReference($os6);
echo "Converted to OS Grid Ref: " . $os6x->toString() . " - " . $os6x->toSixFigureString();
Six figure string: TG514131
Converted to OS Grid Ref: (651400, 313100) - TG514131

Convert UTM Reference to Latitude/Longitude

$utm1 = new UTMRef(456463.99, 3335334.05, "E", 12);
echo "UTM Reference: " . $utm1->toString() . "<br />";
$ll3 = $utm1->toLatLng();
echo "Converted to Lat/Long: " . $ll3->toString();
UTM Reference: 12E 456463.99 3335334.05
Converted to Lat/Long: (-60.11669998541, -111.78330000822)

Convert Latitude/Longitude to UTM Reference

$ll4 = new LatLng(-60.1167, -111.7833);
echo "Latitude/Longitude: " . $ll4->toString() . "<br />";
$utm2 = $ll4->toUTMRef();
echo "Converted to UTM Ref: " . $utm2->toString() ;
Latitude/Longitude: (-60.1167, -111.7833)
Converted to UTM Ref: 12E 456463.99047613 3335334.0478552

(c) 2005, Jonathan Stott