I wonder whether someone may be able to help me please.
I currently use the code below (originating from nearby.org.uk) to allow a user to convert OS Grid Refernces to a Lat & Lng co-ordinate via an HTML form.
function converttolatlng() {
var gr = document.getElementById('osgridref').value;
var osgb = new GT_OSGB();
if (osgb.parseGridRef(gr)) {
var wgs84 = osgb.getWGS84();
document.getElementById('osgb36lat').value = wgs84.latitude;
document.getElementById('osgb36lon').value = wgs84.longitude;
}
else {
document.getElementById('osgb36lat').value = "n/a";
document.getElementById('osgb36lon').value = "n/a";
}
}
However on a monthly basis I need to convert an xml list of approx 22,000 OS Grid References. Because of the amount of data involved it’s obviously not practical to use an HTML form so I’ve been working on a way to automate this. The list originates in an XML format, so I’ve put together the code below to extract the XML data into PHP.
<?
$objDOM = new DOMDocument();
$objDOM->load("xmlfile.xml");
$note = $objDOM->getElementsByTagName("Details");
foreach( $note as $value )
{
$NGR = $value->getElementsByTagName("NGR");
$ngr = $NGR->item(0)->nodeValue;
echo "$ngr <br>";
}
I’ve now come to a point where I’m a little unsure about what to do next. I know that I need to convert the Grid References through the Javascript function and in addition create the Lat & Lng fields that ordinarily would be on my HTML form. I then need to be able to upload this data to a table in a mySQL database.
Could someone perhaps take a look at this please and show me what I need to to convert the Grid References and how I can upload them to my database.
Many thanks
UPDATE
This an extract of the xml file:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <Details>
<LISTENTRY>1003886</LISTENTRY>
<NGR>SS 00000 11111</NGR>
</Details>
and please find a link to the ‘geotools’ file here.
Passing through a webservice the way you describe should really be your last option, maybe this php code from the Geograph project is what you are looking for. If not I’d suggest you translate the js code to php.
EDIT:
how to integrate? I’d start by reading a bit on OSB36 to understand what the 3 elements in
<NGR>actually are and how they should be interpreted. The hardest part is the translation of the grid digits to an offset (you need the reverse ofosgb36_to_gridref). One way of doing that can be derived fromparseGridRefingeotools2.js