Please visit my new campsite listing site ukcampingmap.co.uk


Archive for August, 2009

Polish – your new favourite language

Sunday, August 30th, 2009
The faces say it all - mutual incomprehensibility

The faces say it all - mutual incomprehensibility

I speak Welsh and get a lot of stick for its seemingly endless streams of consonants, but just look at this:

Wczoraj w nocy uruchomiliśmy (z użytkownikiem @seler) w końcu Digerati – videocast rozrywkowo-technologiczny or whatever. Projekt non-profit robiony w wolnym czasie. Jakość obrazu i dźwięku pozostawia wiele do życzenia, ale obiecuję, że od 6stego odcinka będzie już kozacka, bo zainwestowaliśmy w sprzęt ;] Dodającym do subskrypcji na itunes i rss stawiam piwo ;]

Polish surely would win in a head to head indecipherability contest.

A chip off the old fox

Sunday, August 30th, 2009

I can’t understand why people have a problem with the BBC. Well, I can, but I have no respect for the point of view. The general argument goes thus:

The BBC has a state-funded huge budget stifling the market, which is bad for competition, news, programming … (never mind that the BBC actually concentrates a lot on very high-quality, balanced programming, raising the bar for everyone else, and actually welcomes strong competition).

But James Murdoch, son of Voldemort Rupert, now head of the Murdoch media empire in UK, and probable heir to the global monolith that is NewsCorp… James Murdoch, one of the most influential media figures in the world, has just had a pop. To comment on a few choice pieces:

This year is the 150th anniversary of Darwin’s The Origin of Species. [...] It argued that the most dramatic evolutionary changes can occur through an entirely natural process. Darwin proved that evolution is unmanaged.[...]  The number who reject Darwin and cling to the concept of creationism is substantial.  And it crops up in some surprising places. For example, right here in the broadcasting sector in the UK.

I love/hate it when people misappropriate evolutionary theory. Darwin didn’t prove evolution is unmanaged, he merely examined evidence that evolution was happening and coined the theory of natural selection to explain how it happened. Below our Jimmy M displays his ignorance of what the theory of evolution says.

The consensus appears to be that creationism – the belief in a managed process with an omniscient authority -  is the only way to achieve successful outcomes.  There is general agreement that the natural operation of the market is inadequate, and that a better outcome can be achieved through the wisdom and activity of governments and regulators.

Now, Darwin said nothing about whether the results of natural selection are better than creationism (in fact, he’d maybe see it as a meaningless question), he only argued that natural selection is what happens. So whether creationism (what an absolutely absurd way for Jimmy to make his speech sound intelligent and relevant) or natural selection is the best way to manage the development of a system must be evaluated as a separate exercise, external to the system itself – defining what constitutes a good result and weighing up which method gets you closest to a good result. There is nothing in the theory of evolution that is anti-’creationism’ at all (other than that in the case of nature it dissolves the need for postulating a creator).

When I say this I feel like a crazy relative who everyone is a little embarrassed by and for sure is not to be taken too seriously.

No wonder.

Creationism penalises the poorest in our society with regressive taxes and policies – like the licence fee and digital switchover;

Still using the laughable creationism/evolution debate paradigm, J.Murd? OK – I’ll play along. There are plenty of examples (arguably most of the activity of government) where state control does the very opposite. Healthcare, education, rubbish collection – these are all examples where central control and collection of fees/taxes leads to all sorts of things being affordable by the poor. Without the licencing fee the money for the BBC’s high-quality output would need to come from somewhere, probably from subscription fees which surely would be at least about as high as the licence fee.

The study of evolution reminds us that  it is very difficult to predict the outcomes of events. [...]

Witness the international banana market.  In the 1950s the banana export industry faced a problem:  the then dominant Gros Michel – or ‘Big Mike’ – variety was being wiped out by a fungus called Panama Disease.  The industry took the decision to replace the entire world export crop with a supposedly disease-resistant variety called the Cavendish banana – the one we eat today.  Unfortunately it now appears that these bananas may themselves be vulnerable to a different kind of Panama Disease. Since Cavendish bananas are genetically identical sterile clones, they cannot build up any resistance.

