Friday, July 21, 2017

Markdown blogging example

Sunday, September 13, 2015

Thoughts on the perfect city

What is the most important feature of the property (I mean a flat,  house,  appartment or bungalow)? More important than the amout of rooms or how well it's decorated? It's a location!

Would you like a penthouse in a desert? A small house by the sea is a dream of millions. But our cities are far from being a dream come true. Some people live next to the park or a lake,  but most are not. I was thinking - how should have our cities been built to make everybody happy with their property.

In the past, people took what the nature gave them - a cave, leather drape for a door, a branch as a weapon. With time people started making their own tools and build their homes. But we still build cities randomly - where there's some place not too steep and not too wet and not too dry. I think with time people start making their cities according to the plan larger than just "filling the area with buildings packed as close as possible to each other like in Tetris".

Most poeople like fresh air of the forest. So a city must be a forest. Many like lakes. There should be lakes in the forest. Cities should have good bycicle infrastructure. Bycicle roads in a forest. Cars and roads are still needed. Transporting heavy stuff or lazy people is important. Quickly. So we need highways in a forest.

For a biker a 100 m. detour is more annoying that to a car driver. So we build straight and convenient bycicle roads, without traffic lights! Cars can drive around a bit without much trouble.

Now the houses. Current high building are an efficient use of land but that is not a priority anymore. They are a problem for disabled people, for firefighters, for moms with children, for elderly - effectively for all of us at some stage in life.

So we build a 2-3 storey houses. In which arrangement? Currently every house is isolated from it's neighbors. Often with a fence. Everybody has their own garden,  their own lawnmower, own drill, vacuum clener, Internet connection, phone line, network router, printer, etc. This is very ineffective. It's a share nothing architecture. I suggest to build houses arranged in a circle around a central shared area.

This shared area may have a small playground, a common house used by everybody in a circular block, a common garden. In a house there can be a set of instruments and tools, a large table with chairs for everybody in a block, storage shelves for common and private stuff. People should be encouraged to share things they don't use everyday with their closest neighbors. Why not? Of couse, you can have your private juice maker if you like. I can't convince you. You either support the idea of sharing or you don't.

Parking is underground under the buildings. With space for visitor cars and bikes. Cars can be shared as well, by the way.

So we have circular districts of houses with a common shared house in the center located in a forest with bycicle routes to other circular districts, but in which arrangement?

Current cities are either circular (like Moscow) or square (like New York). Circular are more natural as they evolved around a palace, castle or church. We always have a center or a downtown in every city, where there are more of everything (government institutions, restaurants, entertainment, shops) and the property prices are higher. This leads to traffic into and out of the center,  parking problems, pollution, waste management - all the city problems we heard of so much.

My suggestion is to disperse all that can be usually found in the center equally thoughout the forest-city. There will be a bakery for each X of the circular districts, a kindergarden, school, cinema, hospital and everything else. Yes, this way we need more of these facilities and they will be smaller. Different facilities will have a different dispercity level, of course. But they should not tend to be situated close to each other. Or maybe we can have islands of public service buildings for every X of the living blocks.

Do you imagine a homogenous city consisting of neighborhoods in a forest? Such city can be easily expanded in any direction by just adding living districts with some islands of public service areas. These areas should be reachable in 5-10 minutes by bike.

Would you live in such a city? Let me know in the comments.

About Mapping Services and Other PHP Libraries for using in Mashup application

How I built an using third party libraries from is a site where users may submit images of different locations on Earth (provided with geo-coordinates) so that other users may find nice places to visit. When building the site I had used many different PHP and JavaScript libraries. Here I'm sharing the information I leaned about these libs.


First of all, I use myself and recommend composer. You list the libraries you need to use in your project and then say composer update and all listed libraries get installed into the vendor/ folder. The same command is installing updates to the libs, if available. That's great - no need to check for updates manually. Below is part of my composer.json file. The rest of the post will shortly highlight each of these libraries.

