Category: 6-months training

Publish Post on Page through Facebook API

So, here comes very interesting task. As I mentioned in my previous blog about #LuvLdh Event, this blog is related to that only.

Me and my friend Amrit was given a task to make a web app which will allow user to login through facebook, upload images or document and automatically publish it on page.

This blog will cover very small part of the problem i.e. how user can publish post on a page. We used Facebook SDK for Python.

We followed following links to  solve it.


Install facebook module of Python as given in

Generate Access Token

  1. Login to facebook account.
  2. In new tab, browse to
  3. Go to “Tools and Support” and then “Graph API Explorer”.
  4. Click “Get Token” and then “Get User Access Token”. It will prompt out permission options.
  5. In order to publish post on your account, Check box “publish actions” permission and then click “Get Access Token”. It will ask for whom should see the post(preferably public). To know more about permissions browse to

Congratulations, you have generated your long string of access token.

import facebook

graph = facebook.GraphAPI(access_token="your_generated_token", version=2.10)


The above command will create new object “graph” with your own access token.

graph.put_photo(image=open('img.jpg', 'rb'),
                message='Look at this cool photo!')

This will publish photo on your account. Likewise to publish text or anything follow given links.

Publish Post as a Normal User into a Page.

Now recreate Token with more permissions. This time check box

Scopes user_location, user_likes, user_events, user_photos, user_about_me, user_posts, email, publish_actions, manage_pages, pages_show_list, publish_pages, public_profile


Here “public_profile” means to view your post to public. This permission is must. This is obtain when we click “get access token” it ask for to show the post to [public, friends,  only me]. Select Public. It took complete one day to debug the problem.:P

Now check the Page_id of the page in which you want to publish. It is displayed in URL or you also see in the “About” Section of the page.

Create the new object just like “graph” object created with new access token.

graph.put_wall_post(message=’Really Worked!!’, profile_id=’your_page_id’)

Likewise, an admin of the page can also publish the post. Rather an admin can select any option among [public, friends,  only me].

Make sure in the General settings of the page there is no bar for a normal user and everything is set to public(if applicable).

Now check post to the page Community section. You will be able to see the post.:)

For any kind of doubt feel free to comment.


Admin Levels at Different Zoom Levels

Few days back, I got a task to show admin_levels at various zoom levels. I here explain the task in detail.

It means like to show all states (admin_level=4) of India at particular zoom level with a different colour. Likewise to show all districts (having a particular admin_level) with another colour. It was quite interesting task.

To know more about admin_levels you can browse to the link.

The algorithm behind it is to give the reference to particular admin level. For example column “admin_level_2” will refer to all the rows where admin_level=2.

For this you must know basics of Postgresql.

– id: “zoom_levels”
name: “zoom_levels”
class: “”
geometry: “polygon”
<<: *extents
<<: *osm2pgsql
table: |-
(SELECT boundary,
COALESCE(tags->’name:pa’,tags->’name:hi’,name) as name, way, COALESCE(
‘admin_level_’ || CASE WHEN admin_level IN (‘2’, ‘3’,’4′,’5′,’6′,’7′,’8′,’9′,’10’) THEN admin_level ELSE NULL END
) as admin_level, access,
way_area/NULLIF(!pixel_width!::real*!pixel_height!::real,0) AS way_pixels
FROM planet_osm_polygon
WHERE admin_level IN (‘2’, ‘3’,’4′,’5′,’6′,’7′,’8′,’9′,’10’)
) AS zoom_levels
minzoom: 3

The above is the complete layer with id “zoom_levels”. The styles to this layer will be given through this ID.

Now create fresh mss file called zoom-levels.mss

#zoom_levels {
[admin_level = ‘admin_level_2’][zoom>=3][zoom<5] {
text-name: “[name]”;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;
line-color: #000;
polygon-fill: #765543;
polygon-opacity: 0.5;}
[admin_level = ‘admin_level_3’][zoom>=5][zoom<6] {
polygon-fill: @landform-color * 0.1;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_4’][zoom>=6][zoom<8]{
polygon-fill: #cccccc;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-line-spacing: -1.65;
text-halo-radius: @standard-halo-radius;

text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_5’][zoom>=8][zoom<10] {
polygon-fill: #ccc149;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_6’][zoom>=10][zoom<12] {
polygon-fill: #cc1039;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_7’][zoom>=12][zoom<14] {
polygon-fill: #1e7b79;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_8’][zoom>=14][zoom<16] {
polygon-fill: #6c7b6d;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_9’][zoom>=16][zoom<17] {
polygon-fill: #d68035;
line-color: #000;
polygon-opacity: 0.5;
text-name: “[name]”;
text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;}
[admin_level = ‘admin_level_10’][zoom>=17][zoom<19] {
polygon-fill: #295332;
line-color: #000;
polygon-opacity: 0.5;
text-name: “[name]”;

text-line-spacing: -1.3;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts


Include this mss file in the project.mml file as other mss files are included.

Now it’s time to compile the code.:)