There are important lessons here: attempts to manage natural diversity have unpredictable consequences and are more likely than not to fail over the long-term.

I do like the banana story, Jay-Mur. I find it whimsical. Though not entirely relevant. How’s about a better example: livestock have been bred over many, many generations to give better fleeces, milk and meat. Do you oppose that management of natural diversity? Of course not. Getting rid of all media channels in the world/UK except for the BBC I would of course oppose (though it’s no worse than having a Murdoch media monopoly), but that is not the situation being faced.

Fourth question.  Is this creationism good for investment?  No.  A heavily regulated environment with a  large public sector crowds out the opportunity for profit, hinders the creation of new jobs,  and dampens innovation in our sector.

This is far from true. The BBC contract out more and more work to independent companies, so there is still a lot of diversity. Because the investment comes from the BBC and not other largemedia conglomerates (let’s face it, the BBC isn’t the only bull in the TV shop) you could argue it protects diversity as the BBC has to spend money on innovation, minority interests, regional programming … diversity, in short.

We don’t even have the basics in place to protect creative work. Whether it’s shoplifting at HMV or pirating the same movie online, theft is theft. They are both crimes and should be treated accordingly.  The government dithers – dimly aware of what it has to do but afraid to do it.

Do you see other countries tackling it better, J-Mu?

As originally with news and sport, so now with the arts and drama.  Sky now offers four dedicated arts channels.  Original commissioning by channels that customers choose to pay for is expanding and will continue to do so, not just from Sky but from the likes of National Geographic, History, MTV and the Disney Channel, to name a few.  Sky alone now invests over £1 billion a year
in UK content.

Remind me – was your speech about how stifled the rest of the industry is by the BBC, or about how the rest of the industry is doing pretty well too? It’s begining to sound like you’re making a mountain out of an imaginary molehill. I would add to the above though that I’ve watched a fair bit of cable/satellite TV over the years and, while it’s difficult to define exactly what, they definitely don’t do some things like the BBC do them. I think subtlety is the lacking ingredient. Having a TV channel that doesn’t have to attract advertising for each programme definitely enables the production of programmes that aren’t so brazen about garbbing your attention. David Attenborough, Adam Curtis, The Royle Family, The Office – these might all have never come to light without an organisation that isn’t always searching for more bang in order to get more buck.

And now I’ll stop quoting as this post is getting ridiculously long, but you get the jist: Jammon has quite an axe to grind against a country whose citizen’s don’t mind paying a public body to produce a lot of good quality TV and other media because it’s unbalanced, stifling, power grabbing… sinister, even… nothing like his dad.

I think I get it

Thursday, August 27th, 2009

‘It’ being object oriented programming (OOP).

For a long time I got frustrated with reading tutorials telling you about inheritance and other useful features of using OOP’s class-based approach. The trouble is that in the example scenarios given (eg a ‘bird’ class which inherits from an ‘animal’ class) the set theory-like approach to writing code to represent things makes sense. But when it comes to building a website or something similar, and creating classes which do not display a nested structure, the analogy with set theory falls away and largely (at least in the websites I’ve worked on) you’re left with a convoluted structure which, at first glance, could just be done away with in favour of using normal variables and functions instead of properties and methods. I could see how perhaps the stronger structure might help organise code, and was gradually getting mor ecomfortable with the techniques, but still didn’t really get why using OOP was so important.

But now I think I do, courtesy of wikipedia, which has the best summary of the motivation behind OOP I’ve ever read:

The methodology focuses on data rather than processes, with programs composed of self-sufficient modules (objects) each containing all the information needed to manipulate its own data structure. This is in contrast to the existing modular programming which had been dominant for many years that focused on the function of a module, rather than specifically the data, but equally provided for code reuse, and self-sufficient reusable units of programming logic, enabling collaboration through the use of linked modules (subroutines). This more conventional approach, which still persists, tends to consider data and behavior separately.

