Google Buys Waze

In an expected move (it was almost done a few days ago, and has been in the making),
Google announced it bought Waze. Waze_logo
Waze – The Social GPS App from Israel, has been greatly popular within car drivers, helping people dodge traffic jams,
road blocks, and a lot more, was created at 2008 (which makes it one of the firsts of its kind).

The Sum is still undisclosed,
but probably was around 1.3-1.4 Billion (at least that’s what was reported earlier)

More information

Tagged with: , , ,
Posted in Technology

Getting Distance between 2 points with MySQL Query


Calculating the distance between 2 points (longitude and latitude) certainly sounds like something that should be solved on the code layer, not on the database layer.

However, more often than not, it’s a lot faster to write it inside a query and get the result for a big list of locations.

So how is it done?
By using the Haversine formula (it’s not hard, I swear).
To calculate the great-circle distance of two points on a sphere
(for more information, please go to your local library, if year > 2004, check wikipedia)

The implementation of which (in a MySQL query), looks like this:

SELECT 6353 * 2 * ASIN(SQRT( POWER(SIN((lat1 -
abs(lat2)) * pi()/180 / 2),2) + COS(lat1 * pi()/180 ) * COS( 
abs(lat2) *  pi()/180) * POWER(SIN((long1 - long2) *  pi()/180 / 2), 2) ))

(6353 – Is the Earth’s Radius (in KM))
You can test it out, for instance, let’s get the distance between
the Eiffel Tower (48.858278,2.294254) and Big Ben (51.500705,-0.124575),
which should be about 340 KM aerial distance (man Europe is small!)
so running our query with the Longitudes and Latitudes set:

SELECT ROUND(6353 * 2 * ASIN(SQRT( POWER(SIN((48.858278 -
abs(51.500705)) * pi()/180 / 2),2) + COS(48.858278 * pi()/180 ) * COS( 
abs(51.500705) *  pi()/180) * POWER(SIN((2.294254 - -0.124575) *  pi()/180 / 2), 2) )), 2)

gets us 339.58.
As you can see I used the ROUND(..,2) function to make the result a bit more readable.

If you want to use this often, you might want to write a function that calculates distance:

CREATE FUNCTION calc_distance (lat1 DECIMAL(10,6), long1 DECIMAL(10,6), lat2 DECIMAL(10,6), long2 DECIMAL(10,6))
RETURNS DECIMAL(10,6)
RETURN (6353 * 2 * ASIN(SQRT( POWER(SIN((lat1 - abs(lat2)) * pi()/180 / 2),2) + COS(lat1 * pi()/180 ) * COS( abs(lat2) *  pi()/180) * POWER(SIN((long1 - long2) *  pi()/180 / 2), 2) )))

SELECT ROUND(calc_distance(51.500705,-0.124575,48.858278,2.294254), 2) --339.58

That’s better, now we can just use the function in queries to quickly calculate distances.

As a side note:
I do highly recommend using MySQL Spatial extensions for more complex calculations, though
honestly it seems that PostgreSQL has the upper hand in that area.

Posted in Technology

Defining custom XPath functions on Python lxml

Python & XPath
Parsing with lxml (Python library ) is a very good experience, especially compared to PHP.
An example advantage is that every element found by XPath is once again searchable
(whereas with PHP you can only provide it as a context, but you must provide the DOM ).
However, I came across a case where I needed to split the result to a list (split on python, explode on PHP).

This should be doable when using the “tokenize” function within the XPath query, but for some reason lxml doesn’t support it (neither does PHP, I think it’s an XPath 2.0 function ).
The solution : make your own custom functions!

from lxml import etree
ns = etree.FunctionNamespace(None)
ns['tokenize'] = tokenize
def tokenize(context, string, split_token):
    if type(string) is list:
        string = string[0]
    return string.split(split_token)

#example: element.xpath('tokenize(//p,";")')

This is basically it, you just have to make sure the function you’re pointing to actually exists.
However, to make this a bit more useful, I decided to separate all my custom functions and pack them in another file (OOP FTW),