{"name": "have-you-been-here",
    "require": {
        "components/jquery": "*",
        "components/jqueryui": "*",
        "components/bootstrap": "*",
        "hybridauth/hybridauth": "dev-master",
        "blueimp/jquery-file-upload": "dev-master",
        "hpneo/gmaps": "dev-master",
        "": "*",
        "needim/noty": "dev-master",
        "phayes/geoPHP": "dev-master",
        "anthonymartin/geo-location": "dev-master",
        "DanElliottPalmer/GeoJSON-Parser": "dev-master",
        "jrburke/requirejs": "dev-master",
        "flesler/jquery.scrollTo": "dev-master",
        "bradwedell/php-google-map-api": "dev-master",
        "ezyang/htmlpurifier": "dev-master",
        "fg/essence": "dev-master",
        "jakiestfu/Snap.js": "dev-master",
        "components/modernizr": "dev-master"


jQuery is a must in any project. It changes the way you will write JS. Your code would not look like plain JS anymore, but would become easier to read and shorter to write.
JQueryUI is a collection of widgets like a calendar, slider, ... and a nice CSS for these widgets and error messages.


Even better CSS framework than jQueryUI. Just like nobody is writing pure JS without any libraries, you shouldn't write your own CSS from the scratch.


Allows your users to login with Google, Twitter, Facebook and other accounts with OAuth. You can easily add any other OAuth provider which is not pre-configured in Hubridauth.


The best ever file upload lib with support for drag-n'-drop and unobtrusive degradation to standard HTML uploads. Accepting uploads in PHP is a little awkward (no data in $_FILES) but they provide a PHP class for handling uploads on the server.

Map libraries

I find the original Google Maps API too complicated for simple tasks like showing a map at the specified location and showing a single marker with a tooltip information.


Therefore I have gladly used a php-google-map-api. Usage is pretty simple, her3 it shows a map of a specific location and a marker:


$map = new GoogleMapAPI();
$map->_minify_js = TRUE;
$map->width = '100%';
$map->addMarkerByCoords($coords[1], $coords[0], $this->model->getName());
$this->index->header[__METHOD__] = $map->getHeaderJS();
$this->index->header[__METHOD__] .= $map->getMapJS();
$content .= $map->getOnLoad();
$content .= '<div class="popin">'.$map->getMap().'</div>';


This was fine for a static map, but once I needed an interaction with a user in the browser like repositioning the map after geolocation - I had to revert back to JS.
Luckily I've found a gmaps.js a wrapper to Google Maps API with MUCH simplified API than one provided by Google. Making a map is as simple as this:

var map = new GMaps({
  div: '#map',
  lat: -12.043333,
  lng: -77.028333

Adding a marker is so simple:

  lat: -12.043333,
  lng: -77.028333,
  title: 'Lima',
  click: function(e) {
    alert('You clicked in this marker');

Geolocation in one line with four event handlers!

  success: function(position) {
    map.setCenter(position.coords.latitude, position.coords.longitude);
  error: function(error) {
    alert('Geolocation failed: '+error.message);
  not_supported: function() {
    alert("Your browser does not support geolocation");
  always: function() {

Definitely a way to go if you're new to maps.

GeoPHP and GeoJSON

Once you have a map you most likely need to display more than one marker and download and update markers according to the view area of the map (when user is panning or zooming). Surprisingly, Google doesn't provide any help in this. So everybody is inventing their own JSON/XML format for transferring marker information between server and JS. And,  accordingly, a custom JS to parse and display markers.
There is one standard which kind of goes in the direction of standardizing the geo data transfer - GeoJSON. It allows you to transfer points, polygons, filled areas. I was interested of transferring markers (points in GeoJSON) with additional information (title, tooltip text, click link). Unfortunately point data type is not meant for markers - all additional data are transferred as custom properties and the propery names are not standardized. But there's at least a PHP lib for generating GeoJSON and JS lib for parsing it.
Generation is done without any library like this:

$json = new stdClass();
$json->type = 'FeatureCollection';
$json->features = array();
foreach ($this->data as $row) {
    $feature = new stdClass();
    $feature->type = 'Feature';
    $feature->id = $row['id'];
    $feature->geometry = new stdClass();
    $feature->geometry->type = 'Point';
    $feature->geometry->coordinates = array($row['lon'], $row['lat']);

    $properties = (object)$row;
    $feature->properties = $properties;
    $json->features[] = $feature;
echo json_encode($json);

AJAX requesting and parsing is done with GeoJSON JS lib:

GeoJSON.loadJSON(jsonURL, function(e) {
    var f = GeoJSON.parse(e);

    for (var a = 0; a < f.length; a++) {
        // add to the map
        var latlng = f[a].getCenter();
        if (!self.contains(latlng)) {
            var marker = new google.maps.Marker({
                position: latlng,
                title: f[a]
   = f[a];

As you can see, generation of the markers and reading marker properties is up to you to program.

Server side map support

I always thought (without having a clue) that one needs complicated queries for searching markers in the database. I've seen PostgreSQL even has a plugin for geo support in the DB.
In fact - it's very simple. Google Maps gives you north-east and south-west coordinates of the currently displayed map. This makes your SQL as simple as

WHERE lat BETWEEN 10 and 20
  AND lon BETWEEN 30 and 40

This, of course, requires that you have lat and lon in the database. If you need to geocode addresses into coordinates you cat use Geocoder-PHP.


There is a. lthough one situation where it gets more complicated. When geolocation has beed done in browser, you get lat/lon of the center of the map, but there is no predefined zoom level and hence - no map boundaries to use in the query above.
I decided to start searching for places in the 1 km area around the location. If there are no or less than three places found - I search in the 2, 4, 8, 16 etc. km area. To be able to search for markers inside the specified radius around the specified location requires converting location/radius into bounding-box coordinates. The math of the process is described nicely here. A GeoLocation.php library has a function for this.

function getBoundingBoxFrom(array $pos, $km) {
    $edison = GeoLocation::fromDegrees($pos['lat'], $pos['lon']);
    $coordinates = $edison->boundingCoordinates($km, 'kilometers');

    $lat1 = $coordinates[0]->getLatitudeInDegrees();
    $lon1 = $coordinates[0]->getLongitudeInDegrees();

    $lat2 = $coordinates[1]->getLatitudeInDegrees();
    $lon2 = $coordinates[1]->getLongitudeInDegrees();

    return array($lat1, $lat2, $lon1, $lon2);

SQL will look like this:

list($lat1, $lat2, $lon1, $lon2) = $this->getBoundingBoxFrom($pos, $km);
$pc = new PlaceCollection(NULL, array(
    'lat' => new SQLBetween($lat1, $lat2),
    'lon' => new SQLBetween($lon1, $lon2),

Additional map operations

Autofitting map area to the set of found and diaplayed markers.

After retrieving some markers from the above query and transferring them to JS with GeoJSON one needs to zoom the map so all the markers are visible. Probably one could do the calculations in PHP and transfer that somehow in GeoJSON but this operation is easily done with standard Google Maps API.
You collext all markers in an array and get Boundary class do the work.

Reacting on user panning and zooming.

This is easily done qith standard API.

The event 'idle' is triggered after the map is done panning/zooming. Note that this event should be registered AFTER initial displaying of markers as we do boundary fitting, which triggers pan/zoom itself.

Sanitizing user comments

This is very important. Noone wants his site to be vulnerable to XSS or contain viruses. I thought PHP function 'strip_tags' is good enough for this. You are allowed to define unharmful tags which will be kept (like <i>, <b>). But is appears the function is not safe enough. See explanation on the page of ###. It also reviews other solutions and thwir shortcomings. Using ### is as simple as

Embedding content from user submitted links

OEmbwd stansard allows you to embed contend from other sites when all you have is a URL of the content to embed. This ia how you can have YouTube links to be shown as a player and Share? presentations as presentations. ### is the best library for this. Usage:

It comes with a list of oEmbes aites and you can add moee. Other libraries arw listed here, but I disn't like them as much as ###.

Slideable drawer

I believe this cocept is invented by Android or maybe Facebook app. Anyway, I fins it vwry convenient as a way to hide the sidwbar on the narrow screen devices. After reciewing suggested libraries at ###, I have choaen Snap. It requires a very specific HTML layout and uaes absolute CSS positioning so I had some work to embed it into Bootstrap layout, especially as I dont want any drawer functionality on the desktop beowsers. So I have a browser setection after the pagw ia loaded and rhen I manually adjuat Bootstrap layput to fit the requiremwnts of the Snap.

On mobile browser it looks like this.


See how I detect mobile browser in the coee above? Modernizr is testing different browser feautirws and als adds correspinding classes to the tag, allowing you

Tuesday, February 10, 2015

Idea: Autobahn (Motorway) information app (site)

Imagine you drive on the autobahn and don't have a navigation system. Or you know the way and don't need the navigation system and it's too boring to enter the destination. But you still want to know what awaits you ahead on the way. So you open an app (or site and it will:

  1. Detect your location with navigator.geolocation.getCurrentPosition(function(a) {console.log(a);})
  2. Query OpenStreetMaps with Overpass API to find which is the autobahn you're driving and where are the nearest exits, parkings, fuel stations and distances to the nearest large cities.
    Example query:
  3. Parsing the data is described here:
  4. It shows the queries information on the site (in the app, it's very easy to make an Android app which works exactly like the site using WebView, see
  5. Separately it should download the information from the TMC traffic jams. This doesn't seem to be available for free though.
  6. It may display local ads. Needs to be popular to make sense.
As most people have a navi hardware or software, this it mostly for nerds.

Friday, December 19, 2014

Time Tracking from Commit Messages

Everybody hates time tracking. Mostly because it takes some time (even if it's just several minutes) to enter the TT system, enter the duration, choose the project and maybe write a comment. Programmers have it a little easier. They are used to writing comments after their work into a VCS (Mercurial, Git, etc.).

Why not combine the two. What if you could write a time you spent on the feature directly into the commit message? Some VCS support this already.

Trac (

It's a plugin which is not easy to configure but you can enter the time spent in brackets like this "(1h)".

Freckle Time Tracking (

"So Freckle Time Tracking now comes with Github integration, which means you can instantly log time from Git commit messages when you push updates to Github."

Entering times looks like this "f:2.5". More details:

Redmine (

Supports it since 5 years ago. Format "time 30 m" on a new line.


That's the largest issue tracker, of course it supports it. Format: "#time 1w 2d 4h 30m".


I'm the developer of a small time-tracking system and I don't use any of the above. So I've made a small PHP script which would analyze the commit log and collect the times from it. Format: "[0.5h]" or "[22:15-23:00]". The results would look like this, but it's easy to customize.

Using all the power of millions of idle computers

Do you know how busy is your computer on average? You think 50%? Open a task manager and check yourself. Mine was busy at 2-3% only. That is having a browser, text editor and a couple other app open. Unless you are archiving or extracting an archive, your computer is almost doing nothing. It's the power wasted. Think of thoussands and millions of computers turned on but doing almost nothing. That's maybe not the case for computer professionals, but even they don't use more than 20% of what they have. Whatever you do: browsing, watchin video, listening to music, typing... Especially typing - it can do millions of operations between each two letters that you type. Why doesn't it?
In 2000, when I was graduating, one of the graduation topics was about "agents" - small applications running on multiple computers on the network performing some distributed task. It was a hot topic back then and it looked like in no time, all computers will run some kind of agent.
Now, in 2014, I can't recollect I've heard the word "agent" in years. What happened? And what can we do to revive the idea?

What are agents?

If you have head of SETI@Home or Folding@Home you should know what I mean with an "agent". It's: - a small application - running on multiple computers - receiving tasks to do from the Internet - processing something - sending results back to the Internet server.
This effectively allows to use the power of thousands (millions) computers to do a very complicated task which is split into smalled chunks in parallel. This is a huge potential. Imagine how much would it cost to purchase a thousand average computers, place them somewhere, connect them all, pay for the power they consume and maintenance. This is what companies like Google do. They have the money, many other organizations don't.

My suggestion

Programming a new system like SETI@Home is not a simple task. Redoing it again for another project is a waste of time and not DRY( But most complicated is to enlist users to download and install one more client on their computers. Why not having a general agent system which is capable of running any task? Users only need to install it once, but the participating organizations would only need to program the actual computational algorithms (about 20%) - not the 80% of the infrastructural code. This will allow smaller ogranizations to gain access to free computer power - benefiting the society. Users only need to install an agent once and rest assured they are helping the world by just not turning their computer off.
It should be written in Java. I don't like Java so much, but there's no other language which can be run on almost any computer that exists. It also features a relatively good security by isolating the running code from the operating system.
The client would be represented by a task bar icon. When you open the details it should show a project it is working on (SETI, Folding or anything else), current CPU idle percentage, CPU idle percentage if no task was running, total processing time spent since the installation and some kind of reward points collected for donating your computer. It should detect when computer needs some extra power for doing user-initiated task and pause the task processing so that users don't feel that their computer is any slower than before.
A server should be able to maintain a list of projects and hold the source and processed data for the project. The results may be obtained by the project's owner organization. A website of the project can show the total processing power in use (FLOPS).
BTW, do you know that the total processing power of Bitcoin mining machines have exceeded 16 petaFLOPS? The world's top ten (all ten of them) supercomputers can only do 5% of that. And Bitcoin mining is NOT what your average neigbour is doing. The amount of processing power being wasted is just mind blowing. The cure for cancer maybe found just 10, 100, 1000 times faster. Imagine a thousand other projects which are NOT done because of lack of computer power.
Would you allow your computer to do some research while you are browsing?

Monday, July 07, 2014

Pringles Trash Bin for the Car

I often feel sorry when I see car drivers throwing trash and cigarette buds out of the window. As if the whole world is a trash bin. I bet they don't do this at home. For those who retain some kind of conscience I have a tip.

  1. Buy one Pringles chips can and make it empty. You dont need to eat the chips - they aren't healthy food. 
  2. Then stick the empty can into the bottle holder in your car. You may need to squash the base of the can if it doesn't fit. 
Now you have the whole liter of the free space for the litter in the car. It has a tiny footprint, its held tight, it doesn't have a bottle neck, its easy to empty when it gets full. And it protects the environment.
Another observation/hint: I need to exchange the can for a new one about once in several months as it gets dirty and sticky with a bubble gum :-)

Monday, February 17, 2014

Youtube Trends 4@4AM source data

When trying to see episodes of 4@4AM which I missed last week, I've come across the source of the information for the video player.

Which also has a HTML representation:

I've not succeeded with retrieving the last week data though I've learned that the only thing they need to do to update list of videos is to edit the online spreadsheet and add four new lines to it. That's clever.