Web GIS application development step by step Part 2

As promised, here is the second part of web GIS development tutorial. What we have done till now

  1. Installed Ubuntu
  2. Installed synaptic package installer (to make installation job easy)
  3. Installed PostgreSQL
    1.  Created Role, user and database
    2. Changed config files to make server listen on localhost and other IPs
  4. Installed apache web server
    1. Installed php module
    2. Installed PostgreSQL driver for php
  5. Installed Java
  6. Installed GeoServer

Its time to create a web GIS application. When I installed apache web server, a folder was created in /var folder i.e. www. I created a new folder in /var/www folder named it myapp and inside my app I created an other folder known as lib (/var/www/myapp/lib). All libraries will go into this folder (keeping our code manageable).

The most important library required for a web GIS application is mapping library. For beginners, mapping library is the one which shows map (tiles to precise), allows zoom in/out, pan, adding overlayer and many other functionaries. For mapping libraries multiple options are available, some are based on JavaScript and some are based on Flash etc. I like the ones based on JavaScript and particularly like OpenLayers for its richness.

I downloaded OpenLayers from its web site. Unzipped it into my lib folder (/var/www/myapp/lib/openlayers). I wrote a small html web page (myapp.html) and added OpenLayers to it for demo web GIS application, following is an explanation of it.

We need to load OpenLayers library into our html page, for this we will use the following line in header section of our html page.

<script type="text/javascript" src="http://localhost/myapp/lib/openlayers/lib/OpenLayers.js"></script>
<link rel="stylesheet" type="text/css" href="http://localhost/myapp/lib/openlayers/theme/default/style.css" />

When this code will run on side browser, link element will set styles for zoom buttons etc. and script element will fetching OpenLayers JavaScript library.

Note: You can link OpenLayers library from their web site but I think serving it from your own local web server is better.

A DIV element is required to embed map into an html page. Why DIV because contents inside DIV can be updated without updating the host html page, thus, when we zoom in on a map only contents of DIV are updated. Following is html code for DIV element.

<div id="map"></div>

Now that DIV is created (named ‘map’),  we need to create a JavaScript function that can create map inside this DIV (basically link OpenLayers library to this DIV). I added following code in head section of my html page.

 function init()
    // variables
    var map, layer;

    // connect OLs map object with DIV map
    map = new OpenLayers.Map('map');

    // create a layer object
    layer = new OpenLayers.Layer.O SM( "Simple OSM Map");

    // add layer object to map object

   // Zoom to level 2 and set 0,0 as map center
   map.setCenter(new OpenLayers.LonLat(0 ,0),2);


Wait we are not finished yet (please be patient 🙂 ). We need to call the JavaScript function (init()) when ever our myapp.html page loads. For this, I called init() function on page load (see below)

<body onload = "init()">

Now, when myapp.html will be loaded, init() function will be called and map will appear in web browser. Now every thing was set, I opened http://localhost/myapp/myapp.html in my web browser and got the following output.

Screenshot from 2014-06-01 15:49:13

I know that init() function needs explanation, thus, lets dissect it.

First are two variables map and layer. Both are simple JavaScript variables which will be used to pint to OpenLayers map and layer objects. Always use var keyword while defining variable inside a function otherwise they take global scope.

Second line creates an OpenLayer map object and pass our DIV map as argument. Now our DIV will be a canvas for drawing tiles and will also respond to mouse drag and double click (along with my more functionalities).

Third line creates a layer object and this layer object is specific to OpenStreetMap (OSM). What is happening at the backend is that code is requesting OSM mapping servers for maps (basically WMS). Don’t worry I will explain WMS in next tutorial.

Fourth line is adding the newly created layer to map (see reference to newly created layer is passed in the addLayer()).

Finally, in fifth line, map was centered at 0,0 and zoom level 2 was set.

For all OpenLayers functions, please have a look at OpenLayes API documentation. I wish they can add one liner examples to each function’s help. For JavaScript, have a look at codeadademy and w3schools  web sites.

In Next tutorial, I will talk about GeoServer’s role in a web GIS application (with example) and its further use. PostGIS installation, configuration and usage and some JS GUI libraries.

Full code for the demo myapp.html is below

<html xmlns="http://www.w3.org/1999/xhtml">
 <meta http-equiv="content-type" content="text/html; charset=UTF8" />
 <meta name="content-language" content="en" />
 <title>Web Map Application</title>

 <script src="http://maps.google.com/maps/api/js?v=3&amp;sensor=false"></script>

 <script type="text/javascript" src="http://localhost/myapp/lib/openlayers/lib/OpenLayers.js"></script>
 <link rel="stylesheet" type="text/css" href="http://localhost/myapp/lib/openlayers/theme/default/style.css" />

