Category: 6-months training

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.

http://wiki.openstreetmap.org/wiki/Tag:boundary%3Dadministrative

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
Datasource:
<<: *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,
religion,
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
properties:
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-size:12;
text-halo-radius: @standard-halo-radius;
text-fill: #000;
text-face-name: @book-fonts;
line-color: #000;
polygon-fill: #765543;
polygon-gamma:0.4;
polygon-opacity: 0.5;}
[admin_level = ‘admin_level_3’][zoom>=5][zoom<6] {
polygon-fill: @landform-color * 0.1;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-size:12;
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;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-size:16;
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;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-size:11;
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;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-size:14;
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;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-size:13;
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;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.65;
text-name: “[name]”;
text-size:13;
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;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.5;
text-name: “[name]”;
text-size:13;
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;
polygon-gamma:0.4;
line-color: #000;
polygon-opacity: 0.5;
text-name: “[name]”;

text-size:12;
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.

admin.png

 

Advertisements

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 ->

Preference

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.

punjabi.png

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

https://github.com/GreatDevelopers/pbOSM

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.

http://punjabi.indiatyping.com/

https://www.google.co.in/search?client=ubuntu&channel=fs&q=english+punjabi+translator&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=GklaWefiEcXy8AeNnKrQDQ

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

Output

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
((2^x-1),(2^x-1))

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;

Output

-[ 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

Output

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.

http://www.postgresqltutorial.com/postgresql-hstore/

Command to check database size.

\l+

 

 

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.

$wget http://wsgw.mass.gov/data/gispub/shape/state/boundaries.zip
$unzip boundaries.zip

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'”

Output

Initializing…
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 http://qgis.org/debian xenial main
deb-src http://qgis.org/debian 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.

https://docs.qgis.org/2.8/en/docs/

JOSM Installation

It’s a one-line command only.

sudo apt install josm

You can learn JOSM from following tutorials.

https://www.google.co.in/search?client=ubuntu&channel=fs&q=josm+tutorial&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=j01FWavgKtbmugTdvYHQAw

 

Upgrade Carto Compiler

Today, I tried to upgrade Carto Compiler.

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

http://tilemill-project.github.io/tilemill/docs/manual/carto/

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.

Solution

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.