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

One thought on “Web GIS application development step by step Part 3

  1. Pingback: Web GIS application development step by step Part 3 | Geo-How-To News

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s