After reading this it’s all become clear to me what the point is – creating a more concrete connection between a thing and what can be done to it. In a real world situation I would liken it to having the steering wheel in the car, always ready to be used to steer the car, as opposed to having a steering wheel which can be passed around and maybe tried on other things, eg a chicken, a whale, a front-bench tory… where the results would be unpredictable (apart from the tory, who would almost certainly find the experience arousing). And as long as you define the steering wheel at a high enough level (eg the ‘vehicle’ class has a steering wheel, which the ‘car’ class inherits) you don’t fall foul of code duplication either.

The joy of feedback

Wednesday, August 26th, 2009

I won’t go into details, but I’ve had quite a tough month or so in the web programming side of my life. A lot of hard work and dedication leading to a very unsatisfactory ending – the first time in my adult life I’ve left a job with a bitter taste in my mouth.

So I thought I’d write this to say thank you to everyone who has, in the same time period, contributed to the small flood of comments relating to my two jQuery plugins – fullTextArea and crossSelect. Up until recently I had no idea anyone was even using them, so it really brightened the last 4 weeks to know that the free time I put into programming is appreciated (even if the same can’t be said for paid work all the time).

So thank you again for restoring my faith in the programming community. I’ll get on to incorporating the best suggestions as soon as I can.

Where you headin’, luv?

Friday, August 21st, 2009

In previous posts I’ve covered the inherent difficulty of geocoding addresses and postcodes in the UK, specifically in order use the geocodes with Google maps. I learned a lot about the limitations of Google’s various geocoding services. To sum up the situation:

Previously I’d been geocoding large batches of addresses/postcodes, but having got the geocodes in order to plot points on the map, I wanted to add search functionality to the map, so that it would zoom in on a given address/postcode. I wanted it to be accurate for both addresses and postcodes, so using what I’d learned, I wrote a javascript function, geocodeUKAddress, which always returns the best geocode that Google can offer, so your website can be as reliable as a London cabbie (again, I can’t embed it here as javascript has a habit of breaking wordpress, though there must be some way to make it safe – I will research).

You will need to include the following in ther head of your webpage too

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=your_api_key_here" type="text/javascript"></script>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>

I can’t take credit for the regular expression that recognises UK postcodes, but apart from that, it’s all my own work, which anyone can feel free to use. Unless you maybe work for Rupert Murdoch.

Dream team

Thursday, August 20th, 2009

While checking up on an old site I worked on I discovered the site is now completely broken (it’s back up now – a slightly different design but still built on the work I did I’m happy to see). Doing a  bit more research I found that online fantasy football games are a  hot topic on fitness forums.

Maybe you can play it on the wii these days.

What shape is County Durham?

Thursday, August 20th, 2009

Anyone got any idea which it is?

Geocoding in the UK

Sunday, August 16th, 2009

The art of geocoding addresses in the UK, as I previously explained, is a soul-destroying process, frought with inaccuracy, bugs and convoluted workarounds. And for all that work you end up with a set of points of which a great deal are probably somewhat inaccurate and at least some of which are completely wrong. UK addresses (and probably those elsewhere in the world) are complicated creatures, which Google’s geocoding engine often interprets wrongly.

Postcodes, on the other hand, are rather easier; there is a well-defined relationship between a UK postcode and its corresponding (usually pretty small) piece of the British countryside. But google’s geocoding api will only return a geocode for the postcode sector (ie will give a geocode for LL12 5 when you searched for LL12 5TH). However, someone did figure out a way of using Google’s local search API combined with google maps to geocode UK postcodes. Since he blogged about it the API has changed, so below is an outline of how to geocode a batch of postcodes in the UK using just some simple php, the current google ajax search API and a little javascript (jQuery isn’t essential, but cuts down on coding a bit). The javascript is the crucial step.

Assuming you have a database full of postcodes and id numbers, and 2 empty columns to store latitude and longitude values, this is how it’s done. (Download source geocode.zip).

1. Create a html page geocode.html with the following content:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >

<head>
<title></title>
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="" type="text/css" media="screen" />
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript" src="geocode.js"></script>
</head>
<body>
<div id="counter"></div>
</body>
</html>