<script type="text/javascript">

 function init()

    var map, layer;

    // connect OLs map object with DIV map
    map = new OpenLayers.Map("map");

    // create a layer object
    layer = new OpenLayers.Layer.OSM( "Simple OSM Map");

    // add layer object to map object

     // setup tiled layer
    var tiled = new OpenLayers.Layer.WMS(
                    "cite:Africa_boundaries_3857 - Tiled", "http://localhost:8080/geoserver/cite/wms",
            LAYERS: 'cite:Africa_boundaries_3857',
            STYLES: '',
            format: 'image/png',
            tiled: true,
            transparent: true,

           buffer: 0,
           displayOutsideMaxExtent: true,
           isBaseLayer: false,

   // add new layer object to map object

  // Zoom to level 2 and set 0,0 as map center 
  map.setCenter(new OpenLayers.LonLat(0 ,0),2); 


<body onload = "init()">

<div id="map" style="width:100%;height:95%; z-index: 0" > </div>


Web GIS application development step by step Part 1


Many people asked me about web GIS  development and from where to start. Some questions were basic like ‘how to show a shape file on Google maps’ to advance level as ’tiles are coming from local tile cache or GIS server’. Since I am into Open Source web GIS application development, thus, I decided to write a blog about it that can help the beginners and answer some medium to advance level questions.

First step is to setup a development environment. Keeping the development environment Open source and free from end to end (from Operating system to client side APIs), I selected Ubuntu as the operating system. I installed Ubuntu 12.04 and rest of the story is as follows

Package updates are required, thus, do the following.

sudo apt-get update
sudo apt-get upgrade

this will take some time, depends on your internet speed. I found synaptic package manager missing from my newly installed Ubuntu 12.04 (desktop), thus, I Installed synaptic package manager using commands

Note: Remember, answers to questions on console are case sensitive.

 sudo apt-get install synaptic

I like Google Chrome because it has an easy to use debugger, thus, installed Chrome using synaptic package installer. Keep in mind, chrome for Ubuntu 64 bit is only available for AMD by-default.

To keep a backup backup of some important files, I always use Dropbox. To install Dropbox, python-gpgme package needs to be installed (again using synaptic package installer) for verification of binary signature. Finally, I downloaded Dropbox installation package and installed it.

Behind every successful web application there is a database :). Not that we will be needing a database for spatial purposes (in this tutorial) but it is required for many other purposes in a web application e.g. keeping users details etc. Few choices from Open Source community are MySQL, SQLite and PostgreSQL. PostgreSQL is the best Open Source database especially when it comes to spatial functions. Again, I used synaptic package manager to install postgreSQL (as you can see in in the image below, I installed postgreSQL 9.1.x)



At this moment, postgreSQL is installed but One has to create a role, user and database, thus, I executed following commands through terminal

sudo -u postgres createuser

following questions required user response

Enter name of role to add: scriptNdebug
Shall the new role be a superuser? (y/n) y
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y

The above command will create a user, now for database

sudo -u postgres createdb myDB

response will be


Now user scriptNdebug needs to have access to myDB database, thus, logged in to command line tool of PostgreSQL

i.e. psql using super user postgres

sudo -u postgres psql 
postgres=# alter user scriptNdebug with encrypted password 'password';

response is

postgres=# grant all privileges on database myDB to scriptNdebug;

response is


What if I want to stop PostgreSQL database server. Following are some useful commands (use them from terminal)


/etc/init.d/postgresql start


/etc/init.d/postgresql stop


/etc/init.d/postgresql restart

If you want server to listen on a specific IP, then change pg_hba_config file. If the file does not exists, then simply rename pg_hba_config.sample at location /usr/share/postgresql/9.1/ to pg_hba_config and make the following changes

host    all        all     @authmethod@

 the above change will make the server listen on IPs from to I want to list on all IPs for all users and ‘password’ as authentication method, thus, I made the following changes

 host    all       all     all     password 

Similarly, if you want to listen on localhost, then make the following changes in file postgresql.config. If the file does not exists, then simply rename postgresql.config.sample at location /usr/share/postgresql/9.1/ to postgresql.config and make the following changes

listen_addresses = 'localhost'

This file is to be changed if one wants to change port 5432. Do not forget to restart PostgreSQL server load the new settings.

Now web server needs to be installed, I selected apache, used synaptic package manager to install apache (version 2.2.22-1ubuntu1.4). This will create a folder ‘www’ in folder /var/. We will use the www folder when we will create a test web GIS application later in this tutorial. If you want to use php as server side development language, install php module for apache server. Just use synaptic to install libapache2-mod-php5. Also install (using synaptic) php5-pgsql module to use postgreSQL with php. Restart apache (use command below) to load these modules.

sudo /etc/init.d/apache2 restart

Finally it is time to install the most important component of Web GIS application i.e. GIS Server. It is important to know what a GIS Server Is? It is a software that listens for specific request (by clients). These requests could be for different services  (read OGC specification on GIS server for details) e.g. getMap request etc. A GIS server loads a dataset (e.g. a shape file or a Geo-Tiff) renders it, cut image into tiles and sends it back to the requesting client. Every time a client pans or zooms, GIS server receives requests and sends image tiles as response. Format of image tiles is specifically selected due to its size. In Free and Open Source Software for Geo  (FOSS4Geo) there are multiple choices for GIS Servers e.g. MapServer, GeoServer, mapnik. qGIS server, MapGuide etc. Some are written in C and some are in Java.

GeoServer has an edge over others because of its administration tool i.e. web based administration interface. With GeoServer, deploying a dataset (let say a shape file) as a WMS is very easy. Thus I decided to install GeoServer. Before installing GeoServer, Java needs to be installed as GeoServer require Java. I used  synaptic package manager to install openjdk-6-jre.  Then downloaded GeoServer from its web site. Unzipped the downloaded file into a folder. Moved to bin folder and edited startup.sh file to add JAVA_HOME variable as shown below (your path might be different).


All is set now, started GeoServer from terminal by using the following command

 sudo ./startup.sh

Note: Do not close this terminal this terminal window because it will shutdown GeoServer.

Started a web browser, and typed http://localhost/geoserver to open administration application and used admin as user name and geoserver as password. Application menu is on left side, clicked on Layer preview to view sample WMS layers. One can click on Open Layers link against any sample layer (as shown in figure below) to open it in a new tab.



Please read GeoServer user manual to know how to deploy your datasets as WMS layers (I will create a WMS in next part of this tutorial). In next part, I will also create a web GIS sample application using Open Layers and explain web GIS application functionality.