Postcode Integration for UK


#21

The coordinates came from the first geocoding. We are getting somewhere. This is first ive attempted this so thats why your seeing me kind of explore it.


#22

So coordinate which I imagine are center of the postcode area and thats the closest address to the centre position?

Im loving it, is good to see the process, only ever used API for web modules like recapcha etc and thats just reg for the key and put in module… just glad your more clued up than me :slight_smile:


#23

Ok check this out… now just enter this.

https://maps.googleapis.com/maps/api/geocode/json?components=postal_code:8ST|country=:GB&key=AIzaSyBPim1IKs10ovTDkWgtKKjEx5EyS3D8r1Q

That is the query we needed. Now if we parse formatted_address it can pull up all addresses linked to 8ST

I am still learning your POSTCODE so correct me if im wrong.

so EC1Y 8ST is that a full postcode?


#24

Can you give me a postcode example that might return multiple addresses?


#25

They are quite complex sometimes.
Generally if you take mine;
GL56 9NY
GL - Gloucestershire/Main sorting office
56 - Local sorting office (in Moreton)
Last bits are generally sequential (kind of) if on the High street in town at one end the first pub is GL560AF, the hotel I work at maybe 300m down the street is GL560AW
Imagine the last couple are more to do with post delivery routes maybe.

In town they are generally much more specific, GL560AW for hotel usually only ever gives hotel when putting in on websites with searches.

Mine GL569NY would in theory give at least 30-40 houses/addresses of = House, Todenham, Moreton-in-Marsh

Average format is XX00 0XX where X is letters and 0 is numbers but cities can be shorter like centeral london could be E1 in place of the GL56 part.

When @emre said about regex on entity fields I tried to find a regex format code for UK postcodes and there are so many - definatly gave the impression its hard to authenticate/format


#26

OK so I need to dig little further but it looks like we should be able to do this.


#27

What exactly am I looking at with this link?
A list of addresses?
Just they dont seem to be in any one perticular place, they are from all over, saw cornwal (very south end of country, 6hrs drive from here) swindon which is maybe 40-50 minutes and another saying highlands (didnt see full address but sounds like scotland to me lol)
But if your searching 8ST presume thats resulting in all the addresses with postcode containing that?


#28

I was wrong. ignore that thread. I am looking deeper on how to get the addresses. We might need to come back to this later but it looks possible for sure.


#29

If its translates to other country postcodes I think this will be big big thing for many people.
The distance script you posted about the other day beside this would be the ultimate tool for takeaway/delivery

I need to get to bed anyway, been up for over 38 hrs :frowning: had an all nighter setting up the Thai restaurant products & menu etc I posted a video of earlier as they are opening Tuesday LOL


#30

Found an interesting page;
http://ben-major.co.uk/2012/02/using-google-maps-to-lookup-uk-postcodes/

// First, we need to take their postcode and get the lat/lng pair:
$postcode = $_REQUEST['postcode'];

// Sanitize their postcode:
$search_code = urlencode($postcode);
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $search_code . '&sensor=false';
$json = json_decode(file_get_contents($url));

$lat = $json->results[0]->geometry->location->lat;
$lng = $json->results[0]->geometry->location->lng;

// Now build the lookup:
$address_url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' . $lat . ',' . $lng . '&sensor=false';
$address_json = json_decode(file_get_contents($address_url));
$address_data = $address_json->results[0]->address_components;

$street = str_replace('Dr', 'Drive', $address_data[1]->long_name);
$town = $address_data[2]->long_name;
$county = $address_data[3]->long_name;

$array = array('street' => $street, 'town' => $town, 'county' => $county);
echo json_encode($array);

Looking a the comments they say it can only return a single address rather than a list but if you can resolve down to street that will work we’ll for majority of people I think.


#31

@kendash not wanting to pester as not a vital aspect that is holding me up but give a shout when you have a sec to look at this again.

On a separate note, I have acquired a postcode database from a friend out of his booking system.
Issue with that is its in an unfamiliar format and in 5 tables where postcode has a number for street, town etc which is a reference to separate street, town tablets.
Am going to attempt to compile it in to a text file to work in samba over the next week or so which will be handy for future projects.


#32

for records, that regex seems working fine for validating post codes.

(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})

Using a regex is very useful as SambaPOS won’t lookup for entries that does not matches. That makes it work faster for big databases.


#33

