Search code examples
phplaravelgeocode

Class 'OSRef' not found


I'm fairly new to Laravel and PHP in general.

I'm trying to use PHPCoord to convert Northing/Easting to Lat/Lng.

I've installed it via Composer, and it appears in my composer.json file and vendor directory, but I don't know how to reference it in my code?

I'm pretty sure it's to do with name-spacing or the lack of an autoload.php, but I could be a million miles off.

I've tried use PHPCoord\PHPCoord; and use Php-Coord\Php-Coord;, but it fails to find the class.

(In the latter it doesn't seem to like the hyphen.)

I'm just dropping in the example code in my Laravel controller for now, to see if it's working:

$OSRef = new OSRef(500000, 200000); //Easting, Northing
$LatLng = $OSRef->toLatLng();
$GPSLatLng = $LatLng->toWGS84(); //optional, for GPS compatibility

$lat =  $LatLng->getLat();
$long = $LatLng->getLng();

dd($lat);

But I get Class 'App\Http\Controllers\OSRef' not found


Solution

  • You have to include the OSRef with its namespace. Have a look at this answer.

    So in your code you have to add this code at the beginning of the file. Like so:

    <?php
    
    namespace App\Http\Controlles;
    
    use PHPCoord\OSRef;
    
    class YourClass {
    
    public function yourMethod() {
    }
        $OSRef = new OSRef(500000, 200000); //Easting, Northing
        $LatLng = $OSRef->toLatLng();
        $GPSLatLng = $LatLng->toWGS84(); //optional, for GPS compatibility
    
        $lat =  $LatLng->getLat();
        $long = $LatLng->getLng();
    
        dd($lat);
    }