carto project.mml > style.xml

Re-render the tiles. You may see the desired output at admin_level=4. It’s obvious that you should have India database.



Playing with Languages in OSM

Hie Everyone.

As the title clearly explains the moto of the blog. So, let’s begin.

The logic which I have applied to display the name of the places in the OSM map is ->


Punjabi>Hindi>Original Name

Punjabi name is preferred first. If it null then Hindi will be preferred followed by original name.

The sample image is shown here.


The whole source code is the following link. You can clone it from here.

Now, without wasting much, I here repeat that particular code. It’s very easy. You can customize it in your own way.

Open the project.mml file. In sql queries replace the column name with the line

COALESCE(tags->’name:pa’,tags->’name:hi’,name) as name

Here coalesce function will display the first not null value. If all values are null then it will display no value.

Make sure you have installed the fonts of the corresponding language.

$sudo apt-get install fonts-noto-cjk fonts-noto-hinted fonts-noto-unhinted fonts-hanazono ttf-unifont

The ttf files of all these fonts should be included in the fonts directory which your Mapnik is loading. In my case it is /usr/local/share/fonts/

You can translate English word to Punjabi from the following websites.

If you have any query related to it. Please do comment.

Thank you

On Demand or On fly tiles Render

Hey All. Today I want to discuss the concept of tiles rendering.

There are two types of rendering the tiles.

  1. Rendering on demand- The tiles are render when you zoom or go to that location.
  2. Rendering on the fly- Pre-render the tiles without zooming it again and again.

The command to render the list of map tiles by sending the request to rendering daemon. It will pre-render the tiles.

$ render_list -m default -a -z 0 -Z 10


debug: init_storage_backend: initialising file storage backend at: /var/lib/mod_tile
Rendering client
Starting 1 rendering threads
Rendering all tiles from zoom 0 to zoom 10
Rendering all tiles for zoom 0 from (0, 0) to (0, 0)
Rendering all tiles for zoom 1 from (0, 0) to (1, 1)
Rendering all tiles for zoom 2 from (0, 0) to (3, 3)
Rendering all tiles for zoom 3 from (0, 0) to (7, 7)
Rendering all tiles for zoom 4 from (0, 0) to (15, 15)
Rendering all tiles for zoom 5 from (0, 0) to (31, 31)
Rendering all tiles for zoom 6 from (0, 0) to (63, 63)
Rendering all tiles for zoom 7 from (0, 0) to (127, 127)
Rendering all tiles for zoom 8 from (0, 0) to (255, 255)

Here, the flag -m means long prompt. This command takes some time. While the command was executing, I got to know the maths behind the tiles at a particular level.

Assume zoom level is x then the tiles are render from (0,0) to

Screenshot from 2017-06-19 20-36-47.png

This picture clearly shows what above explained.

Postgresql Commands

Today, I have downloaded map of a country and imported it into the postgresql database through osm2pqsql.

All the relations in the database were having millions of rows. So, while selecting all of them at once I was getting error.

First I here tell how to open the psql shell. If you are not sudo user. Don’t Worry. We have a solution for that also.

To open shell as a superuser.

$sudo -u postgres psql

It will prompt to psql command line. To connect to particular database

\c gis

That’s all.

If you are a normal user. Request to sudo user to create new role for you and create new database for you.

postgres=# create role tamil login password ‘tamil’;

postgres=# CREATE DATABASE mydatabase WITH OWNER = tamil;

Then you access that particular database with sudo power.

$ psql -h localhost -d mydatabase -U tamil

Then , Dr. H S Rai suggested me to limit the number of rows. From there I got a keyword Limit and searched relevant query. I was not having an idea about this query initially. Without wasting more time in telling my story I here directly code the query.

gis=# select * from planet_osm_nodes LIMIT 3;


-[ RECORD 1 ]—
id | 15382126
lat | 115301682
lon | 849624182
tags |
-[ RECORD 2 ]—
id | 15382127
lat | 115310352
lon | 849612464
tags |
-[ RECORD 3 ]—
id | 15382129
lat | 115327713
lon | 849590461
tags |

Another postgresql query to clear the screen of the psql command line.

gis=# \! clear