(Make sure you specify the correct location for your local javascript files)

2. Create a php file (in the same directory), geocode.php, with the following rough structure (it will only be accessed via ajax, so is very stripped down):

<?php
require_once ('mysqlConnect.php'); //or other database connection details
if($_GET)
{
 //var_dump($_GET);
 update_record();
 send_new_data();
}

//gets the next record without a geocode and sends the id and postcode to the browser
function send_new_data() {
 $query = @mysql_query("SELECT id, postcode FROM geocode_table WHERE lat = '' AND postcode != '' ORDER BY id LIMIT 1");
 if(($query) &&mysql_num_rows($query)) {
  $row = mysql_fetch_array($query, MYSQL_ASSOC);
  echo $row['id'].','.$row['postcode'];
 } else {
  echo 'stop';
 }
}

//updates the last record with data sent from browser
function update_record() {
 $id = $_GET['id'];
 $lat = $_GET['lat'];
 $lng = $_GET['lng'];
 if($id > 0)
 {
  $update = "UPDATE geocode_table SET lat = '".$lat."', lng = '".$lng."' WHERE id = ".$id;
  $result = @mysql_query($update);
  if (!$result) {
   die('Invalid query: ' . mysql_error());
  }
 }
}
?>

3. Create a javascript file geocode.js, saved in the same directory again (I would paste it here but it keeps breaking wordpress)

4. Running the code

Once you’ve altered the database connection details, and SQL query to suit your setup, simply open geocode.html in your browser. A counter will tell you which record you’re on. To stop the code simply close your browser/browser tab.

How it all works

In a nutshell (ignoring the special case of starting off the loop) the code repeatedly performs the following process:

….in geocode.php, send_new_data() finds a record which has no latitude value and sends it’s id number and postcode as an ajax response to set_and_get_next(). This keeps track of the id in a global variable and sends the postcode to getPointFromPostcode(), which uses google’s local search to get a geocode. Once it’s found a geocode it passes it to set_and_get_next(), which sends it to geocode.php in an ajax request. There update_record()… well… updates the record, and send_new_data() finds a record which has no la….

Compared to my previous approach iterating a script over large sets of data, using ajax is very sleek. Similarly to a pure php script I can load from a browser, though with much of the resource intensive scripting taking place on my or google’s server. But with ajax there’s no problem with the browser timing out from time to time, or baulking at the number of times a page is requested. It’s a little harder to code, and probably less efficient… but I like it. And I’ll definitely be using my shiny new geocoded postcode data.

To splice or not to delete

Friday, August 14th, 2009

I haven’t blogged much about programming recently due to being too busy… too busy to blog, not too busy to program.

This is just a swift post to highlight an important distinction that I haven’t seen mentioned anywhere else, and which recently I realised the siginificance of.

In javascript, what’s the difference between

delete my_array[i];

and

my_array.splice(i, 1);

Superficially they’re the same – they each remove the ith element from an array. But the crucial difference is that delete leaves a gap in the array, so there will no longer be an ith element – the array jumps straight from the (i-1)th to the (i+1)th element. But splice moves the (i+1)th element into the ith place in the array, and so on, shortening the array by one but moving everything along to fill the gap.

Why is this important?

If your code relies often on iterating through an array thus:

for(i=0;i<my_array.length;i++)
{
    //some code
}

then any future iterations over the array will throw errors if you used delete to remove the ith element. But the iterations should still work if you used the splice method.

One note of caution, if you carry out

my_array.splice(i, 1);

within a loop over i, you should add the line

i--;

immediately after, to make sure you don’t miss out the next array element (which is now, of course, not in the (i+1)th place, but in the ith place).

I’ve heard of drowning your sorrows, but this is ridiculous

Friday, August 14th, 2009

From the 2003 Licensing Act:

174 Certifying of premises on grounds of national security

(1) A Minister of the Crown may issue a certificate under this section in respect of any premises, if he considers that it is appropriate to do so for the purposes of safeguarding national security.

It’s reassuring to know that if the terrorists do attack, the government is able to turn anywhere it likes into a pub or club.