League\Geotools\Coordinate\Coordinate::toDecimalDegrees PHP Method

toDecimalDegrees() private method

Converts a valid and acceptable geographic coordinates to decimal degrees coordinate.
See also: http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
private toDecimalDegrees ( string $coordinates ) : array
$coordinates string A valid and acceptable geographic coordinates.
return array An array of coordinate in decimal degree.
    private function toDecimalDegrees($coordinates)
    {
        // 40.446195, -79.948862
        if (preg_match('/(\\-?[0-9]{1,2}\\.?\\d*)[, ] ?(\\-?[0-9]{1,3}\\.?\\d*)$/', $coordinates, $match)) {
            return array($match[1], $match[2]);
        }
        // 40° 26.7717, -79° 56.93172
        if (preg_match('/(\\-?[0-9]{1,2})\\D+([0-9]{1,2}\\.?\\d*)[, ] ?(\\-?[0-9]{1,3})\\D+([0-9]{1,2}\\.?\\d*)$/i', $coordinates, $match)) {
            return array($match[1] + $match[2] / 60, $match[3] < 0 ? $match[3] - $match[4] / 60 : $match[3] + $match[4] / 60);
        }
        // 40.446195N 79.948862W
        if (preg_match('/([0-9]{1,2}\\.?\\d*)\\D*([ns]{1})[, ] ?([0-9]{1,3}\\.?\\d*)\\D*([we]{1})$/i', $coordinates, $match)) {
            return array('N' === strtoupper($match[2]) ? $match[1] : -$match[1], 'E' === strtoupper($match[4]) ? $match[3] : -$match[3]);
        }
        // 40°26.7717S 79°56.93172E
        // 25°59.86′N,21°09.81′W
        if (preg_match('/([0-9]{1,2})\\D+([0-9]{1,2}\\.?\\d*)\\D*([ns]{1})[, ] ?([0-9]{1,3})\\D+([0-9]{1,2}\\.?\\d*)\\D*([we]{1})$/i', $coordinates, $match)) {
            $latitude = $match[1] + $match[2] / 60;
            $longitude = $match[4] + $match[5] / 60;
            return array('N' === strtoupper($match[3]) ? $latitude : -$latitude, 'E' === strtoupper($match[6]) ? $longitude : -$longitude);
        }
        // 40:26:46N, 079:56:55W
        // 40:26:46.302N 079:56:55.903W
        // 40°26′47″N 079°58′36″W
        // 40d 26′ 47″ N 079d 58′ 36″ W
        if (preg_match('/([0-9]{1,2})\\D+([0-9]{1,2})\\D+([0-9]{1,2}\\.?\\d*)\\D*([ns]{1})[, ] ?([0-9]{1,3})\\D+([0-9]{1,2})\\D+([0-9]{1,2}\\.?\\d*)\\D*([we]{1})$/i', $coordinates, $match)) {
            $latitude = $match[1] + ($match[2] * 60 + $match[3]) / 3600;
            $longitude = $match[5] + ($match[6] * 60 + $match[7]) / 3600;
            return array('N' === strtoupper($match[4]) ? $latitude : -$latitude, 'E' === strtoupper($match[8]) ? $longitude : -$longitude);
        }
        throw new InvalidArgumentException('It should be a valid and acceptable ways to write geographic coordinates !');
    }