You can also use shortcut  Ctrl +l

Command to check the extensions created in the postgres.

postgres=# \dx


List of installed extensions
Name | Version | Schema | Description
hstore | 1.3 | public | data type for storing sets of (key, value) pairs
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
postgis | 2.2.1 | public | PostGIS geometry, geography, and raster spatial types and functions
(3 rows)

I want to know what basically happen when we create an extension. By creating an extension “postgis” it creates the relation “spatial_ref_sys”. You can check it also.

new=# create extension postgis;

It created a table

new=# \dt

List of relations
Schema | Name | Type | Owner
public | spatial_ref_sys | table | postgres
(1 row)

To retrieve the value of a first ten rows of column tag with particular key

gis=# SELECT tags->’name:hi’ from planet_osm_polygon limit 10;

COALESCE function will output the first not null value among the arguments.

gis=# SELECT coalesce(tags->’name:hi’,name) from planet_osm_polygon limit 10;

To play more with the column having datatype hstore you can follow the tutorial.

Command to check database size.




Convert Shapefile to database, Vice Versa

Shapefile to Database

Today, I tried to convert shapefile to sql file. Ultimately to import it into the postgis database.

For it you should have shapefile downloaded. If not having you can download it from below.


Tool used to convert shapefile to sql file is shp2pgsql.

$shp2pgsql -G BOUNDARY_POLY.shp > BOUNDARY_POLY.sql

Make sure you should be in the directory where shapefile located. Here, sql file has insert commands to the relation country. So there must be table created if your sql file don’t have created table.

new=# CREATE TABLE country(id integer NOT NULL, name text, boundary geography(MultiPolygon,4326),name_alias text, CONSTRAINT “countryId” PRIMARY KEY (id ));

Command to dump sql file into the database.

postgres@amisha:~/9.5/main$ psql new < BOUNDARY_POLY.sql

Database to shapefile

To convert database back to shapefile, I have used pgsql2shp.

$ pgsql2shp -f am3 -h localhost -u postgres gis “SELECT * from planet_osm_line where admin_level=’4′ and name=’Punjab'”


Done (postgis major version: 2).
Output shape: PolyLine
Dumping: X [19 rows].

Thank you.

QGIS and JOSM Installation

Steps to install QGIS

  1. Go to apt sources file.

$sudo vim /etc/apt/sources.list

  1. Add these two lines at the end.

deb xenial main
deb-src xenial main

  1. Then download the package information from the sources by update command.

sudo apt-get update
sudo apt-get install qgis python-qgis qgis-plugin-grass

It’s installed. Now, you can browse to QGIS from dashboard. But I run it from terminal. It’s easy.

You can learn QGIS from following tutorials.

JOSM Installation

It’s a one-line command only.

sudo apt install josm

You can learn JOSM from following tutorials.


Upgrade Carto Compiler

Today, I tried to upgrade Carto Compiler.

To be acquainted with Cartocss, one can follow this tutorial.

The current version of carto compiler is

$ carto –version
carto 0.9.5 (Carto map stylesheet compiler)

To upgrade it through npm. You should have nodejs, npm installed in order to upgrade carto.

$ sudo npm install -g carto

After upgrading it, I again checked the version. But I got the error.

$ carto –version
/usr/bin/env: ‘node’: No such file or directory

To solve this error I created the symbolic link of node from nodejs.

$ sudo ln -s /usr/bin/

nodejs /usr/bin/node

Problem Solved. It is upgraded.

$ carto –version
carto 0.18.1 (Carto map stylesheet compiler)


Saturday Meet

Hello Everyone. Today was the first Saturday meet of 6-months training. It was very different from other Saturday’s as we have not given presentation on our daily progress rather delivered interactive lecture from one of the GD-member <Harmanpreet Singh>.

He told us about how to efficient your work even in the entirely different environment. He does job in Chennai in XYZ start-up company. He is the only one from Punjab and sometime face problem while communication. That company has a strict schedule and norms which are to be followed by everyone. Moreover, all over there are fan of Windows;)

<quote>Working in small start-up company is directly proportional to the learning or exploring yourself</quote>

Today, I came to know how GD started. It was because of three GD Members Mr. Vikas Mahajan and two more. They completed their training from Dr. Rai Sir only.

Coming to the technical work. Today, I solved the issue of brightness in Ubuntu 16.04 LTS.

After upgrading Ubuntu to 16.04 I was not able to change the brightness of my laptop.


sudo add-apt-repository ppa:apandada1/brightness-controller
sudo apt-get update
sudo apt-get install brightness-controller

It’s a work of three commands and you are done.