For clarity’s sake I decided make all the helper functions start with the prefix “xpath_func_”
e.g. “xpath_func_remove_spaces”, “xpath_func_replace_dollar_signs” and so on.

helper.py:

def xpath_func_tokenize(context, string, split_token):
    if type(string) is list:
        string = string[0]
    return string.split(split_token)
def xpath_func_make_title(context, string):
    if type(string) is list:
        string = string[0]
    return string.title()

main.py

from lxml import etree
import helper
ns = etree.FunctionNamespace(None)
for helper_func in dir(helper):
    func_prefix = 'xpath_func_'
    if helper_func.startswith(func_prefix):
        ns[helper_func[len(func_prefix):]] = getattr(helper,helper_func)  #function's name is without the prefix

A working example can be found on this Github Repository

Posted in Technology

New Google Maps

I’ve recently got an invitation to the new Google Maps.
The slick new interface allows you (among others ) to zoom way out and see the earth.
More than just some Google Earth features that were implemented,
the entire interface is definitely more Social oriented,
you can got photos of the places you’re looking at a lot easier.

The map is definitely more dynamic, showing only things you’re looking for.
Directions have improved a lot, they now include Flying, and Public Transportation gives a lot more information:
google_maps_fly_directions
oh, so that’s how you get from NY to SF, I was looking forward to a 44 hour drive

The search results are a lot easier to sift through, Street View, Inside Look (street view INSIDE!), and Pictures are available without getting into a separate page.
google_maps_search_results
Good on ya, San Francisco!

And after all, this slick new design means nothing if you can’t watch the Sun setting on the Earth (live, kinda)
new_google_maps
Might need some sunglasses to view this image.

Tagged with: ,
Posted in Technology

Yahoo increases Flickr’s storage to 1 Tera Byte

With a move reminiscing of Google’s 1GB Inbox (Gmail) back at 2004,flickr
Yahoo is attempting to set a new standard when it comes to Photo storage.

The redesign and huge storage increase for Flickr was made 4 days ago, on May 20th,
naturally the upgrade prices and deals have been changed accordingly (no one is going to pay for 100GB anymore),
and there is an update to the Android & iOS apps, which I must say (on Android) is pretty gorgeous.

Similarly to Email services following Google’s new standard (or just disappearing),
one could hope the same will happen after Yahoo’s announcement, especially Picasa,
(these days probably Google+), not to mention Dropbox (one can dream, can’t one?)

Until then, if a change is not made in the next few months,
I might consider using Flickr to store my Photos

Tagged with: ,
Posted in Technology

New Langauge added to App Engine – PHP

A few new things were introduced on Google I/O yesterday.appengine_with_php

Soon you’ll be able to talk to Google.com by saying
“Ok, Google” and then search for whatever you’d like.
Some advancements were shown for Google+ and for Chrome, nothing major though.
There was even a Samsung Galaxy S4 with Vanilla Android introduced (are you suggesting 8 GB of Samsung crapware is not something everyone wants??),
and a new IDE for Android development called Android Studio (now that’s getting more interesting).

Another good thing released in the past day, is a support for a new language on the Google App Engine.
Of all the languages I would’ve suspected Google to support, PHP is definitely a surprise.

I mean, Google doesn’t do much with PHP, one would think that with the ongoing work on V8,
that they might add support for JS, Dart, even Ruby would’ve made a little sense (seems to be popular on Heroku).

Something that seems interesting though, is that Google might be trying to get a huge chunk
of the developer community (face it, most web developers are PHP developers),
and get it to deploy their apps on App Engine (loads of advantages for that).

With Heroku not supporting PHP (though Amazon Web Services do),
this might be a smart move.

I guess only time will tell.

On a more personal note, being with over a decade experience with PHP,
I’m definitely more than glad that the support was added,
and will look into deploying some apps in the near future!

Tagged with: , , , ,
Posted in Technology
Welcome to KidsIL
A blog for Technology, Brainstorming & Berlin!


You should try Berlin On Feier, an App I built for finding the best parties in Berlin.