Skip to content
    • About

Reza Snowdon's Blog

  • MySQL UK post code table with corresponding longitude and latitude

    July 1st, 2014

    (Edit March 2016) Please note the freemaptools site now maintains a MySQL version so it would probably be best to use theirs instead.

    One of the projects I have been working on recently involved the calculation of the distance between two postcodes. Google provides a decent API and retrieving the distance between two post codes with map routing is very simple. For example to calculate the distance between Fountains Abbey and Northallerton high street using their postcodes would only require:

    https://maps.googleapis.com/maps/api/distancematrix/json?origins=HG43DY&destinations=DL78LW&sensor=false&units=imperial

    The downside to this is that there are request limits imposed on this API and you can only make a certain amount before you need to hand over some cash to Google. Due to the nature of the project, I realized these limits would quickly be met and as a result found the Google API to be unsuitable.
    An alternative would be to find a database with all the UK post codes and their longitude and latitude coordinates. You could then easily lookup the coordinates of a post code and pass them to a routing engine (e.g. Open Source Routing Machine) to calculate distances by road or potentially just a simple ‘as the crow flies’ distance calculation using trigonometry.Open Source Routing Machine Logo

    It didn’t take long to find the fantastic freemaptools.com which has a csv containing all UK postcodes and their coordinates. Unfortunately they only had an MSSQL version so I whipped up a MySQL version which worked nicely and is just below for your perusal and use. The same goes for the output the script generates:

    Output of the script: mysql_postcodes.zip (35M) Last updated 01/07/2014

    The PHP script on github

    Readme

    PHP code for taking the csv found at http://www.freemaptools.com/download-uk-postcode-lat-lng.htm and importing it into a mysql database. You can use the script in two ways:

    1. Fill in the database details at the start of the file and run the queries as they are built.

      php import.php postcodes.csv

    2. Specify ‘echo’ as the 2nd argument and output the sql to a file.

      php import.php postcodes.csv echo > mysql_postcodes.sql

     

     

  • Arduino working delay function

    May 11th, 2014

    I recently started actually playing with my arduino a bit more and got to a point where I needed to monitor user input on the device during a delay.

    As I understand it, the delay() function stops the microcontroller in its tracks and all it does is count the interval specified (e.g. delay(1000) denotes 1000ms). The arduino playground provides a great example which I tweaked so it all sat in a function and could essentially replace all the delay function calls.

    I used code from: http://arduino.cc/en/Tutorial/BlinkWithoutDelay as a basis and wrapped the key bits up in a function which you should just be able to copy and paste and replace all your delay calls with this function but of course remove the random number sum code as that’s just an example.

    // Used instead of delay so various things can still be checked (e.g. button presses, saving data).
    int workingDelay(int interval) {
      unsigned long previousMillis = millis();
      while (true) {
        unsigned long currentMillis = millis();
        Serial.println(currentMillis);
        // If the current miliseconds run is more than the interval, the delay has been done, so exit.
        if (currentMillis - previousMillis > interval) {
          return 0;
        } else {
          // running during delay
          int randNum1 = random(300);
          int randNum2 = random(300);
          int randNumSum = randNum1 + randNum2;
          Serial.println("Sum result:");
          Serial.println(randNumSum);
          // end running during delay
        }
      }
    }

    There is of course one thing to bear in mind which is that if you put a function that say takes 3 seconds in and use the delay button specifying an interval of 1 second, it’s just not going to work.

  • libmagic/magic.h library tutorial with example. (It’s magic!)

    December 22nd, 2013

    Libmagic is generally used to determine a file type, for example if it’s a jpg, avi, or whatever, you can point it to a file and it will determine what sort of file it is using magic numbers.

    I found only a few links on the library and the majority were all clones of the man file with this one being the best I found.

    http://swoolley.org/man.cgi/3/libmagic

    My googling skills/patience failed me it seems as I couldn’t find anything more detailed. However, I looked at some other projects that used it and experimented and got it working. Here is some example code, which takes “/file/you/want.yay” and returns a mime to it (e.g. text/x-c; charset=us-ascii). You can get it to return different stuff, check the previous link for more details.

    #include <stdio.h>
    #include <magic.h>
    
    int main(void)
    {
    	char *actual_file = "/file/you/want.yay";
    	const char *magic_full;
    	magic_t magic_cookie;
    	/*MAGIC_MIME tells magic to return a mime of the file, but you can specify different things*/
    	magic_cookie = magic_open(MAGIC_MIME);
    		if (magic_cookie == NULL) {
    	        printf("unable to initialize magic library\n");
    	        return 1;
    		    }
    	    printf("Loading default magic database\n");
    	    if (magic_load(magic_cookie, NULL) != 0) {
    	        printf("cannot load magic database - %s\n", magic_error(magic_cookie));
    	        magic_close(magic_cookie);
    	        return 1;
    	    }
    	magic_full = magic_file(magic_cookie, actual_file);
    	printf("%s\n", magic_full);
    	magic_close(magic_cookie);
            return 0;
    }
    
    

    To compile it do: “gcc magic_test.c -o magics -lmagic”. Furthermore make sure the libmagic library is installed. “apt-get install libmagic-dev” should do it if you’re using a machine with debian/ubuntu.

    Apologies for the bad formatting, it seems eclipse and wordpress don’t agree with each other.

←Previous Page
1 2 3

Proudly powered by WordPress