Web GIS application development step by step Part 3

I promised that I will publish 4 to 5 tutorials in this series and I will, however there was a bit of gap between this and the last post ūüė¶ but here I am keeping my promise, beware this part is a big one.

This tutorial is divided into three parts, in first part, I will discuss OpenLayers, second part, GeoServer, and finally in the last part, I will show an example. But before I can start, I want to bring your attention to another excellent blog post at mangomap.com, its title is “5 things GIS professional need to know about web“. Good to know these things no matter at what level you are.

Now the above tips covered, lets discuss Client-Server architecture because web GIS is based on this architecture. In Client-Serve architecture, a software at server side waits for clients to make requests and returns specific information based on request for example like a web site. Thats it for now, you can read more about it here.

Lets move to OpenLayers. What is OpenLayers and Why I am interested in it, in fact I recommended it to many of my colleges in the past few years. OpenLayers is a JavaScript API that lets you show your maps on the web. It is the most essential part of client side when it comes to web GIS. One must know few things about web GIS to know the working the of OpenLayers.

In web GIS, we need a software (on server side) to read our data (software has to understand the source format of data in order to read), render it in memory (unless raw data is required at client side) and then pass it on to us over the internet. Now, if our data is in shape file format or it is in GeoTif format, it should not be passed due to the client side limitations e.g. shape file is not a web GIS format (plus it will actually be raw data) and GeoTiff could take a lot of time due to size. Thus, this software should send the rendered images in the form of internet friendly formats and we know that there are two image formats which are perfect for internet i.e. JPEG and PNG. Thus, this software should render our data in JPEG/PNG format and pass it on to us over the internet. Ideally, this software should know what area of map we are looking at to render it and send it back to us to avoid rendering all the map, plus this way the size of the image will be small. Thus, when ever we pan or zoom, a new image should be generated and passed on to us. Such a software is called GIS Server and the mapping service is called Web Map Serivce (WMS, read more about it on OGC). Don’t worry, I am not going to ask you to write GIS Server (and implement WMS) because it already exists, in fact we have a lot of good GIS Servers available for example GeoServer, MapServer, Mapnik, QGIS Server and ESRI GIS Server is also in the list (but its not free).

There is a problem over here (I hope you have noticed), what if the JPEG/PNG size is big, it will also take some time to download from server. Well, its true and I have covered it in one of my earlier post,  its better if you have a look at it now. In Summery, a GIS Server does the following (these are the high lights remember)

  • Reads the source data
  • Renders it according to the users viewing bounding box. The rendering is performed based on the styling information provided (default or custom)
  • perform tiling
  • send back the data

In this series, I will only work with GeoServer. Now, I am not going to teach you how to install GeoServer, read part 1 (link ) of this series for “how to install and configure development environment”. However, I just want to past below a paragraph I wrote in first part (I liked it :)).

“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.”

OpenLayers, as mapping API (remember it is for client side), requests map (actually JPEG/PNG tiles) from GIS Server and when GIS Server (like GeoServer) returns OpenLayers shows them in a mapping area (basically in a HTML Div element). When ever we pan or zoom in/out, OpenLayers send a new bounding box to GIS Server requesting for a new tiles.

Why I have selected GeoServer, because of its admintration application. The last time I worked with MapServer was in 2010 and the experience was painful. I have not seen a decent administration application for MapServer but GeoServer has address this issue excellently. Plus the GeoWebCache is also integrated into the GeoServer and can be accessed through admin application (I will talk about it at the end of this tutorial).

Data prepartion 
We will be needing a shape file for out test application. Make sure 
that your shape file is in spherical Mercator projection because we 
will be overlaying it on OSM base map which is in spherical Mercator.

Enough of the explaining, If you have installed GeoServer, stat it by using the following command

 sudo ./startup.sh

The startup.sh script is in /your_path/geoserver_version_number/bin folder (see the GeoServer Section for details in part 1 of this tutorial series). Let GeoServer load properly and then open your web browser and type http://localhost:8080/geoserver and login with default user name “admin” and “geoserver” as password, you should be seeing a web page like the one below

admin_panelGeoServer has excellent user manual, read it (it will help a lot). Read more about the admin panel at here. In this part, I will focus on WMS. See the video below on how to create a WMS (just the first 15 minutes are enough for this part of the tutorial)

or see Lession 4 of Pennsylvania State University “Open Web Mapping” course (its good).

