Measuring Distance

Measuring distance in geographic or projected coordinate systems requires great circle calculations. Both MgGeometry::Buffer() and MgGeometry::Distance() accept a measurement parameter that defines the great circle to be used. If the measurement parameter is null, the calculation is done using a linear algorithm.

Create the measurement parameter, an MgCoordinateSystemMeasure object, from the MgCoordinateSystem object.

Distance is calculated in the units of the SRS. MgCoordinateSystem includes two methods, ConvertCoordinateSystemUnitsToMeters() and ConvertMetersToCoordinateSystemUnits() to convert to and from linear distances.

For example, to calculate the distance between two MgGeometry objects $a and $b, using the coordinate system $srs, perform the following steps:

    $measure = new MgCoordinateSystemMeasure($srs);

    $distInMapUnits = $a->Distance($b, $measure);

    $distInMeters = $srs->ConvertCoordinateSystemUnitsToMeters(

       $distInMapUnits);

Another way to calculate the distance is to use MgCoordinateSystemMeasure::GetDistance(), as in the following:

    $distInMapUnits = $measure->GetDistance($a, $b);