Also on next version you’ll be able to use {CALL:X} tag as value source instead of file name. That will allow us to integrate custom fields to web services. While calling use it as

{CALL:lib.function('$1')}
  • $1 is a place holder and replaces with entered value by operator. For example on post code case $1 will be entered post code value.
  • That function should return a comma separated string value. That result will map to fields by using mapping configuration as usual. On next version that configuration has it’s own setting.
  • As you know calling web services is a little expensive as it will take a while to retrieve values. Using regex formatting will be useful to trigger it to work as a full post code entered. Other than reading addresses we can also calculate servicing fee and store it as a separate custom field. So we won’t need to execute web services during operation.

#34

Is there any way to show a list of results if you used the full address database where it gave you list of house name/number as well as street like on many website?
As I understand it at the minute samba will fill the details using the first matching entry?
While filling to street level is probably more than adequate, if you spent the money of a full PAF database which gave list of full address for a postcode it would be nice to be able to fully routinise it.


#35

Well @JTRTech what I’m really doing is trying to extract ideas from your discussions and try implement needed features. We’ll never implement features specifically to integrate SambaPOS to UK postcode system but I’ll implement all needed features to allow integrate that kind of “things” to SambaPOS. In fact that first brought to attention with Holland’s postcode system as the example and I’ve implemented features in general to use any kind of data as value source. I’m living in Turkey. Here, there is no similar system. There is postal code system but too few people knows what their postal code is and it is not a requirement for postal addresses. So I don’t know what PAF database is :slight_smile: how people spends money for it or how it works.

What I’ve did by implementing JScript integration will allow any kind of integration. It is possible to use ask question dialog inside JScript and so it might be possible to show a result list. I don’t know if it will be easier to just type the address or try to pick correct one from 50 addresses but when needed I can improve ask question dialog to handle too many options. Again I don’t know if it will be 50, more or less. Just guessing that.

So you’ll think you’re not a programmer and how you’ll implement that. This is more about improving our community. Remember how Columbian ticket numbers requirement implemented. Someone building a global solution should learn details for all kind of local laws & requirements. It is a hard task for even biggest POS software manufacturers. That’s why what we’re doing here -as a community- is amazing. We allow people to change SambaPOS for their local needs.

You may also think why we’re progressing slow for that kind of features compared to others. This is also about how many people interested about specific features. In fact discussions about Google distance API inspired me to allow JScript integration. As people discuss stuff here we’ll move faster and have better features.


#36

All completely understandable :smile:
Am not expecting anything specificity for me, and understand development has to be very general.
As they say, if the question isn’t asked you never know :slight_smile:
I personally hope that kendash will be able to have time to work something out for address lookup using google as that would be a free method and generally globally applicable. Wish I had better understanding and could do myself :frowning:


#37

Haha OK of course it is not personal. This is how we approach to that kind of needs. Same thing applies to website integrations, hardware integrations etc… I mean instead of working specifically to integrate a popular online ordering system to SambaPOS I prefer to focus on general solutions to integrate all online ordering systems to SambaPOS. So interested people can integrate their services for SambaPOS users.

I don’t mean I’ll never write integration code. I’ll still write code to solve that kind of needs. The difference is these won’t exists in SambaPOS codebase. They’ll shared as JScript by using Auto Configuration feature.


#38

I’ll visit this but I’m going to spend my time on employee scheduling atm. I was hoping you might catch on to what i was doing.


#39

I wish mate, I got just of what you were doing but how you were doing it :-S not so much.


#40

This is @JTRTech’s sample code that translated to SambaPOS JScript.

function Read(postcode)
{
   var country = 'GB'
   var urlfmt = 'https://maps.googleapis.com/maps/api/geocode/json?components=postal_code:'+postcode;
   if(country != undefined) 
     urlfmt += '|country:'+country;
   var content = web.Download(urlfmt);
   var obj = JSON.parse(content);
   var lat = obj.results[0].geometry.location.lat;
   var lng = obj.results[0].geometry.location.lng;

   var addrurl = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' + lat + ',' + lng + '&sensor=false'
   var addr = web.Download(addrurl);
   var addrObject = JSON.parse(addr);
   var data = addrObject.results[0].address_components;
   
   var street = data[1].long_name;
   var local = data[2].long_name;
   var town = data[3].long_name;
   var area = data[4].long_name;

   return postcode + ',' + street + ',' + local + ',' + town + ',' + area;
}