Summery is

  • Create a workspace (not necessarily every time)
  • Create a datastore (every time because this is where you are linking you data (shape file, geoTif, database etc) to GeoServer
  • Create layer (for each dataset, styles etc)

By using the above resources, I hope you have created/published a layer (have EPSG:3857) by now. Lets see how it looks, in GeoServer menu, click ‚ÄúLayer Preview‚ÄĚ, this page has all the layers arranged in five columns (type, Name, Title, Comman Formats and All Formats).Search your layers through the list and click on OpenLayers (forth column i.e. Common Formats) link (in the same line). A new web page will open with an interface similar to the one below (but with your dataset of course ;))

layer_preview

What you are looking at is a web GIS application (Yes it is, simple and small but ‚ÄúA web GIS application‚ÄĚ). It has controls (or components) like Map, zoom/ pand , mouse control and few more. All these controls are provided by OpenLayers API. But wait! We haven’t installed OpenLayers, when was it installed. When you installed GeoServer, the installation has its own web server and OpenLayers, thus, it is using these two to show preview pages (see I told you, GeoServer has it all).

Now lets look at the code behind this layer preview page, right click on the page (the one showing your layer), and click ‚Äúview page source‚ÄĚ. WOW, HTML+JavaScript, its 400 lines of code. But we are only interested in the section where our layer is being added to the map (it is at line 145). Search your layer’s name in the code, it should look some thing like the code below

// setup tiled layer
     tiled = new OpenLayers.Layer.WMS(
                    "cite:Africa_boundaries_3857 - Tiled", "http://localhost:8080/geoserver/cite/wms",
     {
        LAYERS: 'cite:Africa_boundaries_3857',
        STYLES: '',
        format: format,
        tiled: true,
        tilesOrigin : map.maxExtent.left + ',' + map.maxExtent.bottom
      },
      {
        buffer: 0,
        displayOutsideMaxExtent: true,
        isBaseLayer: true,
        yx : {'EPSG:3857' : false}
       } 
   );

The first line is creating an object tiled (just a variable name) of WMS class (of parent class Layer), the constructor for WMS class takes layer’s title, URL , name, format, isBaseLayer, transparent etc as arguments (don’t worry with time and a lot of reading you will understand all of it ;)).

Now we will use the same code above, to add this layer to our previously created web application (remember we created a web application in part 2). Lets look at the init() function again

<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.O SM( "Simple OSM Map");

    // add layer object to map object
    map.addLayer(layer);

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

The init function

  1. Creats a map object and assigns¬†it¬†to the web page’s map DIV
  2. Creats a layer object and attaches it to the map object
  3. Sets map center and zoom level

We need to add our layer’s code ¬†below the OSM layer and add it on to the map but first we need to make two changes to our code.

  1. Instead of format variable , use ‘image/png’
  2. isBaseLayer:true , change it to false

Finally add the layer’s code to init() ¬†function like I have done below


 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
    map.addLayer(layer);

     // 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,
            //tilesOrigin : map.maxExtent.left + ',' + map.maxExtent.bottom
      },
      {
           buffer: 0,
           displayOutsideMaxExtent: true,
           isBaseLayer: false,
           //yx : {'EPSG:3857' : false}
       } 
   );

   // add new layer object to map object
   map.addLayer(tiled);

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

}

I have also added “tiled” layer to map object (fourth line from bottom).¬†Now save code and open¬†http://localhost/myapp/myapp.html in any web browser (as discussed in part 2) and when I did, I got the following output.

part3_final_map

In next part, I will discuss more about GeoServer and layer styles.

See you next time.

Advertisements

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
    map.addLayer(layer);

   // 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">
<head>
 <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
    map.addLayer(layer);

     // 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
   map.addLayer(tiled);

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

}

</script>
</head>
<body onload = "init()">


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


</body>
</html>


Web GIS application development step by step Part 1

GeoNetwork2

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)

postgreSQL

 

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

The above command will create a user, now for database

sudo -u postgres createdb myDB

response will be

CREATE DATABASE

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

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

response is

GRANT

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

Start

/etc/init.d/postgresql start

Stop

/etc/init.d/postgresql stop

Restart

/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

TYPE    DATABASE   USER    ADDRESS            METHOD
host    all        all     192.168.0.1/32     @authmethod@

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

TYPE    DATABASE   USER    ADDRESS METHOD
 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).

JAVA_HOME="/usr/lib/jvm/java-6-openjdk-i386"

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.

geoserver

 

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.