Link to home
Start Free TrialLog in
Avatar of Letterpart
LetterpartFlag for United Kingdom of Great Britain and Northern Ireland

asked on

PHP - How do I pass the output of variable->string() into variable = new ()

PHP - How do I pass the output: $os6x->toString() to: $os1 = new OSRef(651409.903, 313177.270);?

I'm using the PHPcoord package to try and convert a UK 6 figure Grid reference to Lat & Long.

According to the examples given in the link above. This is a two part process.

Part 1) Converting the 6 Figure Grid Reference into an OSGB grid reference.

ie: Convert TG514131 into (651400, 313100).

The example given is:

$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

Open in new window


Part 2) converts the OSGB reference into Lat & Long by:

$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();

Open in new window


The problem I am having is that I can't get the output of stage 1:

$os6x->toString()

Open in new window

into the input of stage 2:

$os1 = new OSRef(651409.903, 313177.270);
I've tried converting $os6x->toString() into a variable and then using:

$myvariable = $os6x->toString();
$os1 = new OSRef($myvariable);

Open in new window


or

$os1 = new OSRef$myvariable;

Open in new window


or

$os1 = new OSRef . $myvariable;

Open in new window


and unsurprisingly, none work.

As you can tell I'm not very good at this and am struggling to find any help on Google.

My next step would be to pattern match $myvariable and then split it into two variables and plug them in like this:

$os1 = new OSRef($number1, $number2);

Open in new window


But I'm sure this is not the correct way to achieve this and I'm missing something very simple.
SOLUTION
Avatar of skullnobrains
skullnobrains

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"IF" I'm understanding what you're using, and what you're trying to accomplish correctly (and I could need more information), you can't

The "OSRef" is a grid reference and can't be converted to latitude/longitude

What is it you're trying to do exactly
Avatar of Letterpart

ASKER

Hi @skullnobrains

var_dump(get_object_vars($os6x));

Open in new window


gives me:

C:\wamp64\www\mrt\index.php:81:
array (size=2)
  'easting' => int 292900
  'northing' => float 668000

Open in new window


I'm now struggling to get these values out so I can use them in the second stage (also not sure why one is an int and the other a float as both look like integers to me).

@kenfcamp

Yes. I'm trying to convert a 6 figure grid reference to lat/long so I can then plot markers on a Google Map. It is possible. Ordnance Survey have provided the workings for it (Annex C ) and the PHPcoord package does all the calculations for me. I just need to put the two stages together with my very limited knowledge & skills in PHP.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is this what you're looking for?

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


$os1 = getOSRefFromSixFigureReference($os6);
#$os1 = new OSRef(651409.903, 313177.270);
echo "\n";
echo "OS Grid Reference: " . $os1->toString() . " - " . $os1->toSixFigureString() . "<br />";
echo "\n";
$ll1 = $os1->toLatLng();


// Uncomment to use WGS84
#$ll1->OSGB36ToWGS84();

echo "Converted to Lat/Long: " . $ll1->toString();

Open in new window


Ken
Actually, the last part of my script probably won't work (the echo part). Calling toLatLng() on an OSRef object will return a new LatLng object, which has 2 properties: lat, lng, so you would probably want something like:

$os6 = "TG514131";
$os6x = getOSRefFromSixFigureReference($os6);
$myLatLng = $os6x->toLatLng();
echo $myLatLng->lat;
echo $myLatLng->lng;

Open in new window

@Chris Stanyon

Thank you. I had been all over Google trying to work this out with no luck and it was "so simple". always is when you know the answer.

I've now past this into Lat Long and have my values ready to populate my GeoJSON file and ultimately the Google Map.
Good stuff :)

Doesn't seem to be a whole lot of documentation on it, so it's a case of scanning the source code on GitHub to figure things out.
Thanks for your help. This is for a charity and they will be chuffed with the finished project.