Letterpart
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:
Part 2) converts the OSGB reference into Lat & Long by:
The problem I am having is that I can't get the output of stage 1:
$os1 = new OSRef(651409.903, 313177.270);
I've tried converting $os6x->toString() into a variable and then using:
or
or
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:
But I'm sure this is not the correct way to achieve this and I'm missing something very simple.
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
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();
The problem I am having is that I can't get the output of stage 1:
$os6x->toString()
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);
or
$os1 = new OSRef$myvariable;
or
$os1 = new OSRef . $myvariable;
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);
But I'm sure this is not the correct way to achieve this and I'm missing something very simple.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi @skullnobrains
gives me:
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.
var_dump(get_object_vars($os6x));
gives me:
C:\wamp64\www\mrt\index.php:81:
array (size=2)
'easting' => int 292900
'northing' => float 668000
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Is this what you're looking for?
Ken
$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();
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;
ASKER
@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.
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.
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.
ASKER
Thanks for your help. This is for a charity and they will be chuffed with the finished project.
The "OSRef" is a grid reference and can't be converted to latitude/longitude
What is it you're trying to do exactly